Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

caminte

Package Overview
Dependencies
Maintainers
1
Versions
100
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

caminte - npm Package Compare versions

Comparing version 0.1.2 to 0.1.3

62

lib/abstract-class.js

@@ -35,2 +35,3 @@ /**

}
AbstractClass.__proto__ = Validatable;

@@ -305,33 +306,23 @@ AbstractClass.prototype.__proto__ = Validatable.prototype;

var Model = this;
if (query.id) {
if (this.schema.adapter.updateOrCreate) {
var inst = new Model(query);
this.schema.adapter.updateOrCreate(Model.modelName, inst.toObject(), function (err, found) {
var obj;
if (found) {
inst._initProperties(found);
obj = inst;
} else {
obj = null;
}
callback(err, obj);
});
} else {
this.findById(query.id, function (err, inst) {
if (err)
return callback(err);
if (inst) {
inst.updateAttributes(data, callback);
} else {
data = helpers.merge(data, query);
var obj = new Model(data);
obj.save(data, callback);
}
});
}
this.findById(query.id, function (err, inst) {
if (err)
return callback(err);
if (inst) {
inst.updateAttributes(data, callback);
} else {
data = helpers.merge(data, query);
var obj = new Model(data);
obj.save(data, callback);
}
});
} else {
Model.findOne({
Model.all({
where: query
}, function (err, inst) {
if (err) {
}, function (err, insts) {
if (err || (insts && insts.length > 1)) {
if(insts.length > 1) {
return callback(new Error('Found more than one record'), insts);
}
if (err.message && !/NotFound/gi.test(err.message)) {

@@ -341,3 +332,4 @@ return callback(err, inst);

}
if (inst) {
if (insts[0]) {
var inst = insts[0];
inst.updateAttributes(data, callback);

@@ -1140,3 +1132,3 @@ } else {

}
query.where[fk] = typeof this.id === 'object'? this.id.toString(): this.id;
query.where[fk] = typeof this.id === 'object' ? this.id.toString() : this.id;
anotherClass.all(query, function (err, inst) {

@@ -1154,3 +1146,3 @@ if (err) {

}
query.where[fk] = typeof this.id === 'object'? this.id.toString(): this.id;
query.where[fk] = typeof this.id === 'object' ? this.id.toString() : this.id;
anotherClass.update(query, data, function (err, inst) {

@@ -1175,3 +1167,3 @@ if (err) {

}
query.where[fk] = typeof this.id === 'object'? this.id.toString(): this.id;
query.where[fk] = typeof this.id === 'object' ? this.id.toString() : this.id;
anotherClass.remove(query, function (err, inst) {

@@ -1186,3 +1178,3 @@ if (err) {

function destroy(id, callback) {
id = typeof id === 'object'? id.toString(): id;
id = typeof id === 'object' ? id.toString() : id;
this.findById(id, function (err, inst) {

@@ -1244,4 +1236,4 @@ if (err)

anotherClass.findById(id, function (err, inst) {
var sid = typeof inst.id === 'object'? inst.id.toString(): inst.id;
var fid = typeof this[fk] === 'object'? this[fk].toString(): this[fk];
var sid = typeof inst.id === 'object' ? inst.id.toString() : inst.id;
var fid = typeof this[fk] === 'object' ? this[fk].toString() : this[fk];
if (err)

@@ -1248,0 +1240,0 @@ return cb(err);

@@ -11,5 +11,6 @@ /**

exports.initialize = function initializeSchema(schema, callback) {
if (!mongodb)
'use strict';
if (!mongodb) {
return;
}
var s = schema.settings;

@@ -62,10 +63,12 @@

this.collections = {};
this.schema = schema;
this.s = s;
var server;
if (s.rs) {
set = [];
var sets = [];
for (i = 0, n = s.hosts.length; i < n; i++) {
set.push(new mongodb.Server(s.hosts[i], s.ports[i], {auto_reconnect: true}));
sets.push(new mongodb.Server(s.hosts[i], s.ports[i], {auto_reconnect: true}));
}
server = new mongodb.ReplSetServers(set, {rs_name: s.rs});
server = new mongodb.ReplSetServers(sets, {rs_name: s.rs});

@@ -77,4 +80,5 @@ } else {

new mongodb.Db(s.database, server, {safe: s.safe}).open(function (err, client) {
if (err)
if (err) {
throw err;
}
if (s.username && s.password) {

@@ -104,3 +108,2 @@ var t = this;

if (typeof descr.properties[k].index !== 'undefined' || typeof descr.properties[k].unique !== 'undefined') {
// console.log(descr.model.modelName)
var fields = {}, params = {};

@@ -107,0 +110,0 @@ fields[k] = 1;

@@ -274,37 +274,2 @@ /**

MySQL.prototype.updateOrCreate = function (model, data, callback) {
'use strict';
var mysql = this;
var fieldsNames = [];
var fieldValues = [];
var combined = [];
var props = this._models[model].properties;
Object.keys(data).forEach(function (key) {
if (props[key] || key === 'id') {
var k = '`' + key + '`';
var v;
if (key !== 'id') {
v = mysql.toDatabase(props[key], data[key]);
} else {
v = data[key];
}
fieldsNames.push(k);
fieldValues.push(v);
if (key !== 'id')
combined.push(k + ' = ' + v);
}
});
var sql = 'INSERT INTO ' + this.tableEscaped(model);
sql += ' (' + fieldsNames.join(', ') + ')';
sql += ' VALUES (' + fieldValues.join(', ') + ')';
sql += ' ON DUPLICATE KEY UPDATE ' + combined.join(', ');
this.query(sql, function (err, info) {
if (!err && info && info.insertId) {
data.id = info.insertId;
}
callback(err, data);
});
};
/**

@@ -446,2 +411,5 @@ * Update rows

}
if ((props[key].type.name || '').toString().toLowerCase() === 'json' && typeof val == "string") {
val = JSON.parse(val);
}
}

@@ -747,6 +715,6 @@ data[key] = val;

var foreignKeys = this._models[model].settings.foreignKeys || [];
primaryKeys = primaryKeys.slice(0);
foreignKeys = foreignKeys.slice(0);
Object.keys(this._models[model].properties).forEach(function (prop) {

@@ -753,0 +721,0 @@ if (prop === 'id') {

@@ -9,8 +9,32 @@ /**

exports.initialize = function initializeSchema(schema, callback) {
// 'http://username:password@localhost:7474'
if (!schema.settings.url) {
var auth = '';
var url = schema.settings.host || 'localhost';
var port = schema.settings.port || 7474;
url += ':' + port;
if (schema.settings.username) {
auth = schema.settings.username;
if (schema.settings.password) {
auth += ':' + schema.settings.password;
}
}
if (auth) {
url = auth + '@' + url;
}
if (schema.settings.database) {
url += '/' + schema.settings.database;
} else {
url += '/';
}
url = 'http://' + url;
schema.settings.url = url;
}
schema.client = new neo4j.GraphDatabase(schema.settings.url);
schema.adapter = new Neo4j(schema.client);
schema.adapter = new Neo4j(schema.settings, schema.client);
process.nextTick(callback);
};
function Neo4j(client) {
function Neo4j(s, client) {
this.name = 'neo4j';

@@ -20,2 +44,3 @@ this._models = {};

this.cache = {};
this.s = s;
}

@@ -29,2 +54,10 @@

/**
* Update existing database collections.
* @param {Function} cb
*/
Neo4j.prototype.autoupdate = function (cb) {
cb();
};
Neo4j.prototype.createIndexHelper = function(cls, indexName) {

@@ -31,0 +64,0 @@ var db = this.client;

@@ -131,47 +131,2 @@ /**

/**
* Must invoke callback(err, data)
* @param {String} model
* @param {Object} data
* @param {Function} callback
*/
PG.prototype.updateOrCreate = function (model, data, callback) {
'use strict';
var pg = this;
var fieldsNames = [];
var fieldValues = [];
var combined = [];
var props = this._models[model].properties;
Object.keys(data).forEach(function (key) {
if (props[key] || key === 'id') {
var k = '"' + key + '"';
var v;
if (key !== 'id') {
v = pg.toDatabase(props[key], data[key]);
} else {
v = data[key];
}
fieldsNames.push(k);
fieldValues.push(v);
if (key !== 'id')
combined.push(k + ' = ' + v);
}
});
var sql = 'UPDATE ' + this.tableEscaped(model);
sql += ' SET ' + combined + ' WHERE id = ' + data.id + ';';
sql += ' INSERT INTO ' + this.tableEscaped(model);
sql += ' (' + fieldsNames.join(', ') + ')';
sql += ' SELECT ' + fieldValues.join(', ');
sql += ' WHERE NOT EXISTS (SELECT 1 FROM ' + this.tableEscaped(model);
sql += ' WHERE id = ' + data.id + ') RETURNING id';
this.query(sql, function (err, info) {
if (!err && info && info[0] && info[0].id) {
data.id = info[0].id;
}
callback(err, data);
});
};
/**
* Update rows

@@ -178,0 +133,0 @@ * @param {String} model

@@ -378,35 +378,2 @@ /**

BridgeToRedis.prototype.updateOrCreate = BridgeToRedis.prototype.upsert = OrCreate = function (model, filter, callback) {
if ('function' === typeof filter) {
callback = filter;
filter = {};
}
if (!filter) {
filter = {};
}
filter.limit = 1;
var self = this;
self.all(model, filter, function (err, data) {
if (!data || !data.id) {
return self.create(model, data, callback);
}
var client = self.client;
var key = 'id:' + model;
self.save(model, data, function (error, obj) {
client.get(key, function (err, id) {
if (!id || data.id > parseInt(id, 10)) {
client.set([key, data.id], ok);
} else {
ok();
}
});
function ok() {
callback(error, obj);
}
});
});
};
BridgeToRedis.prototype.exists = function (model, id, callback) {

@@ -413,0 +380,0 @@ this.client.exists(model + ':' + id, function (err, exists) {

@@ -110,17 +110,22 @@ /**

SQLite3.prototype.updateOrCreate = function (model, data, callback) {
data = data || {};
var questions = [];
var values = Object.keys(data).map(function (key) {
questions.push('?');
return data[key];
});
var sql = 'INSERT OR REPLACE INTO ' + this.tableEscaped(model) + ' (' + Object.keys(data).join(',') + ') VALUES (';
sql += questions.join(',');
sql += ')';
this.command(sql, values, function (err) {
if (!err && this) {
data.id = this.lastID;
SQLite3.prototype.updateOrCreate = function (model, filter, data, callback) {
filter = filter || {};
var self = this, Model = self._models[model].model;
self.all(model, {where: filter}, function (err, found) {
if(err || (found && found.length > 1)) {
return callback(err || new Error("Found multiple instances"));
}
callback(err, data);
if (found && found.length === 1) {
var inst = self.fromDatabase(model, found[0]);
var obj = new Model;
obj._initProperties(inst, false);
obj.updateAttributes(data, function(err){
callback(err, obj);
});
} else {
Object.keys(filter).forEach(function(key){
data[key] = filter[key];
});
self.create(model, data, callback)
}
});

@@ -239,4 +244,4 @@ };

if (props[key]) {
if (props[key].type.name == 'JSON' && typeof val == "string") data[key] = JSON.parse(val);
else data[key] = val;
if ((props[key].type.name || '').toString().toLowerCase() === 'json' && typeof val == "string") { data[key] = JSON.parse(val); }
else { data[key] = val; }
}

@@ -327,8 +332,8 @@ });

self.queryAll('PRAGMA TABLE_INFO(' + self.tableEscaped(model) + ');', function (err, fields) {
if(err) done(err);
if (err) done(err);
self.queryAll('PRAGMA INDEX_LIST(' + self.tableEscaped(model) + ');', function (err, indexes) {
if(err) done(err);
if (err) done(err);
if (!err && fields.length) {
self.alterTable(model, fields, indexes, done);
} else {
} else {
self.createTable(model, indexes, done);

@@ -641,5 +646,5 @@ }

var primaryKeys = this._models[model].settings.primaryKeys || [];
primaryKeys = primaryKeys.slice(0);
props.forEach(function (prop) {

@@ -656,17 +661,17 @@ if (prop === 'id') {

for (var i = 0, length = primaryKeys.length; i < length; i++) {
if(props.indexOf(primaryKeys[i]) === -1) {
if(primaryKeys[i] === 'id') {
id = true;
sql.push('`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL');
} else {
sql.push('`' + primaryKeys[i] + '` ' + self.propertySettingsSQL(model, primaryKeys[i]));
}
}
if (props.indexOf(primaryKeys[i]) === -1) {
if (primaryKeys[i] === 'id') {
id = true;
sql.push('`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL');
} else {
sql.push('`' + primaryKeys[i] + '` ' + self.propertySettingsSQL(model, primaryKeys[i]));
}
}
}
if(!id) {
sql.push('PRIMARY KEY (`' + primaryKeys.join('`, `') + '`)');
if (!id) {
sql.push('PRIMARY KEY (`' + primaryKeys.join('`, `') + '`)');
}
} else {
if(!id) {
sql.push('`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL');
if (!id) {
sql.push('`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL');
}

@@ -673,0 +678,0 @@ }

{
"name": "caminte",
"description": "ORM for every database: redis, mysql, neo4j, mongodb, rethinkdb, postgres, sqlite, tingodb",
"version": "0.1.2",
"version": "0.1.3",
"author": {

@@ -6,0 +6,0 @@ "name": "Aleksej Gordejev",

@@ -38,2 +38,6 @@ /**

after(function (done) {
Article.destroyAll(done);
});
it('#create', function (done) {

@@ -40,0 +44,0 @@ Article.create(newArticle, function (err, created) {

@@ -32,2 +32,6 @@ /**

after(function (done) {
Category.destroyAll(done);
});
it('#create', function (done) {

@@ -34,0 +38,0 @@ Category.create(newCategory, function (err, created) {

@@ -36,2 +36,6 @@ /**

after(function (done) {
User.destroyAll(done);
});
it('#create', function (done) {

@@ -38,0 +42,0 @@ User.create(newUser, function (err, created) {

@@ -5,16 +5,125 @@ /**

if (!process.env.NODE_ENV) {
process.env.NODE_ENV = 'test';
}
var driver = process.env.CAMINTE_DRIVER || 'sqlite';
var should = require('should');
var caminte = require('../../');
var config = require('./../lib/database');
var dbConf = config[driver];
var UserModel = require('./../lib/User');
var Schema = caminte.Schema;
dbConf.host = process.env.DB_HOST || dbConf.host || '';
var schema = new Schema(dbConf.driver, dbConf);
var User = UserModel(schema);
/*
afterInitialize: null,
beforeValidation: null,
afterValidation: null,
beforeSave: null,
afterSave: null,
beforeCreate: null,
afterCreate: null,
beforeUpdate: null,
afterUpdate: null,
beforeDestroy: null,
afterDestroy: null,
describe(driver + ' - hooks:', function () {
'use strict';
var user, nuser, newUser = {
language: 'en',
first_name: 'Alex',
last_name: 'Gordan',
screen_name: 'alex',
email: 'bubles@example.com',
password: 'AAAAAAAAA',
age: 45
};
*/
before(function (done) {
schema.autoupdate(done);
});
it("#afterInitialize", function (done) {
User.afterInitialize = function () {
User.afterInitialize = null;
return done();
};
user = new User;
});
it("#beforeCreate", function (done) {
User.beforeCreate = function () {
User.beforeCreate = null;
return done();
};
User.create(newUser, function (err) {
should.not.exist(err);
});
});
it("#afterCreate", function (done) {
User.afterCreate = function () {
User.afterCreate = null;
return done();
};
newUser.email = 'bubles@example.org';
User.create(newUser, function (err) {
should.not.exist(err);
});
});
it('#beforeSave', function (done) {
User.beforeSave = function () {
User.beforeSave = null;
return done();
};
user = new User(newUser);
user.email = 'bubles@example.mobi';
user.save(function (err) {
should.not.exist(err);
});
});
it('#afterSave', function (done) {
User.afterSave = function () {
User.afterSave = null;
return done();
};
nuser = new User(newUser);
nuser.email = 'bubles@example.lv';
nuser.save(function (err) {
should.not.exist(err);
});
});
it("#beforeUpdate", function (done) {
User.beforeUpdate = function () {
User.beforeUpdate = null;
return done();
};
user.updateAttributes({
email: "1@1.com"
}, function (err) {
should.not.exist(err);
});
});
it("#afterUpdate", function (done) {
User.afterUpdate = function () {
User.afterUpdate = null;
return done();
};
nuser.updateAttributes({
email: "2@2.com"
}, function (err) {
should.not.exist(err);
});
});
it("#beforeDestroy", function (done) {
User.beforeDestroy = function () {
User.beforeDestroy = null;
return done();
};
user.destroy();
});
it("#afterDestroy", function (done) {
User.afterDestroy = function () {
User.afterDestroy = null;
return done();
};
nuser.destroy();
});
});

@@ -35,3 +35,3 @@ /**

after(function (done) {
done();
Article.destroyAll(done);
});

@@ -38,0 +38,0 @@

@@ -35,3 +35,3 @@ /**

after(function (done) {
done();
Category.destroyAll(done);
});

@@ -38,0 +38,0 @@

@@ -40,3 +40,3 @@ /**

after(function (done) {
done();
User.destroyAll(done);
});

@@ -43,0 +43,0 @@

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc