Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

[refactor] [major] Changed auto-ids to use uuids instead of letting e…

…ach engine assign it's own id. First pass at pre-pending resource types for keys on relational entities to ensure uniqueness across all keys. #83
  • Loading branch information...
commit ee37b6df997b820a68cb24f367fe546dfc5cbb03 1 parent 5e4de03
@Marak Marak authored
Showing with 114 additions and 41 deletions.
  1. +2 −0  lib/resourceful.js
  2. +109 −39 lib/resourceful/resource.js
  3. +3 −2 package.json
View
2  lib/resourceful.js
@@ -25,3 +25,5 @@ resourceful.pluralize = require('./resourceful/common').pluralize;
resourceful.render = require('./resourceful/common').render;
resourceful.resources.Resource = resourceful.define('resource');
+
+resourceful.uuid = require('node-uuid');
View
148 lib/resourceful/resource.js
@@ -180,9 +180,6 @@ Resource._request = function (/* method, [key, obj], callback */) {
if (e) { that.emit('error', e); }
else { that.emit(method,
method === 'destroy' ? obj : res || result); }
- if(typeof key !== 'undefined') {
- result._id = key;
- }
if (callback) {
callback(e || null, result);
}
@@ -199,7 +196,7 @@ Resource.get = function (id, callback) {
}
var newid, oldid;
- if (id._id) {
+ if (id && id._id) {
newid = this._resource.toLowerCase() + "/" + id._id;
oldid = id._id;
}
@@ -209,34 +206,48 @@ Resource.get = function (id, callback) {
}
newid = id;
} else if(id) {
- newid = this._resource.toLowerCase() + "/" + id;
+
+ newid = id;
oldid = id;
+
+ //
+ // If the id passed in is a not a path
+ //
+ if (id.indexOf('/') === -1) {
+ //
+ // If a string id has been passed in a single level of the tree,
+ // assume it's the resource root
+ //
+ newid = this._resource.toLowerCase() + "/" + id;
+ }
} else {
if(callback) {
return callback(new Error('key is undefined'));
}
return;
}
- this._request('get', newid, function(err, res){
- //
- // Remap back original ids
- //
- if(res && typeof res._id !== 'undefined') {
- res._id = oldid;
- }
- if(Array.isArray(res)) {
- for(var r in res) {
- if (res[r] && res[r]._id) {
- res[r]._id = res[r]._id.split('/');
- res[r]._id = res[r]._id.pop();
- }
+
+ this._request('get', newid, function(err, res){
+ //
+ // Remap back original ids
+ //
+ if(res && typeof res._id !== 'undefined') {
+ res._id = oldid;
+ }
+ if(Array.isArray(res)) {
+ for(var r in res) {
+ if (res[r] && res[r]._id) {
+ //res[r]._id = res[r]._id.slice(1).join('/')
+ res[r]._id = res[r]._id.split('/');
+ res[r]._id = res[r]._id.pop();
}
}
- if(res) {
- callback(err, res);
- } else {
- return callback(err, res)
- }
+ }
+ if(res) {
+ callback(err, res);
+ } else {
+ return callback(err, res)
+ }
})
};
@@ -313,10 +324,19 @@ Resource.save = function (obj, callback) {
var newid, oldid;
if (typeof obj._id !== 'undefined') {
- newid = this._resource.toLowerCase() + "/" + obj._id;
- oldid = obj._id;
+
+ if(obj._id.indexOf(this._resource.toLowerCase() + "/") === -1) {
+ newid = this._resource.toLowerCase() + "/" + obj._id;
+ } else {
+ newid = obj._id;
+ }
obj._id = newid;
+ oldid = obj._id.replace(this._resource.toLowerCase() + "/", '');
+ } else {
+ newid = this._resource.toLowerCase() + "/" + resourceful.uuid.v4();
+ oldid = newid;
}
+
return this._request("save", newid, obj, function(err, res){
if (res && res._id && typeof oldid !== 'undefined') {
res._id = oldid;
@@ -385,6 +405,13 @@ Resource.all = function (callback) {
};
Resource.view = function (path, params, callback) {
+ //
+ // TODO: Remove and fix this hack
+ //
+ var x = path.split('/')[1];
+ x = x.replace('by', '');
+ x = x.toLowerCase() + "/";
+ params.key = x + params.key;
return this._request("view", path, params, callback);
};
@@ -516,7 +543,7 @@ function relationship(factory, type, r, options) {
engine._children.call(this, factory, rstringp, rfactory);
} else {
factory[rstringp] = function (id, callback) {
- return rfactory['by' + factory.resource](id, callback);
+ return rfactory['by' + factory.resource](id, callback)
};
}
//
@@ -529,8 +556,9 @@ function relationship(factory, type, r, options) {
//
// Parent.createChild(id, child, callback)
//
- factory['create' + rstringc] = function (id, child, callback) {
- var key = factory.resource.toLowerCase() + '_id';
+ factory['create' + rstringc] = function (parent, child, callback) {
+ var key = factory.resource.toLowerCase() + '_id',
+ id = parent._id;
function notifyParent(c, callback) {
factory.get(id, function(err, p) {
@@ -538,36 +566,80 @@ function relationship(factory, type, r, options) {
if(callback) return callback(err);
}
+ //
+ // TODO: We shouldn't have to reduce ID's here, something is wrong
+ //
+ if(p[rstring + '_ids'].length > 0) {
+ p[rstring + '_ids'] = p[rstring + '_ids'].reduce(function(i){
+ if(typeof i === "string") {
+ var x = i.split('/');
+ x = x[0];
+ if(x === id) {
+ return i;
+ }
+ }
+ });
+ }
+
+ if (p[rstring + '_ids'] && !Array.isArray(p[rstring + '_ids'])) {
+ p[rstring + '_ids'] = [p[rstring + '_ids']];
+ } else {
+ p[rstring + '_ids'] = [];
+ }
+
+ //
+ // Don't double append resource names for self-referencing relationships
+ //
p[rstring + '_ids'].push(c._id || c.id);
- p.save(callback);
+
+ p.save(function(err, result){
+ callback(err, child);
+ });
});
}
+ if (child._id) {
+ //
+ // Remark: If the factory.resource is equal to rstring,
+ // it means that the resource's parent is itself
+ //
+ if(factory.resource.toLowerCase() === rstring) {
+ child = new factory(child);
+ } else {
+ //
+ // Else, the resource's parent is rstring
+ //
+ child._id = rstring + '/' + parent.resource.toLowerCase() + '/' + id + '/' + child._id;
+ }
+ } else {
+ child._id = rstring + '/' + parent._id + '/' + resourceful.uuid.v4();
+ }
+
if (child instanceof rfactory) {
- child[key] = id;
- child._id = id + '/' + child._id;
+ var x = parent._id;
+ child[key] = id.replace(rfactory + '/', '');
+ x = x.split('/');
+ x = x[x.length-1];
+ child._id = x + '/' + child._id;
child.save(function(err) {
if(err) {
if(callback) return callback(err);
}
-
notifyParent(child, callback);
});
} else {
var inheritance = {};
inheritance[key] = id;
child = resourceful.mixin({}, child, inheritance);
- child._id = id + '/' + child._id;
rfactory.create(child, function(err, c) {
if(err) {
if(callback) return callback(err);
}
-
- notifyParent(c, function(e) {
+ notifyParent(c, function(e, r) {
if(e) {
if(callback) return callback(e);
} else {
- if(callback) callback(err, c);
+ if(callback) callback(err, r);
}
});
});
@@ -578,7 +650,7 @@ function relationship(factory, type, r, options) {
// parent.createChild(child, callback)
//
factory.prototype['create' + rstringc] = function (child, callback) {
- factory['create' + rstringc](this._id, child, callback);
+ factory['create' + rstringc](this, child, callback);
};
// Notify child about new parent
@@ -595,7 +667,6 @@ function relationship(factory, type, r, options) {
factory['by' + rstringc] = engine._byParent || function (id, callback) {
var filter = {};
filter[rstring + '_id'] = id;
-
factory.find(filter, callback);
};
}
@@ -617,7 +688,6 @@ function relationship(factory, type, r, options) {
rfactory.get(i[rstring + '_id'], function(err, p) {
if(err) { return next(err); }
-
var key = factory.resource.toLowerCase() + '_ids';
if(~p[key].indexOf(obj)) {
p[key].splice(p[key].indexOf(obj), 1);
View
5 package.json
@@ -22,7 +22,8 @@
"dependencies": {
"cradle": "0.6.x",
"i": "0.3.x",
- "revalidator": "0.1.x"
+ "revalidator": "0.1.x",
+ "node-uuid": "1.3.x"
},
"devDependencies": {
"vows": "~0.6.0"
@@ -33,7 +34,7 @@
},
"scripts": {
"browserify": "browserify lib/browser.js -o build/resourceful.js",
- "test": "vows --spec -i"
+ "test": "vows --spec --isolate"
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.