Dovima.js
Dovima is a high-quality, stand-alone ORM written in ES6. We call it a "supermodel" because it gracefully does everything you expect an ORM to do, plus a whole lot of time-saving, code-cleaning extra features that make it an absolute pleasure to work with.
1. Installation
$ npm install dovima --save
You may wish to run the automated test suite to ensure that Dovima is fully compatible in your environment:
$ cd node_modules/dovima
$ npm test
2. Import / Require
// ES6
import Model from "dovima";
// ES5
var Model = require("dovima");
3. Getting Started
Dovima doesn't require centralized schema files for associations, validations, or anything else. You simply define a model, add the features you want to it, and use it.
3.1 Define a Model
// models/user.js;{}
// test.js;const userAttributes =name: "Bob Builder";const user = userAttributes;usernameshould;
3.2 Define Attribute Validations
// models/user.js;{this;}
// test.js;const user = ;// Check if the model is valid or notuser;// Get a list of all invalid attributes// and the reasons they are invaliduser;
3.3 Define Simple Associations
// models/user.js
import Model from "dovima";
import Article from "./models/article.js";
import Comment from "./models/comment.js";
class User extends Model {
associations() {
this.hasMany("articles", Article);
this.hasMany("comments", Comment);
this.hasMany("articleComments", Comment)
.through("articles");
}
}
// models/article.js
import Model from "dovima";
import User from "./models/user.js";
import Comment from "./models/comment.js";
class Article extends Model {
associations() {
this.belongsTo("user", User);
this.hasMany("comments", Comment);
}
}
// models/comment.js
import Model from "dovima";
import User from "./models/user.js";
import Article from "./models/article.js";
class Comment extends Model {
associations() {
this.belongsTo("user", User);
this.belongsTo("article", Article);
}
}
// test.js
import User from "./models/user.js";
import Article from "./models/article.js";
import Comment from "./models/comment.js";
const user = new User({
name: "Bob Builder"
});
const article = new Article({
title: "How to Build Things!",
user: user
});
const commentAttributes = {
user: user,
article: article,
text: "I really like this article."
};
const comment = new Comment(commentAttributes);
user.articles.push(article);
article.comments.push(comment);
const articleComment = user.articleComments[0];
articleComment.text.should.eql(commentAttributes.text);
3.4 Define Association Validations
// models/user.js;;{this;}{this;}
// test.js;const user = ;// Check if the model is valid or notuser;// Get a list of all invalid attributes// and the reasons they are invaliduser;
3.5 Define Custom Constructor
// models/user.js;{if optionsconvertToDogYearsthisage = attributesage * 7;}
// test.js;const user =age: 8convertToDogYears: true;userageshould;
3.6 Global Data Persistence
Note: Dovima utilizes a database adapter called almaden
to make calls to the database.
// models/user.js;;{this;}
// models/article.js;;{this;}
// test.js;;;;const databaseCredentials =client: "mysql"connection:host : "127.0.0.1"user : "myUser"password : "123456789"database : "test";const database = databaseCredentials;/*** Setting Model.database will cause all models to use it by default.*/Modeldatabase = database;/*** Set up the models*/const user =name: "Norman";const article =title: "Things Happening Around Town";userarticles;/*** Now, all models can .save(callback)*/user;
3.6.1 K'Nex Compatibility
almaden
is built on knex
, so if you have an existing knex connection you'd like to use instead of creating a new one, just use the following instead of passing credentials:
;;;const databaseCredentials =client: "mysql"connection:host : "127.0.0.1"user : "myUser"password : "123456789"database : "test";const database = ;/*** Use existing knex connection*/Modeldatabase =knex: database;
3.7 Finding Models in Database
// models/user.js;;{this;}
// models/article.js;;{this;}
// test.js;;;;const databaseCredentials =client: "mysql"connection:host : "127.0.0.1"user : "myUser"password : "123456789"database : "test";Modeldatabase = databaseCredentials;Userfindone;
3.8 Mocking Model Find Chain
// models/user.js;{}
// test.js;Usermockfindone;Userfindone;
3.9 Mocking Model Instance
// models/user.js;{}
// test.js;const user = id: 1;usermock;user;