typed:model

v0.0.1Published 2 years ago

Meteor Typed Model

A Zod validated, type safe wraper around Meteor Mongo Collections.

Package Status

This package is currently a WIP. Documentation is incomplete and there are no automated tests. That being said, the code is reliable and has been extracted from the JollyRoger project.

Installation

Install typed:model and zod:

meteor add typed:model
meteor npm install zod

Usage

1import { Model, CustomTypes, SchemaHelpers } from 'meteor/typed:model';
2import type { ModelType } from 'meteor/typed:model';
3import { z } from 'zod';
4
5const { nonEmptyString } = CustomTypes;
6const { withCommon } = SchemaHelpers;
7
8const Link = withCommon(
9  z.object({
10    title: nonEmptyString,
11    url: z.string().url(),
12  })
13);
14
15export const LinkModel = new Model('link', Link);
16export type LinkType = ModelType<typeof LinkModel>;
17
18LinkModel.insert({ title: 'Google', url: 'https://google.com' });
19
20// Find a link by title and limit the fields returned to just the title.
21// Notice that the return type is properly inferred to only have a title and
22// and no other extraneous fields as you would have with a normal Meteor collection.
23const foundLink = LinkModel.findOneAsync({ title: 'Google' }, { fields: { title: 1 } });

Attribution

This package is composed mostly of code extracted from the JollyRoger project created by Evan Broder.