Saturday 15 January 2011

node.js - How to get the primary key attribute name of a model? (waterline ORM) -



node.js - How to get the primary key attribute name of a model? (waterline ORM) -

some of models not have id primary key, , need know name of primary key of given model couldn't find way of doing this. searched in waterline docs , source, found in source using attributes object definition stored in unaccessible variable in lib scope.

the "cleaner" way found this:

// file: api/libs/model.js module.exports = { //... primarykeyname: function (modelname) { var key, attr, pk, def = require('../models/' + modelname).attributes; (var key in def.attributes) { attr = def.attributes[key]; if (attr && attr.primarykey) { pk = key; break; } } homecoming pk || 'id'; }, //... };

but sense bit tricky , wondering if there wouldn't improve solution hidden/undocumented helper function or method on model or model instance (ie record)...

ah! should inspect model class console , i'd have answer.

anyway, whoever looking same info, here is:

somemodel.primarykey; // => 'id' or whatever

and voila, simple!

node.js sails.js waterline

No comments:

Post a Comment