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

bson-schema-to-typescript

v0.0.26

Published

Compile MongoDB JSON Schema to TypeScript typings

Downloads

165

Readme

bson-schema-to-typescript

Compile MongoDB JSON Schema to TypeScript typings. This package connects to your MongoDB server, retrieves any stored JSON Schema validators, and compiles them into TypeScript typings.

For more information about MongoDB schema validation, see:

MongoDB Schema Validation and MongoDB $jsonSchema operator

Features

Written in TypeScript and includes type definitions.

Installation

# Yarn:
yarn add -D bson-schema-to-typescript

# NPM:
npm install bson-schema-to-typescript --save-dev

Configuration

Create a bson2ts.json configuration file in the root of your project to modify the configuration defaults. It is optional to provide a configuration file.

The default configuration is:

{
  "uri": "mongodb://localhost:27017",
  "database": "",
  "out": "src/__generated__",
  "bannerComment": [
    "/* eslint-disable */",
    "",
    "/**",
    "* This file was automatically generated by bson-schema-to-typescript.",
    "* https://www.npmjs.com/package/bson-schema-to-typescript",
    "*",
    "* Do not modify it by hand. Instead, modify the MongoDB $jsonSchema validator,",
    "* and run bson2ts to regenerate this file.",
    "*/"
  ],
  "enableConstEnums": true,
  "ignoreMinAndMaxItems": false,
  "strictIndexSignatures": false,
  "unknownAny": true
}

Connecting to MongoDB

The bson2ts CLI connects to the MongoDB server using the connection string provided by the uri configuration option, and the database name provided by the database configuration option.

Environment variables

If the uri or database configuration options start with a $ (dollar sign), they are treated as environment variables and will be expanded.

For example, consider this configuration:

{
  "uri": "mongodb://localhost:27017",
  "database": "$MONGODB_DATABASE"
}

In this case uri will be used as-is (eg. mongodb://localhost:27017) but database will be use the MONGODB_DATABASE environment variable of your system (eg. what ever process.env.MONGODB_DATABASE is set to).

In this case, uri will be used as-is (e.g. mongodb://localhost:27017), but database will use the MONGODB_DATABASE environment variable of your system (e.g. whatever process.env.MONGODB_DATABASE is set to).

CLI usage

Generate typings for each MongoDB collection.

Using npx:

npx bson-schema-to-typescript

Or add a script to package.json:

{
  "scripts": {
    "generate-types": "bson2ts"
  }
}

Then run `yarn generate-types` or `npm run generate-types`

Usage of generated typings

Once you have generated the types, you can implement them like this:

import { Db } from "mongodb";
import { UserDoc } from "./__generated__/UserDoc";
import { PostDoc } from "./__generated__/PostDoc";

export function collections(db: Db) {
  return {
    users: db.collection<UserDoc>("users"),
    posts: db.collection<PostDoc>("posts"),
  };
}

Programmatic usage

import {
  compileBSON,
  getCollectionSchema,
  getDatabaseSchemas,
} from "bson-schema-to-typescript";

Maintainers