Socket
Socket
Sign inDemoInstall

ee-orm

Package Overview
Dependencies
Maintainers
1
Versions
156
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

ee-orm - npm Package Compare versions

Comparing version 0.1.2 to 0.1.3

214

lib/DefaultModel.js

@@ -21,2 +21,5 @@ !function(){

// check for changes
this.on('change', this._setChanged.bind(this));
// remove deprecated parent property

@@ -29,6 +32,7 @@ delete this.parent;

this._setProrperty('_belongsTo', {});
this._setProrperty('_columns', {});
//this._setProrperty('_columns', {});
this._setProrperty('_references', {});
this._setProrperty('_changedReferences', []);
this._setProrperty('_mappingIds', [], true);
this._setProrperty('_hasChanges', false, true);
this._setProrperty('_relatingSets', options.relatingSets);

@@ -44,3 +48,18 @@

, _setChanged: function() {
this._hasChanges = true;
}
, getDefinition: function() {
return this._defintion;
}
, getEntityName: function() {
this._defintion.name;
}
/*
, _initializeRelatingSets: function(partial) {

@@ -72,4 +91,4 @@

}
*/
, _setValues: function(values){

@@ -101,3 +120,8 @@ Object.keys(values).forEach(function(property){

, reload: function(callback){
, isSaved: function() {
this.isFromDatabase() && !this._hasChanges;
}
, reload: function(callback, connection){
var query = {

@@ -117,6 +141,6 @@ select : ['*']

this._orm.getDatabase().query(query, function(err, data){
(connection || this._orm.getDatabase()).query(query, function(err, data){
if (err) callback(err);
else {
if (!data.length) callback(new Error('Failed to load data from database, record doesn\'t exist anymore!'));
if (!data.length) callback(new Error('Failed to load data from database, record doesn\'t exist!'));
else {

@@ -146,27 +170,110 @@ this._setValues(data[0]);

, save: function(){
, delete: function(callback) {
var callback = arg(arguments, 'function', function(){})
, noReload = arg(arguments, 'boolean', false);
, connection = arg(arguments, 'object');
// transactio management
this.getOrm().transaction().getDatabase().getConnection(function(err, connection){
if (err) callback(err);
if (connection) {
this._delete(connection, callback);
}
else {
this.getOrm().transaction().getDatabase().getConnection(function(err, connection){
if (err) callback(err);
else {
this._delete(connection, function(err){
if (err) {
connection.rollback(function(transactionErr){
if (transactionErr) callback(transactionErr);
else callback(err);
}.bind(this));
}
else {
connection.commit(function(err){
if (err) callback(err);
else {
this._fromDb = false;
callback(null, this);
}
}.bind(this));
}
}.bind(this));
}
}.bind(this));
}
return this;
}
, _delete: function(connection, callback) {
var i = 0
, query;
query = {
from : this._defintion.getTableName()
, database : this._defintion.getDatabaseName()
, filter : {}
, limit : 1
};
// insert or update?
if (this._fromDb){
this._defintion.primaryKeys.forEach(function(key){
query.filter[key] = this[key];
i++;
}.bind(this));
if (i === 0) {
log.dir(query);
throw new Error('Failed to create proper delete query, no filter was created (see query definition above)');
}
else {
this._save(connection, noReload, function(err){
if (err) {
connection.rollback(function(transactionErr){
if (transactionErr) callback(transactionErr);
else callback(err);
}.bind(this));
}
else {
connection.commit(function(err){
if (err) callback(err);
else callback(null, this);
}.bind(this));
}
}.bind(this));
connection.query('delete', query, callback);
}
}.bind(this));
}
else callback();
}
, save: function() {
var callback = arg(arguments, 'function', function(){})
, connection = arg(arguments, 'object')
, noReload = arg(arguments, 'boolean', false);
// transactio management
if (connection) {
this._save(connection, noReload, callback);
}
else {
this.getOrm().transaction().getDatabase().getConnection(function(err, connection){
if (err) callback(err);
else {
this._save(connection, noReload, function(err){
if (err) {
connection.rollback(function(transactionErr){
if (transactionErr) callback(transactionErr);
else callback(err);
}.bind(this));
}
else {
connection.commit(function(err){
if (err) callback(err);
else callback(null, this);
}.bind(this));
}
}.bind(this));
}
}.bind(this));
}
return this;

@@ -187,3 +294,3 @@ }

/*this._saveChildren(connection, noReload, function(err){
this._saveChildren(connection, noReload, function(err){
if (err) callback(err);

@@ -193,3 +300,3 @@ else {

}
}.bind(this));*/
}.bind(this));

@@ -205,3 +312,3 @@

this._orm.getDatabase().query('update', query, callback);
connection.query('update', query, callback);
}

@@ -211,3 +318,3 @@ else callback();

else {
this._orm.getDatabase().query('insert', query, function(err, result){ log(err);
connection.query('insert', query, function(err, result){
if (err) callback(err);

@@ -219,6 +326,6 @@ else {

this[this._defintion.primaryKeys[0]] = result.id;
if (!noReload) this.reload(callback);
if (!noReload) this.reload(callback, connection);
else callback(null, this);
}
else throw new Error('Cannot laod record with more than one primarykey!');
else throw new Error('Cannot load record with more than one primarykey!');
}

@@ -234,27 +341,32 @@ else throw new Error('not implemented!');

, _saveChildren: function(connection, noReload, callback) {
async.each(this._changedValues, function(key, next){
var value;
if (this._references[key]) {
// reference changed
value = this._references[key];
// save references
async.each(this._changedReferences, function(key, next){
var value = this._references[key]
, column = this._columns[key].column;;
if (value.isQuery) {
value.limit(1);
value.find(function(err, records){
if (err) next(err);
//else value[this._referencedColumns[]] = records.length ? records.first() : null;
}.bind(this));
// a query was set as reference
if (value.isQuery) {
value.limit(1);
value.findOne(function(err, model) {
if (err) next(err);
else if (model) {
this[column.name] = null;
next();
}
else {
this[column.name] = model[column.referencedColumn];
next();
}
}.bind(this));
}
else {
if (value.isSaved) {
this[column.name] = value[column.referencedColumn];
next();
}
else value._save(connection, noReload, next);
}
else if (this._mappings[key]) {
// mapping changed
this._mappings[key]._save(connection, noReload, next);
}
else if (this._belongsTo[key]) {
// belongsto checnged
this._belongsTo[key]._save(connection, noReload, next);
}
else next();
}.bind(this), function(err, results){

@@ -261,0 +373,0 @@ if (err) callback(results.filter(function(err){return err instanceof Error;})[0]);

@@ -48,3 +48,3 @@ !function(){

, definition : _options.definition
, isFromDB : options._isFromDB
, isFromDB : options && options._isFromDB
, relatingSets : relatingSets

@@ -95,2 +95,4 @@ , getOrm : _options.getOrm

Constructor.getDefinition = this.getDefinition.bind(this);
return Constructor;

@@ -100,3 +102,7 @@ }

, getDefinition: function() {
return this._definition;
}
, setMappingAccessorName: function(mappingName, name) {

@@ -127,2 +133,6 @@ if (!this.Model[name]) {

this._mappings[mappingName].on('change', function(){
this._setChanged();
}.bind(this));
return this._mappings[mappingName];

@@ -154,4 +164,12 @@ }

// make sure the instantiated model can get the correct orm instance (support for transactions)
CustomModel.getOrm = options.getOrm;
properties._columns = {
value: {}
};
// build model

@@ -177,2 +195,7 @@ Object.keys(this._definition.columns).forEach(function(columnName){

properties._columns.value[mappingName] = {
type : 'mapping'
, column : column
};
properties[mapping.name] = this._createMappingGetter(mappingName, {

@@ -199,2 +222,7 @@ orm: options.orm

properties._columns.value[relationName] = {
type : 'belongsTo'
, column : column
};
properties[belongs.name] = {

@@ -212,2 +240,6 @@ enumerable: true

});
this._belongsTo[relationName].on('chnage', function(){
this._setChanged();
}.bind(this));
}

@@ -229,6 +261,6 @@

/*properties._columns.value[referenceName] = {
properties._columns.value[referenceName] = {
type : 'reference'
, column : column
};*/
};

@@ -239,5 +271,8 @@ referenceDefinition.get = function(){

referenceDefinition.set = function(Model){
if (this._references[referenceName] !== Model) this._changedReferences.push(referenceName);
this._references[referenceName] = Model;
referenceDefinition.set = function(newValue){
if (this._references[referenceName] !== newValue) {
this._changedReferences.push(referenceName);
this._references[referenceName] = newValue;
this._setChanged();
}
};

@@ -248,6 +283,6 @@ }

definition.enumerable = true;
/*properties._columns.value[columnName] = {
properties._columns.value[columnName] = {
type : 'scalar'
, column : column
};*/
};
}

@@ -261,4 +296,7 @@

definition.set = function(value){
if (this._values[columnName] !== value) this._changedValues.push(columnName);
this._values[columnName] = value;
if (this._values[columnName] !== value) {
this._changedValues.push(columnName);
this._values[columnName] = value;
this._setChanged();
}
};

@@ -281,2 +319,6 @@ }.bind(this));

});
this._mappings[mappingName].on('chnage', function(){
this._setChanged();
}.bind(this));
}

@@ -303,2 +345,6 @@

});
this._belongsTo[belongingName].on('chnage', function(){
this._setChanged();
}.bind(this));
}

@@ -324,4 +370,7 @@

if (referenceMap[referenceName]) {
if (!existing && this._references[referenceName] !== newReferenceModel) this._changedReferences.push(referenceName);
this._references[referenceName] = newReferenceModel;
if (!existing && this._references[referenceName] !== newReferenceModel) {
this._changedReferences.push(referenceName);
this._references[referenceName] = newReferenceModel;
this._setChanged();
}
}

@@ -328,0 +377,0 @@ else throw new Error('Reference on «'+referenceName+'» on entity «'+options.definition.name+'» doesn\'t exist!');

@@ -10,3 +10,3 @@ !function(){

, async = require('ee-async')
, DBCluster = require('ee-db-cluster'); //require('../../ee-db-cluster');
, DBCluster = require('ee-db-cluster'); //*/require('../../ee-db-cluster');

@@ -13,0 +13,0 @@

@@ -427,3 +427,21 @@ !function(){

, findOne: function(callback) {
new QueryCompiler({
orm : this._orm
, getOrm : this._getOrm
, resource : this._rootResource
}).findOne(callback);
}
, delete: function(callback) {
new QueryCompiler({
orm : this._orm
, getOrm : this._getOrm
, resource : this._rootResource
}).delete(callback);
}
, _setProperty: function(name, value) {

@@ -430,0 +448,0 @@ Object.defineProperty(this, name, {value:value});

@@ -21,12 +21,23 @@ !function(){

, find: function(callback){
, findOne: function(callback) {
this._resource.query.limit = 1;
this.find(function(err, results) {
if (err) callback(err);
else if (results && results.length) callback(null, results.first());
else callback();
}.bind(this));
}
, find: function(callback) {
var resource = this._resource;
// prepare child queries
this._prepareChildResources(resource);
//log(resource.query);
// execut ebase query
this._executeQuery(resource.query, function(err, rows){
this._executeQuery('query', resource.query, function(err, rows){
if (err) callback(err);

@@ -57,2 +68,10 @@ else {

, delete: function(callback) {
this._executeQuery('delete', this._resource.query, callback);
}
, _executeSubqueries: function(rootResource, queries, callback) {

@@ -67,3 +86,3 @@

this._executeQuery(resource.query, function(err, rows){
this._executeQuery('query', resource.query, function(err, rows){
if (err) next(err);

@@ -197,11 +216,11 @@ else {

, _executeQuery: function(query, callback){
, _executeQuery: function(mode, query, callback){
if (this.getOrm().isTransaction) {
this.getOrm().getDatabase().getConnection(function(err, connection){
if (err) callback(err);
else connection.query(query, callback);
else connection.query(mode, query, callback);
}.bind(this));
}
else {
this.getOrm().getDatabase().query(query, callback);
this.getOrm().getDatabase().query(mode, query, callback);
}

@@ -208,0 +227,0 @@ }

@@ -12,3 +12,2 @@ !function(){

//log.warn('new relatingset', options.definition.name);

@@ -37,4 +36,100 @@ return Object.create(Array.prototype, {

// all records that were added to the colelction
, _addedRecords: {
value: []
}
// all records that were removed from the collection
, _removedRecords: {
value: []
}
// busy flag
, _isBusy: {
value: false
}
// collect errors when selecting records to add to this set
, _errors: {
value: []
}
// save changes on the
, save: {value: function() {
// TODO: get transaction, store stuff uing _save
}}
// save changes on the
, _save: {value: function(connection, noReload, callback) {
// wait until the relatingset is idle, then store all records and create relations
if (this._errors.length) {
callback(this.errors[0]);
}
else {
if (this._isBusy) {
this.once('idle', function(){
this._executeSave(connection, noReload, callback);
}.bind(this));
}
else {
this._executeSave(connection, noReload, callback);
}
}
}}
, _executeSave: {value: function(connection, noReload, callback) {
async.wait(function(done){
// delete all deleted relating records
async.each(this._removedRecords, function(model, next){
this._deleteRelationRecords(connection, model, next);
}.bind(this), done);
}.bind(this), function(done){
// create new relating records
async.each(this._removedRecords, function(model, next){
if (model.isSaved()) next(null, model);
else model.save(next);
}.bind(this), function(model, next){
this._createRelationRecords(connection, model, next);
}.bind(this), done);
}.bind(this), function(err){
}.bind(this));
}}
// reload all records
, reload: {value: function(callback) {
// get all records from the database. discards all data that was
// modified but not stored before
this._errors = [];
}}
// marks the relatingset as idle (not executing select queries)
, _idle: {value: function(err) {
if (err) this._errors.push(err);
this._isBusy = false;
this.emit('idle');
}}
// the the relatinset as busy (currently fetching records to add to this relation)
, _busy: {value: function() {
this._isBusy = true;
this.emit('busy');
}}
// push all records
, push: { value: function push (item, callback) {

@@ -45,21 +140,43 @@ callback = callback || function(){};

if (item.isQuery) {
this._busy();
// execute the query, add it to
item.find(function(err, records){
this._idle(err);
}
else {
// handle model (check if the item is a model)
if (item && type.boolean(item.isFromDatabase())){
if (item.isFromDatabase()){
// ready to make relation
this._createRelationRecords(item, callback);
}
if (err) callback(err);
else {
// store item first
item.save(function(err){
if (err) callback(err);
else this._createRelationRecords(item, callback);
records.forEach(function(model){
var err;
// TODO: proper typechek
if (this._isCorrectType(model)){
push.parent(model);
this._addedRecords.push(model);
}
else {
// empty array
records.splice(0, records.length);
err = new Error('Attempt to add models of type «'+model.getName()+'» to a relatingset of type «'+this._definition.name+'» via a query!');
if (callback) callback(err);
else this._errors.push(err);
}
}.bind(this));
}
}.bind(this));
}
else {
// TODO: proper typechek
if (this._isCorrectType(model)){
push.parent(model);
this._addedRecords.push(model);
callback();
}
else callback(new Error('Cannot add item to the relating set!'));
else {
err = new Error('Attempt to add models of type «'+model.getName()+'» to a relatingset of type «'+this._definition.name+'» via a query!');
if (callback) callback(err);
else this._errors.push(err);
}
}

@@ -70,2 +187,3 @@ }}

, addExisiting: { value: function(item){

@@ -76,22 +194,30 @@ proto.push.call(this, item);

, _isCorrectType: { value: function(model){
return mode.getEntityName() === this._definition.model.name;
}}
, _createRelationRecords: { value: function(item, callback){
, _createRelationRecords: { value: function(connection, model, callback) {
var values = {};
values[this._definition.via.fk] = this._relatesTo[this._column.name];
values[this._definition.via.otherFk] = item[this._definition.column.name];
values[this._definition.via.otherFk] = model[this._definition.column.name];
new this._orm[this._database][this._definition.via.model.name](values).save(function(err, relation){
if (err) callback(err);
else {
proto.push.call(this, item);
callback(null, item);
}
}.bind(this));
new this._orm[this._database][this._definition.via.model.name](values).save(connection, callback);
}}
, _deleteRelationRecords: {value: function(connection, model, callback) {
var values = {};
values[this._definition.via.fk] = this._relatesTo[this._column.name];
values[this._definition.via.otherFk] = model[this._definition.column.name];
this._orm[this._database][this._definition.via.model.name](values).delete(connection, callback);
}}
, pop: { value: function pop () {
pop.parent();

@@ -103,3 +229,2 @@ }}

, shift: { value: function shift () {
shift.parent();

@@ -110,7 +235,54 @@ }}

, unshift: { value: function unshift () {
unshift.parent();
}}
// inheriting from the array type, have to implement event by myself
, _events: {value: {}, writable: true}
// on
, on: {value: function(evt, listener){
if (!this._events[evt]) this._events[evt] = [];
this._events[evt].push({fn: listener});
}}
// once
, once: {value: function(evt, listener){
if (!this._events[evt]) this._events[evt] = [];
this._events[evt].push({fn: listener, once: true});
}}
// emit
, emit: {value: function(evt){
var rm = [];
if (this._events[evt]) {
this._events[evt].forEach(function(listener){
listener.fn.apply(null, Array.prototype.slice.call(arguments, 1));
if (listener.once) rm.push(listener);
});
rm.forEach(function(listener){
this.off(evt, listener);
});
}
}}
// off
, off: {value: function(evt, listener){
var index;
if (evt === undefined) this._events = {};
else if (evt) {
if (listener === undefined) delete this._events[evt];
else if(this._events[evt]) {
index = this._events[evt].indexOf(listener);
if (index >= 0) this._events[evt].splice(index, 1);
}
}
}}
});
};
}();
{
"name" : "ee-orm"
, "description" : "a simple yet powerful javascript orm for node.js"
, "version" : "0.1.2"
, "version" : "0.1.3"
, "homepage" : "https://github.com/eventEmitter/ee-orm"

@@ -20,17 +20,17 @@ , "author" : "Michael van der Weg <michael@eventemitter.com> (http://eventemitter.com/)"

, "dependencies": {
"ee-class" : "*"
, "ee-event-emitter" : "*"
, "ee-types" : "*"
, "ee-log" : "*"
, "ee-async" : "*"
, "ee-arguments" : "*"
, "clone" : "0.1.11"
, "pluralize" : "0.0.6"
, "merge" : "1.1.2"
, "ee-db-cluster" : "*"
"ee-class" : "0.3.x"
, "ee-event-emitter" : "0.1.x"
, "ee-types" : "0.1.x"
, "ee-log" : "0.2.x"
, "ee-async" : "0.1.x"
, "ee-arguments" : "0.1.x"
, "clone" : "0.1.x"
, "pluralize" : "0.0.x"
, "merge" : "1.1.x"
, "ee-db-cluster" : "0.1.x"
}
, "devDependencies": {
"mocha" : "*"
, "ee-travis" : "*"
, "ee-mysql-connection" : "*"
"mocha" : "1.18.x"
, "ee-travis" : "0.1.x"
, "ee-mysql-connection" : "0.1.x"
}

@@ -37,0 +37,0 @@ , "optionalDependencies": {}

# ee-orm
description
WIP

@@ -5,0 +5,0 @@ ## installation

@@ -38,6 +38,12 @@

});
*/
orm.eventbox.venue.setMappingAccessorName('venue_media', 'media');
orm.eventbox.venue.setReferenceAccessorName('id_media', 'logo');
//log(orm.eventbox.venue.getDefinition());
var counter = 0;

@@ -47,5 +53,30 @@

console.time("query")
/*
var query = orm.eventbox.event(['*'], {
startdate: ORM.gt(new Date())
}).limit(3).offset(0);
var transaction = orm//.transaction()
var venue = query.getVenues(['*']);
venue.getCity(['*'],{
municipality: 'Bern'
});
query.find(function(err, events){
log(err, events);
});
*/
var transaction = orm.transaction()
, query = transaction.eventbox.event(['*']).limit(10).offset(100);

@@ -74,3 +105,3 @@

//events.first().reload();
//log(events);
log(events);
/*events.forEach(function(event){

@@ -86,3 +117,3 @@ event.venues.forEach(function(venue){

exec();
//exec();

@@ -92,3 +123,3 @@

var e = new orm.eventbox.event({
title: 'test event'
});

@@ -99,3 +130,3 @@

e.save(function(err, evt){
log(err, evt);
});

@@ -108,3 +139,31 @@ }

var remove = function(){
orm.eventbox.event({
id: 243569
}).findOne(function(err, event){
if (err) log(err);
else if (!event) log('event not found');
else {
event.delete(function(err){
log(err);
});
}
});
}
//remove();
var removeDirect = function(){
orm.eventbox.event({
id: 243580
}).limit(1).delete(function(err){
log(err);
});
}
//removeDirect();
var update = function() {

@@ -111,0 +170,0 @@ orm.eventbox.event({id:634534}).update({

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