译者:飞龙

设置用于储存键值对。设置对象是orm(默认值)上的实例,之后会为每个db连接和每个定义过的Model建立快照。所以orm.settings上的更改只会作用于更改之后建立的连接,而db.settings会作用于更改之后定义的模型。

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
});

默认设置的结构是这样的:

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,
        pool            : false,
        debug           : false
    }
};

设置

描述

properties.primary_key

没有定义id属性的模型中,主键的名称

properties.association_key

关联键的属性名称(例如user_id

properties.required

属性是否拥有默认行为

instance.cache

实例是否应该被缓存 (并不是真的缓存,和单例模式相关)

instance.cacheSaveCheck

被缓存的对象是否应该从缓存中返回 (不要修改这个设置,除非你知道自己在做什么)

instance.autoSave

如果开启的话,修改实例的任何属性时会自动保存

instance.autoFetch

是否需要自动获取关联

instance.autoFetchLimit

如果开启了自动获取关联,这个设置是获取关联的深度

instance.cascadeRemove

删除实例时是否要删除关联

instance.returnAllErrors

如果开启,实例保存时会记录下所有的错误并以数组形式返回,而不是遇到第一个错误就中止并返回

connection.reconnect

连接失效时是否尝试重新连接

connection.pool

是否使用驱动带有的连接池(如果支持的话)

connection.debug

向控制台打印带颜色的查询信息