@mirite/zod-to-mongoose
TypeScript icon, indicating that this package has built-in type declarations

0.0.3 • Public • Published

Zod to Mongoose

This is a package for generating Mongoose schemas from Zod schemas. The goal is to improve type safety and reduce the amount of code that needs to be written when working with Mongoose.

Installation

yarn add @mirite/zod-to-mongoose

Usage

Basic

Passing in a Zod schema will return a Mongoose schema.

import { createSchema } from "@mirite/zod-to-mongoose";

const schema = createSchema(
    z.object({
        name: z.string().optional().default("Bob"),
        age: z.number().default(3).optional(),
        isHappy: z.boolean().optional(),
        birthday: z.date().default(new Date()),
    }),
);

const model = mongoose.model("modelName", schema);
const result = await model.create({
    isHappy: true,
    birthday: new Date("1980-01-01"),
});

Advanced

If you pass a connection, the model will be created, registered with the connection, and returned for use.

import { createSchema } from "@mirite/zod-to-mongoose";


const model = createSchema(
    z.object({
        name: z.string().optional().default("Bob"),
        age: z.number().default(3).optional(),
        isHappy: z.boolean().optional(),
        birthday: z.date().default(new Date()),
    }),
    "modelName", // The unique name to give the model
    connection, // Mongoose connection
);

const result = await model.create({
    isHappy: true,
    birthday: new Date("1980-01-01"),
});

Caveats

Only a subset of Zod types are supported. The following types are supported:

  • z.string()
  • z.number()
  • z.boolean()
  • z.date()
  • z.object() (Nested objects are supported)
  • z.union() (Type safety is not guaranteed)

Another major caveat is that no additional validation is performed on the Mongoose schema. It is recommended to use Zod for validation before saving to the database, as well as on retrieval if there are data integrity concerns.

Package Sidebar

Install

npm i @mirite/zod-to-mongoose

Weekly Downloads

1

Version

0.0.3

License

MIT

Unpacked Size

21.2 kB

Total Files

10

Last publish

Collaborators

  • mirite