najm-auth 1.1.6 → 1.1.7

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/index.d.ts CHANGED
@@ -1267,14 +1267,14 @@ type OwnershipStep = JoinStep | OwnerStep;
1267
1267
  /**
1268
1268
  * JOIN step — links two columns across tables.
1269
1269
  * The target table is inferred from the right column.
1270
- * Use aliased tables (`alias(table, '_sc_name')`) to avoid conflicts with display JOINs.
1270
+ * Raw tables are auto-aliased when needed; already-aliased tables are preserved.
1271
1271
  *
1272
- * @example join(grades.studentId, _students.id)
1272
+ * @example join(grades.studentId, students.id)
1273
1273
  */
1274
1274
  declare function join(left: any, right: any): JoinStep;
1275
1275
  /**
1276
1276
  * WHERE step — terminal column that holds the user id.
1277
- * @example where(_teachers.userId)
1277
+ * @example where(teachers.userId)
1278
1278
  */
1279
1279
  declare function where(col: any): OwnerStep;
1280
1280
  type ScopeResult = {
@@ -1318,13 +1318,13 @@ declare class OwnershipToken {
1318
1318
  *
1319
1319
  * @example
1320
1320
  * export const Grade = own(grades)
1321
- * .for('teacher', join(grades.studentId, _s.id), where(_t.userId))
1322
- * .for('parent', join(grades.studentId, _s.id), where(_p.userId))
1321
+ * .for('teacher', join(grades.studentId, students.id), where(teachers.userId))
1322
+ * .for('parent', join(grades.studentId, students.id), where(parents.userId))
1323
1323
  * .writeBy(grades.studentId);
1324
1324
  *
1325
1325
  * // With explicit admin roles (avoids global state):
1326
1326
  * export const Grade = own(grades, { adminRoles: ['admin', 'principal'] })
1327
- * .for('teacher', join(grades.studentId, _s.id), where(_t.userId));
1327
+ * .for('teacher', join(grades.studentId, students.id), where(teachers.userId));
1328
1328
  */
1329
1329
  declare function own(table: any, opts?: OwnershipTokenOptions): OwnershipToken;
1330
1330
 
package/dist/index.js CHANGED
@@ -3019,12 +3019,15 @@ var selectSchema = /* @__PURE__ */ __name((config) => {
3019
3019
  var auth = /* @__PURE__ */ __name((config) => plugin("auth").version("1.0.0").depends(cache(), cookies(), i18n(), guards(), validation(config?.validation), rateLimit(config?.rateLimit), email()).requires("database").contributes(I18N_CONTRIBUTIONS, AUTH_LOCALES).services(auth_exports, users_exports, roles_exports, permissions_exports, tokens_exports).config(AUTH_CONFIG, mergeConfig(config)).set(AUTH_SCHEMA, selectSchema(config)).build(), "auth");
3020
3020
 
3021
3021
  // src/ownership/scopedOwnership.ts
3022
- import { eq as eq6, sql as sql4 } from "drizzle-orm";
3022
+ import { aliasedTable, eq as eq6, getTableColumns, sql as sql4 } from "drizzle-orm";
3023
3023
  var DEFAULT_ADMIN_ROLES = ["admin", "principal"];
3024
+ var DRIZZLE_NAME = /* @__PURE__ */ Symbol.for("drizzle:Name");
3025
+ var DRIZZLE_BASE_NAME = /* @__PURE__ */ Symbol.for("drizzle:BaseName");
3026
+ var DRIZZLE_IS_ALIAS = /* @__PURE__ */ Symbol.for("drizzle:IsAlias");
3024
3027
  function join2(left, right) {
3025
3028
  const table = right.table;
3026
3029
  if (!table)
3027
- throw new Error(`join(): cannot infer table from right column. Use drizzle alias().`);
3030
+ throw new Error("join(): cannot infer table from right column.");
3028
3031
  return { type: "join", left, right, table };
3029
3032
  }
3030
3033
  __name(join2, "join");
@@ -3032,9 +3035,72 @@ function where(col) {
3032
3035
  return { type: "owner", col };
3033
3036
  }
3034
3037
  __name(where, "where");
3038
+ function isAliased(table) {
3039
+ return table?.[DRIZZLE_IS_ALIAS] === true;
3040
+ }
3041
+ __name(isAliased, "isAliased");
3042
+ function autoAlias(table, suffix) {
3043
+ if (isAliased(table))
3044
+ return table;
3045
+ const name = table?.[DRIZZLE_BASE_NAME] ?? table?.[DRIZZLE_NAME] ?? "table";
3046
+ return aliasedTable(table, `_sc_${String(name)}_${suffix}`);
3047
+ }
3048
+ __name(autoAlias, "autoAlias");
3049
+ function getColumns(table) {
3050
+ try {
3051
+ return getTableColumns(table) ?? null;
3052
+ } catch {
3053
+ return null;
3054
+ }
3055
+ }
3056
+ __name(getColumns, "getColumns");
3057
+ function buildColumnMap(rawTable, aliased) {
3058
+ const rawCols = getColumns(rawTable);
3059
+ const aliasedCols = getColumns(aliased);
3060
+ const map = /* @__PURE__ */ new Map();
3061
+ if (!rawCols || !aliasedCols)
3062
+ return map;
3063
+ for (const key of Object.keys(rawCols)) {
3064
+ map.set(rawCols[key], aliasedCols[key]);
3065
+ }
3066
+ return map;
3067
+ }
3068
+ __name(buildColumnMap, "buildColumnMap");
3035
3069
  function compile(steps) {
3036
- const joins = steps.filter((s) => s.type === "join");
3037
- const owner = steps.find((s) => s.type === "owner");
3070
+ const tableMap = /* @__PURE__ */ new Map();
3071
+ const colMap = /* @__PURE__ */ new Map();
3072
+ let aliasIndex = 0;
3073
+ for (const step of steps) {
3074
+ if (step.type !== "join")
3075
+ continue;
3076
+ const rawTable = step.right.table;
3077
+ if (isAliased(rawTable) || tableMap.has(rawTable) || !getColumns(rawTable))
3078
+ continue;
3079
+ const aliased = autoAlias(rawTable, ++aliasIndex);
3080
+ tableMap.set(rawTable, aliased);
3081
+ for (const [rawCol, aliasedCol] of buildColumnMap(rawTable, aliased)) {
3082
+ colMap.set(rawCol, aliasedCol);
3083
+ }
3084
+ }
3085
+ const remapped = steps.map((step) => {
3086
+ if (step.type === "join") {
3087
+ return {
3088
+ ...step,
3089
+ left: colMap.get(step.left) ?? step.left,
3090
+ right: colMap.get(step.right) ?? step.right,
3091
+ table: tableMap.get(step.right.table) ?? step.right.table
3092
+ };
3093
+ }
3094
+ if (step.type === "owner") {
3095
+ return {
3096
+ ...step,
3097
+ col: colMap.get(step.col) ?? step.col
3098
+ };
3099
+ }
3100
+ return step;
3101
+ });
3102
+ const joins = remapped.filter((s) => s.type === "join");
3103
+ const owner = remapped.find((s) => s.type === "owner");
3038
3104
  if (!owner)
3039
3105
  throw new Error("Ownership chain must end with where()");
3040
3106
  return (uid, query) => {
@@ -3056,7 +3122,7 @@ var OwnershipToken = class {
3056
3122
  _writeScopeCol;
3057
3123
  _adminRoles;
3058
3124
  constructor(table, opts) {
3059
- const name = table[/* @__PURE__ */ Symbol.for("drizzle:Name")] ?? table?._.baseName ?? table?._.name ?? "resource";
3125
+ const name = table[DRIZZLE_NAME] ?? table?._.baseName ?? table?._.name ?? "resource";
3060
3126
  this.name = name;
3061
3127
  this.table = table;
3062
3128
  this.symbol = Symbol(name);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "najm-auth",
3
- "version": "1.1.6",
3
+ "version": "1.1.7",
4
4
  "description": "Authentication and authorization library for najm framework",
5
5
  "type": "module",
6
6
  "files": [