Skip to content

Instantly share code, notes, and snippets.

@mphasize
Last active August 29, 2015 14:01
Show Gist options
  • Save mphasize/aeaff696132357797e0b to your computer and use it in GitHub Desktop.
Save mphasize/aeaff696132357797e0b to your computer and use it in GitHub Desktop.
Sails-Many-to-Many through

An example on how to setup Many-to-Many relationships through a junction table with it's own model definition, enabling individual attributes and filtering on the relationship.

module.exports = {
attributes: {
[ ... ]
owners: {
collection: "user",
via: "apps",
through: "team"
}
[ ... ]
}
};
module.exports = {
tableName: 'team',
tables: [ 'user', 'app' ],
junctionTable: true,
attributes: {
id: {
primaryKey: true,
autoIncrement: true,
type: 'integer'
},
user: {
columnName: 'user',
type: 'integer',
foreignKey: true,
references: 'user',
on: 'id',
via: 'app',
groupBy: 'user'
},
app: {
columnName: 'app',
type: 'integer',
foreignKey: true,
references: 'app',
on: 'id',
via: 'user',
groupBy: 'app'
},
role: {
type: "string"
}
}
};
module.exports = {
attributes: {
[ ... ]
apps: {
collection: "app",
via: "owners",
through: "team"
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment