-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathrun.ts
More file actions
51 lines (43 loc) · 1.68 KB
/
Copy pathrun.ts
File metadata and controls
51 lines (43 loc) · 1.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import { TransactionType } from 'typedb-driver';
import { getSessionOrOpenNewOne } from '../../../adapters/typeDB/helpers';
import type { BormConfig, DBHandles } from '../../../types';
export type TqlMutation = {
deletions: string;
deletionMatches: string;
insertions: string;
insertionMatches: string;
};
export const runTQLMutation = async (tqlMutation: TqlMutation, dbHandles: DBHandles, config: BormConfig) => {
if (!tqlMutation) {
throw new Error('TQL request not built');
}
if (!((tqlMutation.deletions && tqlMutation.deletionMatches) || tqlMutation.insertions)) {
throw new Error('TQL request error, no things');
}
//console.log('tqlMutation', tqlMutation);
const { session } = await getSessionOrOpenNewOne(dbHandles, config);
const mutateTransaction = await session.transaction(TransactionType.WRITE);
// deletes and pre-update deletes
const tqlDeletion =
tqlMutation.deletionMatches &&
tqlMutation.deletions &&
`match ${tqlMutation.deletionMatches} delete ${tqlMutation.deletions}`;
// insertions and updates
const tqlInsertion =
tqlMutation.insertions &&
`${tqlMutation.insertionMatches ? `match ${tqlMutation.insertionMatches}` : ''} insert ${tqlMutation.insertions}`;
try {
// does not receive a result
if (tqlDeletion) {
await mutateTransaction.query.delete(tqlDeletion);
}
const insertionsStream = tqlInsertion && mutateTransaction.query.insert(tqlInsertion);
const insertionsRes = insertionsStream ? await insertionsStream.collect() : undefined;
await mutateTransaction.commit();
return { insertions: insertionsRes };
} catch (e: any) {
throw new Error(`Transaction failed: ${e.message}`);
} finally {
await mutateTransaction.close();
}
};