@proteinjs/db 1.11.1 → 1.13.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +32 -0
- package/LICENSE +21 -0
- package/dist/generated/index.js +1 -1
- package/dist/generated/index.js.map +1 -1
- package/dist/src/schema/TableManager.d.ts.map +1 -1
- package/dist/src/schema/TableManager.js +13 -16
- package/dist/src/schema/TableManager.js.map +1 -1
- package/dist/src/transaction/Transaction.d.ts +8 -1
- package/dist/src/transaction/Transaction.d.ts.map +1 -1
- package/dist/src/transaction/Transaction.js +44 -2
- package/dist/src/transaction/Transaction.js.map +1 -1
- package/generated/index.ts +17 -20
- package/package.json +4 -3
- package/src/schema/TableManager.ts +14 -16
- package/src/transaction/Transaction.ts +49 -3
|
@@ -3,7 +3,7 @@ import { DbService, ObjectQuery } from '../services/DbService';
|
|
|
3
3
|
import { getTransactionRunner } from './TransactionRunner';
|
|
4
4
|
import { addDefaultFieldValues, Table } from '../Table';
|
|
5
5
|
import { isInstanceOf } from '@proteinjs/util';
|
|
6
|
-
import { QueryBuilder } from '@proteinjs/db-query';
|
|
6
|
+
import { Condition, QueryBuilder } from '@proteinjs/db-query';
|
|
7
7
|
|
|
8
8
|
export type OperationQueue<R extends Record = Record> = {
|
|
9
9
|
insert: (...args: Parameters<DbService<R>['insert']>) => Promise<R>;
|
|
@@ -144,11 +144,22 @@ export class Transaction implements OperationQueue {
|
|
|
144
144
|
* Queue a delete.
|
|
145
145
|
*
|
|
146
146
|
* If a `QueryBuilder` is passed in for `query`, changes will not be made to the cached `db`.
|
|
147
|
-
* Passing in an `ObjectQuery` will update the cached `db`.
|
|
147
|
+
* Passing in an `ObjectQuery` or a `QueryBuilder` with a `id IN string[]` condition will update the cached `db`.
|
|
148
148
|
*/
|
|
149
149
|
delete<R extends Record = Record>(...args: Parameters<DbService<R>['delete']>): void {
|
|
150
150
|
const [table, query] = args;
|
|
151
|
-
if (
|
|
151
|
+
if (isInstanceOf(query, QueryBuilder)) {
|
|
152
|
+
// Process `id IN string[]` condition, if exists
|
|
153
|
+
const qb = query as QueryBuilder;
|
|
154
|
+
const recordMap = this.recordMap(table.name);
|
|
155
|
+
const ids = this.getIdsFromInCondition(qb);
|
|
156
|
+
ids.forEach((id) => {
|
|
157
|
+
if (this.onDelete && recordMap[id]) {
|
|
158
|
+
this.onDelete(table, recordMap[id]);
|
|
159
|
+
}
|
|
160
|
+
delete recordMap[id];
|
|
161
|
+
});
|
|
162
|
+
} else {
|
|
152
163
|
const objectQuery = query as ObjectQuery<any>;
|
|
153
164
|
const recordMap = this.recordMap(table.name);
|
|
154
165
|
if (objectQuery.id) {
|
|
@@ -171,6 +182,41 @@ export class Transaction implements OperationQueue {
|
|
|
171
182
|
this.ops.push({ name: 'delete', args });
|
|
172
183
|
}
|
|
173
184
|
|
|
185
|
+
/**
|
|
186
|
+
* If a QueryBuilder contains a condition like this:
|
|
187
|
+
* `{ field: 'id', operator: 'IN', value: string[] }`
|
|
188
|
+
* return the ids from the value.
|
|
189
|
+
* @param qb QueryBuilder
|
|
190
|
+
*/
|
|
191
|
+
private getIdsFromInCondition(qb: QueryBuilder): string[] {
|
|
192
|
+
const filterIdInConditions = (nodeId: string): boolean => {
|
|
193
|
+
const node: any = qb.graph.node(nodeId);
|
|
194
|
+
if (node.type !== 'CONDITION') {
|
|
195
|
+
return false;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
const condition = node as Condition<any>;
|
|
199
|
+
if (condition.field !== 'id') {
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
if (condition.operator !== 'IN') {
|
|
204
|
+
return false;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
return Array.isArray(condition.value) && typeof condition.value[0] === 'string';
|
|
208
|
+
};
|
|
209
|
+
|
|
210
|
+
const rootChildren: string[] = qb.graph.successors(qb.rootId) || [];
|
|
211
|
+
const idInConditionId = rootChildren.filter(filterIdInConditions)[0];
|
|
212
|
+
if (!idInConditionId) {
|
|
213
|
+
return [];
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
const { value } = qb.graph.node(idInConditionId) as Condition<any>;
|
|
217
|
+
return value;
|
|
218
|
+
}
|
|
219
|
+
|
|
174
220
|
/**
|
|
175
221
|
* Run the operations in order (not a batch), as a single transaction.
|
|
176
222
|
*/
|