Skip to content

Commit

Permalink
rename method find to findById
Browse files Browse the repository at this point in the history
  • Loading branch information
biggora committed May 8, 2013
1 parent fd0975b commit aa2257e
Show file tree
Hide file tree
Showing 15 changed files with 155 additions and 64 deletions.
62 changes: 50 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,39 @@ most popular database formats.
#### CaminteJS adapters:
mongoose, mysql, sqlite3, riak, postgres, couchdb, mongodb, redis, neo4j, firebird, nano

## Usage

## Installation

First install [node.js](http://nodejs.org/). Then:

$ npm install caminte

## Overview

### Connecting to DB

First, we need to define a connection.

```javascript
var caminte = require('caminte'),
Schema = caminte.Schema,
db = {
driver : "mysql",
host : "localhost",
port : "3306",
username : "test",
password : "test",
database : "test"
};

var schema = new Schema(db.driver, db);
```

### Defining a Model

Models are defined through the `Schema` interface.

```javascript
var Schema = require('caminte').Schema;
var db: {
driver : "mysql",
host : "localhost",
port : "3306",
username : "test",
password : "test",
database : "test"
};
var schema = new Schema(db.driver, db); // port number depends on your configuration
// define models
var Post = schema.define('Post', {
title: { type: String, length: 255 },
Expand All @@ -36,6 +56,24 @@ var User = schema.define('User', {
joinedAt: Date,
age: Number
});
```




## Usage

```javascript
var Schema = require('caminte').Schema;
var db: {
driver : "mysql",
host : "localhost",
port : "3306",
username : "test",
password : "test",
database : "test"
};
var schema = new Schema(db.driver, db); // port number depends on your configuration

// define any custom method
User.prototype.getNameAndAge = function () {
Expand Down Expand Up @@ -91,7 +129,7 @@ user.posts.build
// save as Post.create({userId: user.id}, cb);
user.posts.create(cb)
// find instance by id
User.find(1, cb)
User.findById(1, cb)
// count instances
User.count({where: {userId: user.id}}, cb)
// destroy instance
Expand Down
89 changes: 71 additions & 18 deletions lib/abstract-class.js
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ AbstractClass.upsert = AbstractClass.updateOrCreate = function upsert(data, call
callback(err, obj);
});
} else {
this.find(data.id, function (err, inst) {
this.findById(data.id, function (err, inst) {
if (err) return callback(err);
if (inst) {
inst.updateAttributes(data, callback);
Expand Down Expand Up @@ -318,16 +318,64 @@ AbstractClass.exists = function exists(id, callback) {
}
};

/**
* Find instances of Model, matched by query
* make sure you have marked as `index: true` fields for filter or sort
*
* @param {Object} params (optional)
*
* - where: Object `{ key: val, key2: {gt: 'val2'}}`
* - include: String, Object or Array. See AbstractClass.include documentation.
* - order: String
* - limit: Number
* - skip: Number
*
* @param {Function} callback (required) called with arguments:
*
* - err (null or Error)
* - Array of instances
*/
AbstractClass.find = function find(params, callback) {
if (stillConnecting(this.schema, this, arguments)) return null;
if(typeof params === 'undefined') {
return this;
} else {
if (arguments.length === 1) {
callback = params;
params = {};
}

params = buildQuery(params, this);

var constr = this;
this.schema.adapter.all(this.modelName, params, function (err, data) {
if (data && data.map) {
data.forEach(function (d, i) {
var obj = new constr;
obj._initProperties(d, false);
data[i] = obj;
});
if (data && data.countBeforeLimit) {
data.countBeforeLimit = data.countBeforeLimit;
}
callback(err, data);
}
else
callback(err, []);
});
}
};

/**
* Find object by id
*
* @param {id} id - primary key value
* @param {Function} callback - callback called with (err, instance)
*/
AbstractClass.find = function find(id, callback) {
AbstractClass.findById = function findById(id, callback) {
if (stillConnecting(this.schema, this, arguments)) return;

this.schema.adapter.find(this.modelName, id, function (err, data) {
this.schema.adapter.findById(this.modelName, id, function (err, data) {
var obj = null;
if (data) {
if (!data.id) {
Expand Down Expand Up @@ -357,7 +405,7 @@ AbstractClass.find = function find(id, callback) {
* - err (null or Error)
* - Array of instances
*/
AbstractClass.run = AbstractClass.all = function all(params, callback) {
AbstractClass.exec = AbstractClass.run = AbstractClass.all = function all(params, callback) {
if (stillConnecting(this.schema, this, arguments)) return;

if (arguments.length === 1) {
Expand Down Expand Up @@ -392,18 +440,23 @@ AbstractClass.run = AbstractClass.all = function all(params, callback) {
* @param {Function} callback - callback called with (err, instance)
*/
AbstractClass.findOne = function findOne(params, callback) {
if (stillConnecting(this.schema, this, arguments)) return;
if (stillConnecting(this.schema, this, arguments)) return null;
if(typeof params === 'undefined') {
this._params.limit = 1;
return this;
} else {
if (typeof params === 'function') {
callback = params;
params = {};
}
params = buildQuery(params, this);
params.limit = 1;

if (typeof params === 'function') {
callback = params;
params = {};
this.all(params, function (err, collection) {
if (err || !collection || !collection.length > 0) return callback(err, null);
callback(err, collection[0]);
});
}
params = buildQuery(params, this);
params.limit = 1;
this.all(params, function (err, collection) {
if (err || !collection || !collection.length > 0) return callback(err, null);
callback(err, collection[0]);
});
};

function substractDirtyAttributes(object, data) {
Expand Down Expand Up @@ -877,7 +930,7 @@ AbstractClass.prototype.propertyChanged = function propertyChanged(attr) {
AbstractClass.prototype.reload = function reload(callback) {
if (stillConnecting(this.constructor.schema, this, arguments)) return;

this.constructor.find(this.id, callback);
this.constructor.findById(this.id, callback);
};

/**
Expand Down Expand Up @@ -934,7 +987,7 @@ AbstractClass.hasMany = function hasMany(anotherClass, params) {
anotherClass.schema.defineForeignKey(anotherClass.modelName, fk);

function find(id, callback) {
anotherClass.find(id, function (err, inst) {
anotherClass.findById(id, function (err, inst) {
if (err) return callback(err);
if (!inst) return callback(new Error('Not found'));
if (inst[fk] == this.id) {
Expand All @@ -946,7 +999,7 @@ AbstractClass.hasMany = function hasMany(anotherClass, params) {
}

function destroy(id, callback) {
this.find(id, function (err, inst) {
this.findById(id, function (err, inst) {
if (err) return callback(err);
if (inst) {
inst.destroy(callback);
Expand Down Expand Up @@ -1001,7 +1054,7 @@ AbstractClass.belongsTo = function (anotherClass, params) {
cb(null, null);
return;
}
anotherClass.find(id, function (err,inst) {
anotherClass.findById(id, function (err,inst) {
if (err) return cb(err);
if (!inst) return cb(null, null);
if (inst.id === this[fk]) {
Expand Down
10 changes: 5 additions & 5 deletions lib/adapters/cradle.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ function idealize(data) {
return data;
}
function stringify(data) {
return data ? data.toString() : data
return data ? data.toString() : data
}

function errorHandler(callback, func) {
Expand Down Expand Up @@ -232,14 +232,14 @@ CradleAdapter.prototype.updateOrCreate = function(model, data, callback) {
*/
CradleAdapter.prototype.exists = function(model, id, callback) {
this.client.get(
stringify(id),
stringify(id),
errorHandler(callback, function(doc, cb) {
cb(!!doc);
cb(!!doc);
})
);
};

CradleAdapter.prototype.find = function(model, id, callback) {
CradleAdapter.prototype.findById = function findById(model, id, callback) {
this.client.get(
stringify(id),
errorHandler(callback, function(doc, cb) {
Expand Down Expand Up @@ -314,7 +314,7 @@ CradleAdapter.prototype.destroyAll = function(model, callback) {
callback,
function(docs, cb) {
var docIds = docs.map(function(doc) {
return doc.id;
return doc.id;
});
this.client.get(docIds, function(err, res) {
if(err) cb(err);
Expand Down
2 changes: 1 addition & 1 deletion lib/adapters/firebird.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ FB.prototype.save = function (name, data, callback) {
this.client.execute(sql, params, callback);
};

FB.prototype.find = function find(name, id, callback) {
FB.prototype.findById = function findById(name, id, callback) {
var table = this.schema.tableName(name);
var sql = 'SELECT FIRST 1 * FROM ' + quote(table) + ' WHERE "id" = ?';
this.client.query(sql, id,
Expand Down
2 changes: 1 addition & 1 deletion lib/adapters/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ WebService.prototype.exists = function exists(model, id, callback) {
});
};

WebService.prototype.find = function find(model, id, callback) {
WebService.prototype.findById = function findById(model, id, callback) {
$.getJSON(this.getResourceUrl(model) + '/' + id + '.json', function (res) {
if (res.code === 200) {
callback(null, res.data);
Expand Down
2 changes: 1 addition & 1 deletion lib/adapters/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ Memory.prototype.exists = function exists(model, id, callback) {
}.bind(this));
};

Memory.prototype.find = function find(model, id, callback) {
Memory.prototype.findById = function findById(model, id, callback) {
process.nextTick(function () {
callback(null, this.cache[model][id]);
}.bind(this));
Expand Down
2 changes: 1 addition & 1 deletion lib/adapters/mongodb.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ MongoDB.prototype.exists = function (model, id, callback) {
});
};

MongoDB.prototype.find = function find(model, id, callback) {
MongoDB.prototype.findById = function findById(model, id, callback) {
if (typeof id === 'string') {
id = new ObjectID(id);
}
Expand Down
2 changes: 1 addition & 1 deletion lib/adapters/mongoose.js
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ MongooseAdapter.prototype.findOne = function findOne(model, filter, fields, opti
});
};

MongooseAdapter.prototype.find = function find(model, id, callback) {
MongooseAdapter.prototype.findById = function find(model, id, callback) {
delete this.cache[model][id];
this.getCached(model, id, function (err, data) {
if (err) {
Expand Down
4 changes: 2 additions & 2 deletions lib/adapters/nano.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions lib/adapters/neo4j.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ Neo4j.prototype.updateIndexes = function updateIndexes(model, node, cb) {

Neo4j.prototype.save = function save(model, data, callback) {
var self = this;

this.node(data.id, function (err, node) {
//delete id property since that's redundant and we use the node.id
delete data.id;
Expand All @@ -223,7 +223,7 @@ Neo4j.prototype.exists = function exists(model, id, callback) {
this.node(id, callback);
};

Neo4j.prototype.find = function find(model, id, callback) {
Neo4j.prototype.findById = function findById(model, id, callback) {
delete this.cache[id];
this.node(id, function (err, node) {
if (node && node.data) {
Expand Down Expand Up @@ -347,7 +347,7 @@ Neo4j.prototype.updateAttributes = function updateAttributes(model, id, data, cb

function cleanup(data) {
if (!data) return null;

var res = {};
Object.keys(data).forEach(function (key) {
var v = data[key];
Expand Down
2 changes: 1 addition & 1 deletion lib/adapters/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ BridgeToRedis.prototype.exists = function (model, id, callback) {
});
};

BridgeToRedis.prototype.find = function find(model, id, callback) {
BridgeToRedis.prototype.findById = function findById(model, id, callback) {
var t1 = Date.now();
this.client.hgetall(model + ':' + id, function (err, data) {
this.log('HGETALL ' + model + ':' + id, t1);
Expand Down
2 changes: 1 addition & 1 deletion lib/adapters/redis2.js
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ BridgeToRedis.prototype.exists = function (model, id, callback) {
});
};

BridgeToRedis.prototype.find = function find(model, id, callback) {
BridgeToRedis.prototype.findById = function findById(model, id, callback) {
this.client.hgetall(model + ':' + id, function (err, data) {
if (data && Object.keys(data).length > 0) {
data.id = id;
Expand Down
2 changes: 1 addition & 1 deletion lib/adapters/riak.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Riak.prototype.exists = function (model, id, callback) {
});
};

Riak.prototype.find = function find(model, id, callback) {
Riak.prototype.findById = function findById(model, id, callback) {
this.client.get(model, id, function (err, data, meta) {
if (data && data.id) {
data.id = id;
Expand Down
Loading

0 comments on commit aa2257e

Please sign in to comment.