@prisma-next/adapter-postgres 0.16.0-dev.24 → 0.16.0-dev.26
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/{adapter-CuTsfsz3.mjs → adapter-DlDJIHgB.mjs} +2 -2
- package/dist/{adapter-CuTsfsz3.mjs.map → adapter-DlDJIHgB.mjs.map} +1 -1
- package/dist/adapter.mjs +1 -1
- package/dist/{control-adapter-Xog0HsuP.mjs → control-adapter-DKbwTazN.mjs} +81 -29
- package/dist/control-adapter-DKbwTazN.mjs.map +1 -0
- package/dist/control.d.mts.map +1 -1
- package/dist/control.mjs +2 -2
- package/dist/{descriptor-meta-DpA4aG-x.mjs → descriptor-meta-pc7bA0_C.mjs} +2 -2
- package/dist/{descriptor-meta-DpA4aG-x.mjs.map → descriptor-meta-pc7bA0_C.mjs.map} +1 -1
- package/dist/runtime.mjs +2 -2
- package/package.json +23 -23
- package/src/core/control-adapter.ts +118 -59
- package/dist/control-adapter-Xog0HsuP.mjs.map +0 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { i as adapterError, n as renderLoweredSql, r as createPostgresBuiltinCodecLookup, t as PostgresControlAdapter } from "./control-adapter-
|
|
1
|
+
import { i as adapterError, n as renderLoweredSql, r as createPostgresBuiltinCodecLookup, t as PostgresControlAdapter } from "./control-adapter-DKbwTazN.mjs";
|
|
2
2
|
import { APP_SPACE_ID } from "@prisma-next/framework-components/control";
|
|
3
3
|
import { isDdlNode } from "@prisma-next/sql-relational-core/ast";
|
|
4
4
|
//#region src/core/adapter.ts
|
|
@@ -63,4 +63,4 @@ function createPostgresAdapter(options) {
|
|
|
63
63
|
//#endregion
|
|
64
64
|
export { postgresRawCodecInferer as n, createPostgresAdapter as t };
|
|
65
65
|
|
|
66
|
-
//# sourceMappingURL=adapter-
|
|
66
|
+
//# sourceMappingURL=adapter-DlDJIHgB.mjs.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"adapter-
|
|
1
|
+
{"version":3,"file":"adapter-DlDJIHgB.mjs","names":[],"sources":["../src/core/adapter.ts"],"sourcesContent":["import type { CodecRegistry } from '@prisma-next/framework-components/codec';\nimport { APP_SPACE_ID } from '@prisma-next/framework-components/control';\nimport type {\n Adapter,\n AdapterProfile,\n AnyQueryAst,\n LowererContext,\n RawSqlLiteral,\n SqlQueryable,\n} from '@prisma-next/sql-relational-core/ast';\nimport { isDdlNode } from '@prisma-next/sql-relational-core/ast';\nimport type { RawCodecInferer } from '@prisma-next/sql-relational-core/expression';\nimport type { PostgresDdlNode } from '@prisma-next/target-postgres/ddl';\nimport { adapterError } from './adapter-errors';\nimport { createPostgresBuiltinCodecLookup } from './codec-lookup';\nimport { PostgresControlAdapter } from './control-adapter';\nimport { renderLoweredSql } from './sql-renderer';\nimport type { PostgresAdapterOptions, PostgresContract, PostgresLoweredStatement } from './types';\n\nconst defaultCapabilities = Object.freeze({\n postgres: {\n orderBy: true,\n limit: true,\n lateral: true,\n jsonAgg: true,\n returning: true,\n distinctOn: true,\n },\n sql: {\n enums: true,\n returning: true,\n defaultInInsert: true,\n lateral: true,\n scalarList: true,\n },\n});\n\nclass PostgresAdapterImpl\n implements Adapter<AnyQueryAst, PostgresContract, PostgresLoweredStatement>\n{\n // These fields make the adapter instance structurally compatible with RuntimeAdapterInstance<'sql', 'postgres'> without introducing a runtime-plane dependency.\n readonly familyId = 'sql' as const;\n readonly targetId = 'postgres' as const;\n\n readonly profile: AdapterProfile<'postgres'>;\n private readonly codecLookup: CodecRegistry;\n\n constructor(options?: PostgresAdapterOptions) {\n this.codecLookup = options?.codecLookup ?? createPostgresBuiltinCodecLookup();\n const controlAdapter = new PostgresControlAdapter(this.codecLookup);\n this.profile = Object.freeze({\n id: options?.profileId ?? 'postgres/default@1',\n target: 'postgres',\n capabilities: defaultCapabilities,\n readMarker: (queryable: SqlQueryable) =>\n controlAdapter.readMarkerDiscriminated(\n {\n familyId: 'sql',\n targetId: 'postgres',\n query: async <Row = Record<string, unknown>>(\n sql: string,\n params?: readonly unknown[],\n ) => {\n const result = await queryable.query<Row>(sql, params);\n return { rows: [...result.rows] };\n },\n close: async () => {},\n },\n APP_SPACE_ID,\n ),\n });\n }\n\n lower(\n ast: AnyQueryAst | PostgresDdlNode,\n context: LowererContext<PostgresContract>,\n ): PostgresLoweredStatement {\n if (isDdlNode(ast)) {\n throw adapterError(\n 'RUNTIME.DDL_UNSUPPORTED',\n 'lower() does not lower DDL on the runtime adapter — DDL lowering is a control-plane concern handled by the control adapter.',\n { meta: { surface: 'runtime-adapter' } },\n );\n }\n return renderLoweredSql(ast, context.contract, this.codecLookup);\n }\n}\n\n/** Codec-id lookup for bare-literal interpolations used by `fns.raw` on a postgres client. Contributed as the descriptor's static `rawCodecInferer` slot. */\nexport const postgresRawCodecInferer: RawCodecInferer = {\n inferCodec(value: RawSqlLiteral): string {\n switch (typeof value) {\n case 'number':\n return Number.isSafeInteger(value) && value % 1 === 0 ? 'pg/int4' : 'pg/float8';\n case 'bigint':\n return 'pg/int8';\n case 'string':\n return 'pg/text';\n case 'boolean':\n return 'pg/bool';\n case 'object':\n if (value instanceof Uint8Array) return 'pg/bytea';\n }\n throw adapterError(\n 'RUNTIME.RAW_SQL_UNSUPPORTED_INTERPOLATION',\n 'unsupported JS value type for raw-SQL interpolation: wrap this value in `param(...)` with an explicit codec',\n { meta: { valueType: typeof value } },\n );\n },\n};\n\nexport function createPostgresAdapter(options?: PostgresAdapterOptions) {\n return Object.freeze(new PostgresAdapterImpl(options));\n}\n"],"mappings":";;;;AAmBA,MAAM,sBAAsB,OAAO,OAAO;CACxC,UAAU;EACR,SAAS;EACT,OAAO;EACP,SAAS;EACT,SAAS;EACT,WAAW;EACX,YAAY;CACd;CACA,KAAK;EACH,OAAO;EACP,WAAW;EACX,iBAAiB;EACjB,SAAS;EACT,YAAY;CACd;AACF,CAAC;AAED,IAAM,sBAAN,MAEA;CAEE,WAAoB;CACpB,WAAoB;CAEpB;CACA;CAEA,YAAY,SAAkC;EAC5C,KAAK,cAAc,SAAS,eAAe,iCAAiC;EAC5E,MAAM,iBAAiB,IAAI,uBAAuB,KAAK,WAAW;EAClE,KAAK,UAAU,OAAO,OAAO;GAC3B,IAAI,SAAS,aAAa;GAC1B,QAAQ;GACR,cAAc;GACd,aAAa,cACX,eAAe,wBACb;IACE,UAAU;IACV,UAAU;IACV,OAAO,OACL,KACA,WACG;KAEH,OAAO,EAAE,MAAM,CAAC,IAAG,MADE,UAAU,MAAW,KAAK,MAAM,EAAA,CAC3B,IAAI,EAAE;IAClC;IACA,OAAO,YAAY,CAAC;GACtB,GACA,YACF;EACJ,CAAC;CACH;CAEA,MACE,KACA,SAC0B;EAC1B,IAAI,UAAU,GAAG,GACf,MAAM,aACJ,2BACA,+HACA,EAAE,MAAM,EAAE,SAAS,kBAAkB,EAAE,CACzC;EAEF,OAAO,iBAAiB,KAAK,QAAQ,UAAU,KAAK,WAAW;CACjE;AACF;;AAGA,MAAa,0BAA2C,EACtD,WAAW,OAA8B;CACvC,QAAQ,OAAO,OAAf;EACE,KAAK,UACH,OAAO,OAAO,cAAc,KAAK,KAAK,QAAQ,MAAM,IAAI,YAAY;EACtE,KAAK,UACH,OAAO;EACT,KAAK,UACH,OAAO;EACT,KAAK,WACH,OAAO;EACT,KAAK,UACH,IAAI,iBAAiB,YAAY,OAAO;CAC5C;CACA,MAAM,aACJ,6CACA,+GACA,EAAE,MAAM,EAAE,WAAW,OAAO,MAAM,EAAE,CACtC;AACF,EACF;AAEA,SAAgB,sBAAsB,SAAkC;CACtE,OAAO,OAAO,OAAO,IAAI,oBAAoB,OAAO,CAAC;AACvD"}
|
package/dist/adapter.mjs
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
import { n as postgresRawCodecInferer, t as createPostgresAdapter } from "./adapter-
|
|
1
|
+
import { n as postgresRawCodecInferer, t as createPostgresAdapter } from "./adapter-DlDJIHgB.mjs";
|
|
2
2
|
export { createPostgresAdapter, postgresRawCodecInferer };
|
|
@@ -6,11 +6,12 @@ import { parseMarkerRowSafely, rethrowMarkerReadError, withMarkerReadErrorHandli
|
|
|
6
6
|
import { parseContractMarkerRow } from "@prisma-next/family-sql/verify";
|
|
7
7
|
import { UNBOUND_NAMESPACE_ID } from "@prisma-next/framework-components/ir";
|
|
8
8
|
import { REFERENTIAL_ACTION_SQL } from "@prisma-next/sql-contract/referential-action-sql";
|
|
9
|
+
import { parseWireName } from "@prisma-next/sql-schema-ir/naming";
|
|
9
10
|
import { RelationalSchemaNodeKind } from "@prisma-next/sql-schema-ir/types";
|
|
10
11
|
import { buildControlTableBootstrapQueries, buildSignMarkerBootstrapQueries, int4, int8, jsonb, pgTable, text, textArray, timestamptz } from "@prisma-next/target-postgres/contract-free";
|
|
11
12
|
import { parsePostgresDefault } from "@prisma-next/target-postgres/default-normalizer";
|
|
13
|
+
import { postgresError } from "@prisma-next/target-postgres/errors";
|
|
12
14
|
import { normalizeSchemaNativeType } from "@prisma-next/target-postgres/native-type-normalizer";
|
|
13
|
-
import { parseRlsPolicyWireName } from "@prisma-next/target-postgres/rls-canonicalize";
|
|
14
15
|
import { escapeLiteral, quoteIdentifier, quoteQualifiedName } from "@prisma-next/target-postgres/sql-utils";
|
|
15
16
|
import { PostgresDatabaseSchemaNode, PostgresNamespaceSchemaNode, PostgresNativeEnumSchemaNode, PostgresPolicySchemaNode, PostgresRoleSchemaNode, PostgresSchemaNodeKind, PostgresTableSchemaNode } from "@prisma-next/target-postgres/types";
|
|
16
17
|
import { blindCast } from "@prisma-next/utils/casts";
|
|
@@ -1131,8 +1132,9 @@ var PostgresControlAdapter = class {
|
|
|
1131
1132
|
i.tablename,
|
|
1132
1133
|
i.indexname,
|
|
1133
1134
|
ix.indisunique,
|
|
1134
|
-
ix.indpred
|
|
1135
|
+
pg_get_expr(ix.indpred, ix.indrelid) AS where_predicate,
|
|
1135
1136
|
a.attname,
|
|
1137
|
+
CASE WHEN 0 = ANY(ix.indkey::int[]) THEN pg_get_indexdef(ix.indexrelid, k.ord::int, true) END AS element_def,
|
|
1136
1138
|
k.ord AS index_position,
|
|
1137
1139
|
am.amname,
|
|
1138
1140
|
ic.reloptions
|
|
@@ -1260,44 +1262,50 @@ var PostgresControlAdapter = class {
|
|
|
1260
1262
|
dependsOn: postgresColumnDependsOn(schema, tableName, uq.columns)
|
|
1261
1263
|
}));
|
|
1262
1264
|
const indexesMap = /* @__PURE__ */ new Map();
|
|
1263
|
-
const indexNamesWithExpressionKey = /* @__PURE__ */ new Set();
|
|
1264
1265
|
for (const idxRow of indexesByTable.get(tableName) ?? []) {
|
|
1265
|
-
if (!idxRow.attname) {
|
|
1266
|
-
indexNamesWithExpressionKey.add(idxRow.indexname);
|
|
1267
|
-
continue;
|
|
1268
|
-
}
|
|
1269
1266
|
const existing = indexesMap.get(idxRow.indexname);
|
|
1270
|
-
if (existing) existing.
|
|
1267
|
+
if (existing) existing.elements.push({
|
|
1268
|
+
attname: idxRow.attname,
|
|
1269
|
+
elementDef: idxRow.element_def
|
|
1270
|
+
});
|
|
1271
1271
|
else {
|
|
1272
1272
|
const indexType = idxRow.amname && idxRow.amname !== "btree" ? idxRow.amname : void 0;
|
|
1273
1273
|
const indexOptions = parsePgReloptions(idxRow.reloptions, idxRow.indexname);
|
|
1274
1274
|
indexesMap.set(idxRow.indexname, {
|
|
1275
|
-
columns: [idxRow.attname],
|
|
1276
1275
|
name: idxRow.indexname,
|
|
1276
|
+
elements: [{
|
|
1277
|
+
attname: idxRow.attname,
|
|
1278
|
+
elementDef: idxRow.element_def
|
|
1279
|
+
}],
|
|
1277
1280
|
unique: idxRow.indisunique,
|
|
1278
|
-
|
|
1281
|
+
where: idxRow.where_predicate,
|
|
1279
1282
|
type: indexType,
|
|
1280
1283
|
options: indexOptions
|
|
1281
1284
|
});
|
|
1282
1285
|
}
|
|
1283
1286
|
}
|
|
1284
|
-
const
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
const
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1287
|
+
const indexes = Array.from(indexesMap.values()).map((idx) => {
|
|
1288
|
+
const isExpression = idx.elements.some((el) => el.attname === null);
|
|
1289
|
+
const columnNames = idx.elements.flatMap((el) => el.attname !== null ? [el.attname] : []);
|
|
1290
|
+
const base = {
|
|
1291
|
+
name: idx.name,
|
|
1292
|
+
prefix: parseWireName(idx.name)?.prefix,
|
|
1293
|
+
where: idx.where ?? void 0,
|
|
1294
|
+
unique: idx.unique,
|
|
1295
|
+
partial: idx.where !== null,
|
|
1296
|
+
type: idx.type,
|
|
1297
|
+
options: idx.options,
|
|
1298
|
+
annotations: void 0,
|
|
1299
|
+
dependsOn: postgresColumnDependsOn(schema, tableName, isExpression ? Object.keys(columns) : columnNames)
|
|
1300
|
+
};
|
|
1301
|
+
return isExpression ? {
|
|
1302
|
+
...base,
|
|
1303
|
+
expression: idx.elements.map((el) => el.elementDef ?? "").join(", ")
|
|
1304
|
+
} : {
|
|
1305
|
+
...base,
|
|
1306
|
+
columns: Object.freeze([...columnNames])
|
|
1307
|
+
};
|
|
1308
|
+
});
|
|
1301
1309
|
const checksForTable = [];
|
|
1302
1310
|
for (const checkRow of checksByTable.get(tableName) ?? []) {
|
|
1303
1311
|
const parsed = parseCheckConstraintDef(checkRow.constraintdef);
|
|
@@ -1338,7 +1346,7 @@ var PostgresControlAdapter = class {
|
|
|
1338
1346
|
const operation = mapPgCmd(row.cmd);
|
|
1339
1347
|
const policyRoles = [...new Set(parsePgNameArray(row.roles).map((r) => r.toLowerCase()))].sort();
|
|
1340
1348
|
const permissive = row.permissive.toUpperCase() === "PERMISSIVE";
|
|
1341
|
-
const prefix =
|
|
1349
|
+
const prefix = parseWireName(row.policyname)?.prefix ?? row.policyname;
|
|
1342
1350
|
const policy = new PostgresPolicySchemaNode({
|
|
1343
1351
|
name: row.policyname,
|
|
1344
1352
|
prefix,
|
|
@@ -1822,6 +1830,47 @@ function pgRenderAlterPolicyRename(node) {
|
|
|
1822
1830
|
params: []
|
|
1823
1831
|
};
|
|
1824
1832
|
}
|
|
1833
|
+
/**
|
|
1834
|
+
* Renders one index reloption value: strings single-quote-escaped, finite
|
|
1835
|
+
* numbers verbatim, booleans in the `on`/`off` catalog spelling (the
|
|
1836
|
+
* canonical form the wire hash and the option equality commit to).
|
|
1837
|
+
*/
|
|
1838
|
+
function pgRenderIndexOptionValue(key, value) {
|
|
1839
|
+
if (typeof value === "string") return `'${escapeLiteral(value)}'`;
|
|
1840
|
+
if (typeof value === "number" && Number.isFinite(value)) return String(value);
|
|
1841
|
+
if (typeof value === "boolean") return value ? "on" : "off";
|
|
1842
|
+
throw postgresError("CONTRACT.INDEX_INVALID", `Index option "${key}" must be a string, finite number, or boolean; got ${typeof value}`, { meta: {
|
|
1843
|
+
key,
|
|
1844
|
+
valueType: typeof value
|
|
1845
|
+
} });
|
|
1846
|
+
}
|
|
1847
|
+
/** Qualifies an object name; an absent schema renders it unqualified (unbound namespace). */
|
|
1848
|
+
function pgQualify(schema, name) {
|
|
1849
|
+
return schema === void 0 ? quoteIdentifier(name) : `${quoteIdentifier(schema)}.${quoteIdentifier(name)}`;
|
|
1850
|
+
}
|
|
1851
|
+
function pgRenderCreateIndex(node) {
|
|
1852
|
+
const elementList = "columns" in node.elements ? node.elements.columns.map(quoteIdentifier).join(", ") : node.elements.expression;
|
|
1853
|
+
const unique = node.unique ? "UNIQUE " : "";
|
|
1854
|
+
const using = node.type !== void 0 ? ` USING ${quoteIdentifier(node.type)}` : "";
|
|
1855
|
+
const withClause = node.options !== void 0 && Object.keys(node.options).length > 0 ? ` WITH (${Object.entries(node.options).map(([key, value]) => `${quoteIdentifier(key)} = ${pgRenderIndexOptionValue(key, value)}`).join(", ")})` : "";
|
|
1856
|
+
const whereClause = node.where !== void 0 ? ` WHERE (${node.where})` : "";
|
|
1857
|
+
return {
|
|
1858
|
+
sql: `CREATE ${unique}INDEX ${quoteIdentifier(node.name)} ON ${pgQualify(node.schema, node.table)}${using} (${elementList})${withClause}${whereClause}`,
|
|
1859
|
+
params: []
|
|
1860
|
+
};
|
|
1861
|
+
}
|
|
1862
|
+
function pgRenderDropIndex(node) {
|
|
1863
|
+
return {
|
|
1864
|
+
sql: `DROP INDEX ${pgQualify(node.schema, node.name)}`,
|
|
1865
|
+
params: []
|
|
1866
|
+
};
|
|
1867
|
+
}
|
|
1868
|
+
function pgRenderAlterIndexRename(node) {
|
|
1869
|
+
return {
|
|
1870
|
+
sql: `ALTER INDEX ${pgQualify(node.schema, node.from)} RENAME TO ${quoteIdentifier(node.to)}`,
|
|
1871
|
+
params: []
|
|
1872
|
+
};
|
|
1873
|
+
}
|
|
1825
1874
|
function pgRenderDisableRowLevelSecurity(node) {
|
|
1826
1875
|
return {
|
|
1827
1876
|
sql: `ALTER TABLE ${`${quoteIdentifier(node.schema)}.${quoteIdentifier(node.table)}`} DISABLE ROW LEVEL SECURITY`,
|
|
@@ -1838,10 +1887,13 @@ async function pgRenderDdlExecuteRequest(ast, codecLookup) {
|
|
|
1838
1887
|
createPolicy: (node) => Promise.resolve(pgRenderCreatePolicy(node)),
|
|
1839
1888
|
dropPolicy: (node) => Promise.resolve(pgRenderDropPolicy(node)),
|
|
1840
1889
|
alterPolicyRename: (node) => Promise.resolve(pgRenderAlterPolicyRename(node)),
|
|
1890
|
+
createIndex: (node) => Promise.resolve(pgRenderCreateIndex(node)),
|
|
1891
|
+
dropIndex: (node) => Promise.resolve(pgRenderDropIndex(node)),
|
|
1892
|
+
alterIndexRename: (node) => Promise.resolve(pgRenderAlterIndexRename(node)),
|
|
1841
1893
|
disableRowLevelSecurity: (node) => Promise.resolve(pgRenderDisableRowLevelSecurity(node))
|
|
1842
1894
|
});
|
|
1843
1895
|
}
|
|
1844
1896
|
//#endregion
|
|
1845
1897
|
export { adapterError as i, renderLoweredSql as n, createPostgresBuiltinCodecLookup as r, PostgresControlAdapter as t };
|
|
1846
1898
|
|
|
1847
|
-
//# sourceMappingURL=control-adapter-
|
|
1899
|
+
//# sourceMappingURL=control-adapter-DKbwTazN.mjs.map
|