DomainDaoSupport constructor function.
var DomainDaoSupport = function() {
this.tableConfig = null;
this.sqlTemplate = null;
this.cacheTemplate = null;
this.domainMap = {};
}
Option name | Type | Description |
---|---|---|
domainConfig | Object | domain model mapping configuration |
domainConfig.func | Function | domain constructor function |
domainConfig.tableName | String | model mapping tableName |
domainConfig.primary | Array | primary fields array |
domainConfig.fields | Array | normal fields array |
DomainDaoSupport model mapping configuration.
init domainDaoSupport with domainConfig to set up model mapping
if domainConfig is string type, then do initModelConfig
用domainConfig来初始化domainDaoSupport, 设置model mapping
如果 domainConfig 不是对象,而是String类型,则initModelConfig
DomainDaoSupport.prototype.initConfig = function(domainConfig) {
if (Utils.checkString(domainConfig)) {
return this.initModelConfig(domainConfig);
}
this.doInitConfig(domainConfig);
}
Option name | Type | Description |
---|---|---|
modelId | String |
DomainDaoSupport model mapping configuration with modelId.
init domainDaoSupport with modelId to set up model mapping
用modelId指定的bearcat model来初始化domainDaoSupport, 设置 model mapping
如果 domainConfig 不是对象,而是String类型,则initModelConfig
DomainDaoSupport.prototype.initModelConfig = function(modelId) {
var bearcat = Utils.getBearcat();
var beanFactory = bearcat.getBeanFactory();
var modelDefinition = beanFactory.getModelDefinition(modelId);
var mid = modelDefinition.getMid();
var table = modelDefinition.getTable();
var fieldMap = modelDefinition.getFields();
var primary = [];
var fields = [];
for (var fieldKey in fieldMap) {
var fieldValue = fieldMap[fieldKey];
if (fieldValue.isPrimary()) {
var fieldType = fieldValue.getType();
primary.push({
name: fieldKey,
type: fieldType
})
} else {
fields.push(fieldKey);
}
}
var domainConfig = {
mid: mid,
tableName: table,
primary: primary,
fields: fields
}
this.doInitConfig(domainConfig);
}
Option name | Type | Description |
---|---|---|
domainConfig | Object | domain model mapping configuration |
domainConfig.func | Function | domain constructor function |
domainConfig.key | String | domain cache key |
domainConfig.primary | Array | primary fields array |
domainConfig.fields | Array | normal fields array |
DomainDaoSupport get domain model mapping configuration.
get domainConfig when doing multi table query
多表查询的时候获取domainConfig
DomainDaoSupport.prototype.getConfig = function(domainConfig) {
var func = domainConfig.func;
var key = domainConfig.key;
var primaryFields = domainConfig.primary || [];
var fields = domainConfig.fields || [];
if (!Utils.checkFunction(func)) {
logger.error('domain object have no OR mapping info ' + domainConfig);
return;
}
if (key) {
var domain = this.getDomainMap(key);
if (domain) {
return domain;
}
}
var tableConfig = new TableConfig();
tableConfig.setFunc(func);
tableConfig.setNormalFields(FieldUtil.buildFieldConfig(fields));
tableConfig.setPrimaryFields(FieldUtil.buildFieldConfig(primaryFields));
if (key) {
this.setDomainMap(key, tableConfig);
}
return tableConfig;
}
Option name | Type | Description |
---|---|---|
transactionStatus | Object |
DomainDaoSupport do transaction with transactionStatus.
transaction should be in transaction context to hold the same connection.
dao should implement this method to keep the transaction context.
事务需要在同一个connection中完成,这点由transactionStatus来保证
dao 需要实现 transaction 方法,来把transactionStatus表示的事务状态传给底层的sqlTemplate
SimpleDao.prototype.transaction = function(txStatus) {
this.domainDaoSupport.transaction(txStatus);
return this;
}
DomainDaoSupport.prototype.transaction = function(transactionStatus) {
return this.getSqlTemplate().transaction(transactionStatus);
}
Option name | Type | Description |
---|---|---|
id | Long | |
cb | Function | callback function |
DomainDaoSupport deleteById field id.
id must be the unique primary key
id 必须是唯一主键
DomainDaoSupport.prototype.deleteById = function(id, cb) {
var params = [id];
return this.deleteByPrimaryKey(params, cb);
}
Option name | Type | Description |
---|---|---|
params | Array | primay fields array |
cb | Function | callback function |
DomainDaoSupport deleteByPrimaryKey.
params fields must be corresponded to what your domain defines primary fields
params 参数的顺序必须和domain里面主键定义的顺序相对应
DomainDaoSupport.prototype.deleteByPrimaryKey = function(params, cb) {
var tableConfig = this.getTableConfig();
var primaryFields = tableConfig.getPrimaryFields();
var tableName = tableConfig.getTableName();
var sql = SqlBuilderUtil.buildDeleteSql(tableName, FieldUtil.fieldToNames(primaryFields));
return this.getSqlTemplate().updateRecord(sql, params, cb);
}
Option name | Type | Description |
---|---|---|
objects | Array | model mapping objects |
cb | Function | callback function |
DomainDaoSupport batchAdd model mapping objects.
objects are model mapping domain instances array
if primary field is single and its type is Long
you do not need set this field when batchAdd
bearcat-dao will generate this unique id for your objects
objects 是model mapping对应的domain对象实例数组
如果主键是唯一的类型为Long的,通常情况下这个是一个自增的字段
这时batchAdd的domain对象可以不设置该字段的值
bearcat-dao 会为这些domain对象生成唯一的id值
DomainDaoSupport.prototype.batchAdd = function(objects, cb) {
if (!objects || !objects.length) {
return cb();
}
var tableConfig = this.getTableConfig();
var primaryFields = tableConfig.getPrimaryFields();
var tableName = tableConfig.getTableName();
var fields = tableConfig.getFields(); // all fields, include primary fields
var type = "";
var name = "";
var self = this;
if (primaryFields.length === 1) {
var primaryField = primaryFields[0];
type = primaryField.getType();
name = primaryField.getName();
if (type === Constant.TYPE_LONG) {
var len = objects.length;
var latch = CountDownLatch.createCountDownLatch(len, function(err) {
if (err) {
return cb(err);
}
return self.doBatchAdd(objects, cb);
});
for (var i = 0; i < objects.length; i++) {
(function(object) {
var id = object[name];
if (!id) {
self.getSqlTemplate().allocateRecordId(tableName, function(err, id) {
if (err) {
return cb(err);
}
object[name] = id;
latch.done();
});
} else {
latch.done();
}
})(objects[i]);
}
}
} else {
return self.doBatchAdd(objects, cb);
}
}
Option name | Type | Description |
---|---|---|
objects | Array | model mapping objects |
cb | Function | callback function |
DomainDaoSupport batchUpdate model mapping objects.
domain objects fields must be fullfilled when batchUpdate
a good practise is update what you get
当batchUpdate的时候,domain对象的所有属性必须都有值
一个好的实践就是更新你所查询的domain对象
DomainDaoSupport.prototype.batchUpdate = function(objects, cb) {
if (!objects || !objects.length) {
return cb();
}
var tableConfig = this.getTableConfig();
var primaryFields = tableConfig.getPrimaryFields();
var tableName = tableConfig.getTableName();
var fields = tableConfig.getFields();
if (primaryFields.length) {
var parasList = [];
for (var i = 0; i < objects.length; i++) {
var paras = [];
for (var j = 0; j < fields.length; j++) {
var field = fields[j];
var name = field.getName();
paras.push(objects[i][name]);
}
parasList.push(paras);
}
var normalColumns = FieldUtil.fieldToNames(tableConfig.getNormalFields());
var conditionColumns = FieldUtil.fieldToNames(primaryFields);
var sql = SqlBuilderUtil.buildUpdateSql(tableConfig.getTableName(), normalColumns, conditionColumns);
return this.getSqlTemplate().batchUpdateRecords(sql, parasList, function(err, results) {
if (err) {
return cb(err);
}
if (results) {
cb(null, objects);
} else {
cb(null, null);
}
});
} else {
logger.error('can not batchUpdate object without primary key');
return cb();
}
}
Option name | Type | Description |
---|---|---|
objects | Array | model mapping objects |
cb | Function | callback function |
DomainDaoSupport batchDelete model mapping objects.
doamin objects primary fields must be fullfilled
domain 对象的主键字段对应的值必须要有
DomainDaoSupport.prototype.batchDelete = function(objects, cb) {
if (!objects || !objects.length) {
return cb();
}
var tableConfig = this.getTableConfig();
var primaryFields = tableConfig.getPrimaryFields();
var tableName = tableConfig.getTableName();
var fields = tableConfig.getFields();
if (primaryFields.length) {
return this.batchDeleteByPrimaryKey(objects, cb);
} else {
logger.error('can not batchDelete object without primary key');
return cb();
}
}
Option name | Type | Description |
---|---|---|
columnName | String | column name |
newValue | String | update value |
primarysValue | Array | primary fields values |
cb | Function | callback function |
DomainDaoSupport updateColumn with newValue select by primary fields.
以主键作为查询条件来更新某个字段的值
DomainDaoSupport.prototype.updateColumn = function(columnName, newValue, primarysValue, cb) {
if (!Array.isArray(primarysValue)) {
logger.error('primarysValue must be array');
return cb();
}
var tableConfig = this.getTableConfig();
var primaryFields = tableConfig.getPrimaryFields();
if (primaryFields.length) {
if (primarysValue.length !== primaryFields.length) {
logger.error("primarysValue's size must equal with the primarykey's size");
return cb();
}
var params = primarysValue;
var columns = FieldUtil.fieldToNames(primaryFields);
return this.updateColumnValue(columnName, newValue, columns, params, cb);
} else {
logger.error('can not update object without primary key');
return cb();
}
}
Option name | Type | Description |
---|---|---|
columnName | String | column name |
newValue | String | update value |
conditionColumns | Array | condition fields |
conditionValues | Array | condition fields values |
cb | Function | callback function |
DomainDaoSupport updateColumnValue with newValue select by condition fields.
以查询条件字段作为查询条件来更新某个字段的值
DomainDaoSupport.prototype.updateColumnValue = function(columnName, newValue, conditionColumns, conditionValues, cb) {
if (!Array.isArray(conditionColumns) || !Array.isArray(conditionValues)) {
logger.error('conditionColumns or conditionValues must be array');
return cb();
}
var tableConfig = this.getTableConfig();
var params = [];
params.push(newValue);
for (var i = 0; i < conditionValues.length; i++) {
params.push(conditionValues[i]);
}
var updateColumns = [];
updateColumns.push(columnName);
var sql = SqlBuilderUtil.buildUpdateSql(tableConfig.getTableName(), updateColumns, conditionColumns);
return this.getSqlTemplate().updateRecord(sql, params, cb);
}
Option name | Type | Description |
---|---|---|
sql | String | query sql |
params | Array | query values |
options | Object | |
options.orderColumn | String | order column name |
options.isAsc | Boolean | sort asc ? |
options.limit | Number | limit number |
options.offset | Number | offset number |
options.domain | Object | domain configuration when doing multi table query |
cb | Function | callback function |
DomainDaoSupport getList by sql, params.
select by sql, query params, and the result will be mapped to model mapping domain objects
in options you can pass to set order, limit, offset
sql can be sql template id to specific sql, prefixed with $, "$testResult" for example
if options is string type, it means the resultSet mapping modelId
根据sql,params来进行查询,查询结果会model mapping到domain对象
在options里,你可以传入order, limit, offset
sql 可以是 sql template id,以 $ 开头,比如 "$testResult"
DomainDaoSupport.prototype.getList = function(sql, params, options, cb) {
sql = SqlBuilderUtil.getSql(sql);
var tableConfig = this.getTableConfig();
var mid = tableConfig.getMid();
var fields = tableConfig.getFields();
var func = tableConfig.getFunc();
var self = this;
if (Utils.checkString(options)) {
mid = options || mid;
options = {};
}
if (Utils.checkFunction(options)) {
cb = options;
options = {};
}
options = options || {};
var orderColumn = options.orderColumn;
var isAsc = options.isAsc;
var limit = options.limit;
var offset = options.offset;
var domain = options.domain;
var mid = options.mid || mid;
if (orderColumn) {
sql = SqlBuilderUtil.appendOrder(sql, orderColumn, isAsc);
}
if (Utils.isNotNull(limit) && Utils.isNotNull(offset)) {
sql = SqlBuilderUtil.appendLimit(sql, limit, offset);
}
return this.getSqlTemplate().executeQuery(sql, params, function(err, results) {
if (err) {
return cb(err);
}
var beanObjects = null;
if (mid) {
beanObjects = BeanBuilderUtil.buildModels(mid, results);
return cb(null, beanObjects);
}
if (domain) {
var domainConfig = self.getConfig(domain);
beanObjects = BeanBuilderUtil.buildObjectList(results, domainConfig.getFunc(), domainConfig.getFields());
} else {
beanObjects = BeanBuilderUtil.buildObjectList(results, func, fields);
}
cb(null, beanObjects);
});
}
Option name | Type | Description |
---|---|---|
params | Array | query values |
cb | Function | callback function |
DomainDaoSupport getList by primary fields params.
params fields must be corresponded to what your domain defines primary fields
params 参数的顺序必须和domain里面主键定义的顺序相对应
根据主键来进行查询列表
DomainDaoSupport.prototype.getByPrimary = function(params, cb) {
var tableConfig = this.getTableConfig();
var mid = tableConfig.getMid();
var primaryFields = tableConfig.getPrimaryFields();
var func = tableConfig.getFunc();
if (params.length !== primaryFields.length) {
logger.error('parameter size must be equal with the primary fields size');
return cb();
}
var fields = FieldUtil.fieldToNames(tableConfig.getFields());
var primaryFields = FieldUtil.fieldToNames(tableConfig.getPrimaryFields());
var sql = SqlBuilderUtil.buildSelectSql(tableConfig.getTableName(), fields, primaryFields);
return this.getSqlTemplate().executeQuery(sql, params, function(err, results) {
if (err) {
return cb(err);
}
if (mid) {
beanObjects = BeanBuilderUtil.buildModels(mid, results);
return cb(null, beanObjects);
}
var beanObjects = BeanBuilderUtil.buildObjectList(results, func, tableConfig.getFields());
cb(null, beanObjects);
});
}
Option name | Type | Description |
---|---|---|
id | Long | |
cb | Function | callback function |
DomainDaoSupport getList by id.
id must be the unique primary key
id 必须是唯一主键
DomainDaoSupport.prototype.getById = function(id, cb) {
var params = [id];
return this.getByPrimary(params, cb);
}
Option name | Type | Description |
---|---|---|
sql | String | query sql |
params | Array | query values |
cb | Function | callback function |
DomainDaoSupport getList by sql, params.
select by sql, query params, and the result will be mapped to model mapping domain objects
根据sql,params来进行查询,查询结果会model mapping到domain对象
DomainDaoSupport.prototype.get = function(sql, params, cb) {
var tableConfig = this.getTableConfig();
var mid = tableConfig.getMid();
var func = tableConfig.getFunc();
return this.getSqlTemplate().executeQuery(sql, params, function(err, results) {
if (err) {
return cb(err);
}
if (mid) {
beanObjects = BeanBuilderUtil.buildModels(mid, results);
return cb(null, beanObjects);
}
var beanObjects = BeanBuilderUtil.buildObjectList(results, func, tableConfig.getFields());
cb(null, beanObjects);
});
}
Option name | Type | Description |
---|---|---|
sql | String | query sql |
params | Array | query values |
cb | Function | callback function |
DomainDaoSupport getCount by sql, params.
select count by sql, query params
根据sql,params来查询数量
DomainDaoSupport.prototype.getCount = function(sql, params, cb) {
return this.getSqlTemplate().queryCount(sql, params, cb);
}
Option name | Type | Description |
---|---|---|
sql | String | query sql |
params | Array | query values |
cb | Function | callback function |
objects | Object | model mapping object |
cb | Function | callback function |
DomainDaoSupport add by sql, params or by model mapping object.
通过sql,params来添加数据 或者 通过 model mapping 的 domain object 对象来添加数据
DomainDaoSupport.prototype.add = function(sql, params, cb) {
if (Utils.checkObject(sql) && Utils.checkFunction(params)) {
return this.batchAdd([sql], params);
}
return this.getSqlTemplate().addRecord(sql, params, cb);
}
Option name | Type | Description |
---|---|---|
sql | String | query sql |
params | Array | query values |
cb | Function | callback function |
objects | Object | model mapping object |
cb | Function | callback function |
DomainDaoSupport delete by sql, params or by model mapping object.
通过sql,params来删除数据 或者 通过 model mapping 的 domain object 对象来删除数据
DomainDaoSupport.prototype.delete = function(sql, params, cb) {
if (Utils.checkObject(sql) && Utils.checkFunction(params)) {
return this.batchDelete([sql], params);
}
return this.getSqlTemplate().updateRecord(sql, params, cb);
}
Option name | Type | Description |
---|---|---|
sql | String | query sql |
params | Array | query values |
cb | Function | callback function |
objects | Object | model mapping object |
cb | Function | callback function |
DomainDaoSupport update by sql, params or by model mapping object.
通过sql,params来更新数据 或者 通过 model mapping 的 domain object 对象来更新数据
DomainDaoSupport.prototype.update = function(sql, params, cb) {
if (Utils.checkObject(sql) && Utils.checkFunction(params)) {
return this.batchUpdate([sql], params);
}
return this.getSqlTemplate().updateRecord(sql, params, cb);
}
Option name | Type | Description |
---|---|---|
sql | String | query sql |
params | Array | query values |
cb | Function | callback function |
DomainDaoSupport exists by sql, params.
if exist, result will true, otherwise result will false
如果存在,cb结果是true,否则是false
DomainDaoSupport.prototype.exists = function(sql, params, cb) {
return this.getSqlTemplate().existRecord(sql, params, cb);
}
Option name | Type | Description |
---|---|---|
tableName | String | table name |
cb | Function | callback function |
DomainDaoSupport allocate recordId for table.
为表申请主键id的值
DomainDaoSupport.prototype.allocateRecordId = function(tableName, cb) {
return this.getSqlTemplate().allocateRecordId(tableName, cb);
}
Option name | Type | Description |
---|---|---|
where | String | where sql |
args | Array | query params |
cb | Function | callback function |
DomainDaoSupport getList by where.
select by where sql, query params, and the result will be mapped to model mapping domain objects
根据where查询sql,params来进行查询,查询结果会model mapping到domain对象
DomainDaoSupport.prototype.getByWhere = function(where, args, cb) {
var tableConfig = this.getTableConfig();
var sql = util.format(SQL_OBJECT_SELECT, tableConfig.getTableName(), where);
return this.get(sql, args, cb);
}
Option name | Type | Description |
---|---|---|
where | String | where sql |
args | Array | query params |
options | Object | |
options.orderColumn | String | order column name |
options.isAsc | Boolean | sort asc ? |
options.limit | Number | limit number |
options.offset | Number | offset number |
options.domain | Object | domain configuration when doing multi table query |
cb | Function | callback function |
DomainDaoSupport getList by where.
select by sql, query params, and the result will be mapped to model mapping domain objects
in options you can pass to set order, limit, offset
sql can be sql template id to specific sql, prefixed with $, "$testResult" for example
if options is string type, it means the resultSet mapping modelId
根据sql,params来进行查询,查询结果会model mapping到domain对象
在options里,你可以传入order, limit, offset
sql 可以是 sql template id,以 $ 开头,比如 "$testResult"
DomainDaoSupport.prototype.getListByWhere = function(where, args, options, cb) {
var tableConfig = this.getTableConfig();
var mid = tableConfig.getMid();
var sql = util.format(SQL_SELECT, tableConfig.getTableName(), where);
if (Utils.checkString(options)) {
mid = options || mid;
options = {};
}
if (Utils.checkFunction(options)) {
cb = options;
options = {};
}
options = options || {};
var orderColumn = options.orderColumn;
var isAsc = options.isAsc;
var limit = options.limit;
var offset = options.offset;
var domain = options.domain;
var mid = options.mid || mid;
var opt = {};
if (orderColumn) {
sql = SqlBuilderUtil.appendOrder(sql, orderColumn, isAsc);
}
if (Utils.isNotNull(limit) && Utils.isNotNull(offset)) {
sql = SqlBuilderUtil.appendLimit(sql, limit, offset);
}
if (domain) {
opt['domain'] = domain;
}
if (mid) {
opt['mid'] = mid;
}
return this.getList(sql, args, opt, cb);
}
Option name | Type | Description |
---|---|---|
where | String | where sql |
args | Array | query params |
cb | Function | callback function |
DomainDaoSupport getCount by where.
select count by where sql, query params
根据where查询sql,params来查询数量
DomainDaoSupport.prototype.getCountByWhere = function(where, args, cb) {
var tableConfig = this.getTableConfig();
var sql = util.format(SQL_COUNT_SELECT, tableConfig.getTableName(), where);
return this.getCount(sql, args, cb);
}
Option name | Type | Description |
---|---|---|
where | String | where sql |
args | Array | query params |
cb | Function | callback function |
DomainDaoSupport remove by where.
remove by where sql, query params
根据where查询sql,params来进行删除
DomainDaoSupport.prototype.removeByWhere = function(where, args, cb) {
var tableConfig = this.getTableConfig();
var sql = util.format(REMOVE_OBJECT_SELECT, tableConfig.getTableName(), where);
return this.delete(sql, args, cb);
}
Option name | Type | Description |
---|---|---|
key | String | cache key |
value | String | cache value |
expire | Number | expire time |
DomainDaoSupport add to cache.
add key:value cache, if pass expire, it will set expire time in seconds
添加key:value缓存,如果传了expire,则会设置expire的过期时间, expire单位为秒
DomainDaoSupport.prototype.addToCache = function(key, value, expire) {
return this.getCacheTemplate().addToCache(key, value, expire);
}
Option name | Type | Description |
---|---|---|
key | String | cache key |
cb | Function | callback function |
DomainDaoSupport get from cache.
通过key来查询string value
DomainDaoSupport.prototype.getStringFromCache = function(key, cb) {
return this.getCacheTemplate().getString(key, cb);
}
Option name | Type | Description |
---|---|---|
key | String | cache key |
initCount | Number | init count number |
expire | Number | expire time |
DomainDaoSupport set counter to cache.
set counter with initCount, if pass expire, it will set expire time in seconds
设置一个计数器,初始值为InitCount, 如果传了expire,则会设置expire的过期时间, expire单位为秒
DomainDaoSupport.prototype.setCounter = function(key, initCount, expire) {
return this.getCacheTemplate().setCounter(key, initCount, expire);
}
Option name | Type | Description |
---|---|---|
key | String | cache key |
cb | Function | callback function |
DomainDaoSupport get counter from cache.
查询key对应的计数器的值
DomainDaoSupport.prototype.getCounter = function(key, cb) {
return this.getCacheTemplate().getCounter(key, cb);
}
Option name | Type | Description |
---|---|---|
key | String | cache key |
DomainDaoSupport incr counter by 1.
计数器增加值1
DomainDaoSupport.prototype.incr = function(key) {
return this.getCacheTemplate().incr(key)
}
Option name | Type | Description |
---|---|---|
key | String | cache key |
increment | Number | increment number |
DomainDaoSupport incr counter by increment.
计数器增加值increment
DomainDaoSupport.prototype.incrBy = function(key, increment) {
return this.getCacheTemplate().incrBy(key, increment);
}
Option name | Type | Description |
---|---|---|
key | String | cache key |
DomainDaoSupport remove from cache.
从cache删除key对应的值
DomainDaoSupport.prototype.removeFromCache = function(key) {
return this.getCacheTemplate().delFromCache(key);
}
module.exports = DomainDaoSupport;