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

data-model

v0.0.2

Published

Javascript data schemas. Validate data structures, ensure data types, assign virtual methods to datum, generate virtual properties for a data set.

Downloads

20

Readme

data-model

Javascript data schemas. Validate data structures, ensure data types, assign virtual methods, generate virtual properties for a data set.

Installing

Via Bower

bower install data-model --save

Via NPM

npm install data-model --save

If you are using Node.js

var datamodel = require('data-model');

Creating a Dataset Model

The dataset schema is to define what the expected format of a dataset is to be. There are three patterns that are used: Collections, Schemas (and nested schemas), and primitives. Further, there are two ways to define a schema: Caching, or instance.

Primitive Pattern

Primitives are the most basic element, and end recursive checks. You use primitives to define the type pattern required for proper use.

Example:A string

datamodel('personName')
.setTemplate(String);

Example:An array of strings

datamodel('personName')
.setTemplate([String]);

Example:An array objects with a string property

datamodel('personName')
.setTemplate([{ name:String }]);

Schema Pattern

A schema definition has a similar meaning to what what is understood as the Object javascript primitive. It is a definition of a key/value data structure, and uses the Object primitive syntax for defining the pattern. Nested schemas are supported for complex objects.

Example: A basic schema

datamodel('person')
.setTemplate({
	name:String,
	age:Number
});

Example: A nested Schema

datamodel('person')
.setTemplate({
	name:String,
	details:{
		age:Number
	}
});

Collection Pattern

A collection defines repetition in a data set. The collection is denoted with the array primitive syntax.

Example: Describing an array of simple objects

datamodel('pageHits')
.setTemplate([{
	name:String,
	hits:[Number]
}]);

Example: Describe a simple object with an array

datamodel('myHits')
.setTemplate({
	name:String,
	hits:[Number]
});

Validating Against Models

Examples

Example 1:

datamodel('personName')
.setTemplate(String);

var ValidData = "Jordan";
var InvalidData = 23; //wrong type...

datamodel('personName')
.validate(ValidData); //true

datamodel('personName')
.validate(InvalidData); //false

Example 2:

datamodel('personName')
.setTemplate([String]);

var ValidData = ["Jordan", "Chapian"];
var InvalidData = [23, "Jordan"]; //wrong type

datamodel('personName')
.validate(ValidData); //true

datamodel('personName')
.validate(InvalidData); //false

Example 3:

datamodel('personName')
.setTemplate([{name:String}]);

var ValidData = [{name:"Jordan"}, {name:"Chapian"}];
var InvalidData = [{name:"Jordan"}, "Jordan"]; //wrong type

datamodel('personName')
.validate(ValidData); //true

datamodel('personName')
.validate(InvalidData); //false

Example 4:

datamodel('person')
.setTemplate({
	name:String,
	age:Number
});

var ValidData = {name:'Jordan', age:10};
var InvalidData = {name:'Jordan'}; //missing age..

datamodel('person')
.validate(ValidData); //true

datamodel('person')
.validate(InvalidData); //false

Example 5:

datamodel('person')
.setTemplate({
	name:String,
	details:{
		age:Number
	}
});

var ValidData = {name:'Jordan', details:{age:10}};
var InvalidData = {name:'Jordan', details:{age:"10"}}; //age, wrong type..

datamodel('person')
.validate(ValidData); //true

datamodel('person')
.validate(InvalidData); //false

Example 6:

datamodel('pageHits')
.setTemplate([{
	name:String,
	hits:[Number]
}]);

var ValidDataset = [{name:"Jordan", hits:[5,7,6]}, {name:"Chapian", hits:[]}];
var InvalidDataset = [{name:"Jordan", hits:["5", 7, 6]}, {name:"Chapian", hits:[8]}];

datamodel('pageHits')
.validate(ValidDataset); //true

datamodel('pageHits')
.validate(InvalidDataset); //false

Example 7:

datamodel('myHits')
.setTemplate({
	name:String,
	hits:[Number]
});

var ValidDataset = {name:"Jordan", hits:[5, 10, 15, 20]};
var InvalidDataset =  {name:"Jordan", hits:12};

datamodel('myHits')
.validate(ValidDataset); //true

datamodel('myHits')
.validate(InvalidDataset); //false

Internal Representation

A graph is used to represent a data structure, where each node is represented by one of three subclasses of a TemplateNode. Recursive patterns are used to traverse the structures. Consider the below model specification.

datamodel('users')
.setTemplate([{
    name:String,
    email:String,
    posts:[{
    	title:String,
    	views:Number,
    	body:Number,
    	posted:Date
    }],
    loginPosts:[[Number]]
}]);

This is represented by the following graph structure:

gs