Last active
August 29, 2015 14:09
-
-
Save bakavic/d8dcf52b5a4292b43379 to your computer and use it in GitHub Desktop.
Waterline: Using attributes in join table
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Group.js | |
* | |
* @description :: TODO: You might write a short summary of how this model works and what it represents here. | |
* @docs :: http://sailsjs.org/#!documentation/models | |
*/ | |
module.exports = { | |
attributes: { | |
name : { type: 'string' }, | |
description : { type: 'string' }, | |
products: { | |
collection: 'productGroup', | |
references: 'productGroup', | |
on: 'groupid', | |
via: 'productid', | |
unique: true | |
} | |
}, | |
findWithProductPriority: function (cb) { | |
Group.find({sort: { groupid: 1, prodPriority: 1 }}) | |
.populate('products') | |
.exec(cb); | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Product.js | |
* | |
* @description :: TODO: You might write a short summary of how this model works and what it represents here. | |
* @docs :: http://sailsjs.org/#!documentation/models | |
*/ | |
module.exports = { | |
autoPK: false, | |
attributes: { | |
uuid : { | |
type: 'string', | |
primaryKey: true, | |
required: true | |
}, | |
name : { | |
type: 'string', | |
required: true, | |
}, | |
description : { | |
type: 'string', | |
required: true | |
}, | |
photos : { type: 'json' }, | |
price : { | |
type: 'float', | |
required: true | |
}, | |
avail_modifiers : { | |
type: 'array', | |
required: true | |
}, | |
unit : { | |
type: 'string', | |
enum: ['item', 'kg'], | |
defaultsTo: 'item' | |
}, | |
groups: { | |
collection: 'productGroup', | |
references: 'productGroup', | |
on: 'productid', | |
via: 'groupid', | |
} | |
} | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = { | |
identity : 'ProductGroup', | |
tableName : 'product_group', | |
attributes : { | |
// Due to a bug in the schema generator this seems to be needed at the | |
// model level but not on the actual database. | |
// id: { | |
// type: 'integer', | |
// primaryKey: true | |
// }, | |
productid : { | |
columnName: 'product_id', | |
type: 'integer', | |
foreignKey: true, | |
references: 'product', | |
on: 'id', | |
groupKey: 'product' | |
}, | |
groupid : { | |
model: 'group', | |
columnName: 'group_id', | |
type: 'integer', | |
foreignKey: true, | |
references: 'group', | |
on: 'id', | |
groupKey: 'group' | |
}, | |
prodPriority: { | |
type: 'integer' | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment