A basic ORM/ActiveRecord library for use in smaller Node projects or frameworks. Work in progress.
Creating a new instance of the GCR object creates a connection to a new database.
### model(table, [options])
- table
string
— The name of the table the model is associated with. - options
json
— See above.
Multiple models can also be created from the same database.
var GCR = require('grand-central-records');
var db = new GCR({
adapter: "mysql",
host: "localhost",
database: "test",
username: "admin",
password: "admin"
}, { verbose: true });
var User = db.model("users"),
Project = db.model("projects");
Promises
Execute a query using a callback:
Model.find(52, function(err, res) {...});
Model.find(52).select('id').run(function(err, res) {...});
Or by using promises:
Model.find(52)
.then(function(res) {
return res;
})
.then(function(res) {
})
.fail(function(err) {
throw err;
});
db.parallel([
Model.find(52).run(),
Model.where({ type: 2 }).run()
]).done(function(res) {
}).fail(function(err) {...});
Raw Queries
### query(query, [values], callback)
Execute raw query to database.
db.query('SELECT 1 AS a', function(err, res) {
console.log(res[0].a);
});
db.query('SELECT 1 AS a; SELECT %1 AS a;', ['hello'], function(err, res) {
console.log(res[0].a);
console.log(res[1].a);
});
db.query('SELECT :name AS a', { name: 'hello' }, function(err, res) {
console.log(res[0].a);
})
### queue()
Add query to queue for later execution. Query can be a raw query string, a chained method object, or an array of either. Values can't be passed to objects or arrays (only raw strings);
queue.add(query, [values])
queue.print() || queue.get()
queue.run(callback)
var queue = db.queue();
queue.add('SELECT 1 AS a')
.add('SELECT %1', [2])
.add('SELECT :name AS a', { name: 'hello' })
.run(function(err, res) {
console.log(res[0].a);
console.log(res[2].a);
});
queue.add(Model.find(1))
.add(Model.select('name').limit(1))
.run(function(err, res) {
console.log(res[0]);
console.log(res[1]);
});
queue.add(['SELECT 1 AS a', 'SELECT 2 AS a']);
. . .
console.log(queue.print());
queue.run(function(err, res) {
console.log(res[0].a);
console.log(res[1].a);
});
queue.run().then(function(res) {...})
.fail(function(err) {...});
## Models
To map query results to a model, pass map: true
to model options, like this:
var User = db.model('users', { map: true });
Define a schema for default values and validations [* validations not implemented yet].
var User = db.model('users', {
map: true,
schema: {
first: String,
last: String,
admin: { type: Boolean, default: false },
created_at: Date,
updated_at: Date
}
});
### Expansion of models
Methods are functions that can be called on the model.
var User = db.model('users', {
map: true,
methods: {
add: function(n) {
return this.number + n;
}
}
});
. . .
console.log(user.number);
console.log(user.add(5));
Getters are methods that are called immediately and act as regular values for a model. They can supplement or replace previous values.
var User = db.model('users', {
map: true,
getters: {
first: function() {
return this.first.toUpperCase();
},
fullName: function() {
return this.first + ' ' + this.last;
}
}
});
. . .
console.log(user.first);
console.log(user.last);
console.log(user.fullName);
### insert(data, [callback]), create()
- data
object
array
query
— An object of the data, with correctly named columns, to be inserted into the table. With Postgres & SQLite it also can be an array of objects or a subquery. - callback(err, rows) — Optional callback to run the query.
For Postgres, #insert() also returns the ID attribute.
Animal.insert({ name: 'Siberian Tiger', species: 'P. tigris altaica' });
Animal.insert([
{ name: 'Puma', species: 'Puma concolor' },
{ name: 'Jackalope' }
]);
Person.create(Client.select('name').where({ type: 'new' }));