npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

create-typed-collection

v1.0.2

Published

Create typed safe collections for Meteor

Downloads

166

Readme

Meteor Create Typed Collection

The Meteor Typed Collection package simplifies the creation of Meteor collections in TypeScript, integrating seamlessly with Zod for schema validation. This utility allows you to define a collection with a specified name and schema, including custom methods, providing a type-safe and structured approach to working with collections in a Meteor project.

How it works

The createTypedCollection function is at the core of this package. It takes a ZodObject representing the anticipated shape of your collection documents and optional custom methods. The resulting Meteor collection is enriched with custom methods and adheres to the specified schema.

Usage

  1. Installation: Make sure to install the necessary dependencies, including Zod and your Meteor Typed Collection package.
meteor add hschmaiske:create-typed-collection
meteor npm i zod
  1. Import the function: Import the createTypedCollection function into your TypeScript file.
import { createTypedCollection } from "meteor/hschmaiske:create-typed-collection";
  1. Define your ZodObject for the collection schema: Create a ZodObject that represents the expected structure of your collection documents.
const schema = z.object({
  name: z.string(),
  age: z.number(),
  hobbies: z.array(z.string()),
});
  1. Define your custom methods: Create an object with the custom methods you want to add to your collection. The methods will be added to the collection as static methods.
const customCollectionMethods = {
  async getPeopleWithHobby(hobby: string) {
    return People.find({ hobbies: hobby }).fetchAsync();
  },
};
  1. Create the Typed Collection: Call the createTypedCollection function with the name of your collection and the ZodObject representing the schema. The function returns a Meteor collection that adheres to the specified schema.
const People = createTypedCollection({
  name: "people",
  schema,
  customCollectionMethods
});
  1. Use the Typed Collection: You can now use the collection as you would any other Meteor collection. The collection documents will be type-safe and the custom methods will be available on the collection.
await People.insertAsync({ name: "John", age: 30, hobbies: ["hiking", "biking"] });

const peopleWithHikingHobby = await People.getPeopleWithHobby("hiking");
  1. Enjoy the benefits of type-safety: If you try to insert a document that does not adhere to the specified schema, you will get a TypeScript error. If you try to call a custom method that does not exist on the collection, you will get a TypeScript error.

Optional: You can also use the createTypedCollection function with an existing Meteor collection. This can be useful if you want to add custom methods to an existing collection. The function will return a Meteor collection that adheres to the specified schema and includes the custom methods.

const Users = createTypedCollection({
  instance: Meteor.users,
  schema,
  customCollectionMethods
})

API

Options

| Name | Type | Description | Required | | --- | --- | --- | --- | | name | string | The name of the collection. | Required if instance is not provided. | | schema | ZodObject | The ZodObject representing the schema of the collection documents. | Required. | | customCollectionMethods | object | An object with custom methods to add to the collection. | Optional. | | instance | Mongo.Collection | An existing Meteor collection to add custom methods to. | Optional. |

Returns

A Meteor collection that adheres to the specified schema and includes the custom methods.