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

feat: add relation support #8

Merged
merged 3 commits into from
Mar 30, 2020
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 32 additions & 5 deletions dist/vuex-orm-soft-delete.common.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,19 +193,19 @@ function Query(context, query) {
* false = include soft deletes
* null = exclude soft deletes
*/
query.prototype.softDeletesFilter = null;
query.prototype.softDeleteSelectFilter = null;
/**
* Constraint includes soft deleted models.
*/
query.prototype.withTrashed = function () {
this.softDeletesFilter = false;
this.softDeleteSelectFilter = false;
return this;
};
/**
* Constraint restricts to only soft deleted models.
*/
query.prototype.onlyTrashed = function () {
this.softDeletesFilter = true;
this.softDeleteSelectFilter = true;
return this;
};
/**
Expand Down Expand Up @@ -260,6 +260,33 @@ function Query(context, query) {
.onlyTrashed()
.get();
};
/**
* Patch any new queries so that sub queries, such as relation queries,
* can respect top level modifiers. For many-to-many relations, due to core
* API limitations, we're forced to pass-through intermediate models.
*/
var newQuery = query.prototype.newQuery;
query.prototype.newQuery = function (entity) {
var patchedQuery = newQuery.call(this, entity);
// Only patch queries that are loading relations.
var loadables = Object.keys(this.load);
if (loadables.length > 0) {
patchedQuery.softDeleteSelectFilter = this.softDeleteSelectFilter;
if (entity && entity !== this.entity && this.model.hasPivotFields()) {
var fields = this.model.pivotFields().reduce(function (fields, field) {
Object.keys(field).filter(function (entity) { return loadables.includes(entity); }).forEach(function (entity) {
fields.push(field[entity].pivot.entity);
});
return fields;
}, []);
// Release an entity that is an intermediate to a loadable relation.
if (fields.includes(entity)) {
patchedQuery.softDeleteSelectFilter = false;
}
}
}
return patchedQuery;
};
/**
* Fetch all soft deletes from the store and group by entity.
*/
Expand All @@ -280,11 +307,11 @@ function Query(context, query) {
var _this = this;
return models.filter(function (model) {
// Only soft deletes
if (_this.softDeletesFilter === true) {
if (_this.softDeleteSelectFilter === true) {
return model.$trashed();
}
// Include soft deletes
if (_this.softDeletesFilter === false) {
if (_this.softDeleteSelectFilter === false) {
return models;
}
// Exclude soft deletes
Expand Down
37 changes: 32 additions & 5 deletions dist/vuex-orm-soft-delete.esm.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,19 +191,19 @@ function Query(context, query) {
* false = include soft deletes
* null = exclude soft deletes
*/
query.prototype.softDeletesFilter = null;
query.prototype.softDeleteSelectFilter = null;
/**
* Constraint includes soft deleted models.
*/
query.prototype.withTrashed = function () {
this.softDeletesFilter = false;
this.softDeleteSelectFilter = false;
return this;
};
/**
* Constraint restricts to only soft deleted models.
*/
query.prototype.onlyTrashed = function () {
this.softDeletesFilter = true;
this.softDeleteSelectFilter = true;
return this;
};
/**
Expand Down Expand Up @@ -258,6 +258,33 @@ function Query(context, query) {
.onlyTrashed()
.get();
};
/**
* Patch any new queries so that sub queries, such as relation queries,
* can respect top level modifiers. For many-to-many relations, due to core
* API limitations, we're forced to pass-through intermediate models.
*/
var newQuery = query.prototype.newQuery;
query.prototype.newQuery = function (entity) {
var patchedQuery = newQuery.call(this, entity);
// Only patch queries that are loading relations.
var loadables = Object.keys(this.load);
if (loadables.length > 0) {
patchedQuery.softDeleteSelectFilter = this.softDeleteSelectFilter;
if (entity && entity !== this.entity && this.model.hasPivotFields()) {
var fields = this.model.pivotFields().reduce(function (fields, field) {
Object.keys(field).filter(function (entity) { return loadables.includes(entity); }).forEach(function (entity) {
fields.push(field[entity].pivot.entity);
});
return fields;
}, []);
// Release an entity that is an intermediate to a loadable relation.
if (fields.includes(entity)) {
patchedQuery.softDeleteSelectFilter = false;
}
}
}
return patchedQuery;
};
/**
* Fetch all soft deletes from the store and group by entity.
*/
Expand All @@ -278,11 +305,11 @@ function Query(context, query) {
var _this = this;
return models.filter(function (model) {
// Only soft deletes
if (_this.softDeletesFilter === true) {
if (_this.softDeleteSelectFilter === true) {
return model.$trashed();
}
// Include soft deletes
if (_this.softDeletesFilter === false) {
if (_this.softDeleteSelectFilter === false) {
return models;
}
// Exclude soft deletes
Expand Down
37 changes: 32 additions & 5 deletions dist/vuex-orm-soft-delete.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,19 +197,19 @@
* false = include soft deletes
* null = exclude soft deletes
*/
query.prototype.softDeletesFilter = null;
query.prototype.softDeleteSelectFilter = null;
/**
* Constraint includes soft deleted models.
*/
query.prototype.withTrashed = function () {
this.softDeletesFilter = false;
this.softDeleteSelectFilter = false;
return this;
};
/**
* Constraint restricts to only soft deleted models.
*/
query.prototype.onlyTrashed = function () {
this.softDeletesFilter = true;
this.softDeleteSelectFilter = true;
return this;
};
/**
Expand Down Expand Up @@ -264,6 +264,33 @@
.onlyTrashed()
.get();
};
/**
* Patch any new queries so that sub queries, such as relation queries,
* can respect top level modifiers. For many-to-many relations, due to core
* API limitations, we're forced to pass-through intermediate models.
*/
var newQuery = query.prototype.newQuery;
query.prototype.newQuery = function (entity) {
var patchedQuery = newQuery.call(this, entity);
// Only patch queries that are loading relations.
var loadables = Object.keys(this.load);
if (loadables.length > 0) {
patchedQuery.softDeleteSelectFilter = this.softDeleteSelectFilter;
if (entity && entity !== this.entity && this.model.hasPivotFields()) {
var fields = this.model.pivotFields().reduce(function (fields, field) {
Object.keys(field).filter(function (entity) { return loadables.includes(entity); }).forEach(function (entity) {
fields.push(field[entity].pivot.entity);
});
return fields;
}, []);
// Release an entity that is an intermediate to a loadable relation.
if (fields.includes(entity)) {
patchedQuery.softDeleteSelectFilter = false;
}
}
}
return patchedQuery;
};
/**
* Fetch all soft deletes from the store and group by entity.
*/
Expand All @@ -284,11 +311,11 @@
var _this = this;
return models.filter(function (model) {
// Only soft deletes
if (_this.softDeletesFilter === true) {
if (_this.softDeleteSelectFilter === true) {
return model.$trashed();
}
// Include soft deletes
if (_this.softDeletesFilter === false) {
if (_this.softDeleteSelectFilter === false) {
return models;
}
// Exclude soft deletes
Expand Down
Loading