git-zodyac / mongoose

Converts your Zod schemas into fully-functional Mongoose schemas
https://www.npmjs.com/package/@zodyac/zod-mongoose
31 stars 5 forks source link

timestamps #8

Closed fushugaku closed 3 months ago

fushugaku commented 4 months ago

With mongoose you can define timestamps like this const userSchema = new Schema({ name: String }, { timestamps: true });

how can you do it when using this library?

Kakha01 commented 3 months ago

I roughly got it working like this, however I'm not sure if it is typesafe

export function zodSchema<T extends ZodRawShape>(
  schema: ZodObject<T>,
  options?: SchemaOptions<FlatRecord<any>, {}, {}, {}, {}, any>
): Schema<z.infer<typeof schema>> {
  const definition = parseObject(schema);
  return new Schema<z.infer<typeof schema>>(definition, options);
}

SchemaOptions and FlatRecord come from 'mongoose' library

bebrasmell commented 3 months ago

Will be in next release