Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Small fixes #5

Merged
merged 3 commits into from Sep 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion .eslintrc
Expand Up @@ -236,6 +236,7 @@
],
"wrap-regex": 0,
"no-var": 0,
"max-len": [2, 200, 4]
"max-len": [2, 200, 4],
"no-restricted-globals": [2, "Promise"]
}
}
27 changes: 20 additions & 7 deletions src/index.js
Expand Up @@ -21,7 +21,6 @@ function mapResult(attribute, keys, options, result) {
carry[key].push(row);
}


return carry;
}, {});
} else {
Expand Down Expand Up @@ -77,25 +76,26 @@ function loaderForBTM(model, attributes, options = {}) {
assert(Array.isArray(attributes), 'Attributes for BTM loader should be an array');
assert(attributes.length === 2, 'Attributes for BTM loader should have length two');

let cacheKey = getCacheKey(model, attributes, options);
let cacheKey = getCacheKey(model, attributes, options)
, association = options.association;
delete options.association;

if (!cache.has(cacheKey)) {
cache.set(cacheKey, new DataLoader(keys => {
if (options.limit) {
options.groupedLimit = {
on: options.association,
on: association,
limit: options.limit,
values: keys
};
} else {
options.include = [{
association: options.association.manyFromSource,
association: association.manyFromSource,
where: mergeWhere({
[attributes[1]]: keys
}, options.where)
}];
}
delete options.association;

return model.findAll(options).then(mapResult.bind(null, attributes, keys, options));
}));
Expand Down Expand Up @@ -149,8 +149,13 @@ function loaderForModel(model, attribute, options = {}) {
}

function shimModel(target) {
if (target.findById.__wrapped) return;

shimmer.massWrap(target, ['findById', 'findByPrimary'], original => {
return function batchedFindById(id, options = {}) {
if ([null, undefined].indexOf(id) !== -1) {
return Promise.resolve(null);
}
if (options.transaction) {
return original.apply(this, arguments);
}
Expand All @@ -160,6 +165,8 @@ function shimModel(target) {
}

function shimBelongsTo(target) {
if (target.get.__wrapped) return;

shimmer.wrap(target, 'get', original => {
return function batchedGetBelongsTo(instance, options = {}) {
// targetKeyIsPrimary already handled by sequelize (maps to findById)
Expand All @@ -174,6 +181,8 @@ function shimBelongsTo(target) {
}

function shimHasOne(target) {
if (target.get.__wrapped) return;

shimmer.wrap(target, 'get', original => {
return function batchedGetHasOne(instance, options = {}) {
if (Array.isArray(instance) || options.include || options.transaction) {
Expand All @@ -187,6 +196,8 @@ function shimHasOne(target) {
}

function shimHasMany(target) {
if (target.get.__wrapped) return;

shimmer.wrap(target, 'get', original => {
return function bathedGetHasMany(instances, options = {}) {
if (options.include || options.transaction) {
Expand All @@ -208,15 +219,17 @@ function shimHasMany(target) {
}

function shimBelongsToMany(target) {
if (target.get.__wrapped) return;

shimmer.wrap(target, 'get', original => {
return function bathedGetHasMany(instances, options = {}) {
assert(target.paired, '.paired missing on belongsToMany association. You need to set up both sides of the association');
assert(this.paired, '.paired missing on belongsToMany association. You need to set up both sides of the association');

if (options.include || options.transaction) {
return original.apply(this, arguments);
}

let loader = loaderForBTM(this.target, [this.as, this.foreignKey], {
let loader = loaderForBTM(this.target, [this.paired.manyFromSource.as, this.foreignKey], {
...options,
association: this.paired,
multiple: true
Expand Down