@prisma-next/adapter-postgres 0.14.0-dev.21 → 0.14.0-dev.23

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.
@@ -1,4 +1,4 @@
1
- import { n as renderLoweredSql, r as createPostgresBuiltinCodecLookup, t as PostgresControlAdapter } from "./control-adapter-AffpjrYN.mjs";
1
+ import { n as renderLoweredSql, r as createPostgresBuiltinCodecLookup, t as PostgresControlAdapter } from "./control-adapter-QeICiKLE.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-BvM9axNH.mjs.map
66
+ //# sourceMappingURL=adapter-C9_442Ul.mjs.map
@@ -1 +1 @@
1
- {"version":3,"file":"adapter-BvM9axNH.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 { 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 new Error(\n 'lower() does not lower DDL on the runtime adapter — DDL lowering is a control-plane concern handled by the control 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 new Error(\n 'unsupported JS value type for raw-SQL interpolation: wrap this value in `param(...)` with an explicit codec',\n );\n },\n};\n\nexport function createPostgresAdapter(options?: PostgresAdapterOptions) {\n return Object.freeze(new PostgresAdapterImpl(options));\n}\n"],"mappings":";;;;AAkBA,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,IAAI,MACR,6HACF;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,IAAI,MACR,6GACF;AACF,EACF;AAEA,SAAgB,sBAAsB,SAAkC;CACtE,OAAO,OAAO,OAAO,IAAI,oBAAoB,OAAO,CAAC;AACvD"}
1
+ {"version":3,"file":"adapter-C9_442Ul.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 { 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 new Error(\n 'lower() does not lower DDL on the runtime adapter — DDL lowering is a control-plane concern handled by the control 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 new Error(\n 'unsupported JS value type for raw-SQL interpolation: wrap this value in `param(...)` with an explicit codec',\n );\n },\n};\n\nexport function createPostgresAdapter(options?: PostgresAdapterOptions) {\n return Object.freeze(new PostgresAdapterImpl(options));\n}\n"],"mappings":";;;;AAkBA,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,IAAI,MACR,6HACF;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,IAAI,MACR,6GACF;AACF,EACF;AAEA,SAAgB,sBAAsB,SAAkC;CACtE,OAAO,OAAO,OAAO,IAAI,oBAAoB,OAAO,CAAC;AACvD"}
@@ -1,4 +1,4 @@
1
- import { c as PostgresContract, l as PostgresLoweredStatement, s as PostgresAdapterOptions } from "./types-KXRwRZU8.mjs";
1
+ import { c as PostgresContract, l as PostgresLoweredStatement, s as PostgresAdapterOptions } from "./types-DkGeLfUp.mjs";
2
2
  import { Adapter, AdapterProfile, AnyQueryAst, LowererContext } from "@prisma-next/sql-relational-core/ast";
3
3
  import { RawCodecInferer } from "@prisma-next/sql-relational-core/expression";
4
4
  import { PostgresDdlNode } from "@prisma-next/target-postgres/ddl";
package/dist/adapter.mjs CHANGED
@@ -1,2 +1,2 @@
1
- import { n as postgresRawCodecInferer, t as createPostgresAdapter } from "./adapter-BvM9axNH.mjs";
1
+ import { n as postgresRawCodecInferer, t as createPostgresAdapter } from "./adapter-C9_442Ul.mjs";
2
2
  export { createPostgresAdapter, postgresRawCodecInferer };
@@ -8,9 +8,9 @@ import { REFERENTIAL_ACTION_SQL } from "@prisma-next/sql-contract/referential-ac
8
8
  import { buildControlTableBootstrapQueries, buildSignMarkerBootstrapQueries, int4, int8, jsonb, pgTable, text, textArray, timestamptz } from "@prisma-next/target-postgres/contract-free";
9
9
  import { parsePostgresDefault } from "@prisma-next/target-postgres/default-normalizer";
10
10
  import { normalizeSchemaNativeType } from "@prisma-next/target-postgres/native-type-normalizer";
11
- import { diffPostgresRlsPolicies } from "@prisma-next/target-postgres/planner";
11
+ import { contractToPostgresSchemaIR, diffPostgresSchema, filterIssuesByOwnership } from "@prisma-next/target-postgres/planner";
12
12
  import { escapeLiteral, quoteIdentifier } from "@prisma-next/target-postgres/sql-utils";
13
- import { PostgresRlsPolicy, PostgresRole, PostgresSchemaIR, isPostgresSchemaIR } from "@prisma-next/target-postgres/types";
13
+ import { PostgresRlsPolicy, PostgresRole, PostgresSchemaIR, PostgresTableIR, ensurePostgresSchemaIR, isPostgresSchemaIR } from "@prisma-next/target-postgres/types";
14
14
  import { blindCast } from "@prisma-next/utils/casts";
15
15
  import { ifDefined } from "@prisma-next/utils/defined";
16
16
  import { createAstCodecRegistry, deriveParamMetadata, encodeParamsWithMetadata } from "@prisma-next/sql-runtime";
@@ -671,11 +671,9 @@ var PostgresControlAdapter = class {
671
671
  */
672
672
  normalizeNativeType = normalizeSchemaNativeType;
673
673
  collectSchemaDiffIssues(contract, schema) {
674
- if (!isPostgresSchemaIR(schema)) throw new Error(`RLS verification requires a PostgresSchemaIR; got ${schema.constructor?.name ?? typeof schema}`);
675
- return diffPostgresRlsPolicies({
676
- contract,
677
- schema
678
- });
674
+ if (!isPostgresSchemaIR(schema)) throw new Error(`Postgres schema diff requires a PostgresSchemaIR; got ${schema.constructor?.name ?? typeof schema}`);
675
+ const expected = contractToPostgresSchemaIR(blindCast(contract), { annotationNamespace: "pg" });
676
+ return filterIssuesByOwnership(diffPostgresSchema(expected, ensurePostgresSchemaIR(schema)), new Set([...expected.rlsPolicies.map((p) => p.namespaceId), ...expected.existingSchemas]));
679
677
  }
680
678
  bootstrapControlTableQueries() {
681
679
  return buildControlTableBootstrapQueries();
@@ -906,7 +904,6 @@ var PostgresControlAdapter = class {
906
904
  tables: ir.tables,
907
905
  pgSchemaName: ir.pgSchemaName,
908
906
  pgVersion: ir.pgVersion,
909
- rlsPolicies: ir.rlsPolicies,
910
907
  roles: ir.roles,
911
908
  existingSchemas,
912
909
  nativeEnumTypeNames: ir.nativeEnumTypeNames
@@ -945,11 +942,9 @@ var PostgresControlAdapter = class {
945
942
  const perSchema = [];
946
943
  for (const schema of uniqueSchemas) perSchema.push(await this.introspectSchema(driver, schema));
947
944
  const mergedTables = {};
948
- for (const ir of perSchema) for (const [tableName, table] of Object.entries(ir.tables)) mergedTables[tableName] = table;
949
- const mergedRlsPolicies = [];
950
945
  const mergedRoles = [];
951
946
  for (const ir of perSchema) {
952
- mergedRlsPolicies.push(...ir.rlsPolicies);
947
+ for (const [tableName, table] of Object.entries(ir.tables)) mergedTables[tableName] = table;
953
948
  mergedRoles.push(...ir.roles);
954
949
  }
955
950
  const first = perSchema[0];
@@ -957,7 +952,6 @@ var PostgresControlAdapter = class {
957
952
  tables: mergedTables,
958
953
  pgSchemaName: first?.pgSchemaName ?? "public",
959
954
  pgVersion: first?.pgVersion ?? "unknown",
960
- rlsPolicies: mergedRlsPolicies,
961
955
  roles: mergedRoles,
962
956
  existingSchemas: first?.existingSchemas ?? ["public"],
963
957
  nativeEnumTypeNames: first?.nativeEnumTypeNames ?? []
@@ -1108,7 +1102,7 @@ var PostgresControlAdapter = class {
1108
1102
  }
1109
1103
  constraints.add(row.constraint_name);
1110
1104
  }
1111
- const tables = {};
1105
+ const tableInputs = {};
1112
1106
  for (const tableRow of tablesResult.rows) {
1113
1107
  const tableName = tableRow.table_name;
1114
1108
  const columns = {};
@@ -1211,7 +1205,7 @@ var PostgresControlAdapter = class {
1211
1205
  permittedValues: parsed.permittedValues
1212
1206
  });
1213
1207
  }
1214
- tables[tableName] = {
1208
+ tableInputs[tableName] = {
1215
1209
  name: tableName,
1216
1210
  columns,
1217
1211
  ...ifDefined("primaryKey", primaryKey),
@@ -1236,22 +1230,31 @@ var PostgresControlAdapter = class {
1236
1230
  WHERE rolname NOT LIKE 'pg_%'
1237
1231
  AND rolname != 'postgres'
1238
1232
  ORDER BY rolname`);
1239
- const rlsPolicies = policiesResult.rows.map((row) => {
1233
+ const policiesByTable = /* @__PURE__ */ new Map();
1234
+ for (const row of policiesResult.rows) {
1240
1235
  const operation = mapPgCmd(row.cmd);
1241
- const roles = [...new Set(parsePgNameArray(row.roles).map((r) => r.toLowerCase()))].sort();
1236
+ const policyRoles = [...new Set(parsePgNameArray(row.roles).map((r) => r.toLowerCase()))].sort();
1242
1237
  const permissive = row.permissive.toUpperCase() === "PERMISSIVE";
1243
1238
  const prefix = /^(.+)_([0-9a-f]{8})$/.exec(row.policyname)?.[1] ?? row.policyname;
1244
- return new PostgresRlsPolicy({
1239
+ const policy = new PostgresRlsPolicy({
1245
1240
  name: row.policyname,
1246
1241
  prefix,
1247
1242
  tableName: row.tablename,
1248
1243
  namespaceId: row.schemaname,
1249
1244
  operation,
1250
- roles,
1245
+ roles: policyRoles,
1251
1246
  ...row.qual !== null ? { using: row.qual } : {},
1252
1247
  ...row.with_check !== null ? { withCheck: row.with_check } : {},
1253
1248
  permissive
1254
1249
  });
1250
+ const list = policiesByTable.get(row.tablename) ?? [];
1251
+ list.push(policy);
1252
+ policiesByTable.set(row.tablename, list);
1253
+ }
1254
+ const tables = {};
1255
+ for (const [tableName, input] of Object.entries(tableInputs)) tables[tableName] = new PostgresTableIR({
1256
+ ...input,
1257
+ rlsPolicies: policiesByTable.get(tableName) ?? []
1255
1258
  });
1256
1259
  const roles = rolesResult.rows.map((row) => new PostgresRole({
1257
1260
  name: row.rolname,
@@ -1261,7 +1264,6 @@ var PostgresControlAdapter = class {
1261
1264
  tables,
1262
1265
  pgSchemaName: schema,
1263
1266
  pgVersion: await this.getPostgresVersion(driver),
1264
- rlsPolicies,
1265
1267
  roles,
1266
1268
  existingSchemas: [],
1267
1269
  nativeEnumTypeNames
@@ -1606,4 +1608,4 @@ async function pgRenderDdlExecuteRequest(ast, codecLookup) {
1606
1608
  //#endregion
1607
1609
  export { renderLoweredSql as n, createPostgresBuiltinCodecLookup as r, PostgresControlAdapter as t };
1608
1610
 
1609
- //# sourceMappingURL=control-adapter-AffpjrYN.mjs.map
1611
+ //# sourceMappingURL=control-adapter-QeICiKLE.mjs.map