A simple ORM for Node.js that takes care of the repetitive CRUD tasks, while providing a handy interface for custom queries.
- Accepts mongo-like query language to perform repretitive CRUD (+ count) tasks;
- Provides a handy interface for native queries, e.g. SQL;
- Leverages existing database meta-data instead of redefining the db schema in the application layer;
- Exposes promise and callback APIs.
$ npm install naomi
- Node.js v.4+
Connector | Maintainer | Build status | npm version |
---|---|---|---|
naomi-mysql | jmike |
Install naomi and the appropriate connector for your database, e.g. naomi-mysql
.
$ npm install naomi --save
$ npm install naomi-mysql --save
Register connector with Naomi under name "mysql".
var naomi = require('naomi');
var mysql = require('naomi-mysql');
naomi.register('mysql', mysql);
Use naomi#create() to create a new Database.
var db = naomi.create('mysql', {
host: 'host',
port: 3306,
user: 'user',
password: 'password',
database: 'schema',
});
Connect to database using Database#connect(). Please note you may use the callback interface instead of promises.
db.connect()
.then(() => {
console.log('connected');
})
.catch((err) => {
console.error(err);
});
Run custom queries using Database#execute().
var sql = ;
var params = ;
db.execute({
sql: 'SELECT `firstname`, `lastname` FROM `employees` WHERE `id` = ?;', // please note: ? will be replaced with 1 from params array
params: [1]
})
.then((results) => {
if (results.length === 0) {
console.warn('Not found');
} else {
console.log(results[0]);
}
})
.catch(function (err) {
console.error(err);
});
You wouldn't normally use an ORM to run custom queries. Naomi provides a Collection interface to map database collections (e.g. tables) to objects and run repretitive CRUD tasks.
Use Database#collection() to create a new Collection.
var employees = db.collection('employees', {
id: { type: 'integer', autoinc: true, min: 0 },
firstname: { type: 'string', maxLength: 45, nullable: true },
lastname: { type: 'string', maxLength: 45, nullable: true },
age: { type: 'integer', min: 18, max: 100 }
});
var employees = db.collection('employees');
employees.reverseEngineer()
.then(() => {
console.log('schema updated');
})
.catch(function (err) {
console.error(err);
});
The Collection class exposes the following methods:
- find() - retrieves records from the collection;
- findOne() - retrieves a single record from the collection;
- findStream() - retrieves records from the collection as a readable stream;
- count() - counts records in the collection;
- remove() - removes records from the collection;
- insert() - inserts record(s) to the collection;
- upsert() - upserts record(s) to the collection;
- update() - updates record(s) in the collection with the supplied payload.
Additional collection methods may exist depending on the database connector.
Databases, besides data, contain meta-data; stuff like keys
, datatypes
, indices
, constraints
and relations
.
These meta-data can be extracted from the database and are sufficient for generating basic validation rules and application structure. Yet most ORM tools tend to ignore meta-data living in the database and replicate that information in the application layer, which results to:
- Unnecessary complexity, i.e. you trade a native query language (like SQL) with an ORM-specific API that is equally complex;
- Synchronization issues, i.e. the sky falls on your head every time you change the database schema;
- Reduced expressiveness, i.e. no ORM can fully implement the expressiveness of a native query language.
Naomi is different in 2 ways:
- It provides methods to run repetitive CRUD (+ count) operations using a familiar mongo-like query language. When that's not enough it allows you run native queries directly to the database.
- It exposes simple methods to extract meta-data from the database, thus eliminating the need to recreate the information in the application layer and putting you (not the ORM) in charge of your schema.
With Naomi you have the freedom to create the database using a tool of your choice, e.g. MySQL Workbench or pgAdmin. While this approach may seem intriguing to new developers, it is in fact the natural way of thinking for experienced engineers. Creating a database requires creativity and imagination that machines lack. It is a task made for humans.
This project would not be without the extraordinary work of:
- Petka Antonov (https://github.com/petkaantonov/bluebird)
- Nicolas Morel (https://github.com/hapijs/joi)
- Felix Geisendörfer (https://github.com/felixge/node-mysql)
Source code contributions are most welcome. The following rules apply:
- Follow the Airbnb Style Guide;
- Make sure not to break the tests.
MIT