@anievo-id/rbac-evolution-drizzle 1.1.0 → 1.2.0

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.
Files changed (40) hide show
  1. package/dist/{chunk-WE5ZHXEW.js → chunk-3AH2K6KS.js} +77 -27
  2. package/dist/chunk-3AH2K6KS.js.map +1 -0
  3. package/dist/{chunk-HDQ5ACTJ.js → chunk-CK2CKZPC.js} +49 -6
  4. package/dist/chunk-CK2CKZPC.js.map +1 -0
  5. package/dist/{chunk-D6UKXFGV.js → chunk-CQBJTVGQ.js} +48 -5
  6. package/dist/chunk-CQBJTVGQ.js.map +1 -0
  7. package/dist/{chunk-4KGIPA5W.js → chunk-SPNXRYKH.js} +48 -5
  8. package/dist/chunk-SPNXRYKH.js.map +1 -0
  9. package/dist/index.cjs +209 -30
  10. package/dist/index.cjs.map +1 -1
  11. package/dist/index.d.cts +6 -114
  12. package/dist/index.d.ts +6 -114
  13. package/dist/index.js +4 -4
  14. package/dist/mysql.cjs +119 -26
  15. package/dist/mysql.cjs.map +1 -1
  16. package/dist/mysql.d.cts +447 -5
  17. package/dist/mysql.d.ts +447 -5
  18. package/dist/mysql.js +2 -2
  19. package/dist/names-DD-kYSFO.d.cts +146 -0
  20. package/dist/names-DD-kYSFO.d.ts +146 -0
  21. package/dist/pg.cjs +119 -26
  22. package/dist/pg.cjs.map +1 -1
  23. package/dist/pg.d.cts +447 -5
  24. package/dist/pg.d.ts +447 -5
  25. package/dist/pg.js +2 -2
  26. package/dist/sqlite.cjs +119 -26
  27. package/dist/sqlite.cjs.map +1 -1
  28. package/dist/sqlite.d.cts +469 -5
  29. package/dist/sqlite.d.ts +469 -5
  30. package/dist/sqlite.js +2 -2
  31. package/package.json +5 -2
  32. package/sql/mysql-expiry.sql +9 -0
  33. package/sql/pg-expiry.sql +8 -0
  34. package/sql/sqlite-expiry.sql +10 -0
  35. package/dist/chunk-4KGIPA5W.js.map +0 -1
  36. package/dist/chunk-D6UKXFGV.js.map +0 -1
  37. package/dist/chunk-HDQ5ACTJ.js.map +0 -1
  38. package/dist/chunk-WE5ZHXEW.js.map +0 -1
  39. package/dist/names-BVofJeMN.d.cts +0 -13
  40. package/dist/names-BVofJeMN.d.ts +0 -13
package/dist/index.d.ts CHANGED
@@ -1,117 +1,9 @@
1
- export { PgRbacSchema, drizzlePgAdapter, pgRbacSchema } from './pg.js';
2
- export { SqliteRbacSchema, drizzleSqliteAdapter, sqliteRbacSchema } from './sqlite.js';
3
- export { MysqlRbacSchema, drizzleMysqlAdapter, mysqlRbacSchema } from './mysql.js';
4
- import { RbacAdapter, Permission, FindPermissionsQuery, Id, TeamId, Role, FindRolesQuery, RolePermissionLink, Subject, SubjectLinks } from '@anievo-id/rbac-evolution';
5
- import { Table, AnyColumn } from 'drizzle-orm';
6
- export { R as RbacSchemaOptions, a as RbacTableNames } from './names-BVofJeMN.js';
1
+ export { PgRbacExpirySchema, PgRbacSchema, drizzlePgAdapter, pgRbacSchema } from './pg.js';
2
+ export { SqliteRbacExpirySchema, SqliteRbacSchema, drizzleSqliteAdapter, sqliteRbacSchema } from './sqlite.js';
3
+ export { MysqlRbacExpirySchema, MysqlRbacSchema, drizzleMysqlAdapter, mysqlRbacSchema } from './mysql.js';
4
+ export { D as DrizzleAdapterOptions, a as DrizzleDialect, b as DrizzleRbacAdapter, R as RbacSchemaOptions, c as RbacTableNames, d as RbacTables } from './names-DD-kYSFO.js';
5
+ import '@anievo-id/rbac-evolution';
7
6
  import 'drizzle-orm/pg-core';
8
7
  import 'drizzle-orm/sqlite-core';
9
8
  import 'drizzle-orm/mysql-core';
10
-
11
- /** Structural shape shared by the PostgreSQL, SQLite and MySQL schemas. */
12
- interface RbacTables {
13
- permissions: Table & {
14
- id: AnyColumn;
15
- name: AnyColumn;
16
- guardName: AnyColumn;
17
- };
18
- roles: Table & {
19
- id: AnyColumn;
20
- name: AnyColumn;
21
- guardName: AnyColumn;
22
- teamId: AnyColumn;
23
- };
24
- roleHasPermissions: Table & {
25
- roleId: AnyColumn;
26
- permissionId: AnyColumn;
27
- };
28
- modelHasRoles: Table & {
29
- roleId: AnyColumn;
30
- subjectType: AnyColumn;
31
- subjectId: AnyColumn;
32
- teamId: AnyColumn;
33
- };
34
- modelHasPermissions: Table & {
35
- permissionId: AnyColumn;
36
- subjectType: AnyColumn;
37
- subjectId: AnyColumn;
38
- teamId: AnyColumn;
39
- };
40
- }
41
- /**
42
- * The PostgreSQL, SQLite and MySQL query builders expose the same chainable API
43
- * (`select/insert/delete`, `selectDistinct`) but have incompatible static types. The public factories
44
- * (`drizzlePgAdapter`, `drizzleSqliteAdapter`, `drizzleMysqlAdapter`) are strictly typed; internally the
45
- * builder is used untyped and every row is mapped explicitly.
46
- */
47
- type UntypedDb = any;
48
- /**
49
- * SQL dialect of the Drizzle database. It only changes how inserts are written:
50
- * - `'pg'` / `'sqlite'`: `INSERT ... RETURNING` and `ON CONFLICT DO NOTHING`
51
- * - `'mysql'`: `$returningId()` (no `RETURNING` in MySQL) and a no-op `ON DUPLICATE KEY UPDATE`
52
- */
53
- type DrizzleDialect = 'pg' | 'sqlite' | 'mysql';
54
- /**
55
- * Dialect-agnostic Drizzle implementation of `RbacAdapter`.
56
- * Use `drizzlePgAdapter()`, `drizzleSqliteAdapter()` or `drizzleMysqlAdapter()` to construct it.
57
- */
58
- declare class DrizzleRbacAdapter implements RbacAdapter {
59
- private readonly db;
60
- private readonly t;
61
- private readonly dialect;
62
- constructor(db: UntypedDb, t: RbacTables, dialect?: DrizzleDialect);
63
- /**
64
- * Runs `fn` in a transaction (a savepoint when `db` is already a transaction). SQLite runs without one:
65
- * better-sqlite3 and bun:sqlite only accept synchronous transaction callbacks, and libsql `:memory:` would
66
- * open a separate, empty database.
67
- */
68
- private atomically;
69
- createPermission(data: {
70
- name: string;
71
- guardName: string;
72
- }): Promise<Permission>;
73
- findPermissions(query?: FindPermissionsQuery): Promise<Permission[]>;
74
- deletePermission(id: Id): Promise<void>;
75
- createRole(data: {
76
- name: string;
77
- guardName: string;
78
- teamId: TeamId;
79
- }): Promise<Role>;
80
- findRoles(query?: FindRolesQuery): Promise<Role[]>;
81
- deleteRole(id: Id): Promise<void>;
82
- getRolePermissionLinks(roleIds: readonly Id[]): Promise<RolePermissionLink[]>;
83
- attachRolePermissions(roleId: Id, permissionIds: readonly Id[]): Promise<void>;
84
- detachRolePermissions(roleId: Id, permissionIds?: readonly Id[]): Promise<void>;
85
- getSubjectRoleIds(subject: Subject, teamId: TeamId): Promise<Id[]>;
86
- attachSubjectRoles(subject: Subject, roleIds: readonly Id[], teamId: TeamId): Promise<void>;
87
- detachSubjectRoles(subject: Subject, roleIds: readonly Id[] | undefined, teamId: TeamId): Promise<void>;
88
- getSubjectPermissionIds(subject: Subject, teamId: TeamId): Promise<Id[]>;
89
- attachSubjectPermissions(subject: Subject, permissionIds: readonly Id[], teamId: TeamId): Promise<void>;
90
- detachSubjectPermissions(subject: Subject, permissionIds: readonly Id[] | undefined, teamId: TeamId): Promise<void>;
91
- getSubjectIdsWithRoles(subjectType: string, roleIds: readonly Id[], teamId: TeamId): Promise<Id[]>;
92
- getSubjectIdsWithPermissions(subjectType: string, permissionIds: readonly Id[], teamId: TeamId): Promise<Id[]>;
93
- getSubjectLinks(subject: Subject): Promise<SubjectLinks>;
94
- getTeamLinks(teamId: Id): Promise<SubjectLinks>;
95
- detachTeamLinks(teamId: Id): Promise<void>;
96
- /** Link rows matching `where` (subject and/or team), compared exactly (see `sameLink`). */
97
- private exactRows;
98
- private static toSubjectLinks;
99
- private static first;
100
- /** MySQL has no `INSERT ... RETURNING`: insert, then read the generated auto-increment id. */
101
- private insertReturningId;
102
- /**
103
- * Idempotent multi-row insert into a link table (every column is part of the primary key).
104
- * PostgreSQL / SQLite: `ON CONFLICT DO NOTHING`. MySQL: `ON DUPLICATE KEY UPDATE col = col`, a no-op
105
- * update that — unlike `INSERT IGNORE` — still reports foreign-key and other errors.
106
- */
107
- private insertIgnoringDuplicates;
108
- private static subjectWhere;
109
- /** The subject's link rows, compared exactly (see `sameLink`). */
110
- private linkRows;
111
- private getLinks;
112
- private attach;
113
- private detach;
114
- private reverse;
115
- }
116
-
117
- export { type DrizzleDialect, DrizzleRbacAdapter, type RbacTables };
9
+ import 'drizzle-orm';
package/dist/index.js CHANGED
@@ -1,6 +1,6 @@
1
- export { drizzleMysqlAdapter, mysqlRbacSchema } from './chunk-HDQ5ACTJ.js';
2
- export { drizzlePgAdapter, pgRbacSchema } from './chunk-D6UKXFGV.js';
3
- export { drizzleSqliteAdapter, sqliteRbacSchema } from './chunk-4KGIPA5W.js';
4
- export { DrizzleRbacAdapter } from './chunk-WE5ZHXEW.js';
1
+ export { drizzleMysqlAdapter, mysqlRbacSchema } from './chunk-CK2CKZPC.js';
2
+ export { drizzlePgAdapter, pgRbacSchema } from './chunk-CQBJTVGQ.js';
3
+ export { drizzleSqliteAdapter, sqliteRbacSchema } from './chunk-SPNXRYKH.js';
4
+ export { DrizzleRbacAdapter } from './chunk-3AH2K6KS.js';
5
5
  //# sourceMappingURL=index.js.map
6
6
  //# sourceMappingURL=index.js.map
package/dist/mysql.cjs CHANGED
@@ -32,16 +32,37 @@ var toRole = (row) => ({
32
32
  guardName: row.guardName,
33
33
  teamId: teamFromKey(row.teamId)
34
34
  });
35
+ var expiryOf = (row) => row.expiresAt === void 0 ? {} : { expiresAt: row.expiresAt ?? null };
36
+ var toRoleLink = (row) => ({
37
+ subject: { type: row.subjectType, id: row.subjectId },
38
+ roleId: Number(row.id),
39
+ teamId: teamFromKey(row.teamId),
40
+ ...expiryOf(row)
41
+ });
42
+ var toPermissionLink = (row) => ({
43
+ subject: { type: row.subjectType, id: row.subjectId },
44
+ permissionId: Number(row.id),
45
+ teamId: teamFromKey(row.teamId),
46
+ ...expiryOf(row)
47
+ });
35
48
  var sameLink = (row, subjectType, subjectId, team) => row.subjectType === subjectType && (subjectId === void 0 || row.subjectId === subjectId) && row.teamId === team;
36
49
  var DrizzleRbacAdapter = class _DrizzleRbacAdapter {
37
- constructor(db, t, dialect = "pg") {
50
+ constructor(db, t, dialect = "pg", options = {}) {
38
51
  this.db = db;
39
52
  this.t = t;
40
53
  this.dialect = dialect;
54
+ this.supportsExpiry = options.expiry === true;
55
+ if (this.supportsExpiry && (t.modelHasRoles.expiresAt === void 0 || t.modelHasPermissions.expiresAt === void 0)) {
56
+ throw new TypeError(
57
+ "rbac-evolution-drizzle: `{ expiry: true }` needs the tables with the `expiresAt` column: build them with `{ expiry: true }`."
58
+ );
59
+ }
41
60
  }
42
61
  db;
43
62
  t;
44
63
  dialect;
64
+ /** `true` with `{ expiry: true }`: the engine may then pass `expiresAt`. */
65
+ supportsExpiry;
45
66
  /**
46
67
  * Runs `fn` in a transaction (a savepoint when `db` is already a transaction). SQLite runs without one:
47
68
  * better-sqlite3 and bun:sqlite only accept synchronous transaction callbacks, and libsql `:memory:` would
@@ -166,8 +187,8 @@ var DrizzleRbacAdapter = class _DrizzleRbacAdapter {
166
187
  getSubjectRoleIds(subject, teamId) {
167
188
  return this.getLinks(this.t.modelHasRoles, this.t.modelHasRoles.roleId, subject, teamId);
168
189
  }
169
- attachSubjectRoles(subject, roleIds, teamId) {
170
- return this.attach(this.t.modelHasRoles, "roleId", subject, roleIds, teamId);
190
+ attachSubjectRoles(subject, roleIds, teamId, options) {
191
+ return this.attach(this.t.modelHasRoles, "roleId", this.t.modelHasRoles.roleId, subject, roleIds, teamId, options);
171
192
  }
172
193
  detachSubjectRoles(subject, roleIds, teamId) {
173
194
  return this.detach(this.t.modelHasRoles, this.t.modelHasRoles.roleId, subject, roleIds, teamId);
@@ -175,8 +196,9 @@ var DrizzleRbacAdapter = class _DrizzleRbacAdapter {
175
196
  getSubjectPermissionIds(subject, teamId) {
176
197
  return this.getLinks(this.t.modelHasPermissions, this.t.modelHasPermissions.permissionId, subject, teamId);
177
198
  }
178
- attachSubjectPermissions(subject, permissionIds, teamId) {
179
- return this.attach(this.t.modelHasPermissions, "permissionId", subject, permissionIds, teamId);
199
+ attachSubjectPermissions(subject, permissionIds, teamId, options) {
200
+ const { modelHasPermissions: table } = this.t;
201
+ return this.attach(table, "permissionId", table.permissionId, subject, permissionIds, teamId, options);
180
202
  }
181
203
  detachSubjectPermissions(subject, permissionIds, teamId) {
182
204
  return this.detach(
@@ -187,18 +209,41 @@ var DrizzleRbacAdapter = class _DrizzleRbacAdapter {
187
209
  teamId
188
210
  );
189
211
  }
190
- getSubjectIdsWithRoles(subjectType, roleIds, teamId) {
191
- return this.reverse(this.t.modelHasRoles, this.t.modelHasRoles.roleId, subjectType, roleIds, teamId);
212
+ getSubjectIdsWithRoles(subjectType, roleIds, teamId, options) {
213
+ return this.reverse(this.t.modelHasRoles, this.t.modelHasRoles.roleId, subjectType, roleIds, teamId, options);
192
214
  }
193
- getSubjectIdsWithPermissions(subjectType, permissionIds, teamId) {
215
+ getSubjectIdsWithPermissions(subjectType, permissionIds, teamId, options) {
194
216
  return this.reverse(
195
217
  this.t.modelHasPermissions,
196
218
  this.t.modelHasPermissions.permissionId,
197
219
  subjectType,
198
220
  permissionIds,
199
- teamId
221
+ teamId,
222
+ options
200
223
  );
201
224
  }
225
+ // ----------------------------------------------------------- expiry (optional)
226
+ async getSubjectRoleLinks(subject, teamId) {
227
+ return (await this.linkRows(this.t.modelHasRoles, this.t.modelHasRoles.roleId, subject, teamId)).map(toRoleLink);
228
+ }
229
+ async getSubjectPermissionLinks(subject, teamId) {
230
+ const { modelHasPermissions: table } = this.t;
231
+ return (await this.linkRows(table, table.permissionId, subject, teamId)).map(toPermissionLink);
232
+ }
233
+ async deleteExpiredLinks(now) {
234
+ if (!this.supportsExpiry) return { roles: 0, permissions: 0 };
235
+ const remove = async (table, target) => {
236
+ const query = this.db.delete(table).where(drizzleOrm.lte(table.expiresAt, now));
237
+ if (this.dialect !== "mysql") return (await query.returning({ id: target })).length;
238
+ const result = await query;
239
+ const header = Array.isArray(result) ? result[0] : result;
240
+ return Number(header?.affectedRows ?? header?.rowsAffected ?? 0);
241
+ };
242
+ return {
243
+ roles: await remove(this.t.modelHasRoles, this.t.modelHasRoles.roleId),
244
+ permissions: await remove(this.t.modelHasPermissions, this.t.modelHasPermissions.permissionId)
245
+ };
246
+ }
202
247
  // ------------------------------------------------------------- bulk (optional)
203
248
  async getSubjectLinks(subject) {
204
249
  const where = { subjectType: subject.type, subjectId: String(subject.id) };
@@ -245,21 +290,18 @@ var DrizzleRbacAdapter = class _DrizzleRbacAdapter {
245
290
  if (where.subjectType !== void 0) filters.push(drizzleOrm.eq(table.subjectType, where.subjectType));
246
291
  if (where.subjectId !== void 0) filters.push(drizzleOrm.eq(table.subjectId, where.subjectId));
247
292
  if (where.teamId !== void 0) filters.push(drizzleOrm.eq(table.teamId, where.teamId));
248
- const rows = await db.select({ id: target, subjectType: table.subjectType, subjectId: table.subjectId, teamId: table.teamId }).from(table).where(drizzleOrm.and(...filters));
293
+ const rows = await db.select(this.linkColumns(table, target)).from(table).where(drizzleOrm.and(...filters));
249
294
  return rows.filter(
250
295
  (row) => (where.subjectType === void 0 || row.subjectType === where.subjectType) && (where.subjectId === void 0 || row.subjectId === where.subjectId) && (where.teamId === void 0 || row.teamId === where.teamId)
251
296
  );
252
297
  }
253
298
  static toSubjectLinks(roles, permissions) {
254
- const subject = (row) => ({ type: row.subjectType, id: row.subjectId });
255
- return {
256
- roles: roles.map((row) => ({ subject: subject(row), roleId: Number(row.id), teamId: teamFromKey(row.teamId) })),
257
- permissions: permissions.map((row) => ({
258
- subject: subject(row),
259
- permissionId: Number(row.id),
260
- teamId: teamFromKey(row.teamId)
261
- }))
262
- };
299
+ return { roles: roles.map(toRoleLink), permissions: permissions.map(toPermissionLink) };
300
+ }
301
+ /** The columns of a link row; `expiresAt` only with `{ expiry: true }` (1.1 tables don't have it). */
302
+ linkColumns(table, target) {
303
+ const columns = { id: target, subjectType: table.subjectType, subjectId: table.subjectId, teamId: table.teamId };
304
+ return this.supportsExpiry ? { ...columns, expiresAt: table.expiresAt } : columns;
263
305
  }
264
306
  // -------------------------------------------------------------------- helpers
265
307
  static first(rows) {
@@ -294,22 +336,28 @@ var DrizzleRbacAdapter = class _DrizzleRbacAdapter {
294
336
  }
295
337
  /** The subject's link rows, compared exactly (see `sameLink`). */
296
338
  async linkRows(table, target, subject, teamId) {
297
- const rows = await this.db.select({ id: target, subjectType: table.subjectType, subjectId: table.subjectId, teamId: table.teamId }).from(table).where(drizzleOrm.and(..._DrizzleRbacAdapter.subjectWhere(table, subject, teamId)));
339
+ const rows = await this.db.select(this.linkColumns(table, target)).from(table).where(drizzleOrm.and(..._DrizzleRbacAdapter.subjectWhere(table, subject, teamId)));
298
340
  return rows.filter((row) => sameLink(row, subject.type, String(subject.id), teamKey(teamId)));
299
341
  }
300
342
  async getLinks(table, target, subject, teamId) {
301
343
  return (await this.linkRows(table, target, subject, teamId)).map((row) => Number(row.id));
302
344
  }
303
- async attach(table, targetKey, subject, targetIds, teamId) {
345
+ async attach(table, targetKey, target, subject, targetIds, teamId, options) {
304
346
  const ids = toIntIds(targetIds);
305
347
  if (ids.length === 0) return;
306
348
  const base = { subjectType: subject.type, subjectId: String(subject.id), teamId: teamKey(teamId) };
349
+ const expiry = this.supportsExpiry ? { expiresAt: options?.expiresAt ?? null } : {};
307
350
  await this.insertIgnoringDuplicates(
308
351
  table,
309
- ids.map((id) => ({ ...base, [targetKey]: id })),
352
+ ids.map((id) => ({ ...base, [targetKey]: id, ...expiry })),
310
353
  "subjectType",
311
354
  table.subjectType
312
355
  );
356
+ if (expiry.expiresAt === void 0) return;
357
+ const wanted = new Set(ids);
358
+ const held = (await this.linkRows(table, target, subject, teamId)).map((row) => Number(row.id)).filter((id) => wanted.has(id));
359
+ if (held.length === 0) return;
360
+ await this.db.update(table).set({ expiresAt: expiry.expiresAt }).where(drizzleOrm.and(..._DrizzleRbacAdapter.subjectWhere(table, subject, teamId), drizzleOrm.inArray(target, held)));
313
361
  }
314
362
  async detach(table, target, subject, targetIds, teamId) {
315
363
  const wanted = targetIds === void 0 ? void 0 : new Set(toIntIds(targetIds));
@@ -318,11 +366,13 @@ var DrizzleRbacAdapter = class _DrizzleRbacAdapter {
318
366
  if (ids.length === 0) return;
319
367
  await this.db.delete(table).where(drizzleOrm.and(..._DrizzleRbacAdapter.subjectWhere(table, subject, teamId), drizzleOrm.inArray(target, ids)));
320
368
  }
321
- async reverse(table, target, subjectType, targetIds, teamId) {
369
+ async reverse(table, target, subjectType, targetIds, teamId, options) {
322
370
  const ids = toIntIds(targetIds);
323
371
  if (ids.length === 0) return [];
324
372
  const team = teamKey(teamId);
325
- const rows = await this.db.selectDistinct({ subjectType: table.subjectType, subjectId: table.subjectId, teamId: table.teamId }).from(table).where(drizzleOrm.and(drizzleOrm.eq(table.subjectType, subjectType), drizzleOrm.eq(table.teamId, team), drizzleOrm.inArray(target, ids)));
373
+ const where = [drizzleOrm.eq(table.subjectType, subjectType), drizzleOrm.eq(table.teamId, team), drizzleOrm.inArray(target, ids)];
374
+ if (this.supportsExpiry && options) where.push(drizzleOrm.or(drizzleOrm.isNull(table.expiresAt), drizzleOrm.gt(table.expiresAt, options.now)));
375
+ const rows = await this.db.selectDistinct({ subjectType: table.subjectType, subjectId: table.subjectId, teamId: table.teamId }).from(table).where(drizzleOrm.and(...where));
326
376
  const exact = rows.filter((row) => sameLink(row, subjectType, void 0, team));
327
377
  return [...new Set(exact.map((row) => row.subjectId))];
328
378
  }
@@ -356,6 +406,9 @@ var timestamps = () => ({
356
406
  updatedAt: mysqlCore.timestamp("updated_at", { mode: "date" }).notNull().defaultNow()
357
407
  });
358
408
  function mysqlRbacSchema(options = {}) {
409
+ return options.expiry === true ? buildMysqlExpiry(options) : buildMysql(options);
410
+ }
411
+ function buildMysql(options) {
359
412
  const n = tableNames(options);
360
413
  const permissions = mysqlCore.mysqlTable(
361
414
  n.permissions,
@@ -423,10 +476,50 @@ function mysqlRbacSchema(options = {}) {
423
476
  );
424
477
  return { permissions, roles, roleHasPermissions, modelHasRoles, modelHasPermissions };
425
478
  }
479
+ function buildMysqlExpiry(options) {
480
+ const n = tableNames(options);
481
+ const { permissions, roles, roleHasPermissions } = buildMysql(options);
482
+ const modelHasRoles = mysqlCore.mysqlTable(
483
+ n.modelHasRoles,
484
+ {
485
+ roleId: mysqlCore.int("role_id").notNull().references(() => roles.id, { onDelete: "cascade" }),
486
+ subjectType: exactVarchar("subject_type", { length: MYSQL_LENGTHS.subjectType }).notNull(),
487
+ subjectId: exactVarchar("subject_id", { length: MYSQL_LENGTHS.subjectId }).notNull(),
488
+ /** `''` = global (no team). */
489
+ teamId: exactVarchar("team_id", { length: MYSQL_LENGTHS.teamId }).notNull().default(""),
490
+ /** When the link ends (`NULL` = never). Only used by an adapter created with `{ expiry: true }`. */
491
+ expiresAt: mysqlCore.datetime("expires_at", { mode: "date", fsp: 3 })
492
+ },
493
+ (t) => [
494
+ mysqlCore.primaryKey({ name: `${n.modelHasRoles}_pkey`, columns: [t.subjectType, t.subjectId, t.teamId, t.roleId] }),
495
+ mysqlCore.index(`${n.modelHasRoles}_role_id_index`).on(t.roleId)
496
+ ]
497
+ );
498
+ const modelHasPermissions = mysqlCore.mysqlTable(
499
+ n.modelHasPermissions,
500
+ {
501
+ permissionId: mysqlCore.int("permission_id").notNull().references(() => permissions.id, { onDelete: "cascade" }),
502
+ subjectType: exactVarchar("subject_type", { length: MYSQL_LENGTHS.subjectType }).notNull(),
503
+ subjectId: exactVarchar("subject_id", { length: MYSQL_LENGTHS.subjectId }).notNull(),
504
+ /** `''` = global (no team). */
505
+ teamId: exactVarchar("team_id", { length: MYSQL_LENGTHS.teamId }).notNull().default(""),
506
+ /** When the link ends (`NULL` = never). Only used by an adapter created with `{ expiry: true }`. */
507
+ expiresAt: mysqlCore.datetime("expires_at", { mode: "date", fsp: 3 })
508
+ },
509
+ (t) => [
510
+ mysqlCore.primaryKey({
511
+ name: `${n.modelHasPermissions}_pkey`,
512
+ columns: [t.subjectType, t.subjectId, t.teamId, t.permissionId]
513
+ }),
514
+ mysqlCore.index(`${n.modelHasPermissions}_permission_id_index`).on(t.permissionId)
515
+ ]
516
+ );
517
+ return { permissions, roles, roleHasPermissions, modelHasRoles, modelHasPermissions };
518
+ }
426
519
 
427
520
  // src/mysql.ts
428
- function drizzleMysqlAdapter(db, schema = mysqlRbacSchema()) {
429
- return new DrizzleRbacAdapter(db, schema, "mysql");
521
+ function drizzleMysqlAdapter(db, schema = mysqlRbacSchema(), options = {}) {
522
+ return new DrizzleRbacAdapter(db, schema, "mysql", options);
430
523
  }
431
524
 
432
525
  exports.drizzleMysqlAdapter = drizzleMysqlAdapter;