orchid-orm 1.68.3 → 1.68.5

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.
@@ -1295,6 +1295,44 @@ const dropCheck = ({ changeTableAst: { drop }, changingColumns }, dbCheck, name)
1295
1295
  check: sql
1296
1296
  });
1297
1297
  };
1298
+ const defaultRlsState = {
1299
+ enable: false,
1300
+ force: false
1301
+ };
1302
+ const normalizeRlsFlag = (value, defaultValue) => {
1303
+ if (value === void 0) return defaultValue;
1304
+ return value === true || value === "true" || value === "t";
1305
+ };
1306
+ const processTableRls = (ast, dbStructure, tables, currentSchema) => {
1307
+ const projectRlsDefaults = tables[0]?.internal.rls?.tableRlsDefaults;
1308
+ for (const table of tables) {
1309
+ const tableRls = table.internal.tableRls;
1310
+ if (!tableRls) continue;
1311
+ const schemaName = table.q.schema ?? currentSchema;
1312
+ const dbTable = dbStructure.tables.find((item) => item.schemaName === schemaName && item.name === table.table);
1313
+ if (!dbTable) continue;
1314
+ const codeRls = {
1315
+ enable: normalizeRlsFlag(tableRls.enable ?? projectRlsDefaults?.enable, defaultRlsState.enable),
1316
+ force: normalizeRlsFlag(tableRls.force ?? projectRlsDefaults?.force, defaultRlsState.force)
1317
+ };
1318
+ const dbRls = {
1319
+ enable: normalizeRlsFlag(dbTable.rls?.enable, defaultRlsState.enable),
1320
+ force: normalizeRlsFlag(dbTable.rls?.force, defaultRlsState.force)
1321
+ };
1322
+ if (codeRls.enable !== dbRls.enable) ast.push({
1323
+ type: "tableRls",
1324
+ action: codeRls.enable ? "enable" : "disable",
1325
+ schema: schemaName,
1326
+ table: table.table
1327
+ });
1328
+ if (codeRls.force !== dbRls.force) ast.push({
1329
+ type: "tableRls",
1330
+ action: codeRls.force ? "force" : "noForce",
1331
+ schema: schemaName,
1332
+ table: table.table
1333
+ });
1334
+ }
1335
+ };
1298
1336
  const processTables = async (ast, domainsMap, adapter, dbStructure, config, { structureToAstCtx, codeItems: { tables }, currentSchema, internal: { generatorIgnore }, verifying }, pendingDbTypes) => {
1299
1337
  const createTables = collectCreateTables(tables, dbStructure, currentSchema);
1300
1338
  const compareSql = {
@@ -1308,6 +1346,7 @@ const processTables = async (ast, domainsMap, adapter, dbStructure, config, { st
1308
1346
  await applyChangeTables(adapter, changeTables, structureToAstCtx, dbStructure, domainsMap, ast, currentSchema, config, compareSql, tableExpressions, verifying, pendingDbTypes);
1309
1347
  processForeignKeys(config, ast, changeTables, currentSchema, tableShapes);
1310
1348
  await Promise.all([applyCompareSql(compareSql, adapter), compareSqlExpressions(tableExpressions, adapter)]);
1349
+ processTableRls(ast, dbStructure, tables, currentSchema);
1311
1350
  for (const dbTable of dropTables) ast.push(tableToAst(structureToAstCtx, dbStructure, dbTable, "drop", domainsMap));
1312
1351
  };
1313
1352
  const collectCreateTables = (tables, dbStructure, currentSchema) => {
@@ -1856,6 +1895,7 @@ const verifyMigration = async (adapter, config, migrationCode, generateMigration
1856
1895
  config.log = log;
1857
1896
  for (const changeFn of changeFns) await changeFn(db, true);
1858
1897
  const dbStructure = await introspectDbSchema(trx, {
1898
+ rls: generateMigrationParams.codeItems.tables.some((table) => !!table.internal.tableRls),
1859
1899
  roles,
1860
1900
  loadDefaultPrivileges: defaultPrivileges?.loadDefaultPrivileges
1861
1901
  });
@@ -2043,6 +2083,15 @@ const report = (ast, config, currentSchema) => {
2043
2083
  if (parts.length) code.push(parts.join("\n"));
2044
2084
  break;
2045
2085
  }
2086
+ case "tableRls": {
2087
+ const table = dbItemName({
2088
+ schema: a.schema,
2089
+ name: a.table
2090
+ }, currentSchema);
2091
+ const message = a.action === "enable" ? `${green("+ enable rls")} ${table}` : a.action === "disable" ? `${red("- enable rls")} ${table}` : a.action === "force" ? `${green("+ force rls")} ${table}` : `${red("- force rls")} ${table}`;
2092
+ code.push(message);
2093
+ break;
2094
+ }
2046
2095
  default: exhaustive(a);
2047
2096
  }
2048
2097
  const result = codeToString(code, "", " ");
@@ -2067,7 +2116,7 @@ const generate = async (adapters, config, args, afterPull) => {
2067
2116
  const db = await getDbFromConfig(config, dbPath);
2068
2117
  const { columnTypes, internal } = db.$qb;
2069
2118
  const rolesDbStructureParam = internal.roles ? internal.managedRolesSql ? { whereSql: internal.managedRolesSql } : emptyObject : void 0;
2070
- const { dbStructure } = await migrateAndPullStructures(adapters, config, rolesDbStructureParam, internal.roles ? { loadDefaultPrivileges: true } : void 0, afterPull);
2119
+ const { dbStructure } = await migrateAndPullStructures(adapters, config, db, rolesDbStructureParam, internal.roles ? { loadDefaultPrivileges: true } : void 0, afterPull);
2071
2120
  const [adapter] = adapters;
2072
2121
  const adapterSchema = adapter.getSchema();
2073
2122
  const currentSchema = (typeof adapterSchema === "function" ? adapterSchema() : adapterSchema) ?? "public";
@@ -2119,7 +2168,7 @@ const getDbFromConfig = async (config, dbPath) => {
2119
2168
  if (!db?.$qb) throw new Error(`Unable to import OrchidORM instance as ${config.dbExportedAs ?? "db"} from ${config.dbPath}`);
2120
2169
  return db;
2121
2170
  };
2122
- const migrateAndPullStructures = async (adapters, config, roles, defaultPrivileges, afterPull) => {
2171
+ const migrateAndPullStructures = async (adapters, config, db, roles, defaultPrivileges, afterPull) => {
2123
2172
  if (afterPull) return { dbStructure: {
2124
2173
  version: await getDbVersion(adapters[0]),
2125
2174
  schemas: [],
@@ -2136,6 +2185,7 @@ const migrateAndPullStructures = async (adapters, config, roles, defaultPrivileg
2136
2185
  } };
2137
2186
  for (const adapter of adapters) await migrate(adapter, config);
2138
2187
  const dbStructures = await Promise.all(adapters.map((adapter) => introspectDbSchema(adapter, {
2188
+ rls: hasCodeTablesWithRls(db),
2139
2189
  roles,
2140
2190
  loadDefaultPrivileges: defaultPrivileges?.loadDefaultPrivileges
2141
2191
  })));
@@ -2143,6 +2193,13 @@ const migrateAndPullStructures = async (adapters, config, roles, defaultPrivileg
2143
2193
  for (let i = 1; i < dbStructures.length; i++) compareDbStructures(dbStructure, dbStructures[i], i);
2144
2194
  return { dbStructure };
2145
2195
  };
2196
+ const hasCodeTablesWithRls = (db) => {
2197
+ for (const key in db) {
2198
+ if (key[0] === "$") continue;
2199
+ if (db[key].internal.tableRls) return true;
2200
+ }
2201
+ return false;
2202
+ };
2146
2203
  const compareDbStructures = (a, b, i, path) => {
2147
2204
  let err;
2148
2205
  if (typeof a !== typeof b) err = true;