New kind of NodeJS ORM.
- Use newest feature of ECMAScript (modern javascript).
- Universal database connector (MongoDB, SQL, Redis, REST, CSV...).
- Could create powerful plugin using the "class" inheritance.
const ilorm = require('ilorm');
const schema = ilorm.schema;
const schema = schema.new({
firstName: schema.String().required(),
lastName: schema.String().required(),
children: schema.Array(schema.reference('User'))
birthday: schema.Date(),
weight: schema.Number().min(5).max(500)
});
Function | Description |
---|---|
static new( schema ) | Create a new ilorm schema. |
static string() | Instantiate a SchemaField/String |
static number() | Instantiate a SchemaField/Number |
static boolean() | Instantiate a SchemaField/Boolean |
static date() | Instantiate a SchemaField/Date |
static reference() | Instantiate a SchemaField/Reference |
All SchemaField are children of the class SchemaField. This class contains this method :
Function | Description |
---|---|
required() | The field is required for create an object (per default not required). |
default(value ) |
Set a precise value for default (if you do not set a value at creation). |
Represent a javascript number.
Represent a javascript string.
Represent a javascript boolean.
Represent a javascript date.
Represent a javascript reference to another instance.
const ilorm = require('ilorm');
const ilormMongo = require('ilorm-connector-mongo');
const model = require('ilorm').model;
const userSchema = require('./schema');
const userModel = model('User', userModel, ilormMongo({ db }));
userModel.query()
.firstName.is('Smith')
.findOne()
.then(user => {
user.weight = 30;
return user.save();
});
Function | Description |
---|---|
query() | Instantiate a Query targeting the current Model |
In a query, every field present in the schema could be use to build the query. The [field] part in further documentation are every field declared in your specific schema.
// if your schema is something like this ;
const schema = ilorm.schema({
firstName: ilorm.String().required(),
});
// You could write query like this :
const user = await User.query()
.firstName.is('Smith')
.findOne();
Function | Description |
---|---|
[field].is(value) | Check if the field is equal value |
[field].isNot(value) | Check if the field is not equal with value |
[field].isIn(arrayOfValue) | Check if the field value is one of the array value |
[field].isNotIn(arrayOfValue) | Check if the field value is none of array value |
[field].between(min, max) | Check if the value is between min and max (include) |
[field].min(value) | Check if the value is equal or superior than the value |
[field].max(value) | Check if the value is equal or inferior than the value |
[field].linkedWith(value) | Check if the field (reference) is linked with another model, id, ... |
Used for update or updateOne query only :
Function | Description |
---|---|
[field].set(value ) |
Set the value of the field |
[field].inc(value ) |
Incremente the value of the field by the given value |
Function | Description |
---|---|
find() | Run the query and return a promise with the result (array of instance). |
findOne() | Run the query and return a promise the result (instance). |
count() | Count the number of instance and return it. |
stream() | Run the query with a stream context could be the best solution for big query. |
remove() | Remove the instance which match the query. |
removeOne() | Remove only one instance which math the query. |
update() | Used to update many instance. |
updateOne() | Used to update one instance. |
userModel.query()
.firstName.is('Smith')
.weight.set(30)
.update();
userModel.query()
.stream() //Return a standard stream
.pipe(otherStream);
Instances are returned after a loading (find, or stream). It's a specific item loaded from the database. You can create a new instance from the model :
const instance = new userModel();
instance.firstName = 'Thibauld';
instance.lastName = 'Smith';
instance.save();
Function | Description |
---|---|
save() | Save the instance in the database (auto insert or update) |
remove() | Delete the instance from the database |