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

Add upsert and insert graph options in params #153

Merged
merged 15 commits into from
May 29, 2021
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
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ be updated (if there are any changes at all).
#### Params Operators

- **`mergeAllowUpsert`** - Merge given expression into `allowedUpsert`.
- **`mergeUpsertGraphOptions`** - Merge given options into `upsertGraphOptions`.

### Graph insert

Expand All @@ -414,6 +415,7 @@ Runs on the `.create(data, params)` service method.
#### Params Operators

- **`mergeAllowInsert`** - Merge given expression into `allowedInsert`.
- **`mergeInsertGraphOptions`** - Merge given options into `insertGraphOptions`.

### Transaction

Expand Down
12 changes: 8 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -701,15 +701,17 @@ class Service extends AdapterService {
let promise = q;
const allowedUpsert = this.mergeRelations(this.allowedUpsert, params.mergeAllowUpsert);
const allowedInsert = this.mergeRelations(this.allowedInsert, params.mergeAllowInsert);
const upsertGraphOptions = { ...this.upsertGraphOptions, ...params.mergeUpsertGraphOptions };
const insertGraphOptions = { ...this.insertGraphOptions, ...params.mergeInsertGraphOptions };

if (this.createUseUpsertGraph) {
if (allowedUpsert) {
q.allowGraph(allowedUpsert);
}
q.upsertGraph(data, this.upsertGraphOptions);
q.upsertGraph(data, upsertGraphOptions);
} else if (allowedInsert) {
q.allowGraph(allowedInsert);
q.insertGraph(data, this.insertGraphOptions);
q.insertGraph(data, insertGraphOptions);
} else {
promise = this._batchInsert(data, params);
}
Expand Down Expand Up @@ -753,9 +755,10 @@ class Service extends AdapterService {
}

if (allowedUpsert) {
const upsertGraphOptions = { ...this.upsertGraphOptions, ...params.mergeUpsertGraphOptions };
return this._createQuery(params)
.allowGraph(allowedUpsert)
.upsertGraphAndFetch(newObject, this.upsertGraphOptions).then(this._commitTransaction(transaction), this._rollbackTransaction(transaction));
.upsertGraphAndFetch(newObject, upsertGraphOptions).then(this._commitTransaction(transaction), this._rollbackTransaction(transaction));
}

// NOTE (EK): Delete id field so we don't update it
Expand Down Expand Up @@ -794,6 +797,7 @@ class Service extends AdapterService {
let { filters, query } = this.filterQuery(params);

const allowedUpsert = this.mergeRelations(this.allowedUpsert, params.mergeAllowUpsert);
const upsertGraphOptions = { ...this.upsertGraphOptions, ...params.mergeUpsertGraphOptions };
if (allowedUpsert && id !== null) {
const dataCopy = Object.assign({}, data);
this._checkUpsertId(id, dataCopy);
Expand All @@ -804,7 +808,7 @@ class Service extends AdapterService {
const transaction = await this._createTransaction(params);
return this._createQuery(params)
.allowGraph(allowedUpsert)
.upsertGraphAndFetch(dataCopy, this.upsertGraphOptions)
.upsertGraphAndFetch(dataCopy, upsertGraphOptions)
.then(this._selectFields(params, data)).then(this._commitTransaction(transaction), this._rollbackTransaction(transaction));
});
}
Expand Down