Stormer
The flexible Node.js ORM.
Purpose
Simplifies tasks such as creating and validating schemas for models as well as storing and getting entries from the database.
Contents
- Installation
- Quick Start
- Create instance
- Get instance
- Filter instances
- Update instance
- Schemas
- Errors
- Contributing
Installation
npm install stormer
Quick Start
1. Create a new store
var store = ;
2. Define a new model and its schema
var store = ; var userSchema = id: type: 'String' primaryKey: true firstName: 'String' age: type: 'Number' required: true ; store;
Supported types:
String
Number
Object
Array
Boolean
Date
(v0.9.0 and later)
2.1. Set an alias for a model name
const assert = ;const store = ;let model = id: type: 'String' primaryKey: true ; store;store; ;
3. Implement the required store methods
store { // Use pk to fetch single entry from your db of choice // Returns a Promise // Resolve the promise with the entry as the value if found // Resolve the promise with empty value if not found or reject with a NotFoundError }; store { // Use query to fetch multiple entries matching the query from your db of choice // Returns a Promise // Resolve the promise with an array of entries or an empty array if none is mathcing}; store { // Use obj to create or update the entry in the db of choice // Returns a Promise // Resolve the promise with the set obj};
Create instance
store;
Get instance
store;
Filter instances
store;
Update instance
store;
Schemas
Define a primary key
Any field can be designated as the primary key. Only one field can be designated as the primary key.
// Defines the 'id' field as the primary keyvar schema = id: type: 'String' primaryKey: true ;
Nested schemas a.k.a object types
// Defines an 'address'' property with nested schemavar schema = name: 'String' address: type: 'Object' streetName: 'String' streetNumber: 'Number' poBox: 'Number' ;
Define schemas with Array types
// Defines a 'friends' property with Array typevar schema = firstName: 'String' friends: type: 'Array' of: 'String' ;
Custom property validations
You can define a validate(value)
function on each property. The value
argument passed can be used to check the validity of the value and return either a truthy or falsy value. If a falsy value is returned then a CustomValidationError
is thrown.
// Defines a 'age' property with custom validationvar schema = age: type: 'Number' { return value > 0; } ;
Errors
You can import the errors using require('stormer').errors.<errorName>
-
TypeValidationError
: This error indicates that an operation failed because a schema property didn't conform with the designated type -
CustomValidationError
: This error indicates that an operation failed because a schema property failed a custom validation -
NotFoundError
: This error indicates that the object was not found in the store -
AlreadyExistsError
: This error indicates that the object already exists in the store
Contributing
This project is work in progress and we'd love more people contributing to it.
- Fork the repo
- Apply your changes
- Write tests
- Submit your pull request