Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
### v2.2.0
- Singleton/cache disabled by default (#672)

### v2.1.27
- Fix noisy mysql debug output (#642)

Expand Down
16 changes: 8 additions & 8 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ orm.connect("mysql://username:password@host/database", function (err, db) {
});

// add the table to the database
db.sync(function(err) {
db.sync(function(err) {
if (err) throw err;

// add a row to the person table
Expand All @@ -90,7 +90,7 @@ orm.connect("mysql://username:password@host/database", function (err, db) {
// err.msg = "under-age";
});
});

});
});
});
Expand Down Expand Up @@ -278,7 +278,7 @@ var Person = db.define("person", {

Other options:

- `cache` : (default: `true`) Set it to `false` to disable Instance cache ([Singletons](#singleton)) or set a timeout value (in seconds);
- `cache` : (default: `false`) Set it to `true` to enable Instance cache ([Singletons](#singleton)) 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)
Expand Down Expand Up @@ -516,24 +516,24 @@ db.driver.execQuery(

### Caching & Integrity

Model instances are cached. If multiple different queries will result in the same result, you will
Model instances can be cached (turned off by default). If enabled, multiple different queries will result in the same result - you will
get the same object. If you have other systems that can change your database (or you're developing and need
to make some manual changes) you should remove this feature by disabling cache. This can be done when you're
defining the Model.
to make some manual changes) you shouldn't use this feature.
It can be enabled/disabled per model:

```js
var Person = db.define('person', {
name : String
}, {
cache : false
cache : true
});
```

and also globally:

```js
orm.connect('...', function(err, db) {
db.settings.set('instance.cache', false);
db.settings.set('instance.cache', true);
});
```

Expand Down
23 changes: 8 additions & 15 deletions lib/Associations/Extend.js
Original file line number Diff line number Diff line change
Expand Up @@ -143,24 +143,17 @@ function extendInstance(Model, Instance, Driver, association, opts) {
if (err) {
return cb(err);
}
if (!Extension.isInstance) {
Extension = new association.model(Extension);
}

Instance[association.delAccessor](function (err) {
if (err) {
return cb(err);
}

var fields = Object.keys(association.field);

if (!Extension.isInstance) {
Extension = new association.model(Extension);
}
var fields = Object.keys(association.field);

for (var i = 0; i < Model.id.length; i++) {
Extension[fields[i]] = Instance[Model.id[i]];
}
for (var i = 0; i < Model.id.length; i++) {
Extension[fields[i]] = Instance[Model.id[i]];
}

Extension.save(cb);
});
Extension.save(cb);
});
return this;
},
Expand Down
4 changes: 2 additions & 2 deletions lib/Drivers/DML/mysql.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ Driver.prototype.valueToProperty = function (value, property) {
default:
customType = this.customTypes[property.type];
if(customType && 'valueToProperty' in customType) {
value = customType.valueToProperty(value);
value = customType.valueToProperty(value,property);
}
}
return value;
Expand All @@ -283,7 +283,7 @@ Driver.prototype.propertyToValue = function (value, property) {
default:
customType = this.customTypes[property.type];
if(customType && 'propertyToValue' in customType) {
value = customType.propertyToValue(value);
value = customType.propertyToValue(value,property);
}
}
return value;
Expand Down
67 changes: 35 additions & 32 deletions lib/Instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ exports.Instance = Instance;
function Instance(Model, opts) {
opts = opts || {};
opts.data = opts.data || {};
opts.only = opts.only || [];
opts.extra = opts.extra || {};
opts.keys = opts.keys || "id";
opts.changes = (opts.is_new ? Object.keys(opts.data) : []);
Expand Down Expand Up @@ -47,7 +48,7 @@ function Instance(Model, opts) {
var pending = [], errors = [], required;

Hook.wait(instance, opts.hooks.beforeValidation, function (err) {
var k, i;
var k, i;
if (err) {
return saveError(cb, err);
}
Expand Down Expand Up @@ -302,7 +303,7 @@ function Instance(Model, opts) {
return;
}
if (!instance[assoc.name].isInstance) {
instance[assoc.name] = new assoc.model(instance[assoc.name]);
instance[assoc.name] = new assoc.model(instance[assoc.name]);
}

saveAssociation(assoc.setAccessor, instance[assoc.name]);
Expand Down Expand Up @@ -374,7 +375,7 @@ function Instance(Model, opts) {

var conditions = {};
for (var i = 0; i < opts.keys.length; i++) {
conditions[opts.keys[i]] = opts.data[opts.keys[i]];
conditions[opts.keys[i]] = opts.data[opts.keys[i]];
}

Hook.wait(instance, opts.hooks.beforeRemove, function (err) {
Expand Down Expand Up @@ -493,34 +494,36 @@ function Instance(Model, opts) {
} else if (prop && 'defaultValue' in prop) {
defaultValue = prop.defaultValue;
}
if(opts.only.length==0 || opts.only.indexOf(key) >= 0)
{
setInstanceProperty(key, defaultValue);

Object.defineProperty(instance, key, {
get: function () {
return opts.data[key];
},
set: function (val) {
if (prop.key === true) {
if (prop.type == 'serial' && opts.data[key] != null) {
return;
} else {
opts.originalKeyValues[prop.name] = opts.data[prop.name];
}
}

setInstanceProperty(key, defaultValue);

Object.defineProperty(instance, key, {
get: function () {
return opts.data[key];
},
set: function (val) {
if (prop.key === true) {
if (prop.type == 'serial' && opts.data[key] != null) {
if (!setInstanceProperty(key, val)) {
return;
} else {
opts.originalKeyValues[prop.name] = opts.data[prop.name];
}
}

if (!setInstanceProperty(key, val)) {
return;
}

if (opts.autoSave) {
saveInstanceProperty(key, val);
} else if (opts.changes.indexOf(key) === -1) {
opts.changes.push(key);
}
},
enumerable: true
});
if (opts.autoSave) {
saveInstanceProperty(key, val);
} else if (opts.changes.indexOf(key) === -1) {
opts.changes.push(key);
}
},
enumerable: true
});
}
};
var addInstanceExtraProperty = function (key) {
if (!instance.hasOwnProperty("extra")) {
Expand All @@ -534,8 +537,8 @@ function Instance(Model, opts) {
setInstanceProperty(key, val);

/*if (opts.autoSave) {
saveInstanceProperty(key, val);
}*/if (opts.extrachanges.indexOf(key) === -1) {
saveInstanceProperty(key, val);
}*/if (opts.extrachanges.indexOf(key) === -1) {
opts.extrachanges.push(key);
}
},
Expand Down Expand Up @@ -600,9 +603,9 @@ function Instance(Model, opts) {
cb = arg;
break;
default:
var err = new Error("Unknown parameter type '" + (typeof arg) + "' in Instance.save()");
err.model = Model.table;
throw err;
var err = new Error("Unknown parameter type '" + (typeof arg) + "' in Instance.save()");
err.model = Model.table;
throw err;
}
}

Expand Down
Loading