orchid-orm 1.68.4 → 1.68.6

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.
@@ -1318,6 +1318,44 @@ const dropCheck = ({ changeTableAst: { drop }, changingColumns }, dbCheck, name)
1318
1318
  check: sql
1319
1319
  });
1320
1320
  };
1321
+ const defaultRlsState = {
1322
+ enable: false,
1323
+ force: false
1324
+ };
1325
+ const normalizeRlsFlag = (value, defaultValue) => {
1326
+ if (value === void 0) return defaultValue;
1327
+ return value === true || value === "true" || value === "t";
1328
+ };
1329
+ const processTableRls = (ast, dbStructure, tables, currentSchema) => {
1330
+ const projectRlsDefaults = tables[0]?.internal.rls?.tableRlsDefaults;
1331
+ for (const table of tables) {
1332
+ const tableRls = table.internal.tableRls;
1333
+ if (!tableRls) continue;
1334
+ const schemaName = table.q.schema ?? currentSchema;
1335
+ const dbTable = dbStructure.tables.find((item) => item.schemaName === schemaName && item.name === table.table);
1336
+ if (!dbTable) continue;
1337
+ const codeRls = {
1338
+ enable: normalizeRlsFlag(tableRls.enable ?? projectRlsDefaults?.enable, defaultRlsState.enable),
1339
+ force: normalizeRlsFlag(tableRls.force ?? projectRlsDefaults?.force, defaultRlsState.force)
1340
+ };
1341
+ const dbRls = {
1342
+ enable: normalizeRlsFlag(dbTable.rls?.enable, defaultRlsState.enable),
1343
+ force: normalizeRlsFlag(dbTable.rls?.force, defaultRlsState.force)
1344
+ };
1345
+ if (codeRls.enable !== dbRls.enable) ast.push({
1346
+ type: "tableRls",
1347
+ action: codeRls.enable ? "enable" : "disable",
1348
+ schema: schemaName,
1349
+ table: table.table
1350
+ });
1351
+ if (codeRls.force !== dbRls.force) ast.push({
1352
+ type: "tableRls",
1353
+ action: codeRls.force ? "force" : "noForce",
1354
+ schema: schemaName,
1355
+ table: table.table
1356
+ });
1357
+ }
1358
+ };
1321
1359
  const processTables = async (ast, domainsMap, adapter, dbStructure, config, { structureToAstCtx, codeItems: { tables }, currentSchema, internal: { generatorIgnore }, verifying }, pendingDbTypes) => {
1322
1360
  const createTables = collectCreateTables(tables, dbStructure, currentSchema);
1323
1361
  const compareSql = {
@@ -1331,6 +1369,7 @@ const processTables = async (ast, domainsMap, adapter, dbStructure, config, { st
1331
1369
  await applyChangeTables(adapter, changeTables, structureToAstCtx, dbStructure, domainsMap, ast, currentSchema, config, compareSql, tableExpressions, verifying, pendingDbTypes);
1332
1370
  processForeignKeys(config, ast, changeTables, currentSchema, tableShapes);
1333
1371
  await Promise.all([applyCompareSql(compareSql, adapter), compareSqlExpressions(tableExpressions, adapter)]);
1372
+ processTableRls(ast, dbStructure, tables, currentSchema);
1334
1373
  for (const dbTable of dropTables) ast.push((0, rake_db.tableToAst)(structureToAstCtx, dbStructure, dbTable, "drop", domainsMap));
1335
1374
  };
1336
1375
  const collectCreateTables = (tables, dbStructure, currentSchema) => {
@@ -1879,6 +1918,7 @@ const verifyMigration = async (adapter, config, migrationCode, generateMigration
1879
1918
  config.log = log;
1880
1919
  for (const changeFn of changeFns) await changeFn(db, true);
1881
1920
  const dbStructure = await (0, rake_db.introspectDbSchema)(trx, {
1921
+ rls: generateMigrationParams.codeItems.tables.some((table) => !!table.internal.tableRls),
1882
1922
  roles,
1883
1923
  loadDefaultPrivileges: defaultPrivileges?.loadDefaultPrivileges
1884
1924
  });
@@ -2066,6 +2106,15 @@ const report = (ast, config, currentSchema) => {
2066
2106
  if (parts.length) code.push(parts.join("\n"));
2067
2107
  break;
2068
2108
  }
2109
+ case "tableRls": {
2110
+ const table = dbItemName({
2111
+ schema: a.schema,
2112
+ name: a.table
2113
+ }, currentSchema);
2114
+ 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}`;
2115
+ code.push(message);
2116
+ break;
2117
+ }
2069
2118
  default: (0, pqb_internal.exhaustive)(a);
2070
2119
  }
2071
2120
  const result = (0, pqb_internal.codeToString)(code, "", " ");
@@ -2090,7 +2139,7 @@ const generate = async (adapters, config, args, afterPull) => {
2090
2139
  const db = await getDbFromConfig(config, dbPath);
2091
2140
  const { columnTypes, internal } = db.$qb;
2092
2141
  const rolesDbStructureParam = internal.roles ? internal.managedRolesSql ? { whereSql: internal.managedRolesSql } : pqb_internal.emptyObject : void 0;
2093
- const { dbStructure } = await migrateAndPullStructures(adapters, config, rolesDbStructureParam, internal.roles ? { loadDefaultPrivileges: true } : void 0, afterPull);
2142
+ const { dbStructure } = await migrateAndPullStructures(adapters, config, db, rolesDbStructureParam, internal.roles ? { loadDefaultPrivileges: true } : void 0, afterPull);
2094
2143
  const [adapter] = adapters;
2095
2144
  const adapterSchema = adapter.getSchema();
2096
2145
  const currentSchema = (typeof adapterSchema === "function" ? adapterSchema() : adapterSchema) ?? "public";
@@ -2142,7 +2191,7 @@ const getDbFromConfig = async (config, dbPath) => {
2142
2191
  if (!db?.$qb) throw new Error(`Unable to import OrchidORM instance as ${config.dbExportedAs ?? "db"} from ${config.dbPath}`);
2143
2192
  return db;
2144
2193
  };
2145
- const migrateAndPullStructures = async (adapters, config, roles, defaultPrivileges, afterPull) => {
2194
+ const migrateAndPullStructures = async (adapters, config, db, roles, defaultPrivileges, afterPull) => {
2146
2195
  if (afterPull) return { dbStructure: {
2147
2196
  version: await (0, rake_db.getDbVersion)(adapters[0]),
2148
2197
  schemas: [],
@@ -2159,6 +2208,7 @@ const migrateAndPullStructures = async (adapters, config, roles, defaultPrivileg
2159
2208
  } };
2160
2209
  for (const adapter of adapters) await (0, rake_db.migrate)(adapter, config);
2161
2210
  const dbStructures = await Promise.all(adapters.map((adapter) => (0, rake_db.introspectDbSchema)(adapter, {
2211
+ rls: hasCodeTablesWithRls(db),
2162
2212
  roles,
2163
2213
  loadDefaultPrivileges: defaultPrivileges?.loadDefaultPrivileges
2164
2214
  })));
@@ -2166,6 +2216,13 @@ const migrateAndPullStructures = async (adapters, config, roles, defaultPrivileg
2166
2216
  for (let i = 1; i < dbStructures.length; i++) compareDbStructures(dbStructure, dbStructures[i], i);
2167
2217
  return { dbStructure };
2168
2218
  };
2219
+ const hasCodeTablesWithRls = (db) => {
2220
+ for (const key in db) {
2221
+ if (key[0] === "$") continue;
2222
+ if (db[key].internal.tableRls) return true;
2223
+ }
2224
+ return false;
2225
+ };
2169
2226
  const compareDbStructures = (a, b, i, path) => {
2170
2227
  let err;
2171
2228
  if (typeof a !== typeof b) err = true;