Skip to content
This repository has been archived by the owner on Feb 28, 2023. It is now read-only.

Commit

Permalink
feat: support for sails-v1
Browse files Browse the repository at this point in the history
  • Loading branch information
davepreston committed Jun 7, 2019
1 parent 726ecdf commit 36747d6
Show file tree
Hide file tree
Showing 8 changed files with 1,406 additions and 1,658 deletions.
1,780 changes: 1,017 additions & 763 deletions lib/adapter.js

Large diffs are not rendered by default.

124 changes: 95 additions & 29 deletions lib/sql.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,9 @@ var sql = {
options.skip +
" ";
query = outerOffsetQuery;
if (options.sort && options.sort.length) {
query += parseSort(collectionName, options.sort);
}
}
return query;
},
Expand All @@ -116,7 +119,7 @@ var sql = {

_schema: function (collectionName, attribute, attrName) {
attrName = "[" + attrName + "]";
var type = sqlTypeCast(attribute.type);
var type = sqlTypeCast(attribute.columnType);

if (attribute.primaryKey) {
// If type is an integer, set auto increment
Expand Down Expand Up @@ -178,7 +181,7 @@ var sql = {
return attrStr + ">" + valueStr;
else if (key === ">=" || key === "greaterThanOrEqual")
return attrStr + ">=" + valueStr;
else if (key === "!" || key === "not") {
else if (key === "!" || key === "not" || key === "!=") {
if (value === null) return attrStr + " IS NOT NULL";
else if (_.isArray(value)) {
//return attrStr + ' NOT IN (' + valueStr.split(',') + ')';
Expand Down Expand Up @@ -207,7 +210,9 @@ var sql = {
valueStr = sql.prepareValue(collectionName, value, key);
if (_.isNull(value)) {
return attrStr + " IS NULL";
} else return attrStr + "=" + valueStr;
} else {
return attrStr + "=" + valueStr;
}
}
},

Expand Down Expand Up @@ -260,7 +265,10 @@ var sql = {
// AND
queryPart = sql.build(collectionName, criterion, sql.where, " AND ");
return " ( " + queryPart + " ) ";
} else if (_.isArray(criterion)) {
} else if (_.isArray(criterion) || _.has(criterion, 'in')) {
if (_.has(criterion, 'in')) {
criterion = _.cloneDeep(criterion.in);
}
// IN
var hadNull = (criterion.indexOf(null) > -1);
var criterionWithoutNulls = _.filter(criterion, (value) => { return !_.isNull(value) });
Expand All @@ -271,6 +279,20 @@ var sql = {
queryPart = `((${queryPart}) OR (${preparedKey} is null))`;
}
return queryPart;
} else if (_.has(criterion, 'nin')) {
if (_.has(criterion, 'nin')) {
criterion = _.cloneDeep(criterion.nin);
}
// not IN
var hadNull = (criterion.indexOf(null) > -1);
var criterionWithoutNulls = _.filter(criterion, (value) => { return !_.isNull(value) });
values = sql.values(collectionName, criterionWithoutNulls, key) || "NULL";
var preparedKey = sql.prepareAttribute(collectionName, null, key);
queryPart = preparedKey + " NOT IN (" + values + ")";
if (hadNull) {
queryPart = `((${queryPart}) AND (${preparedKey} is NOT null))`;
}
return queryPart;
} else if (key.toLowerCase() === "like") {
// LIKE
return sql.build(
Expand Down Expand Up @@ -302,7 +324,10 @@ var sql = {
var queryPart = "";

if (options.where) {
queryPart += "WHERE " + sql.where(collectionName, options.where) + " ";
// skip for empty objects
if (!(_.isEmpty(options.where)) && _.isObject(options.where)) {
queryPart += "WHERE " + sql.where(collectionName, options.where) + " ";
}
}

if (options.groupBy) {
Expand All @@ -319,25 +344,8 @@ var sql = {
}

//options are sorted during skip when applicable
if (options.sort && !options.skip) {
queryPart += "ORDER BY ";

// Sort through each sort attribute criteria
_.each(options.sort, function (direction, attrName) {
queryPart += sql.prepareAttribute(collectionName, null, attrName) + " ";

// Basic MongoDB-style numeric sort direction
if (direction === 1) {
queryPart += "ASC, ";
} else {
queryPart += "DESC, ";
}
});

// Remove trailing comma
if (queryPart.slice(-2) === ", ") {
queryPart = queryPart.slice(0, -2) + " ";
}
if (options.sort && options.sort.length && !options.skip) {
queryPart += parseSort(collectionName, options.sort);
}

return queryPart;
Expand Down Expand Up @@ -365,23 +373,77 @@ var sql = {
}
};

function parseSort(collectionName, sort) {

let queryPart = "ORDER BY ";

_.each(sort, function (sortItem) {
// Sort through each sort attribute criteria
_.each(sortItem, function (direction, attrName) {
queryPart += sql.prepareAttribute(collectionName, null, attrName) + " ";

if (_.isString(direction)) {
direction = direction.toUpperCase();
}
// Basic MongoDB-style numeric sort direction
if (direction === 1 || direction === 'ASC') {
queryPart += "ASC, ";
} else {
queryPart += "DESC, ";
}
});
})
// Remove trailing comma
if (queryPart.slice(-2) === ", ") {
queryPart = queryPart.slice(0, -2) + " ";
}
return queryPart;
}

// Cast waterline types into SQL data types
function sqlTypeCast(type) {
type = type && type.toLowerCase();

switch (type) {
case "binary":
// https://github.com/balderdashy/waterline/blob/master/ARCHITECTURE.md#reserved-column-types
case '_numberkey':
return 'INT';
case '_stringkey':
return 'VARCHAR(255)';
case '_numbertimestamp':
return 'BIGINT';
case '_stringtimestamp':
return 'VARCHAR(14)';
case '_string':
return 'VARCHAR(max)';
case '_number':
return 'DOUBLE';
case '_boolean':
return 'TINYINT';
case '_json':
return 'VARCHAR(max)';
case '_ref':
return 'VARCHAR(max)';

case "number":
return "NVARCHAR(max)";

case "boolean":
return "BIT";

case "string":
case "json":
return "NVARCHAR(max)";

case "ref":
return "VARCHAR(max)";

case "binary":
case "array":
case "json":
case "text":
case "varchar":
return "VARCHAR(max)";

case "boolean":
return "BIT";

case "int":
case "integer":
return "INT";
Expand All @@ -396,6 +458,9 @@ function sqlTypeCast(type) {
return "TIME";
case "datetime":
return "DATETIME";
case "bigint":
return "BIGINT";


default:
console.error("Unregistered type given: " + type);
Expand All @@ -419,6 +484,7 @@ function validSubAttrCriteria(c) {
!_.isUndefined(c["<"]) ||
!_.isUndefined(c["<="]) ||
!_.isUndefined(c["!"]) ||
!_.isUndefined(c["!="]) ||
!_.isUndefined(c[">"]) ||
!_.isUndefined(c[">="]) ||
!_.isUndefined(c.startsWith) ||
Expand Down
43 changes: 26 additions & 17 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,27 @@ utils.prepareValue = function (value) {
utils.buildOrderByStatement = function (criteria) {
var queryPart = "ORDER BY ";

// Sort through each sort attribute criteria
_.each(criteria.sort, function (direction, attrName) {
if (!criteria.joinMeta) {
queryPart += "[" + attrName + "] ";
} else {
queryPart += "[" + criteria.joinMeta.child + "].[" + attrName + "] ";
}
_.each(criteria.sort, function (sortItem) {
// Sort through each sort attribute criteria

// Basic MongoDB-style numeric sort direction
if (direction === 1) {
queryPart += "ASC, ";
} else {
queryPart += "DESC, ";
}
// Sort through each sort attribute criteria
_.each(sortItem, function (direction, attrName) {
if (!criteria.joinMeta) {
queryPart += "[" + attrName + "] ";
} else {
queryPart += "[" + criteria.joinMeta.child + "].[" + attrName + "] ";
}

if (_.isString(direction)) {
direction = direction.toUpperCase();
}
// Basic MongoDB-style numeric sort direction
if (direction === 1 || direction === 'ASC') {
queryPart += "ASC, ";
} else {
queryPart += "DESC, ";
}
});
});

// Remove trailing comma
Expand Down Expand Up @@ -153,7 +160,9 @@ utils.buildSelectStatement = function (criteria, table) {
var primaryKeySort = {};
primaryKeySort[criteria.__primaryKey__] = 1;
//@todo what to do with no primary key OR sort?
criteria.sort = criteria.sort || primaryKeySort;
if (!criteria.sort || _.isEmpty(criteria.sort)) {
criteria.sort = [primaryKeySort];
}
query +=
"ROW_NUMBER() OVER (" +
utils.buildOrderByStatement(criteria) +
Expand All @@ -175,8 +184,8 @@ utils.buildSelectStatement = function (criteria, table) {
var joinMeta = criteria.joinMeta;
query += " [" + joinMeta.middleLeft + "].[" + joinMeta.middleLeftKey + "]";
var joinMetaSelectColumns = ",[" + joinMeta.child + "].";
if (Array.isArray(criteria.joinMeta.select)) {
joinMetaSelectColumns += criteria.joinMeta.select.join(
if (Array.isArray(joinMeta.criteria.select)) {
joinMetaSelectColumns += joinMeta.criteria.select.join(
",[" + joinMeta.child + "]."
);
} else {
Expand Down Expand Up @@ -224,7 +233,7 @@ utils.convertDates = function (options) {
}

// only deal with attributes that are dates in DB
if (attributes[attrName] && attributes[attrName].type && attributes[attrName].type.indexOf('date') === 0) {
if (attributes[attrName] && attributes[attrName].type && attributes[attrName] && attributes[attrName].autoMigrations && attributes[attrName].autoMigrations.columnType.indexOf('date') === 0) {
// key pair strings
if (_.isString(attrWhere)) {
return where[attrName] = utils.toSqlDate(moment(attrWhere))
Expand Down
Loading

0 comments on commit 36747d6

Please sign in to comment.