Giter VIP home page Giter VIP logo

meteor-create-typed-collection's Introduction

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.

meteor-create-typed-collection's People

Contributors

henriquealbert avatar

Stargazers

Raphael Bahuau avatar Thiago Marinho avatar vitor avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.