Comparing version 1.0.0-alpha.11 to 1.0.0-alpha.12
@@ -35,3 +35,3 @@ /* UNIQORM | ||
DOUBLE.prototype = { | ||
NUMBER.prototype = { | ||
/** | ||
@@ -38,0 +38,0 @@ * |
217
lib/Model.js
@@ -17,4 +17,3 @@ /* UNIQORM | ||
const Field = require('./Field'); | ||
const modelFind = require('./model/find'); | ||
const modelCreate = require('./model/create'); | ||
const promisify = require('putil-promisify'); | ||
@@ -73,8 +72,15 @@ /** | ||
Model.prototype.Op = sqb.Op; | ||
Model.prototype = { | ||
Op: sqb.Op, | ||
get tableNameFull() { | ||
return (this.schemaName ? this.schemaName + '.' : '') + | ||
this.tableName; | ||
} | ||
}; | ||
Model.prototype.constructor = Model; | ||
/** | ||
* Searches for one specific element in the database | ||
* | ||
* @param {Object} [dbobj] SQB pool or SQB connection | ||
* @param {Object} [options] | ||
@@ -84,3 +90,16 @@ * @param {Function} [callback] | ||
*/ | ||
Model.prototype.findOne = modelFind.findOne; | ||
Model.prototype.findOne = function(options, callback) { | ||
if (typeof options === 'function') { | ||
callback = options; | ||
options = null; | ||
} | ||
options = options || {}; | ||
options.orderBy = null; | ||
options.limit = 1; | ||
return this.findAll(options, function(err, rows) { | ||
if (!err && rows && rows.length) | ||
return callback(null, rows[0]); | ||
callback(err); | ||
}); | ||
}; | ||
@@ -90,3 +109,2 @@ /** | ||
* | ||
* @param {Object} [dbobj] SQB pool or SQB connection | ||
* @param {Object} [options] | ||
@@ -96,3 +114,22 @@ * @param {Function} [callback] | ||
*/ | ||
Model.prototype.findAll = modelFind.findAll; | ||
Model.prototype.findAll = function(options, callback) { | ||
if (typeof options === 'function') { | ||
callback = options; | ||
options = null; | ||
} | ||
const self = this; | ||
if (!callback) | ||
return promisify.fromCallback(function(cb) { | ||
self.findOne(options, cb); | ||
}); | ||
options = prepareFindOptions(self, options || {}); | ||
options.cursor = false; | ||
options.rowset = false; | ||
const query = prepareFindQuery(self, options); | ||
query.execute(options, function(err, result) { | ||
if (!err && result && result.rows.length) | ||
return callback(null, result.rows); | ||
callback(err); | ||
}); | ||
}; | ||
@@ -102,3 +139,2 @@ /** | ||
* | ||
* @param {Object} [dbobj] SQB pool or SQB connection | ||
* @param {Object} [options] | ||
@@ -108,3 +144,22 @@ * @param {Function} [callback] | ||
*/ | ||
Model.prototype.findCursor = modelFind.findCursor; | ||
Model.prototype.findCursor = function(options, callback) { | ||
if (typeof options === 'function') { | ||
callback = options; | ||
options = null; | ||
} | ||
const self = this; | ||
if (!callback) | ||
return promisify.fromCallback(function(cb) { | ||
self.findOne(options, cb); | ||
}); | ||
options = prepareFindOptions(self, options || {}); | ||
options.cursor = true; | ||
options.rowset = false; | ||
const query = prepareFindQuery(self, options); | ||
query.execute(options, function(err, result) { | ||
if (!err && result && result.cursor) | ||
return callback(null, result.cursor); | ||
callback(err); | ||
}); | ||
}; | ||
@@ -114,3 +169,3 @@ /** | ||
* | ||
* @param {Object} [dbobj] SQB pool or SQB connection | ||
* @param {Object} [attributes] | ||
* @param {Object} [options] | ||
@@ -120,4 +175,30 @@ * @param {Function} [callback] | ||
*/ | ||
Model.prototype.create = modelCreate; | ||
Model.prototype.create = function(attributes, options, callback) { | ||
if (typeof attributes !== 'object') | ||
throw new ArgumentError('Attributes required to create model instance'); | ||
if (typeof options === 'function') { | ||
callback = options; | ||
options = null; | ||
} | ||
const self = this; | ||
if (!callback) | ||
return promisify.fromCallback(function(cb) { | ||
self.create(attributes, options, cb); | ||
}); | ||
options = options || {}; | ||
options.objectRows = true; | ||
options.validateFields = options.validateFields || | ||
self.owner.options.validateFields; | ||
if (options.returning && options.returning !== '*') | ||
options.returning = Array.isArray(options.returning) ? options.returning : | ||
[options.returning]; | ||
const query = prepareCreateQuery(self, attributes, options); | ||
query.execute(options, function(err, result) { | ||
if (!err) | ||
return callback(null, (result.rows && result.rows[0]) || true); | ||
callback(err); | ||
}); | ||
}; | ||
/* | ||
@@ -190,1 +271,115 @@ * | ||
} | ||
function buildSelectColumns(model, cols, options) { | ||
const result = {}; | ||
cols.forEach(function(k) { | ||
const m = k.match(/^([\w$]+) *([\w$]*)$/); | ||
if (!m) { | ||
if (!options.validateFields) | ||
return; | ||
throw new ArgumentError('Invalid column definition(%s)', k); | ||
} | ||
const name = m[1]; | ||
const field = model.fields[name]; | ||
if (!field) { | ||
if (!options.validateFields) | ||
return; | ||
throw new ArgumentError('Model (%s) has no field (%s)', model.name, name); | ||
} | ||
const alias = (m[2] || name); | ||
result[alias] = field.fieldName; | ||
}); | ||
return result; | ||
} | ||
function prepareCreateQuery(model, attributes, options) { | ||
const dbobj = options.transaction || model.owner.pool; | ||
/* Prepare returning column list */ | ||
var a = (options.returning === '*') ? | ||
Object.getOwnPropertyNames(model.fields) : | ||
(options.returning && | ||
options.returning.length) ? options.returning : model.primaryKeys; | ||
a = buildSelectColumns(model, a, options); | ||
const query = dbobj | ||
.insert(model.tableNameFull, attributes); | ||
const returningColumns = {}; | ||
Object.getOwnPropertyNames(a).forEach(function(name) { | ||
const col = a[name]; | ||
const field = model.fields[name]; | ||
returningColumns[col] = field.jsType.toLowerCase(); | ||
}); | ||
query.returning(returningColumns); | ||
return query; | ||
} | ||
function prepareFindOptions(model, options) { | ||
options.limit = (parseInt(options.limit) || 0) || 100; | ||
options.objectRows = options.objectRows || options.objectRows == null; | ||
options.validateFields = options.validateFields || | ||
model.owner.options.validateFields; | ||
if (options.fields) | ||
options.fields = Array.isArray(options.fields) ? options.fields : | ||
[options.fields]; | ||
if (options.where) | ||
options.where = Array.isArray(options.where) ? options.where : | ||
[options.where]; | ||
if (options.orderBy) | ||
options.orderBy = Array.isArray(options.orderBy) ? options.orderBy : | ||
[options.orderBy]; | ||
return options; | ||
} | ||
function prepareFindQuery(model, options) { | ||
const dbobj = options.transaction || model.owner.pool; | ||
var orderColumns; | ||
/* Prepare select column list */ | ||
var a = options.fields && options.fields.length ? options.fields : | ||
Object.getOwnPropertyNames(model.fields); | ||
const selectColumns = buildSelectColumns(model, a, options); | ||
/* Prepare order column list */ | ||
if (options.orderBy) { | ||
orderColumns = []; | ||
options.orderBy.forEach(function(n) { | ||
const m = n.match(/^([-+])?([a-zA-Z][\w$]*|\*) *(asc|dsc|desc|ascending|descending)?$/i); | ||
if (!m) | ||
throw new ArgumentError('(%s) does not match order column format', n); | ||
const name = m[2]; | ||
var fieldName; | ||
if (selectColumns[name]) | ||
fieldName = selectColumns[name]; | ||
else { | ||
const field = model.fields[name]; | ||
if (!field) | ||
throw new ArgumentError('Model (%s) has no field (%s)', model.name, name); | ||
fieldName = field.fieldName; | ||
} | ||
const sortOrder = (m[1] && m[1] === '-' ? 'desc' : m[3]); | ||
orderColumns.push(fieldName + (sortOrder ? ' ' + sortOrder : '')); | ||
}); | ||
} | ||
a = []; | ||
Object.getOwnPropertyNames(selectColumns).forEach(function(name) { | ||
const col = selectColumns[name]; | ||
/* Add to columnNames array */ | ||
a.push('t1.' + col + (col !== name ? ' ' + name : '')); | ||
}); | ||
const query = dbobj | ||
.select.apply(dbobj, a) | ||
.from(model.tableNameFull + ' t1'); | ||
if (options.where) | ||
query.where.apply(query, options.where); | ||
if (orderColumns) { | ||
query.orderBy.apply(query, orderColumns); | ||
} | ||
if (options.limit) | ||
query.limit(options.limit); | ||
if (options.offset) | ||
query.offset(options.offset); | ||
// if (options.params) | ||
// query.params(options.params); | ||
return query; | ||
} |
{ | ||
"name": "uniqorm", | ||
"description": "Easy to use, multi-dialect ORM framework for JavaScript", | ||
"version": "1.0.0-alpha.11", | ||
"version": "1.0.0-alpha.12", | ||
"author": "Panates Ltd.", | ||
@@ -39,3 +39,3 @@ "contributors": [ | ||
"peerDependencies": { | ||
"sqb": "^1.0.1-rc.1" | ||
"sqb": "^1.0.1-rc.2" | ||
}, | ||
@@ -42,0 +42,0 @@ "engines": { |
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
41868
25
1550