Comparing version 0.1.4 to 0.1.5
@@ -7,3 +7,3 @@ /** | ||
var safeRequire = utils.safeRequire; | ||
var arango = safeRequire('arango'); | ||
var arango = safeRequire('arangojs'); | ||
var url = require('url'); | ||
@@ -44,6 +44,6 @@ | ||
} | ||
schema.adapter = new Arango(schema, callback); | ||
schema.adapter = new Arango(s, schema, callback); | ||
}; | ||
function ArangoId(fullId) { | ||
var ArangoId = function (fullId) { | ||
var parts = fullId.split('/'); | ||
@@ -70,9 +70,12 @@ this._id = parts[0]; | ||
function Arango(schema, callback) { | ||
function Arango(s, schema, callback) { | ||
var self = this; | ||
self.name = 'arango'; | ||
self._models = {}; | ||
self.server = arango.Connection(schema.settings.url); | ||
self.server.database.create(schema.settings.database).then(function (res) { | ||
console.log('Database ' + schema.settings.database + ' created!'); | ||
self.settings = s; | ||
self.collections = {}; | ||
self.aqlQuery = arango.aqlQuery; | ||
self.client = new arango.Database(schema.settings.url); | ||
self.client.createDatabase(s.database, function (res) { | ||
// console.log('Database ' + schema.settings.database + ' created!'); | ||
}, function (err) { | ||
@@ -83,5 +86,4 @@ if (err.errorNum !== 1207) { | ||
}); | ||
var db = self.server.use('/' + schema.settings.database); | ||
self._client = db; | ||
schema.client = db; | ||
self.client.useDatabase(s.database); | ||
schema.client = self.client; | ||
process.nextTick(function () { | ||
@@ -93,26 +95,102 @@ callback(); | ||
Arango.prototype.define = function (descr) { | ||
var self = this, indexes = [], uniques = []; | ||
var self = this, indexes; | ||
if (!descr.settings) descr.settings = {}; | ||
this._models[descr.model.modelName] = descr; | ||
self._models[descr.model.modelName] = descr; | ||
self.client.useDatabase(self.settings.database); | ||
var collection = self.client.collection(descr.model.modelName); | ||
collection.create(); | ||
self.collections[descr.model.modelName] = collection; | ||
Object.keys(descr.properties).forEach(function (k) { | ||
if (typeof descr.properties[k].index !== 'undefined' || typeof descr.properties[k].unique !== 'undefined') { | ||
if (typeof descr.properties[k].unique !== 'undefined') { | ||
uniques.push(k); | ||
} else { | ||
indexes.push(k); | ||
} | ||
// self.collection(descr.model.modelName).ensureIndex(fields, params); | ||
var keys = self.getModelIndexes(descr.model.modelName); | ||
if (keys.indexes.length) { | ||
self.ensureIndex(descr.model.modelName, keys.indexes, false, function () { | ||
// created | ||
}.bind(self)); | ||
} | ||
if (keys.uniques.length) { | ||
self.ensureIndex(descr.model.modelName, keys.uniques, true, function () { | ||
// created | ||
}.bind(self)); | ||
} | ||
}; | ||
Arango.prototype.getModelIndexes = function (name) { | ||
var model = this._models[name], indexes = [], uniques = []; | ||
Object.keys(model.properties).forEach(function (k) { | ||
if (typeof model.properties[k].unique !== 'undefined') { | ||
uniques.push(k); | ||
} | ||
}); | ||
if (typeof model.properties[k].index !== 'undefined') { | ||
indexes.push(k); | ||
} | ||
}.bind(this)); | ||
return { | ||
indexes: indexes, | ||
uniques: uniques | ||
}; | ||
}; | ||
if (indexes.length) { | ||
self._client.index.createHashIndex(descr.model.modelName, indexes, false, function(err, index){ | ||
if(err) console.log(err, index); | ||
}); | ||
Arango.prototype.collection = function (name) { | ||
if (this.client.collection) { | ||
return this.client.collection(name); | ||
} else { | ||
if (!this.collections[name]) { | ||
this.collections[name] = this.client.collection(name); | ||
} | ||
return this.collections[name]; | ||
} | ||
if (uniques.length) { | ||
self._client.index.createHashIndex(descr.model.modelName, uniques, true, function(err, index){ | ||
if(err) console.log(err, index); | ||
}; | ||
/** | ||
* Update existing database collections. | ||
* @param {Function} callback | ||
*/ | ||
Arango.prototype.autoupdate = function (callback) { | ||
var self = this, pending = Object.keys(self._models).length; | ||
if (!pending) { | ||
return callback && callback(); | ||
} | ||
Object.keys(self._models).forEach(function (model) { | ||
self.collection(model).create(function () { | ||
self.collection(model) | ||
.setProperties({waitForSync: true}, function () { | ||
if (--pending === 0) { | ||
setTimeout(function () { | ||
var keys = self.getModelIndexes(model); | ||
self.ensureIndex(model, keys.indexes, false, function () { | ||
return self.ensureIndex(model, keys.uniques, true, callback); | ||
}); | ||
}, 100); | ||
} | ||
}); | ||
}); | ||
}); | ||
}; | ||
/** | ||
* Re create existing database collections. | ||
* @param {Function} callback | ||
*/ | ||
Arango.prototype.automigrate = function (callback) { | ||
var self = this, pending = 0; | ||
Object.keys(self._models).forEach(function (model) { | ||
pending++; | ||
self.client.collection.delete(model, function (err, result) { | ||
if (!err || result.code == 404) { | ||
self.collection(model).create({waitForSync: true}, function (err) { | ||
if (err) { | ||
return callback && callback(err); | ||
} else { | ||
collectionCreated(); | ||
} | ||
}); | ||
} | ||
}.bind(self)); | ||
}, self); | ||
var collectionCreated = function () { | ||
if (--pending == 0 && callback) { | ||
callback(); | ||
} | ||
} | ||
@@ -126,5 +204,11 @@ }; | ||
Arango.prototype.fromDatabase = function (model, data) { | ||
if (!data) return null; | ||
if (!data) { | ||
return null; | ||
} | ||
var props = this._models[model].properties; | ||
Object.keys(data).forEach(function (key) { | ||
if (/^_/.test(key)) { | ||
delete data[key]; | ||
return; | ||
} | ||
var val = data[key]; | ||
@@ -142,15 +226,17 @@ if (props[key]) { | ||
Arango.prototype.create = function (model, data, callback) { | ||
this.save(model, data, callback); | ||
}; | ||
Arango.prototype.save = function (model, data, callback) { | ||
var self = this; | ||
self._client.document.create(model, data, {}, function (err, res, hdr) { | ||
if (callback) { | ||
if (err) { | ||
callback(err, null); | ||
} else { | ||
data.id = parseInt((res._id || '0').split('/')[1]); | ||
self._client.document.put(res._id, data, {}, function (err, res) { | ||
callback(null, parseInt(res._key)); | ||
}); | ||
} | ||
self.collection(model).save(data, function (err, res) { | ||
if (err) { | ||
return callback && callback(err); | ||
} else { | ||
data.id = parseInt(res._key); | ||
self.collection(model).update(res, {id: data.id}, function (err, res) { | ||
return callback && callback(err, data.id, res._rev); | ||
}); | ||
} | ||
}); | ||
}.bind(this)); | ||
}; | ||
@@ -160,64 +246,29 @@ | ||
var self = this; | ||
// mop: hmmm the test is trying to fetch a model with id 1. However that will trigger a 400 (invalid id format in arangodb) | ||
// fix the test for now but this is of course a hack here | ||
if (typeof id !== 'string') { | ||
id = model + '/' + id; | ||
} | ||
self._client.document.get(id, function (err, data) { | ||
if (err && data.code == 404) { | ||
return callback(null); | ||
self.collection(model).document('' + id, function (err, res) { | ||
var data; | ||
err = (err || {}).code !== 404 ? err : null; | ||
if (!err) { | ||
if (res && res.id) { | ||
res.id = parseInt(res._key); | ||
data = self.fromDatabase(model, res); | ||
} | ||
} | ||
callback(err ? err : null, err ? null : self.fromDatabase(model, data)); | ||
}); | ||
return callback && callback(err, data); | ||
}.bind(this)); | ||
}; | ||
Arango.prototype.exists = function (model, id, callback) { | ||
// mop: megagay...just to fix the fcking test | ||
if (typeof id !== 'string') { | ||
id = model + '/' + id; | ||
} | ||
this._client.document.get(id, function (err, data) { | ||
if (!err) { | ||
callback(null, true); | ||
} else if (data.code == 404) { | ||
callback(null, false); | ||
} else { | ||
callback(err); | ||
} | ||
}); | ||
this.collection(model).document('' + id, function (err, res) { | ||
return callback && callback(err, !err && res.id); | ||
}.bind(this)); | ||
}; | ||
Arango.prototype.all = function (model, filter, callback) { | ||
var key; | ||
var self = this; | ||
var query = this._client.query.new(); | ||
query.for('result') | ||
.in(model) | ||
.return('result'); | ||
Arango.prototype.all = Arango.prototype.find = function (model, filter, callback) { | ||
var self = this, query = ['FOR x IN @@collection']; | ||
var bindVars = { | ||
'@collection': model | ||
}, partName, index = 0; | ||
var queryArgs = {}, index, partName, realKey; | ||
var resultFunction = function (err, res, hdr) { | ||
if (err) { | ||
callback(res); | ||
} else { | ||
var mapFn = function (o) { | ||
o = self.fromDatabase(model, o); | ||
o.id = parseInt((o._id).split('/')[1]); | ||
delete o._id; | ||
return o; | ||
}; | ||
var objs = res.result.map(mapFn); | ||
if (filter && filter.include) { | ||
self._models[model].model.include(objs, filter.include, callback); | ||
} else { | ||
callback(null, objs); | ||
} | ||
} | ||
}; | ||
if (filter) { | ||
if (filter.where) { | ||
var filterQuery = []; | ||
var index = 0; | ||
Object.keys(filter.where).forEach(function (k) { | ||
@@ -227,7 +278,2 @@ var cond = filter.where[k]; | ||
partName = 'where' + (index++); | ||
key = k; | ||
if (key == 'id') { | ||
key = '_id'; | ||
} | ||
if (cond && cond.constructor.name === 'Object') { | ||
@@ -242,175 +288,108 @@ spec = Object.keys(cond)[0]; | ||
} else { | ||
queryArgs[partName] = cond; | ||
filterQuery.push('result.' + key + ' IN @' + partName); | ||
// bindVars[partName] = cond; | ||
// query.push('x.' + k + ' IN @' + partName); | ||
} | ||
} else { | ||
if (key == 'id') { | ||
cond = model + '/' + cond; | ||
} | ||
queryArgs[partName] = cond; | ||
filterQuery.push('result.' + key + ' == @' + partName); | ||
bindVars[partName] = cond; | ||
query.push('FILTER x.`' + k + '` == @' + partName); | ||
} | ||
}); | ||
if (filterQuery.length) { | ||
query.filter(filterQuery.join(' && ')); | ||
} | ||
}.bind(self)); | ||
} | ||
if (filter.order) { | ||
var order = 'result.' + filter.order; | ||
if (typeof order === 'string') order = [order]; | ||
query.sort(order.join(', ')); | ||
// var order = 'i.' + filter.order; | ||
// if (typeof order === 'string') order = [order]; | ||
// query.sort(order.join(', ')); | ||
} | ||
if (filter.limit) { | ||
if (filter.skip) { | ||
query.limit(filter.skip + "," + filter.limit); | ||
} else { | ||
query.limit(filter.limit); | ||
query.push('LIMIT @skip, @limit'); | ||
bindVars['skip'] = filter.skip || 0; | ||
bindVars['limit'] = filter.limit || 20; | ||
} | ||
query.push('RETURN x'); | ||
var maql = self.aqlQuery([query.join(' ')]); | ||
maql.bindVars = bindVars; | ||
if (!Object.keys(bindVars).length) { | ||
maql = query.join(' '); | ||
} | ||
self.client.query(maql, function (err, cursor) { | ||
if (err) { | ||
console.log('query err:', err.message); | ||
} | ||
var data = (cursor || {})._result || []; | ||
if (data && data.length) { | ||
data = data.map(function (i) { | ||
return self.fromDatabase(model, i); | ||
}); | ||
} | ||
return callback && callback(err, data) | ||
}.bind(self)); | ||
} else { | ||
var opt = {}; | ||
if (filter.limit) { | ||
opt.skip = filter.offset || filter.skip || 0; | ||
opt.limit = filter.limit; | ||
} | ||
self.collection(model).all(opt, function (err, res) { | ||
var data = (res || {})._result || []; | ||
if (data && data.length) { | ||
data = data.map(function (i) { | ||
return self.fromDatabase(model, i); | ||
}); | ||
} | ||
return callback && callback(err, data); | ||
}.bind(self)); | ||
} | ||
this._client.query.string = query.toString(); | ||
this._client.query.exec(queryArgs, resultFunction); | ||
}; | ||
Arango.prototype.save = function (model, data, callback) { | ||
var id = data.id; | ||
if (typeof id !== 'string') { | ||
id = model + '/' + id; | ||
} | ||
console.log('inst', id) | ||
this._client.document.put(id, data, {}, function (err, res) { | ||
callback(err ? err : null); | ||
}); | ||
}; | ||
Arango.prototype.updateOrCreate = function (model, data, callback) { | ||
var adapter = this; | ||
if (!data.id) { | ||
return this.create(model, data, callback); | ||
} | ||
// mop: copypasta from mongodb | ||
this.find(model, data.id, function (err, inst) { | ||
if (!err) { | ||
adapter.updateAttributes(model, data.id, data, callback); | ||
} else { | ||
delete data.id; | ||
adapter.create(model, data, function (err, id) { | ||
if (err) return callback(err); | ||
if (id) { | ||
data.id = id; | ||
delete data._id; | ||
callback(null, data); | ||
} else { | ||
callback(null, null); // wtf? | ||
} | ||
}); | ||
} | ||
}); | ||
}; | ||
Arango.prototype.destroy = function (model, id, callback) { | ||
if (typeof id !== 'string') { | ||
id = model + '/' + id; | ||
} | ||
this._client.document.delete(id, function (err) { | ||
callback(err); | ||
}); | ||
return this.collection(model).remove('' + id, function (err, res) { | ||
return callback && callback(err); | ||
}.bind(this)); | ||
}; | ||
Arango.prototype.updateAttributes = function (model, id, newData, callback) { | ||
this.find(model, id, function (err, data) { | ||
if (err) { | ||
callback(err); | ||
} else { | ||
for (var key in newData) { | ||
if (newData.hasOwnProperty(key)) { | ||
data[key] = newData[key]; | ||
} | ||
} | ||
data.id = id; | ||
this.save(model, data, callback); | ||
} | ||
this.collection(model).update('' + id, newData, function (err, data) { | ||
return callback && callback(err, data); | ||
}.bind(this)); | ||
}; | ||
// TODO: implement | ||
Arango.prototype.count = function (model, callback, where) { | ||
if (!where) { | ||
this._client.collection.count(model, function (err, result) { | ||
callback(err ? err : null, err ? null : result.count); | ||
}); | ||
this.collection(model).count(function (err, res) { | ||
return callback && callback(err, (res || {}).count || 0); | ||
}.bind(this)); | ||
} else { | ||
this._client.simple.example(model, where, {}, function (err, result) { | ||
if (err) { | ||
callback(err, result); | ||
} else { | ||
callback(null, result.count); | ||
} | ||
}); | ||
this.collection(model).count(function (err, res) { | ||
return callback && callback(err, (res || {}).count || 0); | ||
}.bind(this)); | ||
} | ||
}; | ||
/** | ||
* Update existing database collections. | ||
* @param {Function} cb | ||
* Update rows | ||
* @param {String} model | ||
* @param {Object} filter | ||
* @param {Object} data | ||
* @param {Function} callback | ||
*/ | ||
Arango.prototype.autoupdate = function (cb) { | ||
var self = this, pending = 0; | ||
Object.keys(this._models).forEach(function (model) { | ||
pending++; | ||
self._client.collection.create(model, {waitForSync: true}, function (err) { | ||
if (err) { | ||
if (cb) { | ||
cb(err); | ||
} else { | ||
console.log(err); | ||
} | ||
} else { | ||
collectionCreated(); | ||
} | ||
}); | ||
}, self); | ||
var collectionCreated = function () { | ||
if (--pending == 0 && cb) { | ||
cb(); | ||
} | ||
Arango.prototype.update = function (model, filter, data, callback) { | ||
if ('function' === typeof filter) { | ||
return filter(new Error("Get parametrs undefined"), null); | ||
} | ||
}; | ||
if ('function' === typeof data) { | ||
return data(new Error("Set parametrs undefined"), null); | ||
} | ||
/** | ||
* Re create existing database collections. | ||
* @param {Function} callback | ||
*/ | ||
Arango.prototype.automigrate = function (callback) { | ||
var self = this, pending = 0; | ||
Object.keys(self._models).forEach(function (model) { | ||
pending++; | ||
self._client.collection.delete(model, function (err, result) { | ||
if (!err || result.code == 404) { | ||
self._client.collection.create(model, {waitForSync: true}, function (err) { | ||
if (err) { | ||
if (callback) { | ||
callback(err); | ||
} else { | ||
console.log(err); | ||
} | ||
} else { | ||
collectionCreated(); | ||
} | ||
}); | ||
} | ||
}.bind(self)); | ||
}, self); | ||
filter = filter || {}; | ||
filter = filter.where ? filter.where : filter; | ||
var collectionCreated = function () { | ||
if (--pending == 0 && callback) { | ||
callback(); | ||
} | ||
} | ||
this.collection(model).updateByExample(filter, data, callback); | ||
}; | ||
// TODO | ||
// TODO: implement | ||
Arango.prototype.remove = function remove(model, filter, callback) { | ||
var cond = buildWhere(filter.where); | ||
this.collection(model).remove(cond, callback); | ||
// var cond = buildWhere(filter.where); | ||
this.collection(model).removeByExample(filter.where, callback); | ||
}; | ||
/** | ||
@@ -421,17 +400,26 @@ * Truncate collection. | ||
Arango.prototype.destroyAll = function (model, callback) { | ||
this._client.collection.truncate(model, function (err, result) { | ||
if (err) { | ||
callback(err, []); | ||
} else { | ||
callback(null); | ||
} | ||
}); | ||
this.collection(model).truncate(function (res) { | ||
var err = (res || {}).error ? res : null; | ||
return callback && callback(err); | ||
}.bind(this)); | ||
}; | ||
Arango.prototype.ensureIndex = function ensureIndex(model, fields, params, callback) { | ||
'use strict'; | ||
self._client.index.createHashIndex(model, fields, params.unique, function(err, index){ | ||
if(err) console.log(err, index); | ||
}); | ||
process.nextTick(callback) | ||
/** | ||
* EnsureIndex in collection. | ||
* @param {String} model | ||
* @param {Array} fields | ||
* @param {Array} unique | ||
* @param {Function} callback | ||
* @returns {*} | ||
*/ | ||
Arango.prototype.ensureIndex = function ensureIndex(model, fields, unique, callback) { | ||
if (!fields || !fields.length) { | ||
return callback && callback(); | ||
} | ||
if (typeof unique === 'function') { | ||
callback = unique; | ||
unique = false; | ||
} | ||
this.collection(model) | ||
.createHashIndex(fields, {unique: unique}, callback); | ||
}; |
@@ -12,3 +12,2 @@ /** | ||
exports.initialize = function initializeSchema(schema, callback) { | ||
'use strict'; | ||
if (!pg) { | ||
@@ -40,11 +39,11 @@ throw new Error('module pg is not defined, try\n npm install pg'); | ||
}); | ||
schema.adapter = new PG(schema.client); | ||
schema.adapter = new PG(s, schema.client); | ||
schema.adapter.connect(schema, callback); | ||
}; | ||
function PG(client) { | ||
'use strict'; | ||
function PG(s, client) { | ||
this.name = 'postgres'; | ||
this._models = {}; | ||
this.client = client; | ||
this.settings = s; | ||
} | ||
@@ -55,3 +54,2 @@ | ||
PG.prototype.connect = function (schema, callback) { | ||
'use strict'; | ||
var self = this; | ||
@@ -70,3 +68,2 @@ createBlankDB(schema, function () { | ||
function createBlankDB(schema, callback) { | ||
'use strict'; | ||
var s = schema.settings; | ||
@@ -95,3 +92,2 @@ pg.connect({ | ||
PG.prototype.tableEscaped = function (model) { | ||
'use strict'; | ||
return this.escapeName(this.table(model)); | ||
@@ -101,8 +97,8 @@ }; | ||
PG.prototype.query = function (sql, callback) { | ||
'use strict'; | ||
var time = Date.now(); | ||
var log = this.log; | ||
this.client.query(sql, function (err, data) { | ||
if (log) | ||
if (log) { | ||
log(sql, time); | ||
} | ||
callback(err, data ? data.rows : null); | ||
@@ -119,3 +115,2 @@ }); | ||
PG.prototype.create = function (model, data, callback) { | ||
'use strict'; | ||
var fields = this.toFields(model, data, true); | ||
@@ -145,3 +140,2 @@ var sql = 'INSERT INTO ' + this.tableEscaped(model) + ''; | ||
PG.prototype.update = function (model, filter, data, callback) { | ||
'use strict'; | ||
if ('function' === typeof filter) { | ||
@@ -185,3 +179,2 @@ return filter(new Error("Get parametrs undefined"), null); | ||
PG.prototype.all = function all(model, filter, callback) { | ||
'use strict'; | ||
if ('function' === typeof filter) { | ||
@@ -218,3 +211,2 @@ callback = filter; | ||
PG.prototype.remove = function remove(model, filter, callback) { | ||
'use strict'; | ||
if ('function' === typeof filter) { | ||
@@ -241,3 +233,2 @@ callback = filter; | ||
PG.prototype.toFields = function (model, data, forCreate) { | ||
'use strict'; | ||
var fields = []; | ||
@@ -286,3 +277,2 @@ var props = this._models[model].properties; | ||
PG.prototype.toDatabase = function (prop, val) { | ||
'use strict'; | ||
if (val === null) { | ||
@@ -322,3 +312,2 @@ // Postgres complains with NULLs in not null columns | ||
} | ||
; | ||
@@ -344,3 +333,2 @@ if (prop.type.name === 'Date') { | ||
PG.prototype.fromDatabase = function (model, data) { | ||
'use strict'; | ||
if (!data) { | ||
@@ -353,3 +341,9 @@ return null; | ||
var val = data[key]; | ||
data[key] = val; | ||
if (props[key]) { | ||
if ((props[key].type.name || '').toString().toLowerCase() === 'json' && typeof val == "string") { | ||
data[key] = JSON.parse(val); | ||
} else { | ||
data[key] = val; | ||
} | ||
} | ||
}); | ||
@@ -362,3 +356,2 @@ return data; | ||
PG.prototype.getColumns = function (model) { | ||
'use strict'; | ||
return '"' + Object.keys(this._models[model].properties).join('", "') + '"'; | ||
@@ -368,3 +361,2 @@ }; | ||
PG.prototype.toFilter = function (model, filter) { | ||
'use strict'; | ||
var self = this, out = ''; | ||
@@ -398,3 +390,2 @@ if (filter && typeof filter.where === 'function') { | ||
function getTableStatus(model, cb) { | ||
'use strict'; | ||
function decoratedCallback(err, data) { | ||
@@ -411,3 +402,2 @@ data.forEach(function (field) { | ||
PG.prototype.autoupdate = function (cb) { | ||
'use strict'; | ||
var self = this; | ||
@@ -438,3 +428,2 @@ var wait = 0; | ||
PG.prototype.isActual = function (cb) { | ||
'use strict'; | ||
var self = this; | ||
@@ -463,3 +452,2 @@ var wait = 0; | ||
PG.prototype.alterTable = function (model, actualFields, indexes, done) { | ||
'use strict'; | ||
var self = this; | ||
@@ -471,5 +459,3 @@ var pendingChanges = getPendingChanges.call(self, model, actualFields); | ||
function getPendingChanges(model, actualFields) { | ||
'use strict'; | ||
var sql = []; | ||
var self = this; | ||
var sql = [], self = this; | ||
sql = sql.concat(getColumnsToAdd.call(self, model, actualFields)); | ||
@@ -482,3 +468,2 @@ sql = sql.concat(getPropertiesToModify.call(self, model, actualFields)); | ||
function getColumnsToAdd(model, actualFields) { | ||
'use strict'; | ||
var self = this; | ||
@@ -500,5 +485,3 @@ var m = self._models[model]; | ||
function addPropertyToActual(model, propName) { | ||
'use strict'; | ||
var self = this; | ||
var p = self._models[model].properties[propName]; | ||
var self = this, p = self._models[model].properties[propName]; | ||
var sqlCommand = 'ADD COLUMN "' + propName + '" ' + datatype(p) + " " + (propertyCanBeNull.call(self, model, propName) ? "" : " NOT NULL"); | ||
@@ -509,3 +492,2 @@ return sqlCommand; | ||
function searchForPropertyInActual(propName, actualFields) { | ||
'use strict'; | ||
var found = false; | ||
@@ -522,8 +504,4 @@ actualFields.forEach(function (f) { | ||
function getPropertiesToModify(model, actualFields) { | ||
'use strict'; | ||
var self = this; | ||
var sql = []; | ||
var m = self._models[model]; | ||
var self = this, sql = [], found, m = self._models[model]; | ||
var propNames = Object.keys(m.properties); | ||
var found; | ||
propNames.forEach(function (propName) { | ||
@@ -567,3 +545,2 @@ if (propName === 'id') | ||
function modifyDatatypeInActual(model, propName) { | ||
'use strict'; | ||
var self = this; | ||
@@ -575,5 +552,3 @@ var sqlCommand = 'ALTER COLUMN "' + propName + '" TYPE ' + datatype(self._models[model].properties[propName]); | ||
function modifyNullabilityInActual(model, propName) { | ||
'use strict'; | ||
var self = this; | ||
var sqlCommand = 'ALTER COLUMN "' + propName + '" '; | ||
var self = this, sqlCommand = 'ALTER COLUMN "' + propName + '" '; | ||
if (propertyCanBeNull.call(self, model, propName)) { | ||
@@ -590,4 +565,3 @@ sqlCommand = sqlCommand + "DROP "; | ||
'use strict'; | ||
var self = this; | ||
var sql = []; | ||
var self = this, sql = []; | ||
actualFields.forEach(function (actualField) { | ||
@@ -634,7 +608,7 @@ if (actualField.Field === 'id') | ||
'use strict'; | ||
var self = this; | ||
var sql = ['"id" SERIAL PRIMARY KEY']; | ||
var self = this, sql = ['"id" SERIAL PRIMARY KEY']; | ||
Object.keys(this._models[model].properties).forEach(function (prop) { | ||
if (prop === 'id') | ||
if (prop === 'id') { | ||
return; | ||
} | ||
sql.push('"' + prop + '" ' + self.propertySettingsSQL(model, prop)); | ||
@@ -647,8 +621,7 @@ }); | ||
PG.prototype.propertySettingsSQL = function (model, propName) { | ||
'use strict'; | ||
var self = this; | ||
var p = self._models[model].properties[propName]; | ||
var self = this, p = self._models[model].properties[propName]; | ||
var result = datatype(p) + ' '; | ||
if (!propertyCanBeNull.call(self, model, propName)) | ||
if (!propertyCanBeNull.call(self, model, propName)) { | ||
result = result + 'NOT NULL '; | ||
} | ||
return result; | ||
@@ -658,3 +631,2 @@ }; | ||
function propertyCanBeNull(model, propName) { | ||
'use strict'; | ||
var p = this._models[model].properties[propName]; | ||
@@ -665,3 +637,2 @@ return !(p.allowNull === false || p['null'] === false); | ||
function escape(val) { | ||
'use strict'; | ||
if (val === undefined || val === null) { | ||
@@ -707,3 +678,2 @@ return 'NULL'; | ||
function datatype(p) { | ||
'use strict'; | ||
switch ((p.type.name || 'string').toLowerCase()) { | ||
@@ -747,3 +717,2 @@ default: | ||
function propertyHasNotBeenDeleted(model, propName) { | ||
'use strict'; | ||
return !!this._models[model].properties[propName]; | ||
@@ -753,3 +722,2 @@ } | ||
PG.prototype.buildWhere = function buildWhere(conds, adapter, model) { | ||
'use strict'; | ||
var cs = [], or = [], | ||
@@ -783,3 +751,2 @@ self = adapter, | ||
function parseCond(cs, key, props, conds, self) { | ||
'use strict'; | ||
var keyEscaped = escapeName(key); | ||
@@ -851,3 +818,2 @@ var val = self.toDatabase(props[key], conds[key]); | ||
PG.prototype.buildOrderBy = function buildOrderBy(order) { | ||
'use strict'; | ||
if (typeof order === 'string') { | ||
@@ -860,3 +826,2 @@ order = [order]; | ||
PG.prototype.buildLimit = function buildLimit(limit, offset) { | ||
'use strict'; | ||
return ' LIMIT ' + limit + ' OFFSET ' + (offset || '0'); | ||
@@ -866,3 +831,2 @@ }; | ||
PG.prototype.buildGroupBy = function buildGroupBy(group) { | ||
'use strict'; | ||
if (typeof group === 'string') { | ||
@@ -875,4 +839,3 @@ group = [group]; | ||
function escapeName(name) { | ||
'use strict'; | ||
return '"' + name.replace(/\./g, '"."') + '"'; | ||
} |
{ | ||
"name": "caminte", | ||
"description": "ORM for every database: redis, mysql, neo4j, mongodb, rethinkdb, postgres, sqlite, tingodb", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"author": { | ||
@@ -124,5 +124,6 @@ "name": "Aleksej Gordejev", | ||
"rethinkdb": ">= 1.16", | ||
"cassandra-driver": ">=2.1.0" | ||
"cassandra-driver": ">=2.1.0", | ||
"arangojs" : ">= 4.2.0" | ||
}, | ||
"optionalDependencies": {} | ||
} |
@@ -24,2 +24,12 @@ /** | ||
driver : 'mysql', | ||
host : gitlab ? 'mysql' : '127.0.0.1', | ||
port : '3306', | ||
username : 'test', | ||
password : 'test', | ||
database : 'test', | ||
autoReconnect : true | ||
}; | ||
module.exports.mariadb = { | ||
driver : 'mysql', | ||
host : gitlab ? 'mariadb' : '127.0.0.1', | ||
@@ -48,3 +58,3 @@ port : '3306', | ||
password : 'test', | ||
database : 'test' | ||
database : 'test.fdb' | ||
}; | ||
@@ -84,3 +94,12 @@ | ||
port : '7474', | ||
database : 'test', | ||
username : 'neo4j', | ||
password : 'test' | ||
}; | ||
module.exports.arango = { | ||
driver : 'arango', | ||
host : gitlab ? 'arangodb' : '127.0.0.1', | ||
port : '8529', | ||
database : 'test' | ||
}; |
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
670075
23
12984