diff --git a/Makefile b/Makefile index d22d381..b6e9a6e 100644 --- a/Makefile +++ b/Makefile @@ -10,7 +10,7 @@ components: clean: rm -fr build components template.js -test: +test: build @node test/server .PHONY: clean test diff --git a/Readme.md b/Readme.md index 9870252..baf1a08 100644 --- a/Readme.md +++ b/Readme.md @@ -42,6 +42,20 @@ var Post = model('Post') .attr('updated_at', { type: 'date' }) ``` +### .primary(name) + + Set the primary attribute key's `name`. + +```js +var model = require('model'); + +var Post = model('Post') + .attr('slug') + .attr('title') + .attr('body') + .primary('slug'); +``` + ### .validate(fn) TODO: validation callback docs diff --git a/lib/static.js b/lib/static.js index 8c8b736..10f1e5c 100644 --- a/lib/static.js +++ b/lib/static.js @@ -33,7 +33,7 @@ exports.url = function(path){ * * User.headers({ * 'X-CSRF-Token': 'some token', - * 'X-API-Token': 'api token + * 'X-API-Token': 'api token * }); * * @param {String|Object} header(s) @@ -88,9 +88,8 @@ exports.attr = function(name, options){ this.attrs[name] = options || {}; // implied pk - if ('_id' == name || 'id' == name) { - this.attrs[name].primaryKey = true; - this.primaryKey = name; + if (!this.primaryKey && ('_id' == name || 'id' == name)) { + this.primary(name); } // getter / setter method @@ -109,6 +108,19 @@ exports.attr = function(name, options){ return this; }; +/** + * Set the primary attribute `name` for the model. + * + * @param {String} name + * @return {Function} self + * @api public + */ + +exports.primary = function(name){ + this.primaryKey = name; + return this; +}; + /** * Remove all and invoke `fn(err)`. * diff --git a/test/statics.js b/test/statics.js index 0993ea0..7713ce3 100644 --- a/test/statics.js +++ b/test/statics.js @@ -20,6 +20,13 @@ describe('Model.url(string)', function(){ }) }) +describe('Model.primary(name)', function(){ + it('should set the model primary key', function(){ + User.primary('id'); + assert('id' == User.primaryKey); + }) +}) + describe('Model.attrs', function(){ it('should hold the defined attrs', function(){ assert('string' == User.attrs.name.type);