graphile-bulk-mutations 1.19.11 → 1.19.13
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/esm/plugins/BulkDeletePlugin.js +1 -1
- package/esm/plugins/BulkInsertPlugin.js +10 -15
- package/esm/plugins/BulkTypesPlugin.js +4 -8
- package/esm/plugins/BulkUpdatePlugin.js +3 -15
- package/esm/plugins/BulkUpsertPlugin.js +6 -14
- package/esm/utils/returning.d.ts +11 -0
- package/esm/utils/returning.js +66 -0
- package/package.json +4 -4
- package/plugins/BulkDeletePlugin.js +1 -1
- package/plugins/BulkInsertPlugin.js +10 -15
- package/plugins/BulkTypesPlugin.js +4 -8
- package/plugins/BulkUpdatePlugin.js +3 -15
- package/plugins/BulkUpsertPlugin.js +6 -14
- package/utils/returning.d.ts +11 -0
- package/utils/returning.js +73 -0
|
@@ -159,7 +159,7 @@ export const BulkDeletePlugin = {
|
|
|
159
159
|
// Use RETURNING <pk_columns> instead of RETURNING *
|
|
160
160
|
// For delete, we capture PKs before rows are gone
|
|
161
161
|
const text = `DELETE FROM ${compiledFrom}\nWHERE ${whereStr}\nRETURNING ${pkReturning}`;
|
|
162
|
-
const mutationResult = await pgClient.query(text, values);
|
|
162
|
+
const mutationResult = await pgClient.query({ text, values });
|
|
163
163
|
const affectedCount = mutationResult.rowCount ?? 0;
|
|
164
164
|
// For delete, rows no longer exist so we can't do a
|
|
165
165
|
// follow-up SELECT. Return the PK values directly.
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import '../augmentations';
|
|
2
2
|
import { sideEffectWithPgClient } from '@dataplan/pg';
|
|
3
3
|
import { discoverNestedRelations } from '../utils/relations';
|
|
4
|
+
import { selectReturningRecords } from '../utils/returning';
|
|
4
5
|
import { buildBulkInsertSQL } from '../utils/sql-builder';
|
|
5
6
|
const version = '0.1.0';
|
|
6
7
|
/**
|
|
@@ -142,7 +143,10 @@ export const BulkInsertPlugin = {
|
|
|
142
143
|
let totalAffected = 0;
|
|
143
144
|
const allPkRows = [];
|
|
144
145
|
for (const batch of batches) {
|
|
145
|
-
const result = await pgClient.query(
|
|
146
|
+
const result = await pgClient.query({
|
|
147
|
+
text: batch.text,
|
|
148
|
+
values: batch.values
|
|
149
|
+
});
|
|
146
150
|
totalAffected += result.rowCount ?? 0;
|
|
147
151
|
if (result.rows) {
|
|
148
152
|
allPkRows.push(...result.rows);
|
|
@@ -177,26 +181,17 @@ export const BulkInsertPlugin = {
|
|
|
177
181
|
});
|
|
178
182
|
const childBatches = buildBulkInsertSQL(rel.childCompiledFrom, rel.childColumnSpecs, childRows, rel.childPkColumns);
|
|
179
183
|
for (const batch of childBatches) {
|
|
180
|
-
const result = await pgClient.query(
|
|
184
|
+
const result = await pgClient.query({
|
|
185
|
+
text: batch.text,
|
|
186
|
+
values: batch.values
|
|
187
|
+
});
|
|
181
188
|
totalAffected += result.rowCount ?? 0;
|
|
182
189
|
}
|
|
183
190
|
}
|
|
184
191
|
}
|
|
185
192
|
}
|
|
186
193
|
// Follow-up SELECT using PKs to respect column-level grants
|
|
187
|
-
|
|
188
|
-
if (allPkRows.length > 0) {
|
|
189
|
-
const pkConditions = allPkRows.map((pkRow, rowIdx) => {
|
|
190
|
-
return pkColumns.map((col, colIdx) => {
|
|
191
|
-
const paramIdx = rowIdx * pkColumns.length + colIdx + 1;
|
|
192
|
-
return `"${col}" = $${paramIdx}`;
|
|
193
|
-
}).join(' AND ');
|
|
194
|
-
});
|
|
195
|
-
const whereClause = pkConditions.map((c) => `(${c})`).join(' OR ');
|
|
196
|
-
const selectParams = allPkRows.flatMap((pkRow) => pkColumns.map((col) => pkRow[col]));
|
|
197
|
-
const selectResult = await pgClient.query(`SELECT * FROM ${compiledFrom} WHERE ${whereClause}`, selectParams);
|
|
198
|
-
returning = selectResult.rows || [];
|
|
199
|
-
}
|
|
194
|
+
const returning = await selectReturningRecords(pgClient, resource.from, resource.codec, pkColumns, allPkRows);
|
|
200
195
|
return {
|
|
201
196
|
affectedCount: totalAffected,
|
|
202
197
|
returning
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import '../augmentations';
|
|
2
|
-
import { pgSelectFromRecords } from '@dataplan/pg';
|
|
3
2
|
import { access } from 'grafast';
|
|
3
|
+
import { pgSelectReturningRecords } from '../utils/returning';
|
|
4
4
|
const version = '0.1.0';
|
|
5
5
|
/**
|
|
6
6
|
* Checks if a resource is suitable for bulk mutations:
|
|
@@ -293,9 +293,7 @@ export const BulkTypesPlugin = {
|
|
|
293
293
|
const filterTypeName = `${typeName}Filter`;
|
|
294
294
|
const filterType = build.getTypeByName(filterTypeName);
|
|
295
295
|
// Fall back to PostGraphile's built-in condition type
|
|
296
|
-
const
|
|
297
|
-
const conditionType = build.getTypeByName(conditionTypeName);
|
|
298
|
-
const whereType = filterType || conditionType;
|
|
296
|
+
const whereType = filterType || build.getTypeByName(inflection.conditionType(typeName));
|
|
299
297
|
return {
|
|
300
298
|
description: 'Condition to select which rows to update.',
|
|
301
299
|
type: whereType
|
|
@@ -318,9 +316,7 @@ export const BulkTypesPlugin = {
|
|
|
318
316
|
where: fieldWithHooks({ fieldName: 'where' }, () => {
|
|
319
317
|
const filterTypeName = `${typeName}Filter`;
|
|
320
318
|
const filterType = build.getTypeByName(filterTypeName);
|
|
321
|
-
const
|
|
322
|
-
const conditionType = build.getTypeByName(conditionTypeName);
|
|
323
|
-
const whereType = filterType || conditionType;
|
|
319
|
+
const whereType = filterType || build.getTypeByName(inflection.conditionType(typeName));
|
|
324
320
|
return {
|
|
325
321
|
description: 'Condition to select which rows to delete.',
|
|
326
322
|
type: whereType
|
|
@@ -359,7 +355,7 @@ export const BulkTypesPlugin = {
|
|
|
359
355
|
type: new build.graphql.GraphQLNonNull(new build.graphql.GraphQLList(new build.graphql.GraphQLNonNull(outputType))),
|
|
360
356
|
plan($parent) {
|
|
361
357
|
const $records = access($parent, 'returning');
|
|
362
|
-
return
|
|
358
|
+
return pgSelectReturningRecords(payloadResource, $records);
|
|
363
359
|
}
|
|
364
360
|
}));
|
|
365
361
|
}
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import '../augmentations';
|
|
2
2
|
import { sideEffectWithPgClient } from '@dataplan/pg';
|
|
3
|
+
import { selectReturningRecords } from '../utils/returning';
|
|
3
4
|
const version = '0.1.0';
|
|
4
5
|
/**
|
|
5
6
|
* BulkUpdatePlugin
|
|
@@ -177,23 +178,10 @@ export const BulkUpdatePlugin = {
|
|
|
177
178
|
: 'TRUE';
|
|
178
179
|
// Use RETURNING <pk_columns> instead of RETURNING *
|
|
179
180
|
const text = `UPDATE ${compiledFrom}\nSET ${setClauses.join(', ')}\nWHERE ${whereStr}\nRETURNING ${pkReturning}`;
|
|
180
|
-
const mutationResult = await pgClient.query(text, values);
|
|
181
|
+
const mutationResult = await pgClient.query({ text, values });
|
|
181
182
|
const affectedCount = mutationResult.rowCount ?? 0;
|
|
182
183
|
// Follow-up SELECT using PKs to respect column-level grants
|
|
183
|
-
|
|
184
|
-
if (mutationResult.rows && mutationResult.rows.length > 0) {
|
|
185
|
-
const pkRows = mutationResult.rows;
|
|
186
|
-
const pkConditions = pkRows.map((pkRow, rowIdx) => {
|
|
187
|
-
return pkColumns.map((col, colIdx) => {
|
|
188
|
-
const paramIdx = rowIdx * pkColumns.length + colIdx + 1;
|
|
189
|
-
return `"${col}" = $${paramIdx}`;
|
|
190
|
-
}).join(' AND ');
|
|
191
|
-
});
|
|
192
|
-
const selectWhere = pkConditions.map((c) => `(${c})`).join(' OR ');
|
|
193
|
-
const selectParams = pkRows.flatMap((pkRow) => pkColumns.map((col) => pkRow[col]));
|
|
194
|
-
const selectResult = await pgClient.query(`SELECT * FROM ${compiledFrom} WHERE ${selectWhere}`, selectParams);
|
|
195
|
-
returning = selectResult.rows || [];
|
|
196
|
-
}
|
|
184
|
+
const returning = await selectReturningRecords(pgClient, resource.from, resource.codec, pkColumns, mutationResult.rows || []);
|
|
197
185
|
return {
|
|
198
186
|
affectedCount,
|
|
199
187
|
returning
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import '../augmentations';
|
|
2
2
|
import { sideEffectWithPgClient } from '@dataplan/pg';
|
|
3
|
+
import { selectReturningRecords } from '../utils/returning';
|
|
3
4
|
import { buildBulkInsertSQL } from '../utils/sql-builder';
|
|
4
5
|
const version = '0.1.0';
|
|
5
6
|
/**
|
|
@@ -128,26 +129,17 @@ export const BulkUpsertPlugin = {
|
|
|
128
129
|
let totalAffected = 0;
|
|
129
130
|
const allPkRows = [];
|
|
130
131
|
for (const batch of batches) {
|
|
131
|
-
const result = await pgClient.query(
|
|
132
|
+
const result = await pgClient.query({
|
|
133
|
+
text: batch.text,
|
|
134
|
+
values: batch.values
|
|
135
|
+
});
|
|
132
136
|
totalAffected += result.rowCount ?? 0;
|
|
133
137
|
if (result.rows) {
|
|
134
138
|
allPkRows.push(...result.rows);
|
|
135
139
|
}
|
|
136
140
|
}
|
|
137
141
|
// Follow-up SELECT using PKs to respect column-level grants
|
|
138
|
-
|
|
139
|
-
if (allPkRows.length > 0) {
|
|
140
|
-
const pkConditions = allPkRows.map((pkRow, rowIdx) => {
|
|
141
|
-
return pkColumns.map((col, colIdx) => {
|
|
142
|
-
const paramIdx = rowIdx * pkColumns.length + colIdx + 1;
|
|
143
|
-
return `"${col}" = $${paramIdx}`;
|
|
144
|
-
}).join(' AND ');
|
|
145
|
-
});
|
|
146
|
-
const whereClause = pkConditions.map((c) => `(${c})`).join(' OR ');
|
|
147
|
-
const selectParams = allPkRows.flatMap((pkRow) => pkColumns.map((col) => pkRow[col]));
|
|
148
|
-
const selectResult = await pgClient.query(`SELECT * FROM ${compiledFrom} WHERE ${whereClause}`, selectParams);
|
|
149
|
-
returning = selectResult.rows || [];
|
|
150
|
-
}
|
|
142
|
+
const returning = await selectReturningRecords(pgClient, resource.from, resource.codec, pkColumns, allPkRows);
|
|
151
143
|
return {
|
|
152
144
|
affectedCount: totalAffected,
|
|
153
145
|
returning
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { PgClient, PgCodec, PgResource } from '@dataplan/pg';
|
|
2
|
+
import type { Step } from 'grafast';
|
|
3
|
+
import type { SQL } from 'pg-sql2';
|
|
4
|
+
export declare function selectReturningRecords(pgClient: PgClient, from: SQL, codec: PgCodec, pkColumns: readonly string[], pkRows: Record<string, unknown>[]): Promise<unknown[]>;
|
|
5
|
+
export declare function pgSelectReturningRecords(resource: PgResource, $records: Step): import("@dataplan/pg").PgSelectStep<PgResource<string, PgCodec<string, {
|
|
6
|
+
[x: string]: import("@dataplan/pg").PgCodecAttribute<PgCodec<string, /*elided*/ any, any, any, PgCodec<string, any, any, any, any, any, any>, PgCodec<string, any, any, any, any, any, any>, PgCodec<string, undefined, any, any, undefined, any, undefined>>, boolean>;
|
|
7
|
+
}, any, any, PgCodec<string, any, any, any, any, any, any>, PgCodec<string, any, any, any, any, any, any>, PgCodec<string, undefined, any, any, undefined, any, undefined>>, readonly import("@dataplan/pg").PgResourceUnique<{
|
|
8
|
+
[x: string]: import("@dataplan/pg").PgCodecAttribute<PgCodec<string, /*elided*/ any, any, any, PgCodec<string, any, any, any, any, any, any>, PgCodec<string, any, any, any, any, any, any>, PgCodec<string, undefined, any, any, undefined, any, undefined>>, boolean>;
|
|
9
|
+
}>[], readonly import("@dataplan/pg").PgResourceParameter<string, PgCodec<string, {
|
|
10
|
+
[x: string]: import("@dataplan/pg").PgCodecAttribute<PgCodec<string, /*elided*/ any, any, any, PgCodec<string, any, any, any, any, any, any>, PgCodec<string, any, any, any, any, any, any>, PgCodec<string, undefined, any, any, undefined, any, undefined>>, boolean>;
|
|
11
|
+
}, any, any, PgCodec<string, any, any, any, any, any, any>, PgCodec<string, any, any, any, any, any, any>, PgCodec<string, undefined, any, any, undefined, any, undefined>>>[], import("@dataplan/pg").PgRegistry<any, any, any, any>>>;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { pgSelect, TYPES } from '@dataplan/pg';
|
|
2
|
+
import { lambda } from 'grafast';
|
|
3
|
+
import sql from 'pg-sql2';
|
|
4
|
+
function returningAttributes(codec) {
|
|
5
|
+
return Object.entries(codec.attributes).filter(([, attribute]) => !attribute.expression && !attribute.via);
|
|
6
|
+
}
|
|
7
|
+
function jsonRecordValue(codec, value) {
|
|
8
|
+
while (codec.domainOfCodec)
|
|
9
|
+
codec = codec.domainOfCodec;
|
|
10
|
+
return sql.isEquivalent(codec.sqlType, TYPES.json.sqlType) || sql.isEquivalent(codec.sqlType, TYPES.jsonb.sqlType)
|
|
11
|
+
? sql `(${value})::json`
|
|
12
|
+
: sql `to_json(${value})`;
|
|
13
|
+
}
|
|
14
|
+
export async function selectReturningRecords(pgClient, from, codec, pkColumns, pkRows) {
|
|
15
|
+
if (pkRows.length === 0)
|
|
16
|
+
return [];
|
|
17
|
+
const alias = sql.identifier('__bulk_returning');
|
|
18
|
+
const attributes = returningAttributes(codec);
|
|
19
|
+
const columns = attributes.map(([name, attribute], index) => {
|
|
20
|
+
const column = sql `${alias}.${sql.identifier(name)}`;
|
|
21
|
+
const encoded = attribute.codec.castFromPg
|
|
22
|
+
? attribute.codec.castFromPg(column, !!attribute.notNull)
|
|
23
|
+
: sql `${column}::text`;
|
|
24
|
+
return sql `${encoded} AS ${sql.identifier(String(index))}`;
|
|
25
|
+
});
|
|
26
|
+
const conditions = pkRows.map((row) => sql `(${sql.join(pkColumns.map((column) => sql `${alias}.${sql.identifier(column)} = ${sql.value(row[column])}`), ' AND ')})`);
|
|
27
|
+
const result = await pgClient.query(sql.compile(sql `
|
|
28
|
+
SELECT ${sql.join(columns, ', ')}
|
|
29
|
+
FROM ${from} AS ${alias}
|
|
30
|
+
WHERE ${sql.join(conditions, ' OR ')}
|
|
31
|
+
`));
|
|
32
|
+
return result.rows.map((row) => Object.fromEntries(attributes.map(([name, attribute], index) => [
|
|
33
|
+
name,
|
|
34
|
+
row[index] == null ? null : attribute.codec.fromPg(row[index])
|
|
35
|
+
])));
|
|
36
|
+
}
|
|
37
|
+
export function pgSelectReturningRecords(resource, $records) {
|
|
38
|
+
const attributes = returningAttributes(resource.codec);
|
|
39
|
+
const $encoded = lambda($records, (records) => records.map((record) => attributes.map(([name, attribute]) => {
|
|
40
|
+
const value = record[name];
|
|
41
|
+
const encoded = value == null ? null : attribute.codec.toPg(value);
|
|
42
|
+
return encoded == null ? null : String(encoded);
|
|
43
|
+
})), true);
|
|
44
|
+
const alias = sql.identifier('__bulk_records');
|
|
45
|
+
const row = sql `${alias}."value"`;
|
|
46
|
+
const fields = sql.identifier('__bulk_attributes');
|
|
47
|
+
const entries = attributes.map(([name, attribute], index) => sql `(
|
|
48
|
+
${sql.literal(name)},
|
|
49
|
+
${jsonRecordValue(attribute.codec, sql `${row}->>${sql.literal(index)}`)}
|
|
50
|
+
)`);
|
|
51
|
+
return pgSelect({
|
|
52
|
+
resource,
|
|
53
|
+
identifiers: [],
|
|
54
|
+
args: [{ step: $encoded, pgCodec: TYPES.json }],
|
|
55
|
+
from: (records) => sql `json_populate_recordset(
|
|
56
|
+
NULL::${resource.codec.sqlType},
|
|
57
|
+
(
|
|
58
|
+
SELECT coalesce(json_agg((
|
|
59
|
+
SELECT json_object_agg(${fields}."key", ${fields}."value")
|
|
60
|
+
FROM (VALUES ${sql.join(entries, ', ')}) AS ${fields}("key", "value")
|
|
61
|
+
)), '[]'::json)
|
|
62
|
+
FROM json_array_elements(${records.placeholder}) AS ${alias}("value")
|
|
63
|
+
)
|
|
64
|
+
)`
|
|
65
|
+
});
|
|
66
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "graphile-bulk-mutations",
|
|
3
|
-
"version": "1.19.
|
|
3
|
+
"version": "1.19.13",
|
|
4
4
|
"description": "PostGraphile v5 plugin for bulk insert, upsert, update, and delete mutations with ON CONFLICT handling",
|
|
5
5
|
"author": "Constructive <developers@constructive.io>",
|
|
6
6
|
"homepage": "https://github.com/constructive-io/constructive",
|
|
@@ -43,9 +43,9 @@
|
|
|
43
43
|
},
|
|
44
44
|
"devDependencies": {
|
|
45
45
|
"@types/node": "^22.19.11",
|
|
46
|
-
"graphile-test": "^5.19.
|
|
46
|
+
"graphile-test": "^5.19.12",
|
|
47
47
|
"makage": "^0.8.0",
|
|
48
|
-
"pgsql-test": "^5.20.
|
|
48
|
+
"pgsql-test": "^5.20.11"
|
|
49
49
|
},
|
|
50
50
|
"peerDependencies": {
|
|
51
51
|
"@dataplan/pg": "^1.1.1",
|
|
@@ -57,5 +57,5 @@
|
|
|
57
57
|
"pg-sql2": "^5.0.1",
|
|
58
58
|
"postgraphile": "^5.1.3"
|
|
59
59
|
},
|
|
60
|
-
"gitHead": "
|
|
60
|
+
"gitHead": "e9d6aa9a9cf617880c3f94336e57e71cb1385947"
|
|
61
61
|
}
|
|
@@ -162,7 +162,7 @@ exports.BulkDeletePlugin = {
|
|
|
162
162
|
// Use RETURNING <pk_columns> instead of RETURNING *
|
|
163
163
|
// For delete, we capture PKs before rows are gone
|
|
164
164
|
const text = `DELETE FROM ${compiledFrom}\nWHERE ${whereStr}\nRETURNING ${pkReturning}`;
|
|
165
|
-
const mutationResult = await pgClient.query(text, values);
|
|
165
|
+
const mutationResult = await pgClient.query({ text, values });
|
|
166
166
|
const affectedCount = mutationResult.rowCount ?? 0;
|
|
167
167
|
// For delete, rows no longer exist so we can't do a
|
|
168
168
|
// follow-up SELECT. Return the PK values directly.
|
|
@@ -4,6 +4,7 @@ exports.BulkInsertPlugin = void 0;
|
|
|
4
4
|
require("../augmentations");
|
|
5
5
|
const pg_1 = require("@dataplan/pg");
|
|
6
6
|
const relations_1 = require("../utils/relations");
|
|
7
|
+
const returning_1 = require("../utils/returning");
|
|
7
8
|
const sql_builder_1 = require("../utils/sql-builder");
|
|
8
9
|
const version = '0.1.0';
|
|
9
10
|
/**
|
|
@@ -145,7 +146,10 @@ exports.BulkInsertPlugin = {
|
|
|
145
146
|
let totalAffected = 0;
|
|
146
147
|
const allPkRows = [];
|
|
147
148
|
for (const batch of batches) {
|
|
148
|
-
const result = await pgClient.query(
|
|
149
|
+
const result = await pgClient.query({
|
|
150
|
+
text: batch.text,
|
|
151
|
+
values: batch.values
|
|
152
|
+
});
|
|
149
153
|
totalAffected += result.rowCount ?? 0;
|
|
150
154
|
if (result.rows) {
|
|
151
155
|
allPkRows.push(...result.rows);
|
|
@@ -180,26 +184,17 @@ exports.BulkInsertPlugin = {
|
|
|
180
184
|
});
|
|
181
185
|
const childBatches = (0, sql_builder_1.buildBulkInsertSQL)(rel.childCompiledFrom, rel.childColumnSpecs, childRows, rel.childPkColumns);
|
|
182
186
|
for (const batch of childBatches) {
|
|
183
|
-
const result = await pgClient.query(
|
|
187
|
+
const result = await pgClient.query({
|
|
188
|
+
text: batch.text,
|
|
189
|
+
values: batch.values
|
|
190
|
+
});
|
|
184
191
|
totalAffected += result.rowCount ?? 0;
|
|
185
192
|
}
|
|
186
193
|
}
|
|
187
194
|
}
|
|
188
195
|
}
|
|
189
196
|
// Follow-up SELECT using PKs to respect column-level grants
|
|
190
|
-
|
|
191
|
-
if (allPkRows.length > 0) {
|
|
192
|
-
const pkConditions = allPkRows.map((pkRow, rowIdx) => {
|
|
193
|
-
return pkColumns.map((col, colIdx) => {
|
|
194
|
-
const paramIdx = rowIdx * pkColumns.length + colIdx + 1;
|
|
195
|
-
return `"${col}" = $${paramIdx}`;
|
|
196
|
-
}).join(' AND ');
|
|
197
|
-
});
|
|
198
|
-
const whereClause = pkConditions.map((c) => `(${c})`).join(' OR ');
|
|
199
|
-
const selectParams = allPkRows.flatMap((pkRow) => pkColumns.map((col) => pkRow[col]));
|
|
200
|
-
const selectResult = await pgClient.query(`SELECT * FROM ${compiledFrom} WHERE ${whereClause}`, selectParams);
|
|
201
|
-
returning = selectResult.rows || [];
|
|
202
|
-
}
|
|
197
|
+
const returning = await (0, returning_1.selectReturningRecords)(pgClient, resource.from, resource.codec, pkColumns, allPkRows);
|
|
203
198
|
return {
|
|
204
199
|
affectedCount: totalAffected,
|
|
205
200
|
returning
|
|
@@ -2,8 +2,8 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.BulkTypesPlugin = void 0;
|
|
4
4
|
require("../augmentations");
|
|
5
|
-
const pg_1 = require("@dataplan/pg");
|
|
6
5
|
const grafast_1 = require("grafast");
|
|
6
|
+
const returning_1 = require("../utils/returning");
|
|
7
7
|
const version = '0.1.0';
|
|
8
8
|
/**
|
|
9
9
|
* Checks if a resource is suitable for bulk mutations:
|
|
@@ -296,9 +296,7 @@ exports.BulkTypesPlugin = {
|
|
|
296
296
|
const filterTypeName = `${typeName}Filter`;
|
|
297
297
|
const filterType = build.getTypeByName(filterTypeName);
|
|
298
298
|
// Fall back to PostGraphile's built-in condition type
|
|
299
|
-
const
|
|
300
|
-
const conditionType = build.getTypeByName(conditionTypeName);
|
|
301
|
-
const whereType = filterType || conditionType;
|
|
299
|
+
const whereType = filterType || build.getTypeByName(inflection.conditionType(typeName));
|
|
302
300
|
return {
|
|
303
301
|
description: 'Condition to select which rows to update.',
|
|
304
302
|
type: whereType
|
|
@@ -321,9 +319,7 @@ exports.BulkTypesPlugin = {
|
|
|
321
319
|
where: fieldWithHooks({ fieldName: 'where' }, () => {
|
|
322
320
|
const filterTypeName = `${typeName}Filter`;
|
|
323
321
|
const filterType = build.getTypeByName(filterTypeName);
|
|
324
|
-
const
|
|
325
|
-
const conditionType = build.getTypeByName(conditionTypeName);
|
|
326
|
-
const whereType = filterType || conditionType;
|
|
322
|
+
const whereType = filterType || build.getTypeByName(inflection.conditionType(typeName));
|
|
327
323
|
return {
|
|
328
324
|
description: 'Condition to select which rows to delete.',
|
|
329
325
|
type: whereType
|
|
@@ -362,7 +358,7 @@ exports.BulkTypesPlugin = {
|
|
|
362
358
|
type: new build.graphql.GraphQLNonNull(new build.graphql.GraphQLList(new build.graphql.GraphQLNonNull(outputType))),
|
|
363
359
|
plan($parent) {
|
|
364
360
|
const $records = (0, grafast_1.access)($parent, 'returning');
|
|
365
|
-
return (0,
|
|
361
|
+
return (0, returning_1.pgSelectReturningRecords)(payloadResource, $records);
|
|
366
362
|
}
|
|
367
363
|
}));
|
|
368
364
|
}
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.BulkUpdatePlugin = void 0;
|
|
4
4
|
require("../augmentations");
|
|
5
5
|
const pg_1 = require("@dataplan/pg");
|
|
6
|
+
const returning_1 = require("../utils/returning");
|
|
6
7
|
const version = '0.1.0';
|
|
7
8
|
/**
|
|
8
9
|
* BulkUpdatePlugin
|
|
@@ -180,23 +181,10 @@ exports.BulkUpdatePlugin = {
|
|
|
180
181
|
: 'TRUE';
|
|
181
182
|
// Use RETURNING <pk_columns> instead of RETURNING *
|
|
182
183
|
const text = `UPDATE ${compiledFrom}\nSET ${setClauses.join(', ')}\nWHERE ${whereStr}\nRETURNING ${pkReturning}`;
|
|
183
|
-
const mutationResult = await pgClient.query(text, values);
|
|
184
|
+
const mutationResult = await pgClient.query({ text, values });
|
|
184
185
|
const affectedCount = mutationResult.rowCount ?? 0;
|
|
185
186
|
// Follow-up SELECT using PKs to respect column-level grants
|
|
186
|
-
|
|
187
|
-
if (mutationResult.rows && mutationResult.rows.length > 0) {
|
|
188
|
-
const pkRows = mutationResult.rows;
|
|
189
|
-
const pkConditions = pkRows.map((pkRow, rowIdx) => {
|
|
190
|
-
return pkColumns.map((col, colIdx) => {
|
|
191
|
-
const paramIdx = rowIdx * pkColumns.length + colIdx + 1;
|
|
192
|
-
return `"${col}" = $${paramIdx}`;
|
|
193
|
-
}).join(' AND ');
|
|
194
|
-
});
|
|
195
|
-
const selectWhere = pkConditions.map((c) => `(${c})`).join(' OR ');
|
|
196
|
-
const selectParams = pkRows.flatMap((pkRow) => pkColumns.map((col) => pkRow[col]));
|
|
197
|
-
const selectResult = await pgClient.query(`SELECT * FROM ${compiledFrom} WHERE ${selectWhere}`, selectParams);
|
|
198
|
-
returning = selectResult.rows || [];
|
|
199
|
-
}
|
|
187
|
+
const returning = await (0, returning_1.selectReturningRecords)(pgClient, resource.from, resource.codec, pkColumns, mutationResult.rows || []);
|
|
200
188
|
return {
|
|
201
189
|
affectedCount,
|
|
202
190
|
returning
|
|
@@ -3,6 +3,7 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
3
3
|
exports.BulkUpsertPlugin = void 0;
|
|
4
4
|
require("../augmentations");
|
|
5
5
|
const pg_1 = require("@dataplan/pg");
|
|
6
|
+
const returning_1 = require("../utils/returning");
|
|
6
7
|
const sql_builder_1 = require("../utils/sql-builder");
|
|
7
8
|
const version = '0.1.0';
|
|
8
9
|
/**
|
|
@@ -131,26 +132,17 @@ exports.BulkUpsertPlugin = {
|
|
|
131
132
|
let totalAffected = 0;
|
|
132
133
|
const allPkRows = [];
|
|
133
134
|
for (const batch of batches) {
|
|
134
|
-
const result = await pgClient.query(
|
|
135
|
+
const result = await pgClient.query({
|
|
136
|
+
text: batch.text,
|
|
137
|
+
values: batch.values
|
|
138
|
+
});
|
|
135
139
|
totalAffected += result.rowCount ?? 0;
|
|
136
140
|
if (result.rows) {
|
|
137
141
|
allPkRows.push(...result.rows);
|
|
138
142
|
}
|
|
139
143
|
}
|
|
140
144
|
// Follow-up SELECT using PKs to respect column-level grants
|
|
141
|
-
|
|
142
|
-
if (allPkRows.length > 0) {
|
|
143
|
-
const pkConditions = allPkRows.map((pkRow, rowIdx) => {
|
|
144
|
-
return pkColumns.map((col, colIdx) => {
|
|
145
|
-
const paramIdx = rowIdx * pkColumns.length + colIdx + 1;
|
|
146
|
-
return `"${col}" = $${paramIdx}`;
|
|
147
|
-
}).join(' AND ');
|
|
148
|
-
});
|
|
149
|
-
const whereClause = pkConditions.map((c) => `(${c})`).join(' OR ');
|
|
150
|
-
const selectParams = allPkRows.flatMap((pkRow) => pkColumns.map((col) => pkRow[col]));
|
|
151
|
-
const selectResult = await pgClient.query(`SELECT * FROM ${compiledFrom} WHERE ${whereClause}`, selectParams);
|
|
152
|
-
returning = selectResult.rows || [];
|
|
153
|
-
}
|
|
145
|
+
const returning = await (0, returning_1.selectReturningRecords)(pgClient, resource.from, resource.codec, pkColumns, allPkRows);
|
|
154
146
|
return {
|
|
155
147
|
affectedCount: totalAffected,
|
|
156
148
|
returning
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { PgClient, PgCodec, PgResource } from '@dataplan/pg';
|
|
2
|
+
import type { Step } from 'grafast';
|
|
3
|
+
import type { SQL } from 'pg-sql2';
|
|
4
|
+
export declare function selectReturningRecords(pgClient: PgClient, from: SQL, codec: PgCodec, pkColumns: readonly string[], pkRows: Record<string, unknown>[]): Promise<unknown[]>;
|
|
5
|
+
export declare function pgSelectReturningRecords(resource: PgResource, $records: Step): import("@dataplan/pg").PgSelectStep<PgResource<string, PgCodec<string, {
|
|
6
|
+
[x: string]: import("@dataplan/pg").PgCodecAttribute<PgCodec<string, /*elided*/ any, any, any, PgCodec<string, any, any, any, any, any, any>, PgCodec<string, any, any, any, any, any, any>, PgCodec<string, undefined, any, any, undefined, any, undefined>>, boolean>;
|
|
7
|
+
}, any, any, PgCodec<string, any, any, any, any, any, any>, PgCodec<string, any, any, any, any, any, any>, PgCodec<string, undefined, any, any, undefined, any, undefined>>, readonly import("@dataplan/pg").PgResourceUnique<{
|
|
8
|
+
[x: string]: import("@dataplan/pg").PgCodecAttribute<PgCodec<string, /*elided*/ any, any, any, PgCodec<string, any, any, any, any, any, any>, PgCodec<string, any, any, any, any, any, any>, PgCodec<string, undefined, any, any, undefined, any, undefined>>, boolean>;
|
|
9
|
+
}>[], readonly import("@dataplan/pg").PgResourceParameter<string, PgCodec<string, {
|
|
10
|
+
[x: string]: import("@dataplan/pg").PgCodecAttribute<PgCodec<string, /*elided*/ any, any, any, PgCodec<string, any, any, any, any, any, any>, PgCodec<string, any, any, any, any, any, any>, PgCodec<string, undefined, any, any, undefined, any, undefined>>, boolean>;
|
|
11
|
+
}, any, any, PgCodec<string, any, any, any, any, any, any>, PgCodec<string, any, any, any, any, any, any>, PgCodec<string, undefined, any, any, undefined, any, undefined>>>[], import("@dataplan/pg").PgRegistry<any, any, any, any>>>;
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.selectReturningRecords = selectReturningRecords;
|
|
7
|
+
exports.pgSelectReturningRecords = pgSelectReturningRecords;
|
|
8
|
+
const pg_1 = require("@dataplan/pg");
|
|
9
|
+
const grafast_1 = require("grafast");
|
|
10
|
+
const pg_sql2_1 = __importDefault(require("pg-sql2"));
|
|
11
|
+
function returningAttributes(codec) {
|
|
12
|
+
return Object.entries(codec.attributes).filter(([, attribute]) => !attribute.expression && !attribute.via);
|
|
13
|
+
}
|
|
14
|
+
function jsonRecordValue(codec, value) {
|
|
15
|
+
while (codec.domainOfCodec)
|
|
16
|
+
codec = codec.domainOfCodec;
|
|
17
|
+
return pg_sql2_1.default.isEquivalent(codec.sqlType, pg_1.TYPES.json.sqlType) || pg_sql2_1.default.isEquivalent(codec.sqlType, pg_1.TYPES.jsonb.sqlType)
|
|
18
|
+
? (0, pg_sql2_1.default) `(${value})::json`
|
|
19
|
+
: (0, pg_sql2_1.default) `to_json(${value})`;
|
|
20
|
+
}
|
|
21
|
+
async function selectReturningRecords(pgClient, from, codec, pkColumns, pkRows) {
|
|
22
|
+
if (pkRows.length === 0)
|
|
23
|
+
return [];
|
|
24
|
+
const alias = pg_sql2_1.default.identifier('__bulk_returning');
|
|
25
|
+
const attributes = returningAttributes(codec);
|
|
26
|
+
const columns = attributes.map(([name, attribute], index) => {
|
|
27
|
+
const column = (0, pg_sql2_1.default) `${alias}.${pg_sql2_1.default.identifier(name)}`;
|
|
28
|
+
const encoded = attribute.codec.castFromPg
|
|
29
|
+
? attribute.codec.castFromPg(column, !!attribute.notNull)
|
|
30
|
+
: (0, pg_sql2_1.default) `${column}::text`;
|
|
31
|
+
return (0, pg_sql2_1.default) `${encoded} AS ${pg_sql2_1.default.identifier(String(index))}`;
|
|
32
|
+
});
|
|
33
|
+
const conditions = pkRows.map((row) => (0, pg_sql2_1.default) `(${pg_sql2_1.default.join(pkColumns.map((column) => (0, pg_sql2_1.default) `${alias}.${pg_sql2_1.default.identifier(column)} = ${pg_sql2_1.default.value(row[column])}`), ' AND ')})`);
|
|
34
|
+
const result = await pgClient.query(pg_sql2_1.default.compile((0, pg_sql2_1.default) `
|
|
35
|
+
SELECT ${pg_sql2_1.default.join(columns, ', ')}
|
|
36
|
+
FROM ${from} AS ${alias}
|
|
37
|
+
WHERE ${pg_sql2_1.default.join(conditions, ' OR ')}
|
|
38
|
+
`));
|
|
39
|
+
return result.rows.map((row) => Object.fromEntries(attributes.map(([name, attribute], index) => [
|
|
40
|
+
name,
|
|
41
|
+
row[index] == null ? null : attribute.codec.fromPg(row[index])
|
|
42
|
+
])));
|
|
43
|
+
}
|
|
44
|
+
function pgSelectReturningRecords(resource, $records) {
|
|
45
|
+
const attributes = returningAttributes(resource.codec);
|
|
46
|
+
const $encoded = (0, grafast_1.lambda)($records, (records) => records.map((record) => attributes.map(([name, attribute]) => {
|
|
47
|
+
const value = record[name];
|
|
48
|
+
const encoded = value == null ? null : attribute.codec.toPg(value);
|
|
49
|
+
return encoded == null ? null : String(encoded);
|
|
50
|
+
})), true);
|
|
51
|
+
const alias = pg_sql2_1.default.identifier('__bulk_records');
|
|
52
|
+
const row = (0, pg_sql2_1.default) `${alias}."value"`;
|
|
53
|
+
const fields = pg_sql2_1.default.identifier('__bulk_attributes');
|
|
54
|
+
const entries = attributes.map(([name, attribute], index) => (0, pg_sql2_1.default) `(
|
|
55
|
+
${pg_sql2_1.default.literal(name)},
|
|
56
|
+
${jsonRecordValue(attribute.codec, (0, pg_sql2_1.default) `${row}->>${pg_sql2_1.default.literal(index)}`)}
|
|
57
|
+
)`);
|
|
58
|
+
return (0, pg_1.pgSelect)({
|
|
59
|
+
resource,
|
|
60
|
+
identifiers: [],
|
|
61
|
+
args: [{ step: $encoded, pgCodec: pg_1.TYPES.json }],
|
|
62
|
+
from: (records) => (0, pg_sql2_1.default) `json_populate_recordset(
|
|
63
|
+
NULL::${resource.codec.sqlType},
|
|
64
|
+
(
|
|
65
|
+
SELECT coalesce(json_agg((
|
|
66
|
+
SELECT json_object_agg(${fields}."key", ${fields}."value")
|
|
67
|
+
FROM (VALUES ${pg_sql2_1.default.join(entries, ', ')}) AS ${fields}("key", "value")
|
|
68
|
+
)), '[]'::json)
|
|
69
|
+
FROM json_array_elements(${records.placeholder}) AS ${alias}("value")
|
|
70
|
+
)
|
|
71
|
+
)`
|
|
72
|
+
});
|
|
73
|
+
}
|