simple-graphql
Advanced tools
Comparing version 0.2.10 to 0.2.12
@@ -28,3 +28,7 @@ declare class DataType { | ||
static cls:any; | ||
static col:any; | ||
static fn:any; | ||
static where:any; | ||
@@ -31,0 +35,0 @@ transaction:any; |
@@ -107,4 +107,4 @@ 'use strict'; | ||
resolve: function () { | ||
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(model, args) { | ||
var after, _args$first, first, before, last, _args$condition, condition, _args$sort, sort, keywords, reverse, _fields, _value, keywordsCondition, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, _field, count, offset, result, index; | ||
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(dbModel, args) { | ||
var after, _args$first, first, before, last, _args$include, include, _args$condition, condition, _args$sort, sort, reverse, count, offset, result, index; | ||
@@ -115,65 +115,14 @@ return regeneratorRuntime.wrap(function _callee$(_context) { | ||
case 0: | ||
after = args.after, _args$first = args.first, first = _args$first === undefined ? 100 : _args$first, before = args.before, last = args.last, _args$condition = args.condition, condition = _args$condition === undefined ? {} : _args$condition, _args$sort = args.sort, sort = _args$sort === undefined ? [{ field: 'id', order: 'ASC' }] : _args$sort, keywords = args.keywords; | ||
after = args.after, _args$first = args.first, first = _args$first === undefined ? 100 : _args$first, before = args.before, last = args.last, _args$include = args.include, include = _args$include === undefined ? [] : _args$include, _args$condition = args.condition, condition = _args$condition === undefined ? {} : _args$condition, _args$sort = args.sort, sort = _args$sort === undefined ? [{ | ||
field: 'id', | ||
order: 'ASC' | ||
}] : _args$sort; | ||
reverse = false; | ||
if (!keywords) { | ||
_context.next = 25; | ||
break; | ||
} | ||
_fields = keywords.fields, _value = keywords.value; | ||
keywordsCondition = {}; | ||
_iteratorNormalCompletion = true; | ||
_didIteratorError = false; | ||
_iteratorError = undefined; | ||
_context.prev = 8; | ||
for (_iterator = _fields[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
_field = _step.value; | ||
keywordsCondition[_field] = { $like: '%' + _value + '%' }; | ||
} | ||
_context.next = 16; | ||
break; | ||
case 12: | ||
_context.prev = 12; | ||
_context.t0 = _context['catch'](8); | ||
_didIteratorError = true; | ||
_iteratorError = _context.t0; | ||
case 16: | ||
_context.prev = 16; | ||
_context.prev = 17; | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
case 19: | ||
_context.prev = 19; | ||
if (!_didIteratorError) { | ||
_context.next = 22; | ||
break; | ||
} | ||
throw _iteratorError; | ||
case 22: | ||
return _context.finish(19); | ||
case 23: | ||
return _context.finish(16); | ||
case 24: | ||
condition.$or = keywordsCondition; | ||
case 25: | ||
_context.next = 27; | ||
return model.count({ | ||
_context.next = 4; | ||
return dbModel.count({ | ||
include: include, | ||
where: condition | ||
}); | ||
case 27: | ||
case 4: | ||
count = _context.sent; | ||
@@ -194,4 +143,5 @@ | ||
offset = Math.max(after != null ? parseInt(after) : 0, 0); | ||
_context.next = 32; | ||
return model.findAll({ | ||
_context.next = 9; | ||
return dbModel.findAll({ | ||
include: include, | ||
where: condition, | ||
@@ -205,3 +155,3 @@ order: sort.map(function (s) { | ||
case 32: | ||
case 9: | ||
result = _context.sent; | ||
@@ -228,3 +178,3 @@ index = 0; | ||
case 35: | ||
case 12: | ||
case 'end': | ||
@@ -234,3 +184,3 @@ return _context.stop(); | ||
} | ||
}, _callee, this, [[8, 12, 16, 24], [17,, 19, 23]]); | ||
}, _callee, this); | ||
})); | ||
@@ -237,0 +187,0 @@ |
@@ -81,3 +81,3 @@ 'use strict'; | ||
if (dbModel.options.underscored) { | ||
attrs[key.replace(/([A-Z])/g, '_$1').replace(/^_/, '').toLocaleLowerCase()] = args[key]; | ||
attrs[_StringHelper2.default.toUnderscoredName(key)] = args[key]; | ||
} else { | ||
@@ -84,0 +84,0 @@ attrs[key] = args[key]; |
@@ -99,3 +99,3 @@ 'use strict'; | ||
if (dbModel.options.underscored) { | ||
values[key.replace(/([A-Z])/g, '_$1').replace(/^_/, '').toLocaleLowerCase()] = args.values[key]; | ||
values[_StringHelper2.default.toUnderscoredName(key)] = args.values[key]; | ||
} else { | ||
@@ -102,0 +102,0 @@ values[key] = args.values[key]; |
{ | ||
"name": "simple-graphql", | ||
"version": "0.2.10", | ||
"version": "0.2.12", | ||
"description": "The simple way to generates GraphQL schemas and Sequelize models from your models definition.", | ||
@@ -37,8 +37,8 @@ "main": "lib/index.js", | ||
"lodash": "^4.17.4", | ||
"moment": "^2.18.1", | ||
"sequelize": "^3.30.2" | ||
"moment": "^2.18.1" | ||
}, | ||
"peerDependencies": { | ||
"graphql": "^0.9.1", | ||
"graphql-relay": "^0.5.1" | ||
"graphql-relay": "^0.5.1", | ||
"sequelize": "^3.30.2" | ||
}, | ||
@@ -45,0 +45,0 @@ "devDependencies": { |
@@ -58,3 +58,4 @@ 'use strict'; | ||
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(root, args, context, info, models) { | ||
var condition, sort; | ||
var condition, sort, _iteratorNormalCompletion2, _didIteratorError2, _iteratorError2, _iterator2, _step2, item; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
@@ -65,8 +66,63 @@ while (1) { | ||
condition = _extends({}, _.get(hasManyCfg, 'options.scope', {})); | ||
sort = _.get(hasManyCfg, 'options.sort', [{ field: 'id', order: 'ASC' }]); | ||
if (!models[hasManyCfg.target].options.underscored) { | ||
_context.next = 25; | ||
break; | ||
} | ||
condition[_StringHelper2.default.toUnderscoredName(_.get(hasManyCfg, 'options.foreignKey', name + 'Id'))] = root.id; | ||
_iteratorNormalCompletion2 = true; | ||
_didIteratorError2 = false; | ||
_iteratorError2 = undefined; | ||
_context.prev = 7; | ||
for (_iterator2 = sort[Symbol.iterator](); !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
item = _step2.value; | ||
item.field = _StringHelper2.default.toUnderscoredName(item.field); | ||
} | ||
_context.next = 15; | ||
break; | ||
case 11: | ||
_context.prev = 11; | ||
_context.t0 = _context['catch'](7); | ||
_didIteratorError2 = true; | ||
_iteratorError2 = _context.t0; | ||
case 15: | ||
_context.prev = 15; | ||
_context.prev = 16; | ||
if (!_iteratorNormalCompletion2 && _iterator2.return) { | ||
_iterator2.return(); | ||
} | ||
case 18: | ||
_context.prev = 18; | ||
if (!_didIteratorError2) { | ||
_context.next = 21; | ||
break; | ||
} | ||
throw _iteratorError2; | ||
case 21: | ||
return _context.finish(18); | ||
case 22: | ||
return _context.finish(15); | ||
case 23: | ||
_context.next = 26; | ||
break; | ||
case 25: | ||
condition[_.get(hasManyCfg, 'options.foreignKey', name + 'Id')] = root.id; | ||
sort = _.get(hasManyCfg, 'options.sort', [{ field: 'id', order: 'ASC' }]); | ||
case 26: | ||
return _context.abrupt('return', _Connection2.default.resolve(models[hasManyCfg.target], _extends({}, args, { condition: condition, sort: sort }))); | ||
case 4: | ||
case 27: | ||
case 'end': | ||
@@ -76,3 +132,3 @@ return _context.stop(); | ||
} | ||
}, _callee, this); | ||
}, _callee, this, [[7, 11, 15, 23], [16,, 18, 22]]); | ||
})); | ||
@@ -79,0 +135,0 @@ |
@@ -53,5 +53,13 @@ 'use strict'; | ||
case 0: | ||
if (!(root[fieldName] != null)) { | ||
_context.next = 4; | ||
break; | ||
} | ||
return _context.abrupt('return', root[fieldName]); | ||
case 4: | ||
return _context.abrupt('return', root['get' + _StringHelper2.default.toInitialUpperCase(fieldName)]()); | ||
case 1: | ||
case 5: | ||
case 'end': | ||
@@ -58,0 +66,0 @@ return _context.stop(); |
@@ -19,2 +19,6 @@ 'use strict'; | ||
var _sequelize = require('sequelize'); | ||
var _sequelize2 = _interopRequireDefault(_sequelize); | ||
var _Model = require('../../Model'); | ||
@@ -44,2 +48,4 @@ | ||
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } | ||
function _asyncToGenerator(fn) { return function () { var gen = fn.apply(this, arguments); return new Promise(function (resolve, reject) { function step(key, arg) { try { var info = gen[key](arg); var value = info.value; } catch (error) { reject(error); return; } if (info.done) { resolve(value); } else { return Promise.resolve(value).then(function (value) { step("next", value); }, function (err) { step("throw", err); }); } } return step("next"); }); }; } | ||
@@ -239,3 +245,4 @@ | ||
var _ref = _asyncToGenerator(regeneratorRuntime.mark(function _callee(args, context, info, models) { | ||
var dbModel, condition; | ||
var dbModel, sort, _iteratorNormalCompletion, _didIteratorError, _iteratorError, _iterator, _step, item, condition, include, _args$keywords, fields, value, keywordsCondition, associationType, _iteratorNormalCompletion4, _didIteratorError4, _iteratorError4, _iterator4, _step4, field, fieldName, type, colFieldName; | ||
return regeneratorRuntime.wrap(function _callee$(_context) { | ||
@@ -246,40 +253,220 @@ while (1) { | ||
dbModel = models[model.name]; | ||
sort = args != null ? args.sort : [{ field: 'id', order: 'ASC' }]; | ||
if (args && args.condition) { | ||
conditionFieldKeys.forEach(function (fieldKey) { | ||
if (args.condition && args.condition[fieldKey]) { | ||
args.condition[fieldKey] = _.mapKeys(args.condition[fieldKey], function (value, key) { | ||
return '$' + key; | ||
}); | ||
if (!dbModel.options.underscored) { | ||
_context.next = 22; | ||
break; | ||
} | ||
_iteratorNormalCompletion = true; | ||
_didIteratorError = false; | ||
_iteratorError = undefined; | ||
_context.prev = 6; | ||
for (_iterator = sort[Symbol.iterator](); !(_iteratorNormalCompletion = (_step = _iterator.next()).done); _iteratorNormalCompletion = true) { | ||
item = _step.value; | ||
item.field = _StringHelper2.default.toUnderscoredName(item.field); | ||
} | ||
_context.next = 14; | ||
break; | ||
case 10: | ||
_context.prev = 10; | ||
_context.t0 = _context['catch'](6); | ||
_didIteratorError = true; | ||
_iteratorError = _context.t0; | ||
case 14: | ||
_context.prev = 14; | ||
_context.prev = 15; | ||
if (!_iteratorNormalCompletion && _iterator.return) { | ||
_iterator.return(); | ||
} | ||
case 17: | ||
_context.prev = 17; | ||
if (!_didIteratorError) { | ||
_context.next = 20; | ||
break; | ||
} | ||
throw _iteratorError; | ||
case 20: | ||
return _context.finish(17); | ||
case 21: | ||
return _context.finish(14); | ||
case 22: | ||
condition = args != null ? _extends({}, args.condition) : {}; | ||
conditionFieldKeys.forEach(function (fieldKey) { | ||
if (condition[fieldKey]) { | ||
condition[fieldKey] = _.mapKeys(condition[fieldKey], function (value, key) { | ||
return '$' + key; | ||
}); | ||
} | ||
}); | ||
_.forOwn(model.config.fields, function (value, key) { | ||
if (value instanceof _ModelRef2.default || value && value.$type instanceof _ModelRef2.default) { | ||
if (!key.endsWith('Id')) { | ||
key = key + 'Id'; | ||
} | ||
}); | ||
condition = {}; | ||
if (typeof condition[key] !== 'undefined') { | ||
if (dbModel.options.underscored) { | ||
var underscoredKey = _StringHelper2.default.toUnderscoredName(key); | ||
if (underscoredKey !== key) { | ||
condition[underscoredKey] = condition[key]; | ||
delete condition[key]; | ||
} | ||
} | ||
} | ||
} | ||
}); | ||
_.forOwn(model.config.fields, function (value, key) { | ||
if (value instanceof _ModelRef2.default || value && value.$type instanceof _ModelRef2.default) { | ||
if (!key.endsWith('Id')) { | ||
key = key + 'Id'; | ||
include = []; | ||
if (!(args && args.keywords)) { | ||
_context.next = 50; | ||
break; | ||
} | ||
_args$keywords = args.keywords, fields = _args$keywords.fields, value = _args$keywords.value; | ||
keywordsCondition = []; | ||
associationType = function associationType(model, fieldName) { | ||
var _iteratorNormalCompletion2 = true; | ||
var _didIteratorError2 = false; | ||
var _iteratorError2 = undefined; | ||
try { | ||
for (var _iterator2 = model.config.associations.hasOne[Symbol.iterator](), _step2; !(_iteratorNormalCompletion2 = (_step2 = _iterator2.next()).done); _iteratorNormalCompletion2 = true) { | ||
var config = _step2.value; | ||
if (_.get(config, 'options.as') === fieldName) { | ||
return config.target; | ||
} | ||
if (typeof args.condition[key] !== 'undefined') { | ||
if (dbModel.options.underscored) { | ||
condition[key.replace(/([A-Z])/g, '_$1').replace(/^_/, '').toLocaleLowerCase()] = args.condition[key]; | ||
} else { | ||
condition[key] = args.condition[key]; | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError2 = true; | ||
_iteratorError2 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion2 && _iterator2.return) { | ||
_iterator2.return(); | ||
} | ||
} else if (typeof args.condition[key] !== 'undefined') { | ||
condition[key] = args.condition[key]; | ||
} finally { | ||
if (_didIteratorError2) { | ||
throw _iteratorError2; | ||
} | ||
} | ||
}); | ||
args.condition = condition; | ||
} | ||
var _iteratorNormalCompletion3 = true; | ||
var _didIteratorError3 = false; | ||
var _iteratorError3 = undefined; | ||
try { | ||
for (var _iterator3 = model.config.associations.belongsTo[Symbol.iterator](), _step3; !(_iteratorNormalCompletion3 = (_step3 = _iterator3.next()).done); _iteratorNormalCompletion3 = true) { | ||
var _config = _step3.value; | ||
if (_.get(_config, 'options.as') === fieldName) { | ||
return _config.target; | ||
} | ||
} | ||
} catch (err) { | ||
_didIteratorError3 = true; | ||
_iteratorError3 = err; | ||
} finally { | ||
try { | ||
if (!_iteratorNormalCompletion3 && _iterator3.return) { | ||
_iterator3.return(); | ||
} | ||
} finally { | ||
if (_didIteratorError3) { | ||
throw _iteratorError3; | ||
} | ||
} | ||
} | ||
return null; | ||
}; | ||
_iteratorNormalCompletion4 = true; | ||
_didIteratorError4 = false; | ||
_iteratorError4 = undefined; | ||
_context.prev = 33; | ||
for (_iterator4 = fields[Symbol.iterator](); !(_iteratorNormalCompletion4 = (_step4 = _iterator4.next()).done); _iteratorNormalCompletion4 = true) { | ||
field = _step4.value; | ||
if (field.indexOf('.') !== -1) { | ||
fieldName = field.split('.')[0]; | ||
type = associationType(model, fieldName); | ||
if (type) { | ||
include.push({ | ||
model: dbModel.sequelize.models[type], | ||
as: fieldName, | ||
required: false | ||
}); | ||
colFieldName = field; | ||
if (dbModel.options.underscored) { | ||
colFieldName = fieldName + _StringHelper2.default.toUnderscoredName(field.substr(field.indexOf('.'))); | ||
} | ||
keywordsCondition.push(_sequelize2.default.where(_sequelize2.default.col(colFieldName), { $like: '%' + value + '%' })); | ||
} else { | ||
keywordsCondition.push(_defineProperty({}, field, { $like: '%' + value + '%' })); | ||
} | ||
} else { | ||
keywordsCondition.push(_defineProperty({}, field, { $like: '%' + value + '%' })); | ||
} | ||
} | ||
if (args && args.keywords && dbModel.options.underscored) { | ||
args.keywords.fields = args.keywords.fields.map(function (field) { | ||
return field.replace(/([A-Z])/g, '_$1').replace(/^_/, '').toLocaleLowerCase(); | ||
}); | ||
_context.next = 41; | ||
break; | ||
case 37: | ||
_context.prev = 37; | ||
_context.t1 = _context['catch'](33); | ||
_didIteratorError4 = true; | ||
_iteratorError4 = _context.t1; | ||
case 41: | ||
_context.prev = 41; | ||
_context.prev = 42; | ||
if (!_iteratorNormalCompletion4 && _iterator4.return) { | ||
_iterator4.return(); | ||
} | ||
return _context.abrupt('return', _index2.default.Connection.resolve(dbModel, args)); | ||
case 44: | ||
_context.prev = 44; | ||
case 4: | ||
if (!_didIteratorError4) { | ||
_context.next = 47; | ||
break; | ||
} | ||
throw _iteratorError4; | ||
case 47: | ||
return _context.finish(44); | ||
case 48: | ||
return _context.finish(41); | ||
case 49: | ||
condition.$or = keywordsCondition; | ||
case 50: | ||
return _context.abrupt('return', _index2.default.Connection.resolve(dbModel, _extends({}, args, { condition: condition, include: include }))); | ||
case 51: | ||
case 'end': | ||
@@ -289,3 +476,3 @@ return _context.stop(); | ||
} | ||
}, _callee, this); | ||
}, _callee, this, [[6, 10, 14, 22], [15,, 17, 21], [33, 37, 41, 49], [42,, 44, 48]]); | ||
})); | ||
@@ -292,0 +479,0 @@ |
@@ -125,14 +125,21 @@ 'use strict'; | ||
fieldName = name.split('.').slice(-1)[0]; | ||
// 判断是否只有model Id, 如果只有model Id, 通过ID 查找相关的model | ||
if (!(root && _lodash2.default.isFunction(root['get' + _StringHelper2.default.toInitialUpperCase(fieldName)]))) { | ||
_context2.next = 3; | ||
if (!_lodash2.default.isFunction(root['get' + _StringHelper2.default.toInitialUpperCase(fieldName)])) { | ||
_context2.next = 7; | ||
break; | ||
} | ||
if (!(root[fieldName] != null && root[fieldName].id != null)) { | ||
_context2.next = 6; | ||
break; | ||
} | ||
return _context2.abrupt('return', root[fieldName]); | ||
case 6: | ||
return _context2.abrupt('return', root['get' + _StringHelper2.default.toInitialUpperCase(fieldName)]()); | ||
case 3: | ||
case 7: | ||
if (!(root && root[fieldName] && (typeof root[fieldName] === 'number' || typeof root[fieldName] === 'string'))) { | ||
_context2.next = 5; | ||
_context2.next = 9; | ||
break; | ||
@@ -143,6 +150,6 @@ } | ||
case 5: | ||
case 9: | ||
return _context2.abrupt('return', root[fieldName]); | ||
case 6: | ||
case 10: | ||
case 'end': | ||
@@ -149,0 +156,0 @@ return _context2.stop(); |
@@ -31,2 +31,6 @@ 'use strict'; | ||
var _StringHelper = require('../utils/StringHelper'); | ||
var _StringHelper2 = _interopRequireDefault(_StringHelper); | ||
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; } | ||
@@ -65,3 +69,3 @@ | ||
if (sequelize.options.define.underscored) { | ||
foreignKey = foreignKey.replace(/([A-Z])/g, '_$1').replace(/^_/, '').toLocaleLowerCase(); | ||
foreignKey = _StringHelper2.default.toUnderscoredName(foreignKey); | ||
} | ||
@@ -103,3 +107,3 @@ if (value && value['$type'] && value.required) { | ||
if (sequelize.options.define.underscored && dbDefinition[key].field == null) { | ||
dbDefinition[key].field = key.replace(/([A-Z])/g, '_$1').replace(/^_/, '').toLocaleLowerCase(); | ||
dbDefinition[key].field = _StringHelper2.default.toUnderscoredName(key); | ||
} | ||
@@ -112,3 +116,4 @@ } else { | ||
// console.log("Create Sequlize Model with config", model.name, dbDefinition, model.config.options["table"]) | ||
return sequelize.define(model.name, dbDefinition, model.config.options['table']); | ||
var dbModel = sequelize.define(model.name, dbDefinition, model.config.options['table']); | ||
return dbModel; | ||
} |
@@ -1,2 +0,2 @@ | ||
"use strict"; | ||
'use strict'; | ||
@@ -12,3 +12,6 @@ Object.defineProperty(exports, "__esModule", { | ||
return str.substring(0, 1).toLowerCase() + str.substring(1); | ||
}, | ||
toUnderscoredName: function toUnderscoredName(str) { | ||
return str.replace(/([A-Z])/g, '_$1').replace(/^_/, '').toLocaleLowerCase(); | ||
} | ||
}; |
{ | ||
"name": "simple-graphql", | ||
"version": "0.2.10", | ||
"version": "0.2.12", | ||
"description": "The simple way to generates GraphQL schemas and Sequelize models from your models definition.", | ||
@@ -37,8 +37,8 @@ "main": "lib/index.js", | ||
"lodash": "^4.17.4", | ||
"moment": "^2.18.1", | ||
"sequelize": "^3.30.2" | ||
"moment": "^2.18.1" | ||
}, | ||
"peerDependencies": { | ||
"graphql": "^0.9.1", | ||
"graphql-relay": "^0.5.1" | ||
"graphql-relay": "^0.5.1", | ||
"sequelize": "^3.30.2" | ||
}, | ||
@@ -45,0 +45,0 @@ "devDependencies": { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
499083
4927
- Removedsequelize@^3.30.2