@js-bits/model

0.0.7 • Public • Published

[WORK IN PROGRESS] Multi-purpose data model

Inspired by Backbone.Model. Re-imagined and modernized.

Model is a static data structure, defined by certain rules using a data schema.

const Profile = new Model({
  firstName: String,
  lastName: String,
  yearBorn: Number,
  'verified?': Boolean, // optional property
});

const author = new Profile({
  firstName: 'Trygve',
  lastName: 'Reenskaug',
  yearBorn: 1930,
});

console.log(`${author}`);
// [object Model]

console.log(JSON.stringify(author, null, '  '));
// {
//   firstName: 'Trygve',
//   lastName: 'Reenskaug',
//   yearBorn: 1930,
//   verified: null
// }

Key features:

  • Strict data schema
  • Runtime data validation
  • Configurable data types
  • Optional deep objects immutability
  • Interactive data store
  • ...

[TBD]

Installation

Install with npm:

npm install @js-bits/model

Install with yarn:

yarn add @js-bits/model

Import where you need it:

import { Model, DataType } from '@js-bits/model';

or require for CommonJS:

const { Model, DataType } = require('@js-bits/model');

How to use

[TBD]

More examples can be found in examples folder.

How is this different from JSON Schema?

JSON Schema serves more as a data format description, while Model is much more functional and gives you more control over data.

[TBD]

Ok. But what about TypeScript?

Code written in TypeScript is only checked for errors before it is executed, during compile time. Moreover, not every project is built with TypeScript. And not every project needs TypeScript.

[TBD]

As for IDE's code-completion capabilities, you can achieve similar result with JSDoc annotations.

Hmm... But we already have Zod

Sure. Model does something similar to what Zod does, but while Zod is "a schema declaration and validation library", Model goes further and is aimed to serve more like a data store. Also, Model uses more natural syntax.

[TBD]

GraphQL?

GraphQL queries tend to grow over time and you have to carry unnecessary data even though you don't use it. Model allows you to control what data exactly you need to operate.

[TBD]

How about immutability, then?

Well, immutability is not a dogma.

[TBD]

But, anyway, you can make your data deeply immutable with this package and still use other benefits (like data validation) at the same time.

Object.freeze(object); // shallow freeze
// versus
new CustomModel(object); // deep freeze

Package Sidebar

Install

npm i @js-bits/model

Weekly Downloads

2

Version

0.0.7

License

ISC

Unpacked Size

114 kB

Total Files

32

Last publish

Collaborators

  • apolo-gh