joist-orm 2.3.0-next.5 → 2.3.0-next.51
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/build/_virtual/_rolldown/runtime.cjs +23 -0
- package/build/_virtual/_rolldown/runtime.js +5 -0
- package/build/codegen.cjs +19 -0
- package/build/codegen.cjs.map +1 -0
- package/build/codegen.d.cts +1 -0
- package/build/codegen.d.mts +1 -0
- package/build/codegen.js +12 -28
- package/build/codegen.js.map +1 -1
- package/build/drivers/PostgresDriver.cjs +294 -0
- package/build/drivers/PostgresDriver.cjs.map +1 -0
- package/build/drivers/PostgresDriver.d.cts +80 -0
- package/build/drivers/PostgresDriver.d.cts.map +1 -0
- package/build/drivers/PostgresDriver.d.mts +80 -0
- package/build/drivers/PostgresDriver.d.mts.map +1 -0
- package/build/drivers/PostgresDriver.js +257 -274
- package/build/drivers/PostgresDriver.js.map +1 -1
- package/build/drivers/WireRowData.cjs +374 -0
- package/build/drivers/WireRowData.cjs.map +1 -0
- package/build/drivers/WireRowData.d.cts +111 -0
- package/build/drivers/WireRowData.d.cts.map +1 -0
- package/build/drivers/WireRowData.d.mts +111 -0
- package/build/drivers/WireRowData.d.mts.map +1 -0
- package/build/drivers/WireRowData.js +370 -0
- package/build/drivers/WireRowData.js.map +1 -0
- package/build/drivers/binaryParsers.cjs +422 -0
- package/build/drivers/binaryParsers.cjs.map +1 -0
- package/build/drivers/binaryParsers.d.cts +80 -0
- package/build/drivers/binaryParsers.d.cts.map +1 -0
- package/build/drivers/binaryParsers.d.mts +80 -0
- package/build/drivers/binaryParsers.d.mts.map +1 -0
- package/build/drivers/binaryParsers.js +412 -0
- package/build/drivers/binaryParsers.js.map +1 -0
- package/build/drivers/patchPgProtocol.cjs +255 -0
- package/build/drivers/patchPgProtocol.cjs.map +1 -0
- package/build/drivers/patchPgProtocol.d.cts +47 -0
- package/build/drivers/patchPgProtocol.d.cts.map +1 -0
- package/build/drivers/patchPgProtocol.d.mts +47 -0
- package/build/drivers/patchPgProtocol.d.mts.map +1 -0
- package/build/drivers/patchPgProtocol.js +253 -0
- package/build/drivers/patchPgProtocol.js.map +1 -0
- package/build/graphql-codegen-export.cjs +10 -0
- package/build/graphql-codegen-export.d.cts +1 -0
- package/build/graphql-codegen-export.d.mts +1 -0
- package/build/graphql-codegen-export.js +2 -19
- package/build/graphql-export.cjs +10 -0
- package/build/graphql-export.d.cts +1 -0
- package/build/graphql-export.d.mts +1 -0
- package/build/graphql-export.js +2 -19
- package/build/index.cjs +41 -0
- package/build/index.d.cts +3 -0
- package/build/index.d.mts +3 -0
- package/build/index.js +3 -27
- package/build/knex-export.cjs +10 -0
- package/build/knex-export.d.cts +1 -0
- package/build/knex-export.d.mts +1 -0
- package/build/knex-export.js +2 -19
- package/build/pg-export.cjs +13 -0
- package/build/pg-export.d.cts +4 -0
- package/build/pg-export.d.mts +4 -0
- package/build/pg-export.js +4 -9
- package/build/pg-migrate.cjs +22 -0
- package/build/pg-migrate.cjs.map +1 -0
- package/build/pg-migrate.d.cts +1 -0
- package/build/pg-migrate.d.mts +1 -0
- package/build/pg-migrate.js +14 -28
- package/build/pg-migrate.js.map +1 -1
- package/build/seed.cjs +49 -0
- package/build/seed.cjs.map +1 -0
- package/build/{seed.d.ts → seed.d.cts} +5 -2
- package/build/seed.d.cts.map +1 -0
- package/build/seed.d.mts +24 -0
- package/build/seed.d.mts.map +1 -0
- package/build/seed.js +39 -45
- package/build/seed.js.map +1 -1
- package/build/tests-export.cjs +10 -0
- package/build/tests-export.d.cts +1 -0
- package/build/tests-export.d.mts +1 -0
- package/build/tests-export.js +2 -19
- package/package.json +154 -38
- package/build/codegen.d.ts +0 -2
- package/build/codegen.d.ts.map +0 -1
- package/build/drivers/PostgresDriver.d.ts +0 -49
- package/build/drivers/PostgresDriver.d.ts.map +0 -1
- package/build/graphql-codegen-export.d.ts +0 -2
- package/build/graphql-codegen-export.d.ts.map +0 -1
- package/build/graphql-codegen-export.js.map +0 -1
- package/build/graphql-export.d.ts +0 -2
- package/build/graphql-export.d.ts.map +0 -1
- package/build/graphql-export.js.map +0 -1
- package/build/index.d.ts +0 -4
- package/build/index.d.ts.map +0 -1
- package/build/index.js.map +0 -1
- package/build/knex-export.d.ts +0 -2
- package/build/knex-export.d.ts.map +0 -1
- package/build/knex-export.js.map +0 -1
- package/build/pg-export.d.ts +0 -3
- package/build/pg-export.d.ts.map +0 -1
- package/build/pg-export.js.map +0 -1
- package/build/pg-migrate.d.ts +0 -2
- package/build/pg-migrate.d.ts.map +0 -1
- package/build/seed.d.ts.map +0 -1
- package/build/tests-export.d.ts +0 -2
- package/build/tests-export.d.ts.map +0 -1
- package/build/tests-export.js.map +0 -1
|
@@ -1,306 +1,289 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
};
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
const pg_1 = __importDefault(require("pg"));
|
|
10
|
-
const pg_types_1 = require("pg-types");
|
|
11
|
-
const postgres_array_1 = __importDefault(require("postgres-array"));
|
|
1
|
+
import { registerDatabaseBinaryParsers, registerTemporalBinaryParsers } from "./binaryParsers.js";
|
|
2
|
+
import { ensureLazyDataRows } from "./patchPgProtocol.js";
|
|
3
|
+
import { executeRowDataQuery, isRowDataCapableClient } from "./WireRowData.js";
|
|
4
|
+
import { JoinRowOperation, PojoRowData, SequenceIdAssigner, buildRawQuery, cleanSql, driverAfterBegin, driverAfterCommit, driverBeforeBegin, driverBeforeCommit, ensureRectangularArraySizes, fail, generateOps, getRuntimeConfig, kq, kqDot, partition } from "joist-core";
|
|
5
|
+
import pg from "pg";
|
|
6
|
+
import { builtins, getTypeParser } from "pg-types";
|
|
7
|
+
import array from "postgres-array";
|
|
8
|
+
//#region src/drivers/PostgresDriver.ts
|
|
12
9
|
/**
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
}
|
|
114
|
-
|
|
10
|
+
* Implements the `Driver` interface for Postgres.
|
|
11
|
+
*
|
|
12
|
+
* This is the canonical driver implementation and leverages several aspects of
|
|
13
|
+
* Postgres for the best performance, i.e.:
|
|
14
|
+
*
|
|
15
|
+
* - Deferred foreign key constraints are used to allow bulk inserting/updating
|
|
16
|
+
* all entities without any topographic sorting/ordering issues.
|
|
17
|
+
*
|
|
18
|
+
* - Sequences are used to bulk-assign + then bulk-insert all entities, to again
|
|
19
|
+
* avoid issues with ordering issues (cannot bulk insert A w/o knowing the id of B).
|
|
20
|
+
*
|
|
21
|
+
* - We use a pg-specific bulk update syntax.
|
|
22
|
+
*
|
|
23
|
+
* Query pipelining is enabled for clients created after driver construction unless the pool was
|
|
24
|
+
* configured with `pipeline: false`.
|
|
25
|
+
*/
|
|
26
|
+
var PostgresDriver = class {
|
|
27
|
+
pool;
|
|
28
|
+
#idAssigner;
|
|
29
|
+
#preloadPlugin;
|
|
30
|
+
#onQuery;
|
|
31
|
+
lazyRows;
|
|
32
|
+
/** Defined only when `lazyRows` is enabled; its presence is the capability signal the EM checks. */
|
|
33
|
+
executeFindRowData;
|
|
34
|
+
#databaseBinaryParsers = void 0;
|
|
35
|
+
constructor(pool, opts) {
|
|
36
|
+
this.pool = pool;
|
|
37
|
+
pool.options.pipeline ??= true;
|
|
38
|
+
this.#idAssigner = opts?.idAssigner ?? new SequenceIdAssigner(async (sql) => {
|
|
39
|
+
this.#onQuery?.(sql);
|
|
40
|
+
return (await pool.query(sql)).rows;
|
|
41
|
+
});
|
|
42
|
+
this.#preloadPlugin = opts?.preloadPlugin;
|
|
43
|
+
this.#onQuery = opts?.onQuery;
|
|
44
|
+
this.lazyRows = (opts?.lazyRows ?? false) && ensureLazyDataRows();
|
|
45
|
+
if (opts?.lazyRows && !this.lazyRows) console.warn("joist-orm: lazyRows was requested, but patching pg-protocol failed; using classic rows.");
|
|
46
|
+
this.executeFindRowData = this.lazyRows ? (em, parsed, settings) => this.#executeFindRowData(em, parsed, settings) : void 0;
|
|
47
|
+
setupLatestPgTypes(getRuntimeConfig().temporal);
|
|
48
|
+
}
|
|
49
|
+
async executeFind(em, parsed, settings) {
|
|
50
|
+
const { sql, bindings } = buildRawQuery(parsed, {
|
|
51
|
+
limit: em.entityLimit,
|
|
52
|
+
...settings
|
|
53
|
+
});
|
|
54
|
+
return this.executeQuery(em, sql, bindings);
|
|
55
|
+
}
|
|
56
|
+
async #executeFindRowData(em, parsed, settings) {
|
|
57
|
+
await (this.#databaseBinaryParsers ??= registerDatabaseBinaryParsers(this.pool).catch((err) => {
|
|
58
|
+
this.#databaseBinaryParsers = void 0;
|
|
59
|
+
throw err;
|
|
60
|
+
}));
|
|
61
|
+
const { sql, bindings } = buildRawQuery(parsed, {
|
|
62
|
+
limit: em.entityLimit,
|
|
63
|
+
...settings
|
|
64
|
+
});
|
|
65
|
+
const pgSql = toPgParams(sql);
|
|
66
|
+
this.#onQuery?.(pgSql);
|
|
67
|
+
const txnClient = em.txn;
|
|
68
|
+
const client = txnClient ?? await this.pool.connect();
|
|
69
|
+
try {
|
|
70
|
+
if (!isRowDataCapableClient(client)) {
|
|
71
|
+
warnUnsupportedClientOnce();
|
|
72
|
+
return new PojoRowData((await client.query(pgSql, bindings)).rows);
|
|
73
|
+
}
|
|
74
|
+
return await executeRowDataQuery(client, pgSql, bindings);
|
|
75
|
+
} finally {
|
|
76
|
+
if (!txnClient) client.release();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
async executeQuery(em, sql, bindings) {
|
|
80
|
+
const pgSql = toPgParams(sql);
|
|
81
|
+
this.#onQuery?.(pgSql);
|
|
82
|
+
return this.getMaybeInTxnClient(em).query(pgSql, bindings).then((result) => result.rows);
|
|
83
|
+
}
|
|
84
|
+
async transaction(em, fn) {
|
|
85
|
+
if (em.txn) return fn(em.txn);
|
|
86
|
+
const client = await this.pool.connect();
|
|
87
|
+
let result;
|
|
88
|
+
try {
|
|
89
|
+
await driverBeforeBegin(em, client);
|
|
90
|
+
this.#onQuery?.("BEGIN;");
|
|
91
|
+
await client.query("BEGIN");
|
|
92
|
+
em.txn = client;
|
|
93
|
+
try {
|
|
94
|
+
await driverAfterBegin(em, client);
|
|
95
|
+
result = await fn(client);
|
|
96
|
+
await driverBeforeCommit(em, client);
|
|
97
|
+
this.#onQuery?.("COMMIT;");
|
|
98
|
+
await client.query("COMMIT");
|
|
99
|
+
} catch (e) {
|
|
100
|
+
await client.query("ROLLBACK");
|
|
101
|
+
throw e;
|
|
102
|
+
} finally {
|
|
103
|
+
em.txn = void 0;
|
|
104
|
+
}
|
|
105
|
+
await driverAfterCommit(em, client);
|
|
106
|
+
} finally {
|
|
107
|
+
client.release();
|
|
108
|
+
}
|
|
109
|
+
return result;
|
|
110
|
+
}
|
|
111
|
+
async assignNewIds(_, todos) {
|
|
112
|
+
return this.#idAssigner.assignNewIds(todos);
|
|
113
|
+
}
|
|
114
|
+
async flush(em, entityTodos, joinRows) {
|
|
115
|
+
const client = em.txn ?? fail("Expected EntityManager.txn to be set");
|
|
116
|
+
await this.#idAssigner.assignNewIds(entityTodos);
|
|
117
|
+
const ops = generateOps(entityTodos);
|
|
118
|
+
const onQuery = this.#onQuery;
|
|
119
|
+
await Promise.all([
|
|
120
|
+
...ops.inserts.map((op) => batchInsert(client, op, onQuery)),
|
|
121
|
+
...ops.updates.map((op) => batchUpdate(client, op, onQuery)),
|
|
122
|
+
...ops.deletes.map((op) => batchDelete(client, op, onQuery)),
|
|
123
|
+
...Object.entries(joinRows).flatMap(([joinTableName, todo]) => {
|
|
124
|
+
return [m2mBatchInsert(client, joinTableName, todo, onQuery), m2mBatchDelete(client, joinTableName, todo, onQuery)];
|
|
125
|
+
})
|
|
126
|
+
]);
|
|
127
|
+
}
|
|
128
|
+
get defaultPlugins() {
|
|
129
|
+
return { preloadPlugin: this.#preloadPlugin };
|
|
130
|
+
}
|
|
131
|
+
getMaybeInTxnClient(em) {
|
|
132
|
+
return em.txn || this.pool;
|
|
133
|
+
}
|
|
134
|
+
};
|
|
115
135
|
async function batchInsert(client, op, onQuery) {
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
136
|
+
const { tableName, columns, columnValues } = op;
|
|
137
|
+
const [cte, bindings] = buildUnnestCte("data", columns, columnValues);
|
|
138
|
+
const pgSql = toPgParams(cleanSql(`
|
|
119
139
|
${cte}
|
|
120
|
-
INSERT INTO ${
|
|
140
|
+
INSERT INTO ${kq(tableName)} (${columns.map((c) => kq(c.columnName)).join(", ")})
|
|
121
141
|
SELECT * FROM data
|
|
122
|
-
`);
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
return client.query(pgSql, bindings);
|
|
142
|
+
`));
|
|
143
|
+
onQuery?.(pgSql);
|
|
144
|
+
return client.query(pgSql, bindings);
|
|
126
145
|
}
|
|
127
146
|
async function batchUpdate(client, op, onQuery) {
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
//
|
|
134
|
-
// ...but Temporal's types don't have this flaw.
|
|
135
|
-
const truncateToMills = !(0, joist_core_1.getRuntimeConfig)().temporal;
|
|
136
|
-
const maybeUpdatedAt = updatedAt && truncateToMills
|
|
137
|
-
? ` AND date_trunc('milliseconds', ${(0, joist_core_1.kqDot)(tableName, updatedAt)}) = data.__original_updated_at`
|
|
138
|
-
: updatedAt
|
|
139
|
-
? ` AND ${(0, joist_core_1.kqDot)(tableName, updatedAt)} = data.__original_updated_at`
|
|
140
|
-
: "";
|
|
141
|
-
const sql = `
|
|
147
|
+
const { tableName, columns, columnValues, updatedAt } = op;
|
|
148
|
+
const [cte, bindings] = buildUnnestCte("data", columns, columnValues);
|
|
149
|
+
const truncateToMills = !getRuntimeConfig().temporal;
|
|
150
|
+
const maybeUpdatedAt = updatedAt && truncateToMills ? ` AND date_trunc('milliseconds', ${kqDot(tableName, updatedAt)}) = data.__original_updated_at` : updatedAt ? ` AND ${kqDot(tableName, updatedAt)} = data.__original_updated_at` : "";
|
|
151
|
+
const sql = `
|
|
142
152
|
${cte}
|
|
143
|
-
UPDATE ${
|
|
144
|
-
SET ${columns
|
|
145
|
-
.filter((c) => c.columnName !== "id" && c.columnName !== "__original_updated_at")
|
|
146
|
-
.map((c) => `${(0, joist_core_1.kq)(c.columnName)} = data.${(0, joist_core_1.kq)(c.columnName)}`)
|
|
147
|
-
.join(", ")}
|
|
153
|
+
UPDATE ${kq(tableName)}
|
|
154
|
+
SET ${columns.filter((c) => c.columnName !== "id" && c.columnName !== "__original_updated_at").map((c) => `${kq(c.columnName)} = data.${kq(c.columnName)}`).join(", ")}
|
|
148
155
|
FROM data
|
|
149
|
-
WHERE ${
|
|
150
|
-
RETURNING ${
|
|
156
|
+
WHERE ${kq(tableName)}.id = data.id ${maybeUpdatedAt}
|
|
157
|
+
RETURNING ${kq(tableName)}.id
|
|
151
158
|
`;
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
}
|
|
159
|
+
const pgSql = toPgParams(cleanSql(sql));
|
|
160
|
+
onQuery?.(pgSql);
|
|
161
|
+
const results = (await client.query(pgSql, bindings)).rows;
|
|
162
|
+
const ids = columnValues[0];
|
|
163
|
+
if (results.length !== ids.length) {
|
|
164
|
+
const updated = new Set(results.map((r) => r.id));
|
|
165
|
+
const missing = ids.filter((id) => !updated.has(id));
|
|
166
|
+
throw new Error(`Oplock failure for ${tableName} rows ${missing.join(", ")}`);
|
|
167
|
+
}
|
|
162
168
|
}
|
|
163
169
|
async function batchDelete(client, op, onQuery) {
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
170
|
+
const { tableName, ids } = op;
|
|
171
|
+
const pgSql = toPgParams(`DELETE FROM ${kq(tableName)} WHERE id = ANY(?)`);
|
|
172
|
+
onQuery?.(pgSql);
|
|
173
|
+
await client.query(pgSql, [ids]);
|
|
168
174
|
}
|
|
169
175
|
/** Creates a CTE named `tableName` that bulk-injects the `columnValues` into a SQL query. */
|
|
170
176
|
function buildUnnestCte(tableName, columns, columnValues) {
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
return `unnest_arrays(?::${c.dbType}[]) as ${(0, joist_core_1.kq)(c.columnName)}`;
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
else {
|
|
182
|
-
return `unnest(?::${c.dbType}[]) as ${(0, joist_core_1.kq)(c.columnName)}`;
|
|
183
|
-
}
|
|
184
|
-
});
|
|
185
|
-
const sql = `WITH ${tableName} AS (SELECT ${selects.join(", ")})`;
|
|
186
|
-
return [sql, columnValues];
|
|
177
|
+
ensureRectangularArraySizes(columns, columnValues);
|
|
178
|
+
return [`WITH ${tableName} AS (SELECT ${columns.map((c) => {
|
|
179
|
+
if (c.dbType.endsWith("[]")) {
|
|
180
|
+
if (c.isNullableArray) return `unnest_arrays(?::${c.dbType}[], true) as ${kq(c.columnName)}`;
|
|
181
|
+
else return `unnest_arrays(?::${c.dbType}[]) as ${kq(c.columnName)}`;
|
|
182
|
+
} else return `unnest(?::${c.dbType}[]) as ${kq(c.columnName)}`;
|
|
183
|
+
}).join(", ")})`, columnValues];
|
|
187
184
|
}
|
|
188
185
|
async function m2mBatchInsert(client, joinTableName, todo, onQuery) {
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
INSERT INTO ${(0, joist_core_1.kq)(joinTableName)} (${(0, joist_core_1.kq)(m2m.columnName)}, ${(0, joist_core_1.kq)(m2m.otherColumnName)})
|
|
186
|
+
const { m2m, newRows } = todo;
|
|
187
|
+
if (newRows.length === 0) return;
|
|
188
|
+
const col1Values = newRows.map((row) => todo.dbValue(row, m2m.columnName));
|
|
189
|
+
const col2Values = newRows.map((row) => todo.dbValue(row, m2m.otherColumnName));
|
|
190
|
+
if (m2m.hasJoinTableId) {
|
|
191
|
+
const pgSql = toPgParams(cleanSql(`
|
|
192
|
+
WITH data AS (SELECT unnest(?::int[]) as ${kq(m2m.columnName)}, unnest(?::int[]) as ${kq(m2m.otherColumnName)})
|
|
193
|
+
INSERT INTO ${kq(joinTableName)} (${kq(m2m.columnName)}, ${kq(m2m.otherColumnName)})
|
|
198
194
|
SELECT * FROM data
|
|
199
|
-
ON CONFLICT (${
|
|
195
|
+
ON CONFLICT (${kq(m2m.columnName)}, ${kq(m2m.otherColumnName)}) DO UPDATE SET id = ${kq(joinTableName)}.id
|
|
200
196
|
RETURNING id;
|
|
201
|
-
`);
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
// insert and let any duplicate be a no-op.
|
|
214
|
-
const sql = (0, joist_core_1.cleanSql)(`
|
|
215
|
-
WITH data AS (SELECT unnest(?::int[]) as ${(0, joist_core_1.kq)(m2m.columnName)}, unnest(?::int[]) as ${(0, joist_core_1.kq)(m2m.otherColumnName)})
|
|
216
|
-
INSERT INTO ${(0, joist_core_1.kq)(joinTableName)} (${(0, joist_core_1.kq)(m2m.columnName)}, ${(0, joist_core_1.kq)(m2m.otherColumnName)})
|
|
197
|
+
`));
|
|
198
|
+
onQuery?.(pgSql);
|
|
199
|
+
const { rows } = await client.query(pgSql, [col1Values, col2Values]);
|
|
200
|
+
for (let i = 0; i < rows.length; i++) {
|
|
201
|
+
newRows[i].id = rows[i].id;
|
|
202
|
+
newRows[i].op = JoinRowOperation.Flushed;
|
|
203
|
+
newRows[i].persisted = true;
|
|
204
|
+
}
|
|
205
|
+
} else {
|
|
206
|
+
const pgSql = toPgParams(cleanSql(`
|
|
207
|
+
WITH data AS (SELECT unnest(?::int[]) as ${kq(m2m.columnName)}, unnest(?::int[]) as ${kq(m2m.otherColumnName)})
|
|
208
|
+
INSERT INTO ${kq(joinTableName)} (${kq(m2m.columnName)}, ${kq(m2m.otherColumnName)})
|
|
217
209
|
SELECT * FROM data
|
|
218
|
-
ON CONFLICT (${
|
|
219
|
-
`);
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
}
|
|
210
|
+
ON CONFLICT (${kq(m2m.columnName)}, ${kq(m2m.otherColumnName)}) DO NOTHING;
|
|
211
|
+
`));
|
|
212
|
+
onQuery?.(pgSql);
|
|
213
|
+
await client.query(pgSql, [col1Values, col2Values]);
|
|
214
|
+
for (const row of newRows) {
|
|
215
|
+
row.op = JoinRowOperation.Flushed;
|
|
216
|
+
row.persisted = true;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
228
219
|
}
|
|
229
220
|
async function m2mBatchDelete(client, joinTableName, todo, onQuery) {
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
const pgSql = toPgParams(`DELETE FROM ${(0, joist_core_1.kq)(joinTableName)} WHERE id = ANY(?)`);
|
|
247
|
-
onQuery?.(pgSql);
|
|
248
|
-
await client.query(pgSql, [haveIds.map((r) => r.id)]);
|
|
249
|
-
}
|
|
250
|
-
if (noIds.length > 0) {
|
|
251
|
-
const data = noIds
|
|
252
|
-
// Watch for m2m rows that got added-then-removed to entities that were themselves added-then-removed,
|
|
253
|
-
// as the deTagId will be undefined for those, as we're skipping adding them to the database.
|
|
254
|
-
.filter((row) => !todo.isNew(row, m2m.columnName) && !todo.isNew(row, m2m.otherColumnName))
|
|
255
|
-
.map((row) => [todo.dbValue(row, m2m.columnName), todo.dbValue(row, m2m.otherColumnName)]);
|
|
256
|
-
if (data.length > 0) {
|
|
257
|
-
const pgSql = toPgParams(`
|
|
258
|
-
DELETE FROM ${(0, joist_core_1.kq)(joinTableName)}
|
|
259
|
-
WHERE (${(0, joist_core_1.kq)(m2m.columnName)}, ${(0, joist_core_1.kq)(m2m.otherColumnName)}) IN (
|
|
221
|
+
const { m2m, deletedRows } = todo;
|
|
222
|
+
if (deletedRows.length === 0) return;
|
|
223
|
+
const [haveIds, noIds] = partition(deletedRows, (r) => {
|
|
224
|
+
return r.id !== void 0 && r.id !== -1;
|
|
225
|
+
});
|
|
226
|
+
if (haveIds.length > 0) {
|
|
227
|
+
const pgSql = toPgParams(`DELETE FROM ${kq(joinTableName)} WHERE id = ANY(?)`);
|
|
228
|
+
onQuery?.(pgSql);
|
|
229
|
+
await client.query(pgSql, [haveIds.map((r) => r.id)]);
|
|
230
|
+
}
|
|
231
|
+
if (noIds.length > 0) {
|
|
232
|
+
const data = noIds.filter((row) => !todo.isNew(row, m2m.columnName) && !todo.isNew(row, m2m.otherColumnName)).map((row) => [todo.dbValue(row, m2m.columnName), todo.dbValue(row, m2m.otherColumnName)]);
|
|
233
|
+
if (data.length > 0) {
|
|
234
|
+
const pgSql = toPgParams(`
|
|
235
|
+
DELETE FROM ${kq(joinTableName)}
|
|
236
|
+
WHERE (${kq(m2m.columnName)}, ${kq(m2m.otherColumnName)}) IN (
|
|
260
237
|
SELECT (data->>0)::int, (data->>1)::int FROM jsonb_array_elements(?) data
|
|
261
238
|
)`);
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
239
|
+
onQuery?.(pgSql);
|
|
240
|
+
await client.query(pgSql, [JSON.stringify(data)]);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
deletedRows.forEach((row) => {
|
|
244
|
+
row.id = void 0;
|
|
245
|
+
row.persisted = false;
|
|
246
|
+
row.op = JoinRowOperation.Flushed;
|
|
247
|
+
});
|
|
271
248
|
}
|
|
272
249
|
/**
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
250
|
+
* Configures the `pg` driver with the best type parsers.
|
|
251
|
+
*
|
|
252
|
+
* In particular, pg's default `TIMESTAMPTZ` parser is very inefficient, and even just
|
|
253
|
+
* pulling in the latest `pg-types` and installing the fixed parser makes a noticable
|
|
254
|
+
* difference.
|
|
255
|
+
*/
|
|
279
256
|
function setupLatestPgTypes(temporal) {
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
}
|
|
293
|
-
else {
|
|
294
|
-
pg_1.default.types.setTypeParser(pg_1.default.types.builtins.TIMESTAMPTZ, (0, pg_types_1.getTypeParser)(pg_types_1.builtins.TIMESTAMPTZ));
|
|
295
|
-
}
|
|
257
|
+
if (temporal) {
|
|
258
|
+
const noop = (s) => s;
|
|
259
|
+
const noopArray = (s) => array.parse(s, noop);
|
|
260
|
+
const { TIMESTAMP, TIMESTAMPTZ, DATE } = pg.types.builtins;
|
|
261
|
+
pg.types.setTypeParser(DATE, noop);
|
|
262
|
+
pg.types.setTypeParser(TIMESTAMP, noop);
|
|
263
|
+
pg.types.setTypeParser(TIMESTAMPTZ, noop);
|
|
264
|
+
pg.types.setTypeParser(1182, noopArray);
|
|
265
|
+
pg.types.setTypeParser(1115, noopArray);
|
|
266
|
+
pg.types.setTypeParser(1185, noopArray);
|
|
267
|
+
registerTemporalBinaryParsers();
|
|
268
|
+
} else pg.types.setTypeParser(pg.types.builtins.TIMESTAMPTZ, getTypeParser(builtins.TIMESTAMPTZ));
|
|
296
269
|
}
|
|
297
270
|
const questionMarks = /(?<!@)\?/g;
|
|
298
271
|
/**
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
272
|
+
* Converts our knex-style `?` placeholders to pg-native `$1, $2, ...` numbered parameters.
|
|
273
|
+
*
|
|
274
|
+
* We skipping postgres operators like `@?`. */
|
|
302
275
|
function toPgParams(sql) {
|
|
303
|
-
|
|
304
|
-
|
|
276
|
+
let i = 0;
|
|
277
|
+
return sql.replace(questionMarks, () => `$${++i}`);
|
|
278
|
+
}
|
|
279
|
+
let warnedUnsupportedClient = false;
|
|
280
|
+
/** Warns once when lazyRows degrades to classic rows for an unsupported client, i.e. pg-native. */
|
|
281
|
+
function warnUnsupportedClientOnce() {
|
|
282
|
+
if (warnedUnsupportedClient) return;
|
|
283
|
+
warnedUnsupportedClient = true;
|
|
284
|
+
console.warn("joist-orm: lazyRows is enabled, but this client does not support it; using classic rows.");
|
|
305
285
|
}
|
|
286
|
+
//#endregion
|
|
287
|
+
export { PostgresDriver, setupLatestPgTypes };
|
|
288
|
+
|
|
306
289
|
//# sourceMappingURL=PostgresDriver.js.map
|