Skip to content
Arek W edited this page Aug 1, 2013 · 9 revisions

Settings are used to store key value pairs. A settings object is instanciated on orm (default values), then a snapshot is created to every db connection and then a snapshot to every defined Model. So changes to orm.settings will take effect only to connections made after the change, as well as changes to db.settings will only affect models defined after the change.

var orm = require("orm");

orm.settings.set("some.deep.value", 123);

orm.connect("....", function (err, db) {
    // db.settings is a snapshot of the settings at the moment
    // of orm.connect(). changes to it don't affect orm.settings

    console.log(db.settings.get("some.deep.value")); // 123
    console.log(db.settings.get("some.deep"));       // { value: 123 }

    db.settings.set("other.value", { some: "object" });

    console.log(db.settings.get("other.value"));     // { some: "object" }
    console.log(orm.settings.get("other.value"));    // undefined
});

Here's the strucutre of the default settings:

var Settings = {
	properties : {
		primary_key     : "id",
		association_key : "{name}_{field}",
		required        : false
	},
	instance   : {
		cache           : true,
		cacheSaveCheck  : true,
		autoSave        : false,
		autoFetch       : false,
		autoFetchLimit  : 1,
		cascadeRemove   : true,
		returnAllErrors : false
	},
	connection : {
		reconnect       : true
	}
};
Setting Description
properties.primary_key the property name for the primary key of models;
properties.association_key the property name of an association key (example: "user_id");
properties.required if the default behaviour of a property is to be required or not;
instance.cache if instances should be cached (not actually cache, this is about Singleton behaviour);
instance.cacheSaveCheck if instances should be returned from cache if the cached instance is saved or not (don't change this unless you know what you're doing);
instance.autoSave if activated, makes instances save instantly when any property is changed;
instance.autoFetch if associations should be fetched automatically or not;
instance.autoFetchLimit if autoFetch is activated, is the depth of associations it should fetch;
instance.cascadeRemove removes associations when removing instances;
instance.returnAllErrors if activated, instance saving will hold all errors and return them as an Array instead of returning on first error;
connection.reconnect try to reconnect when connection is lost.

Clone this wiki locally