Created
October 20, 2015 09:35
-
-
Save manuelbieh/c6756c3d46bc7b77c318 to your computer and use it in GitHub Desktop.
Sequelize scope with limit fixed
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
import Sequelize from 'sequelize'; | |
let sequelize = new Sequelize('test', 'root', '', { | |
host: 'localhost', | |
dialect: 'sqlite', | |
storage: __dirname + '/database.sqlite', | |
logging: console.log, | |
} | |
); | |
let ProductImage = sequelize.define('productImage', { | |
filename: Sequelize.STRING, | |
}, { | |
scopes: { | |
overview: {} | |
} | |
}); | |
let ProductCategory = sequelize.define('productCategory', { | |
title: { | |
type: Sequelize.STRING, | |
unique: true, | |
allowNull: false | |
}, | |
}); | |
let Product = sequelize.define('product', { | |
title: { | |
type: Sequelize.STRING, | |
unique: true, | |
allowNull: false, | |
}, | |
}, { | |
scopes: { | |
overview: { | |
include: [ | |
{ | |
model: ProductImage, | |
required: false, | |
as: 'images', | |
limit: 1 | |
}, | |
{ | |
model: ProductCategory, | |
required: false, | |
as: 'categories', | |
through: { | |
attributes: [] | |
} | |
} | |
], | |
}, | |
} | |
}); | |
let ProductCategoryRelation = sequelize.define('productCategoryRelation', { | |
category_id: Sequelize.INTEGER, | |
product_id: Sequelize.INTEGER, | |
}); | |
ProductCategory.belongsToMany(Product, { | |
foreignKey: 'category_id', | |
through: ProductCategoryRelation, | |
as: 'products' | |
}); | |
Product.belongsToMany(ProductCategory, { | |
foreignKey: 'product_id', | |
through: ProductCategoryRelation, | |
as: 'categories' | |
}); | |
ProductCategoryRelation.hasMany(Product, { | |
foreignKey: 'id' | |
}); | |
ProductCategoryRelation.hasMany(ProductCategory, { | |
foreignKey: 'id' | |
}); | |
Product.hasMany(ProductImage, { | |
onDelete: 'CASCADE', | |
as: 'images', | |
foreignKey: 'product_id' | |
}); | |
//console.log('####### SCOPE #######'); | |
//console.log(typeof Product.options.scopes.overview, Product.options.scopes.overview); | |
let p, c; | |
sequelize.query('SELECT 1+1') | |
.then(() => ProductImage.drop()) | |
.then(() => ProductCategoryRelation.drop()) | |
.then(() => ProductCategory.drop()) | |
.then(() => Product.drop()) | |
.then(() => ProductCategoryRelation.sync()) | |
.then(() => Product.sync()) | |
.then(() => ProductCategory.sync()) | |
.then(() => ProductImage.sync()) | |
.then(() => Product.create({title: 'Test Product'})) | |
.then((product) => { | |
p = product; | |
return ProductCategory.create({ | |
title: 'Test Category' | |
}); | |
}) | |
.then((category) => { | |
c = category; | |
return ProductCategoryRelation.create({ | |
product_id: p.id, | |
category_id: c.id | |
}); | |
}) | |
.then(() => { | |
return ProductImage.bulkCreate([ | |
{filename: 'a.jpg', product_id: p.id}, | |
{filename: 'b.jpg', product_id: p.id}, | |
{filename: 'c.jpg', product_id: p.id}, | |
{filename: 'd.jpg', product_id: p.id} | |
]) | |
}) | |
.then(() => { | |
console.log('----\nThis works:\n----'); | |
return Product.scope('overview').findAll() | |
}) | |
.then((product) => { | |
console.log(JSON.stringify(product, null, 2)) | |
}) | |
.then(() => { | |
return ProductCategory.findById(1) | |
}) | |
.then((category) => { | |
console.log('----\nThis also works now!:\n----'); | |
return category.getProducts({ | |
joinTableAttributes: [], | |
scope: 'overview' | |
}); | |
}) | |
.then((products) => { | |
console.log(JSON.stringify(products, null, 2)); | |
}) | |
.catch((err) => { | |
console.error(err); | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment