From 0d22f4611887c020453b0068d236b491102836f0 Mon Sep 17 00:00:00 2001 From: Mikayla Dobson Date: Mon, 29 Jan 2024 10:44:25 -0600 Subject: [PATCH] update naming and publish info --- README.md | 33 +++++++++-------- examples/main.ts | 8 ++-- package.json | 2 +- pkg/examples/main.ts | 88 ++++++++++++++++++++++++++++++++++++++++++++ pkg/index.ts | 8 ++-- 5 files changed, 114 insertions(+), 25 deletions(-) create mode 100644 pkg/examples/main.ts diff --git a/README.md b/README.md index 47895f9..a2e20cc 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,8 @@ -# Mongo Assert +# Mongo Validate Stronger, more intuitive type checking and assertions for MongoDB. -Mongo Assert extends the functionality of Zod, integrated with the MongoDB Node.js driver, to provide a more intuitive and powerful way to validate and assert MongoDB documents. +Mongo Validate extends the functionality of Zod, integrated with the MongoDB Node.js driver, to provide a more intuitive and powerful way to validate MongoDB documents. Some of its features include: @@ -12,20 +12,20 @@ Some of its features include: ## But why? -Mongo Assert is something I put together out of frustration with existing, heavy handed solutions for MongoDB schema management. Mongo Assert is intended to have a minimal footprint, add on to existing validation infrastructure, and plug in seamlessly to build-time integrity checks. +Mongo Validate is something I put together out of frustration with existing, heavy handed solutions for MongoDB schema management. Mongo Validate is intended to have a minimal footprint, add on to existing validation infrastructure, and plug in seamlessly to build-time integrity checks. ## Usage -Mongo Assert is available as an NPM package. To install it, run: +Mongo Validate is available as an NPM package. To install it, run: ```bash -npm install mongo-assert -pnpm install mongo-assert -yarn add mongo-assert -bun install mongo-assert +npm install mongo-validate +pnpm install mongo-validate +yarn add mongo-validate +bun install mongo-validate ``` -Mongo Assert supports Zod schemas out of the box, so any schemas you may already have defined can be used +Mongo Validate supports Zod schemas out of the box, so any schemas you may already have defined can be used with any MongoDB instance. ## Examples @@ -36,7 +36,7 @@ For all of the examples below, we'll be using the following schema: ```ts import { z } from 'zod'; -import { ObjectId } from "mongo-assert/lib/mongodb" +import { ObjectId } from "mongo-validate/lib/mongodb" export const userSchema = z.object({ _id: ObjectId, @@ -49,13 +49,14 @@ export type User = z.infer; ### Unique field validation -Mongo Assert extends Zod schemas to check that your documents match the desired schema. For example: +Mongo Validate extends Zod schemas to check that your documents match the desired schema. For example: ```ts -import MongoAssert, { type AssertUniqueType } from 'mongo-assert'; +import mongo-validate, { type AssertUniqueType } from 'mongo-validate'; import { userSchema, type User } from "./schemas"; -export const UserEmailsUniqueValidator: AssertUniqueType = MongoAssert.unique.fromSchema(userSchema, ["email"]); +export const UserEmailsUniqueValidator = MongoValidate.assertUnique.fromSchema(userSchema, ["email"]); +// ^? AssertUniqueType ``` The returned object is a Zod schema that can be directly invoked to validate a result from MongoDB. This object @@ -87,7 +88,7 @@ async function main() { ### Reference validation -Mongo Assert also supports validation of references to other collections. +Mongo Validate also supports validation of references to other collections. For example, suppose we have a collection of `Store`s that must be associated to an existing `User`. Let's define our schema first: @@ -107,9 +108,9 @@ export type Store = z.infer; Now, we can define a reference validator between these two collections: ```ts -import MongoAssert from 'mongo-assert'; +import mongo-validate from 'mongo-validate'; -const StoresHaveOwnersValidator = new MongoAssert.relation({ +const StoresHaveOwnersValidator = new MongoValidate.assertRelation({ db: "your-db", mainCollection: "users", relationCollection: "stores", diff --git a/examples/main.ts b/examples/main.ts index aee27fa..fc6fcc2 100644 --- a/examples/main.ts +++ b/examples/main.ts @@ -1,5 +1,5 @@ import { z } from 'zod'; -import MongoAssert from '..'; +import MongoValidator from '..'; import dotenv from 'dotenv'; import path from 'path'; import { createMongoClient, ObjectId } from '../pkg/lib/mongodb'; @@ -52,10 +52,10 @@ async function main() { type AssertUniqueType> = z.ZodEffects>, T[], unknown>; // validate against internal schema - const MusicEntryValidator = MongoAssert.unique.fromSchema(MusicEntry, ["slug"]) satisfies AssertUniqueType; - const MusicCollections = MongoAssert.unique.fromSchema(MusicCollection, ["collectionslug"]); + const MusicEntryValidator = MongoValidator.unique.fromSchema(MusicEntry, ["slug"]) satisfies AssertUniqueType; + const MusicCollections = MongoValidator.unique.fromSchema(MusicCollection, ["collectionslug"]); - const result: Promise = MongoAssert.relation.check({ + const result: Promise = MongoValidator.relation.check({ db: "mikayladotdev", mainCollection: "music", relationCollection: "music-collection", diff --git a/package.json b/package.json index 8b7fbed..4fe83cd 100644 --- a/package.json +++ b/package.json @@ -1,5 +1,5 @@ { - "name": "mongo-assert", + "name": "mongo-validate", "version": "0.0.1", "description": "Lightweight schema validation for MongoDB with Zod", "author": "Mikayla Dobson", diff --git a/pkg/examples/main.ts b/pkg/examples/main.ts new file mode 100644 index 0000000..1757fea --- /dev/null +++ b/pkg/examples/main.ts @@ -0,0 +1,88 @@ +import { z } from 'zod'; +import MongoAssert from '..'; +import dotenv from 'dotenv'; +import path from 'path'; +import { createMongoClient, ObjectId } from '../lib/mongodb'; + +dotenv.config(); + +const MusicEntry = z.object({ + _id: ObjectId, + name: z.string(), + shortdescription: z.string(), + longdescription: z.string().optional(), + artist: z.string().optional(), + compositiondate: z.string().optional(), + pathtoentry: z.string(), + collection: z.string(), + slug: z.string(), +}) + +const MusicCollection = z.object({ + _id: ObjectId, + name: z.string(), + description: z.string().optional(), + collectionslug: z.string(), +}) + +type MusicEntryType = z.infer; +type MusicCollectionType = z.infer; + +async function main() { + try { + const url = process.env.MONGO_URL; + const cert = process.env.MONGO_CERT; + + if (!url || !cert) { + throw new Error('Missing environment variables'); + } + + const client = await createMongoClient(url, { + tlsCertificateKeyFile: path.resolve(__dirname, '..', cert) + }); + + if (!client) return null; + + const mdotdev = client.db('mikayladotdev'); + const music = mdotdev.collection('music'); + + const musicData = await music.find().toArray(); + const collectionData = await mdotdev.collection('music-collection').find().toArray(); + + type AssertUniqueType> = z.ZodEffects>, T[], unknown>; + + // validate against internal schema + const MusicEntryValidator = MongoAssert.assertUnique.fromSchema(MusicEntry, ["slug"]) satisfies AssertUniqueType; + const MusicCollections = MongoAssert.assertUnique.fromSchema(MusicCollection, ["collectionslug"]); + + const result: Promise = MongoAssert.assertRelation.check({ + db: "mikayladotdev", + mainCollection: "music", + relationCollection: "music-collection", + mainSchema: MusicEntry, + relationSchema: MusicCollection, + relations: { + "collection": "collectionslug" + } + }); + + await Promise.allSettled([ + MusicEntryValidator.parseAsync(musicData) satisfies Promise, + MusicCollections.parseAsync(collectionData) satisfies Promise, + result + ]).then((results) => { + results.forEach((result) => { + if (result.status === 'rejected') { + console.log(result.reason); + } + }) + }) + .catch(console.error) + .finally(() => client.close()); + + } catch(e) { + console.log(e); + } +} + +main().catch(console.error); diff --git a/pkg/index.ts b/pkg/index.ts index 95ee22a..73e0673 100644 --- a/pkg/index.ts +++ b/pkg/index.ts @@ -5,8 +5,8 @@ import { AssertConstrained } from './actions/constrained'; export type { AssertUniqueType } from "./actions/unique"; export type { AssertRelationType } from "./actions/relation"; -export default class MongoAssert { - static unique = AssertUnique; - static relation = AssertRelation; - static constrained = AssertConstrained; +export default class MongoValidator { + static assertUnique = AssertUnique; + static assertRelation = AssertRelation; + static assertConstrained = AssertConstrained; }