grand-central-records
Advanced tools
Comparing version 0.1.3 to 0.1.4
@@ -139,2 +139,4 @@ // Custom ORM that connects with MySQL, Postgres, SQLite3 | ||
fn.toString = function() { | ||
if (!this.q) return ''; | ||
var other = " " + | ||
@@ -141,0 +143,0 @@ this.q.others.orderBy + " " + |
@@ -31,3 +31,3 @@ var _ = require('lodash'), | ||
Object.keys(options).forEach(function(key) { | ||
if (this.q) Object.keys(options).forEach(function(key) { | ||
if (_.contains(['action', 'from', 'where', 'table'], key)) { | ||
@@ -34,0 +34,0 @@ this.q[key] = (options[key] + ' ' + (value || '')).trim(); |
var _ = require('lodash'); | ||
var Q = require('q'); | ||
@@ -236,15 +235,21 @@ var Model = exports.Model = function(data, options, db) { | ||
if (callback) callback(err); | ||
else { | ||
var d = Q.defer(); | ||
d.reject(err); | ||
return d.promise; | ||
} | ||
else return this._db.reject(err); | ||
} | ||
var isNew = (this[this._options._idField] == -1); | ||
var method = isNew ? 'insert' : 'update'; | ||
var data = isNew ? this.toJSON() : this.changed(); | ||
if (callback) this._db[method](data, callback); | ||
else return this._db[method](data); | ||
// If model hasn't changed, don't update | ||
if (!isNew && _.isEmpty(data)) { | ||
if (callback) return callback(null, []); | ||
this._db.q = null; | ||
return this._db; | ||
// Otherwise run callback or return promise | ||
} else if (isNew) { | ||
if (callback) this._db.insert(data, callback); | ||
else return this._db.insert(data); | ||
} else { | ||
if (callback) this._db.update(this.id, data, callback); | ||
else return this._db.update(this.id, data); | ||
} | ||
}; | ||
@@ -251,0 +256,0 @@ |
@@ -6,2 +6,3 @@ var _ = require('lodash'); | ||
// Starts a promise chain | ||
@@ -31,2 +32,12 @@ fn.then = function(callback) { | ||
fn._err = null; | ||
// Call this elsewhere to reject promise chain | ||
fn.reject = function(err) { | ||
this._err = err; | ||
this.q = null; | ||
return this; | ||
}; | ||
// Runs callback or returns a promise | ||
@@ -33,0 +44,0 @@ fn.run = function(cb) { |
@@ -30,2 +30,6 @@ var _ = require('lodash'); | ||
newCallback(null, query); | ||
} else if (query === '') { | ||
callback(this._err, []); | ||
this._err = null; | ||
this._rebuild(); | ||
} else { | ||
@@ -32,0 +36,0 @@ this.query(query, values, newCallback); |
{ | ||
"name": "grand-central-records", | ||
"description": "An activerecord-inspired ORM for Node.js", | ||
"version": "0.1.3", | ||
"version": "0.1.4", | ||
"keywords": ["grand central", "rails", "orm", "activerecord"], | ||
@@ -6,0 +6,0 @@ "author": { |
@@ -50,2 +50,4 @@ # Grand Central Records | ||
* [Models](#models) | ||
* [Validations](#validations) | ||
* [Creating & Updating Models](#newmodels) | ||
* [Expansion of models](#expansion) | ||
@@ -286,2 +288,29 @@ * [reload()](#reload) | ||
<a name="newmodels" /> | ||
### Creating & updating models | ||
Creating a new model: | ||
```js | ||
var tiger = Animal.new({ name: 'Tiger' }); | ||
// Run validations and insert into DB: | ||
tiger.save(function(err) { ... }); | ||
// PROMISES // | ||
tiger.save().then(function(err) { ... }); | ||
``` | ||
Updating a model: | ||
```js | ||
Animal.where({ name: 'Tiger' }, function(err, animals) { | ||
animals[0].name = 'Siberian Tiger'; | ||
// Run validations and update in DB: | ||
animals[0].save(function(err) { ... }); | ||
}); | ||
// PROMISES // | ||
Animal.where({ name: 'Tiger' }).then(function(animals) { | ||
animals[0].name = 'Siberian Tiger'; | ||
return animals[0].save().run(); | ||
}).fail(function(err) { ... }); | ||
``` | ||
<a name="expansion" /> | ||
@@ -288,0 +317,0 @@ ### Expansion of models |
102878
2487
544