From 275405521bdc90f1d78a9890658621d2dfc5c593 Mon Sep 17 00:00:00 2001 From: Raymond Feng Date: Sat, 20 Jul 2013 22:37:59 -0700 Subject: [PATCH] Migrate to loopback-data --- lib/mysql.js | 49 +++++++++++++++++++++++++++++++++++++++---- package.json | 14 ++++++++----- test/imported.test.js | 4 ++-- test/init.js | 27 ++++++++---------------- 4 files changed, 65 insertions(+), 29 deletions(-) diff --git a/lib/mysql.js b/lib/mysql.js index 6fd4baa..2526264 100644 --- a/lib/mysql.js +++ b/lib/mysql.js @@ -2,9 +2,9 @@ * Module dependencies */ var mysql = require('mysql'); -var jdb = require('jugglingdb'); -var EnumFactory = require('./enumFactory').EnumFactory; +var jdb = require('loopback-data'); +var EnumFactory = require('./enumFactory').EnumFactory; exports.initialize = function initializeSchema(schema, callback) { if (!mysql) return; @@ -242,9 +242,50 @@ MySQL.prototype.escapeName = function (name) { return '`' + name.replace(/\./g, '`.`') + '`'; }; -MySQL.prototype.all = function all(model, filter, callback) { +MySQL.prototype.getColumns = function(model, props){ + var cols = this._models[model].properties; + var self = this; + var keys = Object.keys(cols); + if (Array.isArray(props) && props.length > 0) { + // No empty array, including all the fields + keys = props; + } else if ('object' === typeof props && Object.keys(props).length > 0) { + // { field1: boolean, field2: boolean ... } + var included = []; + var excluded = []; + keys.forEach(function (k) { + if (props[k]) { + included.push(k); + } else if ((k in props) && !props[k]) { + excluded.push(k); + } + }); + if (included.length > 0) { + keys = included; + } else if (excluded.length > 0) { + excluded.forEach(function (e) { + var index = keys.indexOf(e); + keys.splice(index, 1); + }); + } + } + var names = keys.map(function(c) { + return self.columnEscaped(model, c); + }); + return names.join(', '); +} - var sql = 'SELECT * FROM ' + this.tableEscaped(model); +MySQL.prototype.all = function all(model, filter, callback) { + // Order by id if no order is specified + filter = filter || {}; + if(!filter.order) { + var idNames = this.idNames(model); + if(idNames && idNames.length) { + filter.order = idNames.join(' '); + } + } + + var sql = 'SELECT '+ this.getColumns(model, filter.fields) + ' FROM ' + this.tableEscaped(model); var self = this; var props = this._models[model].properties; diff --git a/package.json b/package.json index 98b5d2c..736e2a5 100644 --- a/package.json +++ b/package.json @@ -1,13 +1,13 @@ { - "name": "jugglingdb-mysql", - "version": "0.0.1-6", - "description": "MySQL adapter for JugglingDB", + "name": "loopback-connector-mysql", + "version": "0.0.1", + "description": "MySQL adapter for Loopback Data", "main": "index.js", "scripts": { "test": "make test" }, "dependencies": { - "jugglingdb": ">= 0.2.0", + "loopback-data": "git+ssh://git@github.com:strongloop/loopback-data.git", "mysql": ">= 2.0.0-alpha3" }, "devDependencies": { @@ -18,10 +18,14 @@ }, "repository": { "type": "git", - "url": "https://github.com/1602/jugglingdb-mysql.git" + "url": "https://github.com/raymondfeng/jugglingdb-mysql.git" }, "author": "Anatoliy Chakkaev ", "maintainers": [ + { + "name": "Raymond Feng", + "url": "https://github.com/raymondfeng" + }, { "name": "dgsan", "url": "https://github.com/dgsan" diff --git a/test/imported.test.js b/test/imported.test.js index 5ef2910..5dcbfe9 100644 --- a/test/imported.test.js +++ b/test/imported.test.js @@ -4,7 +4,7 @@ describe('mysql imported features', function() { require('./init.js'); }); - require('jugglingdb/test/common.batch.js'); - require('jugglingdb/test/include.test.js'); + require('loopback-data/test/common.batch.js'); + require('loopback-data/test/include.test.js'); }); diff --git a/test/init.js b/test/init.js index ecf3c18..f4537d7 100644 --- a/test/init.js +++ b/test/init.js @@ -1,24 +1,15 @@ module.exports = require('should'); -var Schema = require('jugglingdb').Schema; +var Schema = require('loopback-data').Schema; -global.getConfig = function(options) { - - var dbConf = { +global.getSchema = function() { + var db = new Schema(require('../'), { + host: '127.0.0.1', + port: 3306, database: 'myapp_test', - username: 'root' - }; - - if (options) { - for (var el in options) { - dbConf[el] = options[el] - } - } - - return dbConf; -} - -global.getSchema = function(options) { - var db = new Schema(require('../'), getConfig(options)); + username: 'strongloop', + password: 'password' + }); + // db.log = function (a) { console.log(a); }; return db; };