Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Change id generation per default to unique ids (BREAKS BACKWARDS COMP…

…ATIBILITY)

You can use the 'increment' idGenerator to keep the old incremental behaviour or provide your own function
  • Loading branch information...
commit e35b6de899de7a611195a69d4055fa72f368958d 1 parent b4d9035
@maritz authored
View
1  lib/nohm.js
@@ -146,6 +146,7 @@ Nohm.prototype.init = function (name, options) {
}
this.modelName = name;
+ this.idGenerator = options.idGenerator || 'default';
this.properties = {};
this.errors = {};
View
8 lib/retrieve.js
@@ -21,8 +21,7 @@ exports.exists = function (id, callback) {
*/
exports.load = function (id, callback) {
var self = this;
- id = parseInt(id, 10);
- if (isNaN(id) || id < 1) {
+ if (!id) {
Nohm.logError('Trying to pass load() a wrong kind of id. Needs to be a number over 0. (still trying to load it though)');
}
this.getClient().hgetall(this.getHashKey(id), function (err, values) {
@@ -69,10 +68,7 @@ exports.find = function find(searches, callback) {
if (Array.isArray(values) && values.length > 0) {
values = values.forEach(function (val, i) {
if (val) {
- var id = parseInt(val.toString(), 10);
- if (id > 0) {
- found.push(id);
- }
+ found.push(val);
}
});
}
View
63 lib/store.js
@@ -58,6 +58,46 @@ exports.save = function save(callback) {
}
};
+var idGenerators = {
+ 'default': function (cb) {
+ function rnd() {
+ return Math.floor(Math.random() * 1e9).toString(36);
+ }
+ cb((+ new Date).toString(36) + rnd() + rnd());
+ },
+ 'increment': function (cb) {
+ this.getClient().incr(Nohm.prefix.ids + this.modelName, function (err, newId) {
+ if (!err) {
+ cb(newId);
+ } else {
+ console.log('Nohm: Creating a new id by incrementing resulted in a client error: ' + util.inspect(err));
+ if (typeof cb === 'function') {
+ cb.call(self, err);
+ } else {
+ throw err;
+ }
+ }
+ });
+ }
+};
+
+var __generate_id = function (cb) {
+ var newId = 0;
+ var generator = this.idGenerator;
+ if (typeof(generator) === 'function') {
+ generator.call(this, function (id) {
+ if (!id) {
+ Nohm.logError('A custom id generator for model '+this.modelName+' failed to provide an id.');
+ }
+ cb(id);
+ });
+ } else {
+ if (! idGenerators.hasOwnProperty(generator)) {
+ generator = 'default';
+ }
+ idGenerators[generator].call(this, cb);
+ }
+}
/**
* Creates a new empty (!) dataset in the database and calls __update to populate it.
@@ -65,24 +105,15 @@ exports.save = function save(callback) {
*/
var __create = function __create(callback) {
var self = this;
- this.getClient().incr(Nohm.prefix.ids + this.modelName, function (err, newId) {
- if (!err) {
- self.getClient().sadd(Nohm.prefix.idsets + self.modelName, newId, function (err) {
+ __generate_id.call(this, function (newId) {
+ self.getClient().sadd(Nohm.prefix.idsets + self.modelName, newId, function (err) {
+ if (err) { Nohm.logError(err); }
+ self.__setUniqueIds(newId, function (err) {
if (err) { Nohm.logError(err); }
- self.__setUniqueIds(newId, function (err) {
- if (err) { Nohm.logError(err); }
- self.id = newId;
- __update.call(self, true, callback);
- });
+ self.id = newId;
+ __update.call(self, true, callback);
});
- } else {
- console.log('Nohm: Creating an object resulted in a client error: ' + util.inspect(err));
- if (typeof callback === 'function') {
- callback.call(self, err);
- } else {
- throw err;
- }
- }
+ });
});
};
View
25 test/featureTests.js
@@ -69,7 +69,8 @@ var redis = args.redis,
else
return this._super_prop.apply(this, arguments, 0);
}
- }
+ },
+ idGenerator: 'increment'
});
exports.redisClean = function (t) {
@@ -674,4 +675,24 @@ exports.defaultAsFunction = function (t) {
t.ok(test1.p('time') < test2.p('time'), 'time of test2 is not lower than test1');
t.done();
}, 10);
-};
+};
+
+exports.defaultIdGeneration = function (t) {t.expect(3);
+ t.expect(2);
+
+ var TestMockup = nohm.model('TestMockup', {
+ properties: {
+ name: {
+ type: 'string',
+ defaultValue: 'defaultIdGeneration'
+ }
+ }
+ });
+ var test1 = new TestMockup();
+ test1.save(function (err) {
+ t.ok(!err, 'There was an error while saving.');
+ t.same(typeof(test1.id), 'string', 'The generated id was not a string');
+ console.dir(test1.id);
+ t.done();
+ });
+}
View
48 test/findTests.js
@@ -38,7 +38,8 @@ var UserFindMockup = nohm.model('UserFindMockup', {
type: 'bool',
defaultValue: false
}
- }
+ },
+ idGenerator: 'increment'
});
var RoleFindMockup = nohm.model('RoleFindMockup', {
@@ -47,7 +48,8 @@ var RoleFindMockup = nohm.model('RoleFindMockup', {
type: 'string',
value: 'user'
}
- }
+ },
+ idGenerator: 'increment'
});
var errLogger = function (err) {
@@ -535,7 +537,8 @@ exports.shortForms = function (t) {
'notEmpty'
]
}
- }
+ },
+ idGenerator: 'increment'
});
shortFormMockup.save(function (err) {
@@ -565,4 +568,43 @@ exports.shortForms = function (t) {
});
});
});
+}
+
+exports.uuidLoadFind = function (t) {
+ t.expect(6);
+ var uuidMockup = nohm.model('uuidMockup', {
+ properties: {
+ name: {
+ type: 'string',
+ defaultValue: 'testName',
+ index: true,
+ validations: [
+ 'notEmpty'
+ ]
+ }
+ }
+ });
+
+ var test = new uuidMockup();
+ test.p('name', 'uuid');
+
+ var test2 = new uuidMockup();
+ test2.p('name', 'uuid2');
+
+ test.save(function (err) {
+ t.ok(test.id.length > 0, 'There was no proper id generated');
+ test2.save(function (err) {
+ t.ok(test.id !== test2.id, 'The uuids were the same.... ');
+ var loader = new uuidMockup();
+ loader.load(test.id, function (err, props) {
+ t.ok(!err, 'There was an error while loading');
+ t.same(props.name, test.p('name'), 'The loaded properties were not correct.');
+ uuidMockup.find({name: test.p('name')}, function (err, ids) {
+ t.ok(!err, 'There was an error while finding.');
+ t.same([test.id], ids, 'Did not find the correct ids');
+ t.done();
+ });
+ });
+ });
+ });
}
View
9 test/relationTests.js
@@ -11,7 +11,8 @@ var util = require('util'),
'notEmpty'
]
}
- }
+ },
+ idGenerator: 'increment'
}),
CommentLinkMockup = nohm.model('CommentLinkMockup', {
properties: {
@@ -22,7 +23,8 @@ var util = require('util'),
'notEmpty'
]
}
- }
+ },
+ idGenerator: 'increment'
}),
RoleLinkMockup = nohm.model('RoleLinkMockup', {
properties: {
@@ -30,7 +32,8 @@ var util = require('util'),
type: 'string',
defaultValue: 'user'
}
- }
+ },
+ idGenerator: 'increment'
});
exports.instances = function (t) {
Please sign in to comment.
Something went wrong with that request. Please try again.