A rekord binding to Knex.js.
Structure
The suggested structure for using this library is as follows:
- src/connections.js: creates and exports knex objects
- src/models.js: takes connection objects and defines & exports models
Example
// =============================================================================
// connections.js
// =============================================================================
exports.main = require('knex')({
client: 'sqlite3',
useNullAsDefault: true,
connection: {
filename: "./mydb.sqlite"
}
});
// =============================================================================
// models.js
// =============================================================================
var connections = require('./connections.js');
var Rekord = require('rekord');
var Task = Rekord({
name: 'task',
api: connections.main, // api must be a knex connection
fields: ['name', 'done'],
defaults: {
done: false
}
});
// Load models from connections (where load option is used)
Rekord.load();
// Export defined models
exports.Task = Task;
// =============================================================================
// example.js
// =============================================================================
var Rekord = require('rekord');
var Task = require('./models.js').Task;
var task = new Task({
name: 'Better Examples'
});
Rekord.Promise
.then(function() {
return task.$save();
})
.then(function(saved) {
// task saved! let the user know or return a function which returns a promise
})
.catch(function(error) {
// handle an error!
})
;