prostgles-server 4.2.485 → 4.2.487
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/dist/DboBuilder/TableHandler/insert/getInsertTableRules.d.ts +5 -0
- package/dist/DboBuilder/TableHandler/insert/getInsertTableRules.d.ts.map +1 -0
- package/dist/DboBuilder/TableHandler/insert/getInsertTableRules.js +16 -0
- package/dist/DboBuilder/TableHandler/insert/getInsertTableRules.js.map +1 -0
- package/dist/DboBuilder/TableHandler/insert/getReferenceColumnInserts.d.ts +18 -0
- package/dist/DboBuilder/TableHandler/insert/getReferenceColumnInserts.d.ts.map +1 -0
- package/dist/DboBuilder/TableHandler/insert/getReferenceColumnInserts.js +45 -0
- package/dist/DboBuilder/TableHandler/insert/getReferenceColumnInserts.js.map +1 -0
- package/dist/DboBuilder/TableHandler/insert/insertNestedRecords.d.ts +0 -18
- package/dist/DboBuilder/TableHandler/insert/insertNestedRecords.d.ts.map +1 -1
- package/dist/DboBuilder/TableHandler/insert/insertNestedRecords.js +33 -251
- package/dist/DboBuilder/TableHandler/insert/insertNestedRecords.js.map +1 -1
- package/dist/DboBuilder/TableHandler/insert/insertRowWithNestedRecords.d.ts +23 -0
- package/dist/DboBuilder/TableHandler/insert/insertRowWithNestedRecords.d.ts.map +1 -0
- package/dist/DboBuilder/TableHandler/insert/insertRowWithNestedRecords.js +195 -0
- package/dist/DboBuilder/TableHandler/insert/insertRowWithNestedRecords.js.map +1 -0
- package/dist/DboBuilder/TableHandler/update.d.ts.map +1 -1
- package/dist/DboBuilder/TableHandler/update.js +4 -3
- package/dist/DboBuilder/TableHandler/update.js.map +1 -1
- package/lib/DboBuilder/TableHandler/insert/getInsertTableRules.ts +23 -0
- package/lib/DboBuilder/TableHandler/insert/getReferenceColumnInserts.ts +64 -0
- package/lib/DboBuilder/TableHandler/insert/insertNestedRecords.ts +39 -396
- package/lib/DboBuilder/TableHandler/insert/insertRowWithNestedRecords.ts +310 -0
- package/lib/DboBuilder/TableHandler/update.ts +2 -1
- package/package.json +9 -9
|
@@ -1,16 +1,10 @@
|
|
|
1
1
|
import type { AnyObject, InsertParams } from "prostgles-types";
|
|
2
|
-
import {
|
|
3
|
-
getKeys,
|
|
4
|
-
getPossibleNestedInsert,
|
|
5
|
-
isDefined,
|
|
6
|
-
isObject,
|
|
7
|
-
omitKeys,
|
|
8
|
-
type ColumnInfo,
|
|
9
|
-
} from "prostgles-types";
|
|
10
|
-
import type { AuthClientRequest } from "../../../Auth/AuthTypes";
|
|
2
|
+
import { getKeys, isObject } from "prostgles-types";
|
|
11
3
|
import type { ParsedTableRule } from "../../../PublishParser/PublishParser";
|
|
12
|
-
import type { LocalParams
|
|
4
|
+
import type { LocalParams } from "../../DboBuilder";
|
|
13
5
|
import type { TableHandler } from "../TableHandler";
|
|
6
|
+
import { getReferenceColumnInserts } from "./getReferenceColumnInserts";
|
|
7
|
+
import { insertRowWithNestedRecords } from "./insertRowWithNestedRecords";
|
|
14
8
|
|
|
15
9
|
type InsertNestedRecordsArgs = {
|
|
16
10
|
data: AnyObject | AnyObject[];
|
|
@@ -61,10 +55,17 @@ export async function insertNestedRecords(
|
|
|
61
55
|
* */
|
|
62
56
|
const isMultiInsert = Array.isArray(data);
|
|
63
57
|
const insertedRows = (isMultiInsert ? data : [data]) as AnyObject[];
|
|
64
|
-
const
|
|
65
|
-
this.is_media ?
|
|
66
|
-
|
|
67
|
-
|
|
58
|
+
const insertedRowsWithNestedData = insertedRows.map((row) => {
|
|
59
|
+
const extraKeys = this.is_media ? [] : getExtraKeys(row);
|
|
60
|
+
const colInserts = this.is_media ? [] : getReferenceColumnInserts(this, row);
|
|
61
|
+
return {
|
|
62
|
+
row,
|
|
63
|
+
hasNestedData: !!(extraKeys.length || colInserts.length),
|
|
64
|
+
extraKeys,
|
|
65
|
+
colInserts,
|
|
66
|
+
};
|
|
67
|
+
});
|
|
68
|
+
const hasNestedInserts = insertedRowsWithNestedData.some((d) => d.hasNestedData);
|
|
68
69
|
|
|
69
70
|
/**
|
|
70
71
|
* Make sure nested insert uses a transaction
|
|
@@ -83,393 +84,35 @@ export async function insertNestedRecords(
|
|
|
83
84
|
}
|
|
84
85
|
const { onConflict } = insertParams || {};
|
|
85
86
|
const rootOnConflict = isObject(onConflict) ? onConflict.action : onConflict;
|
|
86
|
-
const _data = await Promise.all(
|
|
87
|
-
insertedRows.map(async (row) => {
|
|
88
|
-
// const { preValidate, validate } = tableRules?.insert ?? {};
|
|
89
|
-
// const { tableConfigurator } = this.dboBuilder.prostgles;
|
|
90
|
-
// if(!tableConfigurator) throw "tableConfigurator missing";
|
|
91
|
-
// let row = await tableConfigurator.getPreInsertRow(this, { dbx: this.getFinalDbo(localParams), validate, localParams, row: _row })
|
|
92
|
-
// if (preValidate) {
|
|
93
|
-
// row = await preValidate({ row, dbx: this.tx?.dbTX || this.dboBuilder.dbo, localParams });
|
|
94
|
-
// }
|
|
95
|
-
|
|
96
|
-
/* Potentially a nested join */
|
|
97
|
-
if (hasNestedInserts) {
|
|
98
|
-
const extraKeys = getExtraKeys(row);
|
|
99
|
-
const colInserts = getReferenceColumnInserts(this, row);
|
|
100
|
-
|
|
101
|
-
/* Ensure we're using the same transaction */
|
|
102
|
-
const tableHandler = this.tx ? this : (dbTX![this.name] as TableHandler);
|
|
103
|
-
|
|
104
|
-
const omitedKeys = extraKeys.concat(colInserts.map((c) => c.insertedFieldName));
|
|
105
|
-
|
|
106
|
-
const rootData: AnyObject = omitKeys(row, omitedKeys);
|
|
107
|
-
|
|
108
|
-
let insertedChildren: AnyObject[];
|
|
109
|
-
let targetTableRules: ParsedTableRule;
|
|
110
|
-
|
|
111
|
-
const colInsertsResult = colInserts.map((ci) => ({
|
|
112
|
-
...ci,
|
|
113
|
-
inserted: undefined as AnyObject[] | undefined,
|
|
114
|
-
}));
|
|
115
|
-
|
|
116
|
-
/** Insert referenced first and then populate root data with referenced keys */
|
|
117
|
-
if (colInserts.length) {
|
|
118
|
-
for (const colInsert of colInsertsResult) {
|
|
119
|
-
const newLocalParams: LocalParams = {
|
|
120
|
-
...localParams,
|
|
121
|
-
nestedInsert: {
|
|
122
|
-
depth: (localParams?.nestedInsert?.depth ?? 0) + 1,
|
|
123
|
-
previousData: rootData,
|
|
124
|
-
previousTable: this.name,
|
|
125
|
-
referencingColumn: colInsert.insertedFieldName,
|
|
126
|
-
},
|
|
127
|
-
};
|
|
128
|
-
const colRows = await referencedInsert(
|
|
129
|
-
tableHandler,
|
|
130
|
-
dbTX,
|
|
131
|
-
newLocalParams,
|
|
132
|
-
colInsert.tableName,
|
|
133
|
-
row[colInsert.insertedFieldName] as AnyObject | AnyObject[],
|
|
134
|
-
rootOnConflict,
|
|
135
|
-
);
|
|
136
|
-
const [colRow, ...otherColRows] = colRows;
|
|
137
|
-
if (!Array.isArray(colRows) || !colRow || otherColRows.length) {
|
|
138
|
-
const someFcolsAreNullOrUndefined =
|
|
139
|
-
colRow &&
|
|
140
|
-
colInsert.insertedFieldRef.fcols.some((fcol) => colRow[fcol] === undefined);
|
|
141
|
-
throw new Error(
|
|
142
|
-
[
|
|
143
|
-
"Could not do nested column insert: ",
|
|
144
|
-
someFcolsAreNullOrUndefined ?
|
|
145
|
-
"Some fcols values are undefined"
|
|
146
|
-
: "Unexpected return " + JSON.stringify(colRows),
|
|
147
|
-
].join("\n"),
|
|
148
|
-
);
|
|
149
|
-
}
|
|
150
|
-
colInsert.inserted = colRows;
|
|
151
|
-
|
|
152
|
-
colInsert.insertedFieldRef.fcols.map((fcol, idx) => {
|
|
153
|
-
const col = colInsert.insertedFieldRef.cols[idx];
|
|
154
|
-
if (!col) throw "Invalid column name for colInsert.insertedFieldRef.cols";
|
|
155
|
-
const foreignKey = colRow[fcol] as string | number;
|
|
156
|
-
rootData[col] = foreignKey;
|
|
157
|
-
});
|
|
158
|
-
}
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
const fullRootResult = (await tableHandler.insert(
|
|
162
|
-
rootData,
|
|
163
|
-
{ returning: "*", onConflict: insertParams?.onConflict },
|
|
164
|
-
undefined,
|
|
165
|
-
/** Remove requiredNestedInserts check before doing the actual insert */
|
|
166
|
-
tableRules?.insert?.requiredNestedInserts ?
|
|
167
|
-
{
|
|
168
|
-
...tableRules,
|
|
169
|
-
insert: {
|
|
170
|
-
...tableRules.insert,
|
|
171
|
-
requiredNestedInserts: tableRules.insert.requiredNestedInserts.filter(
|
|
172
|
-
({ ftable }) => !extraKeys.includes(ftable),
|
|
173
|
-
),
|
|
174
|
-
},
|
|
175
|
-
}
|
|
176
|
-
: tableRules,
|
|
177
|
-
localParams,
|
|
178
|
-
)) as AnyObject;
|
|
179
|
-
let returnData: AnyObject | undefined;
|
|
180
|
-
const returning = insertParams?.returning;
|
|
181
|
-
if (returning) {
|
|
182
|
-
returnData = {};
|
|
183
|
-
const returningItems = await this.prepareReturning(
|
|
184
|
-
returning,
|
|
185
|
-
this.parseFieldFilter(tableRules?.insert?.returningFields),
|
|
186
|
-
);
|
|
187
|
-
returningItems
|
|
188
|
-
.filter((s) => s.selected)
|
|
189
|
-
.map((rs) => {
|
|
190
|
-
const colInsertResult = colInsertsResult.find(
|
|
191
|
-
({ insertedFieldName }) => insertedFieldName === rs.columnName,
|
|
192
|
-
);
|
|
193
|
-
const inserted =
|
|
194
|
-
colInsertResult?.singleInsert ?
|
|
195
|
-
colInsertResult.inserted?.[0]
|
|
196
|
-
: colInsertResult?.inserted;
|
|
197
|
-
returnData![rs.alias] = inserted ?? fullRootResult[rs.alias];
|
|
198
|
-
});
|
|
199
|
-
}
|
|
200
|
-
|
|
201
|
-
await Promise.all(
|
|
202
|
-
extraKeys.map(async (targetTable) => {
|
|
203
|
-
const childDataItems = (
|
|
204
|
-
Array.isArray(row[targetTable]) ?
|
|
205
|
-
row[targetTable]
|
|
206
|
-
: [row[targetTable]]) as AnyObject[];
|
|
207
|
-
|
|
208
|
-
/** check */
|
|
209
|
-
if (childDataItems.some((d) => !isObject(d))) {
|
|
210
|
-
throw "Expected array of objects";
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
const childInsert = async (cdata: AnyObject | AnyObject[], tableName: string) => {
|
|
214
|
-
return referencedInsert(this, dbTX, localParams, tableName, cdata, rootOnConflict);
|
|
215
|
-
};
|
|
216
|
-
|
|
217
|
-
const joinPath = getJoinPath(this, targetTable);
|
|
218
|
-
|
|
219
|
-
const { path } = joinPath;
|
|
220
|
-
const [tbl1, tbl2, tbl3] = path;
|
|
221
|
-
targetTableRules = await getInsertTableRules(
|
|
222
|
-
this,
|
|
223
|
-
targetTable,
|
|
224
|
-
localParams?.clientReq,
|
|
225
|
-
localParams?.scope,
|
|
226
|
-
);
|
|
227
|
-
|
|
228
|
-
const cols2 = this.dboBuilder.dbo[tbl2!]!.columns || [];
|
|
229
|
-
if (!this.dboBuilder.dbo[tbl2!]) throw "Invalid/disallowed table: " + tbl2;
|
|
230
|
-
const colsRefT1 = cols2.filter((c) =>
|
|
231
|
-
c.references?.some((rc) => rc.cols.length === 1 && rc.ftable === tbl1),
|
|
232
|
-
);
|
|
233
|
-
|
|
234
|
-
if (!path.length) {
|
|
235
|
-
throw "Nested inserts join path not found for " + [this.name, targetTable].join(", ");
|
|
236
|
-
} else if (path.length === 2) {
|
|
237
|
-
if (targetTable !== tbl2) throw "Did not expect this";
|
|
238
|
-
|
|
239
|
-
if (!colsRefT1.length) {
|
|
240
|
-
throw `Target table ${tbl2} does not reference any columns from the root table ${this.name}. Cannot insert nested data`;
|
|
241
|
-
}
|
|
242
|
-
|
|
243
|
-
insertedChildren = await childInsert(
|
|
244
|
-
childDataItems.map((d) => {
|
|
245
|
-
const result = { ...d };
|
|
246
|
-
colsRefT1.map((col) => {
|
|
247
|
-
result[col.references![0]!.cols[0]!] =
|
|
248
|
-
fullRootResult[col.references![0]!.fcols[0]!];
|
|
249
|
-
});
|
|
250
|
-
return result;
|
|
251
|
-
}),
|
|
252
|
-
targetTable,
|
|
253
|
-
);
|
|
254
|
-
} else if (path.length === 3) {
|
|
255
|
-
if (targetTable !== tbl3) throw "Did not expect this";
|
|
256
|
-
const colsRefT3 = cols2.filter((c) =>
|
|
257
|
-
c.references?.some((rc) => rc.cols.length === 1 && rc.ftable === tbl3),
|
|
258
|
-
);
|
|
259
|
-
if (!colsRefT1.length || !colsRefT3.length)
|
|
260
|
-
throw "Incorrectly referenced or missing columns for nested insert";
|
|
261
|
-
|
|
262
|
-
const fileTable = this.dboBuilder.prostgles.fileManager?.tableName;
|
|
263
|
-
if (targetTable !== fileTable) {
|
|
264
|
-
throw "Only media allowed to have nested inserts more than 2 tables apart";
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
/* We expect tbl2 to have only 2 columns (media_id and foreign_id) */
|
|
268
|
-
if (
|
|
269
|
-
!(
|
|
270
|
-
cols2.filter((c) => c.references?.[0]?.ftable === fileTable).length === 1 &&
|
|
271
|
-
cols2.filter((c) => c.references?.[0]?.ftable === tableHandler.name).length === 1
|
|
272
|
-
)
|
|
273
|
-
) {
|
|
274
|
-
console.log({
|
|
275
|
-
tbl1,
|
|
276
|
-
tbl2,
|
|
277
|
-
tbl3,
|
|
278
|
-
name: tableHandler.name,
|
|
279
|
-
tthisName: this.name,
|
|
280
|
-
});
|
|
281
|
-
throw (
|
|
282
|
-
"Second joining table (" +
|
|
283
|
-
tbl2 +
|
|
284
|
-
") not of expected format. Must contain exactly one reference column for each table (file table and target table) "
|
|
285
|
-
);
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
insertedChildren = await childInsert(childDataItems, targetTable);
|
|
289
|
-
|
|
290
|
-
/* Insert in key_lookup table */
|
|
291
|
-
await Promise.all(
|
|
292
|
-
insertedChildren.map(async (t3Child) => {
|
|
293
|
-
const tbl2Row: AnyObject = {};
|
|
294
|
-
|
|
295
|
-
colsRefT3.map((col) => {
|
|
296
|
-
tbl2Row[col.name] = t3Child[col.references![0]!.fcols[0]!];
|
|
297
|
-
});
|
|
298
|
-
colsRefT1.map((col) => {
|
|
299
|
-
tbl2Row[col.name] = fullRootResult[col.references![0]!.fcols[0]!];
|
|
300
|
-
});
|
|
301
87
|
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
throw "Unexpected path for Nested inserts";
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
/* Return also the nested inserted data */
|
|
311
|
-
if (insertedChildren.length && returning) {
|
|
312
|
-
const targetTableHandler = dbTX![targetTable] as TableHandler;
|
|
313
|
-
const targetReturning = await targetTableHandler.prepareReturning(
|
|
314
|
-
"*",
|
|
315
|
-
targetTableHandler.parseFieldFilter(targetTableRules.insert?.returningFields),
|
|
316
|
-
);
|
|
317
|
-
const clientTargetInserts = insertedChildren.map((d) => {
|
|
318
|
-
const _d = { ...d };
|
|
319
|
-
const res: AnyObject = {};
|
|
320
|
-
targetReturning.map((r) => {
|
|
321
|
-
res[r.alias] = _d[r.alias];
|
|
322
|
-
});
|
|
323
|
-
return res;
|
|
324
|
-
});
|
|
325
|
-
|
|
326
|
-
returnData![targetTable] =
|
|
327
|
-
clientTargetInserts.length === 1 ? clientTargetInserts[0] : clientTargetInserts;
|
|
328
|
-
}
|
|
329
|
-
}),
|
|
330
|
-
);
|
|
331
|
-
|
|
332
|
-
return returnData;
|
|
88
|
+
const _data: (AnyObject | undefined)[] = [];
|
|
89
|
+
for (const { row, colInserts, extraKeys } of insertedRowsWithNestedData) {
|
|
90
|
+
if (hasNestedInserts) {
|
|
91
|
+
if (!dbTX) {
|
|
92
|
+
throw new Error("dbTX missing for nested insert");
|
|
333
93
|
}
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
94
|
+
const nestedInsertResult = await insertRowWithNestedRecords.bind(this)(
|
|
95
|
+
{
|
|
96
|
+
row,
|
|
97
|
+
colInserts,
|
|
98
|
+
extraKeys,
|
|
99
|
+
dbTX,
|
|
100
|
+
rootOnConflict,
|
|
101
|
+
},
|
|
102
|
+
{
|
|
103
|
+
localParams,
|
|
104
|
+
tableRules,
|
|
105
|
+
insertParams,
|
|
106
|
+
},
|
|
107
|
+
);
|
|
108
|
+
_data.push(nestedInsertResult);
|
|
109
|
+
} else {
|
|
110
|
+
_data.push(row);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
338
113
|
|
|
339
114
|
const result = isMultiInsert ? _data : _data[0];
|
|
340
115
|
const res = hasNestedInserts ? { insertResult: result } : { data: result };
|
|
341
116
|
|
|
342
117
|
return res;
|
|
343
118
|
}
|
|
344
|
-
|
|
345
|
-
/* Must be allowed to insert into referenced table */
|
|
346
|
-
export const getInsertTableRules = async (
|
|
347
|
-
tableHandler: TableHandler,
|
|
348
|
-
targetTable: string,
|
|
349
|
-
clientReq: AuthClientRequest | undefined,
|
|
350
|
-
scope: LocalParams["scope"],
|
|
351
|
-
) => {
|
|
352
|
-
const childRules = await tableHandler.dboBuilder.publishParser?.getValidatedRequestRuleWusr(
|
|
353
|
-
{
|
|
354
|
-
tableName: targetTable,
|
|
355
|
-
command: "insert",
|
|
356
|
-
clientReq,
|
|
357
|
-
},
|
|
358
|
-
scope,
|
|
359
|
-
);
|
|
360
|
-
if (!childRules || !childRules.insert)
|
|
361
|
-
throw "Dissallowed nested insert into table " + targetTable;
|
|
362
|
-
return childRules;
|
|
363
|
-
};
|
|
364
|
-
|
|
365
|
-
const getJoinPath = (
|
|
366
|
-
tableHandler: TableHandler,
|
|
367
|
-
targetTable: string,
|
|
368
|
-
): {
|
|
369
|
-
t1: string;
|
|
370
|
-
t2: string;
|
|
371
|
-
path: string[];
|
|
372
|
-
} => {
|
|
373
|
-
const jp = tableHandler.dboBuilder.getShortestJoinPath(tableHandler, targetTable);
|
|
374
|
-
if (!jp) {
|
|
375
|
-
const pref =
|
|
376
|
-
tableHandler.dboBuilder.prostgles.opts.joins !== "inferred" ? "Joins are not inferred! " : "";
|
|
377
|
-
throw new Error(
|
|
378
|
-
`${pref}Could not find a single join path for the nested data ( sourceTable: ${tableHandler.name} targetTable: ${targetTable} ) `,
|
|
379
|
-
);
|
|
380
|
-
}
|
|
381
|
-
return jp;
|
|
382
|
-
};
|
|
383
|
-
|
|
384
|
-
const referencedInsert = async (
|
|
385
|
-
tableHandler: TableHandler,
|
|
386
|
-
dbTX: TableHandlers | undefined,
|
|
387
|
-
localParams: LocalParams | undefined,
|
|
388
|
-
targetTable: string,
|
|
389
|
-
targetData: AnyObject | AnyObject[],
|
|
390
|
-
onConflict: Extract<InsertParams["onConflict"], string> | undefined,
|
|
391
|
-
): Promise<AnyObject[]> => {
|
|
392
|
-
getJoinPath(tableHandler, targetTable);
|
|
393
|
-
|
|
394
|
-
if (!dbTX?.[targetTable] || !("insert" in dbTX[targetTable])) {
|
|
395
|
-
throw new Error("childInsertErr: Table handler missing for referenced table: " + targetTable);
|
|
396
|
-
}
|
|
397
|
-
|
|
398
|
-
const childRules = await getInsertTableRules(
|
|
399
|
-
tableHandler,
|
|
400
|
-
targetTable,
|
|
401
|
-
localParams?.clientReq,
|
|
402
|
-
localParams?.scope,
|
|
403
|
-
);
|
|
404
|
-
|
|
405
|
-
return Promise.all(
|
|
406
|
-
((Array.isArray(targetData) ? targetData : [targetData]) as AnyObject[]).map((m) =>
|
|
407
|
-
(dbTX[targetTable] as TableHandler)
|
|
408
|
-
.insert(m, { returning: "*", onConflict }, undefined, childRules, localParams)
|
|
409
|
-
.catch((e) => {
|
|
410
|
-
return Promise.reject(e);
|
|
411
|
-
}),
|
|
412
|
-
),
|
|
413
|
-
) as Promise<AnyObject[]>;
|
|
414
|
-
};
|
|
415
|
-
|
|
416
|
-
type ReferenceColumnInsert<ExpectSingleInsert> = {
|
|
417
|
-
tableName: string;
|
|
418
|
-
insertedFieldName: string;
|
|
419
|
-
insertedFieldRef: Required<ColumnInfo>["references"][number];
|
|
420
|
-
singleInsert: boolean;
|
|
421
|
-
data: ExpectSingleInsert extends true ? AnyObject : AnyObject | AnyObject[];
|
|
422
|
-
};
|
|
423
|
-
|
|
424
|
-
/**
|
|
425
|
-
* Insert through the reference column. e.g.:
|
|
426
|
-
* {
|
|
427
|
-
* root_field: "data",
|
|
428
|
-
* fkey_column: { ...referenced_table_data }
|
|
429
|
-
* }
|
|
430
|
-
*/
|
|
431
|
-
export const getReferenceColumnInserts = <ExpectSingleInsert extends boolean>(
|
|
432
|
-
tableHandler: TableHandler,
|
|
433
|
-
parentRow: AnyObject,
|
|
434
|
-
expectSingleInsert?: ExpectSingleInsert,
|
|
435
|
-
): ReferenceColumnInsert<ExpectSingleInsert>[] => {
|
|
436
|
-
return Object.entries(parentRow)
|
|
437
|
-
.map(([insertedFieldName, insertedFieldValue]) => {
|
|
438
|
-
if (insertedFieldValue && isObject(insertedFieldValue)) {
|
|
439
|
-
const insertedRefCol = tableHandler.columns.find(
|
|
440
|
-
(c) => c.name === insertedFieldName && c.references?.length,
|
|
441
|
-
);
|
|
442
|
-
if (!insertedRefCol) return undefined;
|
|
443
|
-
const insertedFieldRef = getPossibleNestedInsert(
|
|
444
|
-
insertedRefCol,
|
|
445
|
-
tableHandler.dboBuilder.tablesOrViews ?? [],
|
|
446
|
-
false,
|
|
447
|
-
);
|
|
448
|
-
return (
|
|
449
|
-
insertedFieldRef && {
|
|
450
|
-
insertedFieldName,
|
|
451
|
-
insertedFieldRef,
|
|
452
|
-
}
|
|
453
|
-
);
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
return undefined;
|
|
457
|
-
})
|
|
458
|
-
.filter(isDefined)
|
|
459
|
-
.map(({ insertedFieldName, insertedFieldRef }) => {
|
|
460
|
-
const singleInsert = !Array.isArray(parentRow[insertedFieldName]);
|
|
461
|
-
if (expectSingleInsert && !singleInsert) {
|
|
462
|
-
throw "Expected singleInsert";
|
|
463
|
-
}
|
|
464
|
-
const res = {
|
|
465
|
-
tableName: insertedFieldRef.ftable,
|
|
466
|
-
insertedFieldName,
|
|
467
|
-
insertedFieldRef,
|
|
468
|
-
singleInsert,
|
|
469
|
-
data: parentRow[insertedFieldName] as typeof singleInsert extends true ? AnyObject
|
|
470
|
-
: AnyObject[],
|
|
471
|
-
};
|
|
472
|
-
return res;
|
|
473
|
-
})
|
|
474
|
-
.filter(isDefined);
|
|
475
|
-
};
|