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

kore-schemas

v0.1.1

Published

a set of packages to build a complete scalable platform

Downloads

27

Readme

SWP Schemas

One schema to rule them all

Translators WIP

  • Mongoose
  • Avro
  • JSON Schema
  • Postgres Migrations

Providers

provide additionnal features to schemas, validations, data transformations

Definition

this section is a fork of mongoose schema documentation, thanks for this clear documentation

Each schema define an object

const blogSchema = {
    title:  String,
    author: String,
    body:   String,
    comments: [{ body: String, date: Date }], // arrays of nested objects
    comments2: { // same as comment
      type: Array,
      items { body: String, date: Date }
    },
    comments3: { // same as comment
      type: Array,
      items {
        type: Object,
        object: { body: String, date: Date }
      }
    },
    date: { type: Date, default: Date.now }, // types can have default values
    hidden: Boolean,
    meta: { // nested object
      votes: Number,
      favs:  Number
    },
    meta2: { // same as meta
      type: Object,
      object: {
        votes: Number,
        favs:  Number
      }
    }
  }

Each key in our code blogSchema (same as Object) defines a property in our documents which will be cast to its associated SchemaType. For example, we've defined a property title which will be cast to the String SchemaType and property date which will be cast to a Date SchemaType. Keys may also be assigned nested objects containing further key/type definitions like the meta property above.

The permitted SchemaTypes are:

  • String
  • Number
  • Date
  • Boolean
  • Array
  • Buffer
  • Object

Non Standard type using import {Types} from 'kore-schemas'

  • Types.ObjectId
  • Types.Mixed
const schema1 = {
  test: String // `test` is a path of type String
}

const schema2 = {
  test: { type: String } // `test` is a path of type String
}

Using Providers

In addition to the type property, using providers you can specify additional properties for a path. For example, if you want to lowercase a string before saving using stringUtils provider:

import stringUtils from 'kore-schemas/string-utils-provider' // WIP

const schema = {
  __providers:[stringUtils]
  test: {
    type: String,
    lowercase: true // Always convert `test` to lowercase
  }
}

The lowercase property only works for strings if you had stringUtils provider.

Providers can have certain options which apply for all schema types, and some that apply for specific schema types.

refers to providers usage documentations

certain options are available with default providers

  • required: boolean or function, if true adds a required validator for this property

  • default: Any or function, sets a default value for the path. If the value is a function, the return value of the function is used as the default.

  • validate: function, adds a validator function for this property

  • get: function, defines a custom getter for this property using Object.defineProperty().

  • set: function, defines a custom setter for this property using Object.defineProperty().

  • alias: Defines a virtual with the given name that gets/sets this path.

  • description: for documentation

  • namespace: part of the fullname of object (can be used in type system)

  • name: part of the fullname of object Define a name for this object namespace(org.foo).name(X) => org.foo.X

for compatibility with other translators a schema can have a __type property

const schema = {
  integerOnly: {
    type: Number,
    get: v => Math.round(v),
    set: v => Math.round(v),
    default: 0,
    alias: 'i'
  },
  // with this property we can generate documentation with docTranslator
  __type: {
    namespace: 'org.foo',
    name: 'IntegerWrapper',
    description: 'wrap an integer'
  }
}

const Number = buildSchema(schema)

const num = new Number()

num.i // 0
num.integerOnly // 0

num.integerOnly = 2.001
num.integerOnly // 2
num.i // 2