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

orm

Package Overview
Dependencies
Maintainers
1
Versions
103
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

orm - npm Package Compare versions

Comparing version 2.0.1 to 2.0.2

Changelog.md

2

examples/test-model-person.js

@@ -13,2 +13,4 @@ module.exports = function (db, cb) {

photo : { type: "binary", lazyload: true, lazyname: "avatar" }
}, {
id : "PESSOA"
});

@@ -15,0 +17,0 @@ db.models.person.hasMany("pets", db.models.pet, {

26

examples/test.js
var orm = require("../lib/ORM");
orm.connect("redshift://dresende@localhost:/orm_test", function (err, db) {
// orm.connect("mysql://root:tedua@localhost/orm?pool=true", function (err, db) {
// orm.connect("redshift://dresende@localhost:/orm_test", function (err, db) {
orm.connect("mysql://root:tedua@localhost/orm?pool=true", function (err, db) {
// orm.connect("mongodb:///test", function (err, db) {

@@ -22,12 +22,22 @@ // orm.connect("sqlite://", function (err, db) {

db.sync(function () {
var Deco = new Pet({
name : "Deco"
});
var John = new Person({
name : "Jane",
surname : "Dean",
name : "John",
surname : "Doe",
male : true
});
John.save(function (err) {
console.log("err", err);
John.setPets(Deco, function (err) {
console.log("error", err);
Person.find(function (err, people) {
console.log(people);
John.getPets(function (err, pets) {
console.log("pets", pets);
setTimeout(function () {
John.removePets(function (err) {
console.log("error", err);
process.exit(1);
});
}, 10000);
});

@@ -34,0 +44,0 @@ });

@@ -68,3 +68,3 @@ var InstanceConstructor = require("../Instance").Instance;

exports.extend = function (Instance, Driver, associations, opts, cb) {
exports.extend = function (Model, Instance, Driver, associations, opts, cb) {
if (associations.length === 0) {

@@ -84,7 +84,7 @@ return cb();

for (var i = 0; i < associations.length; i++) {
extendInstance(Instance, Driver, associations[i], opts, extendDone);
extendInstance(Model, Instance, Driver, associations[i], opts, extendDone);
}
};
function extendInstance(Instance, Driver, association, opts, cb) {
function extendInstance(Model, Instance, Driver, association, opts, cb) {
Object.defineProperty(Instance, association.hasAccessor, {

@@ -103,3 +103,3 @@ value: function () {

table: association.mergeTable,
id: Instance[association.model.id],
id: Instance[Model.id],
id_prop: association.mergeId,

@@ -109,7 +109,7 @@ assoc_prop: association.mergeAssocId

conditions[association.mergeTable + "." + association.mergeId] = Instance[association.model.id];
conditions[association.mergeTable + "." + association.mergeId] = Instance[Model.id];
conditions[association.mergeTable + "." + association.mergeAssocId] = [];
for (var i = 0; i < Instances.length; i++) {
conditions[association.mergeTable + "." + association.mergeAssocId].push(Instances[i].id);
conditions[association.mergeTable + "." + association.mergeAssocId].push(Instances[i][association.model.id]);
}

@@ -159,3 +159,3 @@

table: association.mergeTable,
id: Instance[association.model.id],
id: Instance[Model.id],
id_prop: association.mergeId,

@@ -168,3 +168,3 @@ assoc_prop: association.mergeAssocId

}
conditions[association.mergeTable + "." + association.mergeId] = Instance[association.model.id];
conditions[association.mergeTable + "." + association.mergeId] = Instance[Model.id];

@@ -174,2 +174,3 @@ if (cb === null) {

}
console.log(conditions);
association.model.find(conditions, limit, options, cb);

@@ -202,23 +203,34 @@ return this;

var associationIds = [];
conditions[association.mergeId] = Instance[association.model.id];
var run = function () {
if (Associations.length === 0) {
return Driver.remove(association.mergeTable, conditions, cb);
}
if (Associations.length === 0) {
Driver.remove(association.mergeTable, conditions, cb);
return this;
}
for (var i = 0; i < Associations.length; i++) {
if (Associations[i].id) {
associationIds.push(Associations[i].id);
}
}
for (var i = 0; i < Associations.length; i++) {
if (Associations[i].id) {
associationIds.push(Associations[i].id);
if (associationIds.length === 0) {
return cb(null);
}
}
if (associationIds.length === 0) {
return cb(null);
}
conditions[association.mergeAssocId] = associationIds;
conditions[association.mergeAssocId] = associationIds;
Driver.remove(association.mergeTable, conditions, cb);
};
conditions[association.mergeId] = Instance[Model.id];
Driver.remove(association.mergeTable, conditions, cb);
if (this.saved()) {
run();
} else {
this.save(function (err) {
if (err) {
return cb(err);
}
return run();
});
}
return this;

@@ -233,3 +245,36 @@ },

var cb;
var run = function () {
var saveNextAssociation = function () {
if (Associations.length === 0) {
return cb();
}
var Association = Associations.pop();
Association.save(function (err) {
if (err) {
return cb(err);
}
var data = {};
data[association.mergeId] = Instance[Model.id];
data[association.mergeAssocId] = Association.id;
for (var k in opts) {
data[k] = opts[k];
}
Driver.insert(association.mergeTable, data, function (err) {
if (err) {
return cb(err);
}
return saveNextAssociation();
});
});
};
return saveNextAssociation();
};
for (var i = 0; i < arguments.length; i++) {

@@ -250,10 +295,6 @@ switch (typeof arguments[i]) {

var saveNextAssociation = function () {
if (Associations.length === 0) {
return cb();
}
var Association = Associations.pop();
Association.save(function (err) {
if (this.saved()) {
run();
} else {
this.save(function (err) {
if (err) {

@@ -263,22 +304,6 @@ return cb(err);

var data = {};
data[association.mergeId] = Instance[association.model.id];
data[association.mergeAssocId] = Association.id;
for (var k in opts) {
data[k] = opts[k];
}
Driver.insert(association.mergeTable, data, function (err) {
if (err) {
return cb(err);
}
return saveNextAssociation();
});
return run();
});
};
}
saveNextAssociation();
return this;

@@ -285,0 +310,0 @@ },

@@ -39,3 +39,3 @@ var Settings = require("../Settings");

exports.extend = function (Instance, Driver, associations, opts, cb) {
exports.extend = function (Model, Instance, Driver, associations, opts, cb) {
if (associations.length === 0) {

@@ -55,7 +55,7 @@ return cb();

for (var i = 0; i < associations.length; i++) {
extendInstance(Instance, Driver, associations[i], opts, extendDone);
extendInstance(Model, Instance, Driver, associations[i], opts, extendDone);
}
};
function extendInstance(Instance, Driver, association, opts, cb) {
function extendInstance(Model, Instance, Driver, association, opts, cb) {
Object.defineProperty(Instance, association.hasAccessor, {

@@ -88,5 +88,5 @@ value: function (opts, cb) {

if (association.reversed) {
if (Instance[association.model.id]) {
if (Instance[Model.id]) {
var conditions = {};
conditions[association.field] = Instance[association.model.id];
conditions[association.field] = Instance[Model.id];
association.model.find(conditions, opts, cb);

@@ -93,0 +93,0 @@ } else {

@@ -41,6 +41,6 @@ var Instance = require("./Instance").Instance;

}
OneAssociation.extend(instance, opts.driver, one_associations, {
OneAssociation.extend(model, instance, opts.driver, one_associations, {
autoFetch : false
}, function () {
ManyAssociation.extend(instance, opts.driver, many_associations, {
ManyAssociation.extend(model, instance, opts.driver, many_associations, {
autoFetch : false

@@ -69,6 +69,6 @@ }, function () {

});
OneAssociation.extend(instance, opts.driver, one_associations, {
OneAssociation.extend(model, instance, opts.driver, one_associations, {
autoFetch : false
}, function () {
ManyAssociation.extend(instance, opts.driver, many_associations, {
ManyAssociation.extend(model, instance, opts.driver, many_associations, {
autoFetch : false

@@ -174,7 +174,7 @@ }, function () {

}
OneAssociation.extend(instance, opts.driver, one_associations, {
OneAssociation.extend(model, instance, opts.driver, one_associations, {
autoFetch : (options.autoFetchLimit === 0 ? false : opts.autoFetch),
autoFetchLimit : options.autoFetchLimit || opts.autoFetchLimit
}, function () {
ManyAssociation.extend(instance, opts.driver, many_associations, {
ManyAssociation.extend(model, instance, opts.driver, many_associations, {
autoFetch : (options.autoFetchLimit === 0 ? false : opts.autoFetch),

@@ -269,7 +269,7 @@ autoFetchLimit : options.autoFetchLimit || opts.autoFetchLimit

}
OneAssociation.extend(instance, opts.driver, one_associations, {
OneAssociation.extend(model, instance, opts.driver, one_associations, {
autoFetch : (options.autoFetchLimit === 0 ? false : opts.autoFetch),
autoFetchLimit : options.autoFetchLimit
}, function () {
ManyAssociation.extend(instance, opts.driver, many_associations, {
ManyAssociation.extend(model, instance, opts.driver, many_associations, {
autoFetch : (options.autoFetchLimit === 0 ? false : opts.autoFetch),

@@ -276,0 +276,0 @@ autoFetchLimit : options.autoFetchLimit

@@ -109,7 +109,7 @@ var util = require("util");

properties : properties,
cache : opts.cache,
cache : opts.hasOwnProperty("cache") ? opts.cache : this.settings.get("instance.cache"),
id : opts.id || this.settings.get("properties.primary_key"),
autoSave : opts.autoSave || false,
autoFetch : opts.autoFetch || false,
autoFetchLimit : opts.autoFetchLimit,
autoSave : opts.hasOwnProperty("autoSave") ? opts.autoSave : this.settings.get("instance.autoSave"),
autoFetch : opts.hasOwnProperty("autoFetch") ? opts.autoFetch : this.settings.get("instance.autoFetch"),
autoFetchLimit : opts.autoFetchLimit || this.settings.get("instance.autoFetchLimit"),
hooks : opts.hooks || {},

@@ -116,0 +116,0 @@ methods : opts.methods || {},

@@ -5,2 +5,8 @@ var default_settings = {

association_key : "{name}_id"
},
instance : {
cache : true,
autoSave : false,
autoFetch : false,
autoFetchLimit : 1
}

@@ -7,0 +13,0 @@ };

@@ -14,3 +14,3 @@ {

],
"version": "2.0.1",
"version": "2.0.2",
"license": "MIT",

@@ -17,0 +17,0 @@ "repository": {

@@ -11,3 +11,3 @@ ## Object Relational Mapping

Current stable version: **2.0.1**
Current stable version: **2.0.2**

@@ -174,2 +174,47 @@ ## DBMS Support

## Advanced Options
Using [Settings](#settings) or directly on Model definition you can tweak some options.
For example, each Model instance has a unique ID in the database. This table column is
by default "id" but you can change it.
```js
var Person = db.define("person", {
name : String
}, {
id : "person_id"
});
// or just do it globally..
db.settings.set("properties.primary_key", "UID");
// ..and then define your Models
var Pet = db.define("pet", {
name : String
});
```
**Pet** model will have 2 columns, an `UID` and a `name`.
Other options:
- `cache` : (default: `true`) Set it to `false` to disable Instance cache (Singletons) or set a timeout value (in seconds);
- `autoSave` : (default: `false`) Set it to `true` to save an Instance right after changing any property;
- `autoFetch` : (default: `false`) Set it to `true` to fetch associations when fetching an instance from the database;
- `autoFetchLimit` : (default: `1`) If `autoFetch` is enabled this defines how many hoops (associations of associations)
you want it to automatically fetch.
## Hooks
If you want to listen for a type of event than occurs in instances of a Model, you can attach a function that
will be called when that event happens. There are some events possible:
- `afterLoad` : (no parameters) Right after loading and preparing an instance to be used;
- `beforeSave` : (no parameters) Right before trying to save;
- `afterSave` : (bool success) Right after saving;
- `beforeCreate` : (no parameters) Right before trying to save a new instance;
- `beforeRemove` : (no parameters) Right before trying to remove an instance.
All hook function are called with `this` as the instance so you can access anything you want related to it.
## Finding Items

@@ -176,0 +221,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