Skip to content

Commit

Permalink
schema fix
Browse files Browse the repository at this point in the history
  • Loading branch information
openhoat committed Jul 29, 2016
1 parent fad9964 commit e5d4640
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 138 deletions.
41 changes: 18 additions & 23 deletions lib/entity.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ class Entity {
const type = clazz.type;
logger.enabledLevels.trace && log.trace('searching entity "%s" matching index "%s" with "%s"', type, name, value);
return clazz.findIdsByIndex(name, value)
.then(result => Promise.map(result, clazz.load.bind(clazz))
.then(result => _.sortBy(result, 'value.' + (sort || ohm.getSchemaId(type))))
);
.then(result => Promise.map(result, clazz.load.bind(clazz)))
.then(result => {
if (!sort) {
return result;
}
return _.sortBy(result, `value.${sort}`);
});
}

static findIdsByIndex(name, value) {
Expand Down Expand Up @@ -116,7 +120,12 @@ class Entity {
return clazz.load(id);
}));
})
.then(result => _.sortBy(result, 'value.' + (sort || ohm.getSchemaId(type))));
.then(result => {
if (!sort) {
return result;
}
return _.sortBy(result, `value.${sort}`);
});
}

static load(id) {
Expand Down Expand Up @@ -234,7 +243,7 @@ class Entity {
delete(multi) {
const entity = this;
let id, type;
const localMulti = ohm.createLocalMulti.apply(null, arguments);
const localMulti = ohm.createLocalMulti(multi);
return Promise.resolve()
.then(() => {
type = entity.type;
Expand Down Expand Up @@ -266,21 +275,7 @@ class Entity {
if (typeof entity[idName] !== 'undefined') {
return;
}
if (schema.idGenerator !== null) {
if (typeof schema.idGenerator === 'function') {
return schema.idGenerator()
.then(id => {
entity.value[idName] = id.toString();
});
}
if (schema.idGenerator === 'increment') {
return ohm.exec('incr', ohm.toHash(ohm.config.idsHashPrefix, type/*_.kebabCase(type)*/))
.then(id => {
entity.value[idName] = id.toString();
});
}
entity.value[idName] = ohm.generateId(schema.idGenerator);
}
entity.value[idName] = ohm.generateId(schema.idGenerator);
});
}

Expand Down Expand Up @@ -354,14 +349,14 @@ class Entity {
return;
}
return Promise.each(Array.isArray(value) ? value : [value], value => {
const k = util.format.call(null, link.key, value);
const reverseK = link.reverseKey && util.format.call(null, link.reverseKey, id);
const k = util.format(link.key, value);
const reverseK = link.reverseKey && util.format(link.reverseKey, id);
return iterator(link, name, k, reverseK, value);
});
}
return Promise.resolve()
.then(() => {
const k = util.format.call(null, link.reverseKey, id);
const k = util.format(link.reverseKey, id);
return iterator(link, name, k);
});
});
Expand Down
65 changes: 36 additions & 29 deletions lib/ohm.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ Promise.promisifyAll(redis.Multi.prototype);

const idPattern = '^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$';
const idSchema = {type: 'string', pattern: idPattern};
const idNullableSchema = {type: ['string', 'null'], pattern: idPattern};

let Entity;

Expand All @@ -37,6 +38,7 @@ const ohm = {
entityClasses: {},
idPattern,
idSchema,
idNullableSchema,
init: (opt = {}) => {
logger.enabledLevels.debug && log.debug('initializing redis ohm client');
_.extend(ohm.config, opt);
Expand All @@ -49,7 +51,6 @@ const ohm = {
ohm.entityClasses = {};
ohm.schemas = {};
_.forIn(ohm.config.schemas, (schemaSpec, schemaName) => {
let linkNames, key;
if (!schemaSpec[ohm.config.schemaMetaPrefix]) {
logger.enabledLevels.debug && log.debug('schema "%s" has no meta : ignore', schemaName);
return;
Expand All @@ -64,12 +65,16 @@ const ohm = {
});
_.forIn(meta.operations, (value, type) => {
_.forIn(value, (value, operation) => {
key = ['operations', type, operation, 'title'].join('.');
logger.enabledLevels.trace && log.trace('setting property "%s" to schema "%s"', key, schemaName);
_.set(meta, key, util.format(schemaSpec.title, type, operation));
key = ['operations', type, operation, 'type'].join('.');
logger.enabledLevels.trace && log.trace('setting property "%s" to schema "%s"', key, schemaName);
_.set(meta, key, schemaSpec.type);
{
const key = ['operations', type, operation, 'title'].join('.');
logger.enabledLevels.trace && log.trace('setting property "%s" to schema "%s"', key, schemaName);
_.set(meta, key, util.format(schemaSpec.title, type, operation));
}
{
const key = ['operations', type, operation, 'type'].join('.');
logger.enabledLevels.trace && log.trace('setting property "%s" to schema "%s"', key, schemaName);
_.set(meta, key, schemaSpec.type);
}
});
});
const schemaId = ohm.getSchemaId(schemaName);
Expand All @@ -83,7 +88,6 @@ const ohm = {
});
logger.enabledLevels.trace && log.trace('setting meta datas to schema "%s"', schemaName);
if (schemaSpec[ohm.config.schemaMetaPrefix].links) {
linkNames = schemaSpec[ohm.config.schemaMetaPrefix].links.map(link => link.as);
schemaSpec[ohm.config.schemaMetaPrefix].links.forEach(link => {
if (typeof schema.properties[link.as] === 'undefined') {
logger.enabledLevels.trace && log.trace('setting link property "%s" to schema "%s"', link.as, schemaName);
Expand All @@ -93,40 +97,43 @@ const ohm = {
items: idSchema
};
} else {
schema.properties[link.as] = idSchema;
schema.properties[link.as] = idNullableSchema;
}
}
});
}
key = 'operations.db.new.excludeProperties';
if (typeof _.get(meta, key) === 'undefined') {
logger.enabledLevels.trace && log.trace('setting meta "%s" to schema "%s"', key, schemaName);
_.set(meta, key, [schemaId]);
}
key = 'operations.db.save.required';
if (typeof _.get(meta, key) === 'undefined') {
logger.enabledLevels.trace && log.trace('setting meta "%s" to schema "%s"', key, schemaName);
_.set(meta, key, [schemaId]);
}
key = 'operations.db.save.excludeProperties';
if (typeof _.get(meta, key) === 'undefined' && linkNames && linkNames.length) {
logger.enabledLevels.trace && log.trace('setting meta "%s" to schema "%s"', key, schemaName);
_.set(meta, key, linkNames);
/*key = 'operations.db.new.excludeProperties';
if (typeof _.get(meta, key) === 'undefined') {
logger.enabledLevels.trace && log.trace('setting meta "%s" to schema "%s"', key, schemaName);
_.set(meta, key, [schemaId]);
}*/
{
const key = 'operations.db.save.required';
if (typeof _.get(meta, key) === 'undefined') {
logger.enabledLevels.trace && log.trace('setting meta "%s" to schema "%s"', key, schemaName);
_.set(meta, key, [schemaId]);
}
}
key = 'operations.db.save.minProperties';
if (typeof _.get(meta, key) === 'undefined') {
logger.enabledLevels.trace && log.trace('setting meta "%s" to schema "%s"', key, schemaName);
_.set(meta, key, 2);
/*key = 'operations.db.save.excludeProperties';
if (typeof _.get(meta, key) === 'undefined' && linkNames && linkNames.length) {
logger.enabledLevels.trace && log.trace('setting meta "%s" to schema "%s"', key, schemaName);
_.set(meta, key, linkNames);
}*/
{
const key = 'operations.db.save.minProperties';
if (typeof _.get(meta, key) === 'undefined') {
logger.enabledLevels.trace && log.trace('setting meta "%s" to schema "%s"', key, schemaName);
_.set(meta, key, 2);
}
}
_.forIn(meta.operations, (value, type) => {
_.forIn(value, (value, operation) => {
const key = ['operations', type, operation, 'properties'].join('.');
if (value.includeProperties) {
_.set(meta, key, _.pick(schema.properties, value.includeProperties));
delete value.includeProperties;
} else {
_.set(meta, key, _.cloneDeep(schema.properties));
}
_.set(meta, key, _.cloneDeep(schema.properties));
if (value.excludeProperties) {
_.set(meta, key, _.omit(_.get(meta, key), value.excludeProperties));
delete value.excludeProperties;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "hw-redis-ohm",
"version": "0.5.7",
"version": "0.5.8",
"description": "Redis Object Hash Mapping",
"main": "lib/ohm.js",
"scripts": {
Expand Down

0 comments on commit e5d4640

Please sign in to comment.