Permalink
Browse files

Mongoose order/limit/offset and more

- Lazy dependencies loading
- Travis mongoose inetration
- All tests should run on travis
- Model.count for mongoose
  • Loading branch information...
1 parent 393db58 commit 643c2879fef3195ecfc0fe5e7633d854a03e9316 @1602 committed Jan 30, 2012
View
@@ -6,3 +6,4 @@ before_install: git submodule init && git submodule --quiet update
before_script:
- "mysql -e 'create database myapp_test;'"
- "psql -c 'create database myapp_test;' -U postgres"
+ - mongo mydb_test --eval 'db.addUser("travis", "test");'
View
@@ -1,9 +1,13 @@
+var safeRequire = require('../utils').safeRequire;
+
/**
* Module dependencies
*/
-var mongoose = require('mongoose');
+var mongoose = safeRequire('mongoose');
exports.initialize = function initializeSchema(schema, callback) {
+ if (!mongoose) return;
+
if (!schema.settings.url) {
var url = schema.settings.host || 'localhost';
if (schema.settings.port) url += ':' + schema.settings.port;
@@ -113,36 +117,27 @@ MongooseAdapter.prototype.all = function all(model, filter, callback) {
if (!filter) {
filter = {};
}
- this._models[model].find(typeof filter.where === 'function' ? {} : filter.where, function (err, data) {
+ var query = this._models[model].find({});
+ if (filter.where) {
+ Object.keys(filter.where).forEach(function (k) {
+ query.where(k, filter.where[k]);
+ });
+ }
+ if (filter.order) {
+ query.asc(filter.order);
+ }
+ if (filter.limit) {
+ query.limit(filter.limit);
+ }
+ if (filter.skip) {
+ query.skip(filter.skip);
+ }
+ query.exec(function (err, data) {
if (err) return callback(err);
callback(null, data);
});
};
-function applyFilter(filter) {
- if (typeof filter === 'function') {
- return filter;
- }
- var keys = Object.keys(filter);
- return function (obj) {
- var pass = true;
- keys.forEach(function (key) {
- if (!test(filter[key], obj[key])) {
- pass = false;
- }
- });
- return pass;
- }
-
- function test(example, value) {
- if (typeof value === 'string' && example && example.constructor.name === 'RegExp') {
- return value.match(example);
- }
- // not strict equality
- return example == value;
- }
-}
-
MongooseAdapter.prototype.destroyAll = function destroyAll(model, callback) {
var wait = 0;
this._models[model].find(function (err, data) {
@@ -163,8 +158,8 @@ MongooseAdapter.prototype.destroyAll = function destroyAll(model, callback) {
};
-MongooseAdapter.prototype.count = function count(model, callback) {
- this._models[model].count(callback);
+MongooseAdapter.prototype.count = function count(model, callback, where) {
+ this._models[model].count(where || {}, callback);
};
MongooseAdapter.prototype.updateAttributes = function updateAttrs(model, id, data, cb) {
View
@@ -1,9 +1,13 @@
+var safeRequire = require('../utils').safeRequire;
+
/**
* Module dependencies
*/
-var mysql = require('mysql');
+var mysql = safeRequire('mysql');
exports.initialize = function initializeSchema(schema, callback) {
+ if (!mysql) return;
+
var s = schema.settings;
schema.client = mysql.createClient({
host: s.host || 'localhost',
View
@@ -299,7 +299,6 @@ function applyFilter(filter) {
if (typeof value === 'object' && value.constructor.name === 'Date' && typeof example === 'object' && example.constructor.name === 'Date') {
return example.toString() === value.toString();
}
- console.log(example,'==', value, example == value);
// not strict equality
return example == value;
}
View
@@ -1,10 +1,15 @@
+var safeRequire = require('../utils').safeRequire;
+
/**
* Module dependencies
*/
-var Client = require('pg').Client;
+var pg = safeRequire('pg');
var Hash = require('hashish');
exports.initialize = function initializeSchema(schema, callback) {
+ if (!pg) return;
+
+ var Client = pg.Client;
var s = schema.settings;
schema.client = new Client(s.url ? s.url : {
host: s.host || 'localhost',
View
@@ -1,9 +1,13 @@
+var safeRequire = require('../utils').safeRequire;
+
/**
* Module dependencies
*/
-var redis = require('redis');
+var redis = safeRequire('redis');
exports.initialize = function initializeSchema(schema, callback) {
+ if (!redis) return;
+
schema.client = redis.createClient(
schema.settings.port,
schema.settings.host,
@@ -1,6 +1,10 @@
-var Sequelize = require('sequelize');
+var safeRequire = require('../utils').safeRequire;
+
+var Sequelize = safeRequire('sequelize');
exports.initialize = function initializeSchema(schema, callback) {
+ if (!Sequelize) return;
+
schema.adapter = new SequelizeAdapter(schema);
process.nextTick(callback);
};
View
@@ -1,11 +1,15 @@
+var safeRequire = require('../utils').safeRequire;
+
/**
* Module dependencies
*/
-var sqlite3 = require('sqlite3').verbose();
+var sqlite3 = safeRequire('sqlite3');
exports.initialize = function initializeSchema(schema, callback) {
+ if (!sqlite3) return;
var s = schema.settings;
- var db = new sqlite3.Database(s.database);
+ var Database = sqlite3.verbose().Database;
+ var db = new Database(s.database);
schema.client = db;
View
@@ -1,28 +1,28 @@
{
"name": "jugglingdb",
"author": "Anatoliy Chakkaev",
- "description": "ORM for every database: redis, mysql, neo4j, mongodb, postgres",
+ "description": "ORM for every database: redis, mysql, neo4j, mongodb, postgres, sqlite",
"version": "0.0.8",
"repository": {
"url": "https://github.com/1602/jugglingdb"
},
"main": "index.js",
"scripts": {
- "test": "ONLY=memory ./support/nodeunit/bin/nodeunit test/*_test.* && ONLY=redis nodeunit test/common_test.js && ONLY=mysql nodeunit test/common_test.js && ONLY=postgres nodeunit test/common_test.js && ONLY=sqlite3 nodeunit test/common_test.js"
+ "test": "EXCEPT=sequelize nodeunit test/*_test*"
},
"engines": [
"node >= 0.4.0"
],
"dependencies": {
+ "hashish": "*"
+ },
+ "devDependencies": {
"redis": ">= 0.6.7",
"mongoose": ">= 2.2.3",
"mysql": ">= 0.9.4",
"sequelize": "*",
"pg": "*",
- "hashish": "*",
- "sqlite3": "*"
- },
- "devDependencies": {
+ "sqlite3": "*",
"nodeunit": ">= 0",
"coffee-script": ">= 0"
}
View
@@ -23,10 +23,8 @@ var schemas = {
database: ':memory:'
},
neo4j: { url: 'http://localhost:7474/' },
- // mongoose: { url: 'mongodb://localhost/test' },
- mongoose: {
- database: 'test'
- },
+ mongoose: { url: 'mongodb://travis:test@localhost:27017/myapp' },
+ // mongoose: { database: 'test' },
redis: {},
memory: {}
};
@@ -35,9 +33,10 @@ var specificTest = getSpecificTests();
Object.keys(schemas).forEach(function (schemaName) {
if (process.env.ONLY && process.env.ONLY !== schemaName) return;
+ if (process.env.EXCEPT && process.env.EXCEPT === schemaName) return;
context(schemaName, function () {
var schema = new Schema(schemaName, schemas[schemaName]);
- schema.log = console.log;
+ // schema.log = console.log;
testOrm(schema);
if (specificTest[schemaName]) specificTest[schemaName](schema);
});
@@ -2,7 +2,7 @@ juggling = require('../index')
Schema = juggling.Schema
Text = Schema.Text
-DBNAME = process.env.DBNAME || 'migrationtest'
+DBNAME = process.env.DBNAME || 'myapp_test'
DBUSER = process.env.DBUSER || 'root'
DBPASS = ''
DBENGINE = process.env.DBENGINE || 'mysql'

0 comments on commit 643c287

Please sign in to comment.