Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Waterline 0.12 Upgrades #294

Merged
merged 5 commits into from
Mar 18, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 34 additions & 29 deletions lib/adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ module.exports = (function() {


// Direct access to query
query: function(connectionName, collectionName, query, data, cb, connection) {
query: function(connectionName, collectionName, query, data, cb, noop, connection) {

if (_.isFunction(data)) {
cb = data;
Expand All @@ -111,7 +111,7 @@ module.exports = (function() {

// Fetch the schema for a collection
// (contains attributes and autoIncrement value)
describe: function(connectionName, collectionName, cb, connection) {
describe: function(connectionName, collectionName, cb, noop, connection) {

if(_.isUndefined(connection)) {
return spawnConnection(connectionName, __DESCRIBE__, cb);
Expand Down Expand Up @@ -187,7 +187,7 @@ module.exports = (function() {
},

// Create a new collection
define: function(connectionName, collectionName, definition, cb, connection) {
define: function(connectionName, collectionName, definition, cb, noop, connection) {
var self = this;

if(_.isUndefined(connection)) {
Expand Down Expand Up @@ -242,7 +242,7 @@ module.exports = (function() {
},

// Drop an existing collection
drop: function(connectionName, collectionName, relations, cb, connection) {
drop: function(connectionName, collectionName, relations, cb, noop, connection) {

if(typeof relations === 'function') {
cb = relations;
Expand Down Expand Up @@ -291,7 +291,7 @@ module.exports = (function() {
},

//
addAttribute: function (connectionName, collectionName, attrName, attrDef, cb, connection) {
addAttribute: function (connectionName, collectionName, attrName, attrDef, cb, noop, connection) {

if(_.isUndefined(connection)) {
return spawnConnection(connectionName, __ADD_ATTRIBUTE__, cb);
Expand Down Expand Up @@ -321,7 +321,7 @@ module.exports = (function() {
},

//
removeAttribute: function (connectionName, collectionName, attrName, cb, connection) {
removeAttribute: function (connectionName, collectionName, attrName, cb, noop, connection) {

if(_.isUndefined(connection)) {
return spawnConnection(connectionName, __REMOVE_ATTRIBUTE__, cb);
Expand Down Expand Up @@ -355,7 +355,7 @@ module.exports = (function() {
// (that is unless you want some enhanced functionality-- then please be my guest!)

// Create one or more new models in the collection
create: function(connectionName, collectionName, data, cb, connection) {
create: function(connectionName, collectionName, data, cb, noop, connection) {

if(_.isUndefined(connection)) {
return spawnConnection(connectionName, __CREATE__, cb);
Expand Down Expand Up @@ -390,7 +390,7 @@ module.exports = (function() {

// Run query
log('MySQL.create: ', _query.query);

if(!connection.query) { console.log('CONNECTION', connection); }
connection.query(_query.query, function(err, result) {
if (err) return cb( handleQueryError(err) );

Expand All @@ -416,7 +416,7 @@ module.exports = (function() {

// Override of createEach to share a single connection
// instead of using a separate connection for each request
createEach: function (connectionName, collectionName, valuesList, cb, connection) {
createEach: function (connectionName, collectionName, valuesList, cb, noop, connection) {

if(_.isUndefined(connection)) {
return spawnConnection(connectionName, __CREATE_EACH__, cb);
Expand Down Expand Up @@ -499,7 +499,7 @@ module.exports = (function() {
* @param {[type]} cb [description]
* @return {[type]} [description]
*/
join: function (connectionName, collectionName, options, cb, connection) {
join: function (connectionName, collectionName, options, cb, noop, connection) {

if(_.isUndefined(connection)) {
return spawnConnection(connectionName, __JOIN__, cb);
Expand All @@ -525,7 +525,7 @@ module.exports = (function() {
* @param {Function} _cb
*/
$find: function (collectionName, criteria, _cb) {
return adapter.find(connectionName, collectionName, criteria, _cb, client);
return adapter.find(connectionName, collectionName, criteria, _cb, noop, client);
},

/**
Expand Down Expand Up @@ -698,30 +698,35 @@ module.exports = (function() {

parentRecords.forEach(function(parent) {
if(_.isNumber(parent[pk])) {
qs += q.qs.replace('^?^', parent[pk]) + ' UNION ';
qs += q.qs.replace('^?^', parent[pk]) + ' UNION ALL ';
} else {
qs += q.qs.replace('^?^', '"' + parent[pk] + '"') + ' UNION ';
qs += q.qs.replace('^?^', '"' + parent[pk] + '"') + ' UNION ALL ';
}
});

// Remove the last UNION
qs = qs.slice(0, -7);
qs = qs.slice(0, -10);

// Add a final sort to the Union clause for integration
if(parentRecords.length > 1) {
var addedOrder = false;

function addSort(sortKey, sorts) {
if (!sortKey.match(/^[0-9,a-z,A-Z$_]+$/)) {
return;
}
if (!addedOrder) {
addedOrder = true;
qs += ' ORDER BY ';
}
// ID sorts are ambiguous
if(sortKey === 'id') {
return;
}

if (!sortKey.match(/^[0-9,a-z,A-Z$_]+$/)) {
return;
}
if (!addedOrder) {
addedOrder = true;
qs += ' ORDER BY ';
}

var direction = sorts[sortKey] === 1 ? 'ASC' : 'DESC';
qs += sortKey + ' ' + direction;
var direction = sorts[sortKey] === 1 ? 'ASC' : 'DESC';
qs += sortKey + ' ' + direction;
}

if(!Array.isArray(q.instructions)) {
Expand Down Expand Up @@ -799,7 +804,7 @@ module.exports = (function() {
// Find one or more models from the collection
// using where, limit, skip, and order
// In where: handle `or`, `and`, and `like` queries
find: function(connectionName, collectionName, options, cb, connection) {
find: function(connectionName, collectionName, options, cb, noop, connection) {

if(_.isUndefined(connection)) {
return spawnConnection(connectionName, __FIND__, cb);
Expand Down Expand Up @@ -846,7 +851,7 @@ module.exports = (function() {
// Count one model from the collection
// using where, limit, skip, and order
// In where: handle `or`, `and`, and `like` queries
count: function(connectionName, collectionName, options, cb, connection) {
count: function(connectionName, collectionName, options, cb, noop, connection) {

if(_.isUndefined(connection)) {
return spawnConnection(connectionName, __COUNT__, cb);
Expand Down Expand Up @@ -893,7 +898,7 @@ module.exports = (function() {
// Stream one or more models from the collection
// using where, limit, skip, and order
// In where: handle `or`, `and`, and `like` queries
stream: function(connectionName, collectionName, options, stream, connection) {
stream: function(connectionName, collectionName, options, stream, noop, connection) {

if(_.isUndefined(connection)) {
return spawnConnection(connectionName, __STREAM__);
Expand Down Expand Up @@ -952,7 +957,7 @@ module.exports = (function() {
},

// Update one or more models in the collection
update: function(connectionName, collectionName, options, values, cb, connection) {
update: function(connectionName, collectionName, options, values, cb, noop, connection) {

if(_.isUndefined(connection)) {
return spawnConnection(connectionName, __UPDATE__, cb);
Expand Down Expand Up @@ -1048,7 +1053,7 @@ module.exports = (function() {
},

// Delete one or more models from the collection
destroy: function(connectionName, collectionName, options, cb, connection) {
destroy: function(connectionName, collectionName, options, cb, noop, connection) {

if(_.isUndefined(connection)) {
return spawnConnection(connectionName, __DESTROY__, cb);
Expand Down Expand Up @@ -1079,7 +1084,7 @@ module.exports = (function() {
async.auto({

findRecords: function(next) {
adapter.find(connectionName, collectionName, options, next, connection);
adapter.find(connectionName, collectionName, options, next, noop, connection);
},

destroyRecords: ['findRecords', function(next) {
Expand Down
40 changes: 27 additions & 13 deletions lib/connections/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,42 @@ module.exports.configure = function ( connections ) {
// Build up a schema for this connection that can be used throughout the adapter
var schema = {};

_.each(_.keys(collections), function(coll) {
var collection = collections[coll];
if(!collection) return;
_.each(_.keys(collections), function(collName) {
var collection = collections[collName];
if(!collection) {
return;
}

var _schema = collection.waterline && collection.waterline.schema && collection.waterline.schema[collection.identity];
if(!_schema) return;
// Normalize schema into a sane object and discard all the WL context
var wlSchema = collection.waterline && collection.waterline.schema && collection.waterline.schema[collection.identity];
var _schema = {};
_schema.attributes = wlSchema.attributes || {};
_schema.definition = collection.definition || {};
_schema.meta = collection.meta || {};
_schema.tableName = wlSchema.tableName;
_schema.connection = wlSchema.connection;

// Set defaults to ensure values are set
if(!_schema.attributes) _schema.attributes = {};
if(!_schema.tableName) _schema.tableName = coll;
if(!_schema.attributes) {
_schema.attributes = {};
}

// If the connection names are't the same we don't need it in the schema
if(!_.includes(collections[coll].connection, connection.identity)) {
if(!_schema.tableName) {
_schema.tableName = collName;
}

// If the connection names aren't the same we don't need it in the schema
if(!_.includes(_schema.connection, connection.identity)) {
return;
}

// If the tableName is different from the identity, store the tableName in the schema
var schemaKey = coll;
if(_schema.tableName != coll) {
// If the tableName is different from the identity, store the tableName
// in the schema.
var schemaKey = collName;
if(_schema.tableName !== collName) {
schemaKey = _schema.tableName;
}

// Store the normalized schema
schema[schemaKey] = _schema;
});

Expand Down
89 changes: 0 additions & 89 deletions npm-shrinkwrap.json

This file was deleted.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,16 @@
"mysql": "2.10.2",
"waterline-cursor": "0.0.6",
"waterline-errors": "0.10.1",
"waterline-sequel": "0.5.7"
"waterline-sequel": "0.6.0"
},
"devDependencies": {
"captains-log": "~0.11.11",
"mocha": "2.4.1",
"should": "8.2.0",
"waterline-adapter-tests": "~0.11.1"
"waterline-adapter-tests": "~0.12.0"
},
"waterlineAdapter": {
"waterlineVersion": "~0.10.0",
"waterlineVersion": "~0.12.0",
"interfaces": [
"semantic",
"queryable",
Expand Down
3 changes: 2 additions & 1 deletion test/unit/support/bootstrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ Support.Schema = function(name, def) {
connection: 'test',
identity: name,
tableName: name,
attributes: def || Support.Definition
attributes: def || Support.Definition,
definition: def || Support.Definition
};
}

Expand Down