Skip to content

Commit

Permalink
Add upsert and insert graph options in params (#153)
Browse files Browse the repository at this point in the history
* Fix schema use with graph

* Add $startTransaction params

* allow for atomic graph upsert/insert
* allow for atomic multi create

* Fix $startTransaction (atomic)

* Is now $atomic
* Better testing
* README infos

* Fix confilts with upstream

* Fix/Add comments

* Fix patch query with complex query

* Fixed typo

* Add upsert and insert graph options in params

* allow for query based insert and upsert graph options
typical use case is different options based on authenticated user rights

* Fix param names in previous commit

Co-authored-by: Dekel Barzilay <[email protected]>
  • Loading branch information
Thomas-git and dekelev authored May 29, 2021
1 parent 21026c9 commit 087dfe2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
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

0 comments on commit 087dfe2

Please sign in to comment.