Skip to content

Instantly share code, notes, and snippets.

@bakavic
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bakavic/d8dcf52b5a4292b43379 to your computer and use it in GitHub Desktop.
Save bakavic/d8dcf52b5a4292b43379 to your computer and use it in GitHub Desktop.
Waterline: Using attributes in join table
/**
* 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);
}
};
/**
* 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',
}
}
};
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