Migrate to loopback-data

This commit is contained in:
Raymond Feng 2013-07-20 22:37:59 -07:00
parent e109adc737
commit 275405521b
4 changed files with 65 additions and 29 deletions

View File

@ -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;

View File

@ -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 <mail@anatoliy.in>",
"maintainers": [
{
"name": "Raymond Feng",
"url": "https://github.com/raymondfeng"
},
{
"name": "dgsan",
"url": "https://github.com/dgsan"

View File

@ -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');
});

View File

@ -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;
};