diff --git a/.travis.yml b/.travis.yml index e16441f3..7eb1eb01 100644 --- a/.travis.yml +++ b/.travis.yml @@ -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");' diff --git a/lib/adapters/mongoose.js b/lib/adapters/mongoose.js index 329eb59c..69fbb573 100644 --- a/lib/adapters/mongoose.js +++ b/lib/adapters/mongoose.js @@ -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) { diff --git a/lib/adapters/mysql.js b/lib/adapters/mysql.js index 957a7195..981a6696 100644 --- a/lib/adapters/mysql.js +++ b/lib/adapters/mysql.js @@ -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', diff --git a/lib/adapters/neo4j.js b/lib/adapters/neo4j.js index 22be99f8..50202eac 100644 --- a/lib/adapters/neo4j.js +++ b/lib/adapters/neo4j.js @@ -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; } diff --git a/lib/adapters/postgres.js b/lib/adapters/postgres.js index 69ced489..e2e1497c 100644 --- a/lib/adapters/postgres.js +++ b/lib/adapters/postgres.js @@ -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', diff --git a/lib/adapters/redis.js b/lib/adapters/redis.js index 09f5572a..da042f7d 100644 --- a/lib/adapters/redis.js +++ b/lib/adapters/redis.js @@ -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, diff --git a/lib/adapters/sequelize.js b/lib/adapters/sequelize.js index a88c4ee4..41d8aef6 100644 --- a/lib/adapters/sequelize.js +++ b/lib/adapters/sequelize.js @@ -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); }; diff --git a/lib/adapters/sqlite3.js b/lib/adapters/sqlite3.js index 3c6fac3b..6e318c54 100644 --- a/lib/adapters/sqlite3.js +++ b/lib/adapters/sqlite3.js @@ -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; diff --git a/package.json b/package.json index 295fb322..26698b78 100644 --- a/package.json +++ b/package.json @@ -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" } diff --git a/test/common_test.js b/test/common_test.js index 008e6bdc..afaca696 100644 --- a/test/common_test.js +++ b/test/common_test.js @@ -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); }); diff --git a/test/migration_test.coffee b/test/migration_test.coffee index 9aeb89f4..9bf25532 100644 --- a/test/migration_test.coffee +++ b/test/migration_test.coffee @@ -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'