@dudousxd/nestjs-authz-drizzle 0.1.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.
- package/CHANGELOG.md +37 -0
- package/LICENSE +21 -0
- package/README.md +173 -0
- package/dist/authz-rbac.module.d.ts +47 -0
- package/dist/authz-rbac.module.d.ts.map +1 -0
- package/dist/authz-rbac.module.js +236 -0
- package/dist/authz-rbac.module.js.map +1 -0
- package/dist/drizzle-authz.store.d.ts +99 -0
- package/dist/drizzle-authz.store.d.ts.map +1 -0
- package/dist/drizzle-authz.store.js +307 -0
- package/dist/drizzle-authz.store.js.map +1 -0
- package/dist/index.d.ts +11 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/schema.d.ts +423 -0
- package/dist/schema.d.ts.map +1 -0
- package/dist/schema.js +169 -0
- package/dist/schema.js.map +1 -0
- package/dist/scope.d.ts +46 -0
- package/dist/scope.d.ts.map +1 -0
- package/dist/scope.js +95 -0
- package/dist/scope.js.map +1 -0
- package/dist/types.d.ts +46 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/package.json +74 -0
package/dist/schema.d.ts
ADDED
|
@@ -0,0 +1,423 @@
|
|
|
1
|
+
import type { AuthzStoreOptions, TableNames } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* Default table names — the canonical cross-adapter names (the TypeORM, MikroORM and Prisma
|
|
4
|
+
* adapters use the same ones), so a store swap keeps the data where it is.
|
|
5
|
+
*/
|
|
6
|
+
export declare const DEFAULT_TABLE_NAMES: {
|
|
7
|
+
readonly roles: "authz_roles";
|
|
8
|
+
readonly permissions: "authz_permissions";
|
|
9
|
+
readonly rolePermission: "authz_role_permission";
|
|
10
|
+
readonly userRole: "authz_user_role";
|
|
11
|
+
readonly userPermission: "authz_user_permission";
|
|
12
|
+
};
|
|
13
|
+
/**
|
|
14
|
+
* The "no tenant" sentinel for the user-role pivot's `tenantId`. A GLOBAL (unscoped) assignment
|
|
15
|
+
* stores this empty string rather than `NULL` so the column can be part of the composite primary
|
|
16
|
+
* key with portable uniqueness (SQL treats `NULL`s as distinct). Same value as the TypeORM adapter.
|
|
17
|
+
*/
|
|
18
|
+
export declare const GLOBAL_TENANT = "";
|
|
19
|
+
/** Resolve BYO table names over {@link DEFAULT_TABLE_NAMES}, validating each. */
|
|
20
|
+
export declare function resolveTableNames(tableNames?: TableNames): Required<TableNames>;
|
|
21
|
+
/**
|
|
22
|
+
* Build the five RBAC tables as Drizzle `pgTable`s. Pure — call it once at module scope and
|
|
23
|
+
* re-export the result from the schema file your `drizzle.config.ts` points at, so drizzle-kit
|
|
24
|
+
* generates the migration for you:
|
|
25
|
+
*
|
|
26
|
+
* ```ts
|
|
27
|
+
* // db/schema.ts
|
|
28
|
+
* import { createAuthzTables } from '@dudousxd/nestjs-authz-drizzle';
|
|
29
|
+
* export const authz = createAuthzTables(); // or { tableNames, schema }
|
|
30
|
+
* export const { roles: authzRoles, userRole: authzUserRole } = authz;
|
|
31
|
+
* ```
|
|
32
|
+
*
|
|
33
|
+
* Pass the SAME object to the store (`new DrizzleAuthzStore(db, { tables: authz })`) so the
|
|
34
|
+
* queries and the migration can never disagree. Index/constraint names are derived from the
|
|
35
|
+
* table names, so relocating the tables keeps them unique.
|
|
36
|
+
*/
|
|
37
|
+
export declare function createAuthzTables(opts?: Pick<AuthzStoreOptions, 'tableNames' | 'schema'>): {
|
|
38
|
+
roles: import("drizzle-orm/pg-core").PgTableWithColumns<{
|
|
39
|
+
name: string;
|
|
40
|
+
schema: undefined;
|
|
41
|
+
columns: {
|
|
42
|
+
id: import("drizzle-orm/pg-core").PgColumn<{
|
|
43
|
+
name: "id";
|
|
44
|
+
tableName: string;
|
|
45
|
+
dataType: "string";
|
|
46
|
+
columnType: "PgVarchar";
|
|
47
|
+
data: string;
|
|
48
|
+
driverParam: string;
|
|
49
|
+
notNull: true;
|
|
50
|
+
hasDefault: false;
|
|
51
|
+
isPrimaryKey: true;
|
|
52
|
+
isAutoincrement: false;
|
|
53
|
+
hasRuntimeDefault: false;
|
|
54
|
+
enumValues: [string, ...string[]];
|
|
55
|
+
baseColumn: never;
|
|
56
|
+
identity: undefined;
|
|
57
|
+
generated: undefined;
|
|
58
|
+
}, {}, {
|
|
59
|
+
length: 191;
|
|
60
|
+
}>;
|
|
61
|
+
name: import("drizzle-orm/pg-core").PgColumn<{
|
|
62
|
+
name: "name";
|
|
63
|
+
tableName: string;
|
|
64
|
+
dataType: "string";
|
|
65
|
+
columnType: "PgVarchar";
|
|
66
|
+
data: string;
|
|
67
|
+
driverParam: string;
|
|
68
|
+
notNull: true;
|
|
69
|
+
hasDefault: false;
|
|
70
|
+
isPrimaryKey: false;
|
|
71
|
+
isAutoincrement: false;
|
|
72
|
+
hasRuntimeDefault: false;
|
|
73
|
+
enumValues: [string, ...string[]];
|
|
74
|
+
baseColumn: never;
|
|
75
|
+
identity: undefined;
|
|
76
|
+
generated: undefined;
|
|
77
|
+
}, {}, {
|
|
78
|
+
length: 191;
|
|
79
|
+
}>;
|
|
80
|
+
guard: import("drizzle-orm/pg-core").PgColumn<{
|
|
81
|
+
name: "guard";
|
|
82
|
+
tableName: string;
|
|
83
|
+
dataType: "string";
|
|
84
|
+
columnType: "PgVarchar";
|
|
85
|
+
data: string;
|
|
86
|
+
driverParam: string;
|
|
87
|
+
notNull: false;
|
|
88
|
+
hasDefault: false;
|
|
89
|
+
isPrimaryKey: false;
|
|
90
|
+
isAutoincrement: false;
|
|
91
|
+
hasRuntimeDefault: false;
|
|
92
|
+
enumValues: [string, ...string[]];
|
|
93
|
+
baseColumn: never;
|
|
94
|
+
identity: undefined;
|
|
95
|
+
generated: undefined;
|
|
96
|
+
}, {}, {
|
|
97
|
+
length: 191;
|
|
98
|
+
}>;
|
|
99
|
+
createdAt: import("drizzle-orm/pg-core").PgColumn<{
|
|
100
|
+
name: "createdAt";
|
|
101
|
+
tableName: string;
|
|
102
|
+
dataType: "date";
|
|
103
|
+
columnType: "PgTimestamp";
|
|
104
|
+
data: Date;
|
|
105
|
+
driverParam: string;
|
|
106
|
+
notNull: true;
|
|
107
|
+
hasDefault: true;
|
|
108
|
+
isPrimaryKey: false;
|
|
109
|
+
isAutoincrement: false;
|
|
110
|
+
hasRuntimeDefault: false;
|
|
111
|
+
enumValues: undefined;
|
|
112
|
+
baseColumn: never;
|
|
113
|
+
identity: undefined;
|
|
114
|
+
generated: undefined;
|
|
115
|
+
}, {}, {}>;
|
|
116
|
+
};
|
|
117
|
+
dialect: "pg";
|
|
118
|
+
}>;
|
|
119
|
+
permissions: import("drizzle-orm/pg-core").PgTableWithColumns<{
|
|
120
|
+
name: string;
|
|
121
|
+
schema: undefined;
|
|
122
|
+
columns: {
|
|
123
|
+
id: import("drizzle-orm/pg-core").PgColumn<{
|
|
124
|
+
name: "id";
|
|
125
|
+
tableName: string;
|
|
126
|
+
dataType: "string";
|
|
127
|
+
columnType: "PgVarchar";
|
|
128
|
+
data: string;
|
|
129
|
+
driverParam: string;
|
|
130
|
+
notNull: true;
|
|
131
|
+
hasDefault: false;
|
|
132
|
+
isPrimaryKey: true;
|
|
133
|
+
isAutoincrement: false;
|
|
134
|
+
hasRuntimeDefault: false;
|
|
135
|
+
enumValues: [string, ...string[]];
|
|
136
|
+
baseColumn: never;
|
|
137
|
+
identity: undefined;
|
|
138
|
+
generated: undefined;
|
|
139
|
+
}, {}, {
|
|
140
|
+
length: 191;
|
|
141
|
+
}>;
|
|
142
|
+
name: import("drizzle-orm/pg-core").PgColumn<{
|
|
143
|
+
name: "name";
|
|
144
|
+
tableName: string;
|
|
145
|
+
dataType: "string";
|
|
146
|
+
columnType: "PgVarchar";
|
|
147
|
+
data: string;
|
|
148
|
+
driverParam: string;
|
|
149
|
+
notNull: true;
|
|
150
|
+
hasDefault: false;
|
|
151
|
+
isPrimaryKey: false;
|
|
152
|
+
isAutoincrement: false;
|
|
153
|
+
hasRuntimeDefault: false;
|
|
154
|
+
enumValues: [string, ...string[]];
|
|
155
|
+
baseColumn: never;
|
|
156
|
+
identity: undefined;
|
|
157
|
+
generated: undefined;
|
|
158
|
+
}, {}, {
|
|
159
|
+
length: 191;
|
|
160
|
+
}>;
|
|
161
|
+
guard: import("drizzle-orm/pg-core").PgColumn<{
|
|
162
|
+
name: "guard";
|
|
163
|
+
tableName: string;
|
|
164
|
+
dataType: "string";
|
|
165
|
+
columnType: "PgVarchar";
|
|
166
|
+
data: string;
|
|
167
|
+
driverParam: string;
|
|
168
|
+
notNull: false;
|
|
169
|
+
hasDefault: false;
|
|
170
|
+
isPrimaryKey: false;
|
|
171
|
+
isAutoincrement: false;
|
|
172
|
+
hasRuntimeDefault: false;
|
|
173
|
+
enumValues: [string, ...string[]];
|
|
174
|
+
baseColumn: never;
|
|
175
|
+
identity: undefined;
|
|
176
|
+
generated: undefined;
|
|
177
|
+
}, {}, {
|
|
178
|
+
length: 191;
|
|
179
|
+
}>;
|
|
180
|
+
createdAt: import("drizzle-orm/pg-core").PgColumn<{
|
|
181
|
+
name: "createdAt";
|
|
182
|
+
tableName: string;
|
|
183
|
+
dataType: "date";
|
|
184
|
+
columnType: "PgTimestamp";
|
|
185
|
+
data: Date;
|
|
186
|
+
driverParam: string;
|
|
187
|
+
notNull: true;
|
|
188
|
+
hasDefault: true;
|
|
189
|
+
isPrimaryKey: false;
|
|
190
|
+
isAutoincrement: false;
|
|
191
|
+
hasRuntimeDefault: false;
|
|
192
|
+
enumValues: undefined;
|
|
193
|
+
baseColumn: never;
|
|
194
|
+
identity: undefined;
|
|
195
|
+
generated: undefined;
|
|
196
|
+
}, {}, {}>;
|
|
197
|
+
};
|
|
198
|
+
dialect: "pg";
|
|
199
|
+
}>;
|
|
200
|
+
rolePermission: import("drizzle-orm/pg-core").PgTableWithColumns<{
|
|
201
|
+
name: string;
|
|
202
|
+
schema: undefined;
|
|
203
|
+
columns: {
|
|
204
|
+
roleId: import("drizzle-orm/pg-core").PgColumn<{
|
|
205
|
+
name: string;
|
|
206
|
+
tableName: string;
|
|
207
|
+
dataType: "string";
|
|
208
|
+
columnType: "PgVarchar";
|
|
209
|
+
data: string;
|
|
210
|
+
driverParam: string;
|
|
211
|
+
notNull: true;
|
|
212
|
+
hasDefault: false;
|
|
213
|
+
isPrimaryKey: false;
|
|
214
|
+
isAutoincrement: false;
|
|
215
|
+
hasRuntimeDefault: false;
|
|
216
|
+
enumValues: [string, ...string[]];
|
|
217
|
+
baseColumn: never;
|
|
218
|
+
identity: undefined;
|
|
219
|
+
generated: undefined;
|
|
220
|
+
}, {}, {
|
|
221
|
+
length: 191;
|
|
222
|
+
}>;
|
|
223
|
+
permissionId: import("drizzle-orm/pg-core").PgColumn<{
|
|
224
|
+
name: string;
|
|
225
|
+
tableName: string;
|
|
226
|
+
dataType: "string";
|
|
227
|
+
columnType: "PgVarchar";
|
|
228
|
+
data: string;
|
|
229
|
+
driverParam: string;
|
|
230
|
+
notNull: true;
|
|
231
|
+
hasDefault: false;
|
|
232
|
+
isPrimaryKey: false;
|
|
233
|
+
isAutoincrement: false;
|
|
234
|
+
hasRuntimeDefault: false;
|
|
235
|
+
enumValues: [string, ...string[]];
|
|
236
|
+
baseColumn: never;
|
|
237
|
+
identity: undefined;
|
|
238
|
+
generated: undefined;
|
|
239
|
+
}, {}, {
|
|
240
|
+
length: 191;
|
|
241
|
+
}>;
|
|
242
|
+
};
|
|
243
|
+
dialect: "pg";
|
|
244
|
+
}>;
|
|
245
|
+
userRole: import("drizzle-orm/pg-core").PgTableWithColumns<{
|
|
246
|
+
name: string;
|
|
247
|
+
schema: undefined;
|
|
248
|
+
columns: {
|
|
249
|
+
userType: import("drizzle-orm/pg-core").PgColumn<{
|
|
250
|
+
name: string;
|
|
251
|
+
tableName: string;
|
|
252
|
+
dataType: "string";
|
|
253
|
+
columnType: "PgVarchar";
|
|
254
|
+
data: string;
|
|
255
|
+
driverParam: string;
|
|
256
|
+
notNull: true;
|
|
257
|
+
hasDefault: false;
|
|
258
|
+
isPrimaryKey: false;
|
|
259
|
+
isAutoincrement: false;
|
|
260
|
+
hasRuntimeDefault: false;
|
|
261
|
+
enumValues: [string, ...string[]];
|
|
262
|
+
baseColumn: never;
|
|
263
|
+
identity: undefined;
|
|
264
|
+
generated: undefined;
|
|
265
|
+
}, {}, {
|
|
266
|
+
length: 191;
|
|
267
|
+
}>;
|
|
268
|
+
userId: import("drizzle-orm/pg-core").PgColumn<{
|
|
269
|
+
name: string;
|
|
270
|
+
tableName: string;
|
|
271
|
+
dataType: "string";
|
|
272
|
+
columnType: "PgVarchar";
|
|
273
|
+
data: string;
|
|
274
|
+
driverParam: string;
|
|
275
|
+
notNull: true;
|
|
276
|
+
hasDefault: false;
|
|
277
|
+
isPrimaryKey: false;
|
|
278
|
+
isAutoincrement: false;
|
|
279
|
+
hasRuntimeDefault: false;
|
|
280
|
+
enumValues: [string, ...string[]];
|
|
281
|
+
baseColumn: never;
|
|
282
|
+
identity: undefined;
|
|
283
|
+
generated: undefined;
|
|
284
|
+
}, {}, {
|
|
285
|
+
length: 191;
|
|
286
|
+
}>;
|
|
287
|
+
roleId: import("drizzle-orm/pg-core").PgColumn<{
|
|
288
|
+
name: string;
|
|
289
|
+
tableName: string;
|
|
290
|
+
dataType: "string";
|
|
291
|
+
columnType: "PgVarchar";
|
|
292
|
+
data: string;
|
|
293
|
+
driverParam: string;
|
|
294
|
+
notNull: true;
|
|
295
|
+
hasDefault: false;
|
|
296
|
+
isPrimaryKey: false;
|
|
297
|
+
isAutoincrement: false;
|
|
298
|
+
hasRuntimeDefault: false;
|
|
299
|
+
enumValues: [string, ...string[]];
|
|
300
|
+
baseColumn: never;
|
|
301
|
+
identity: undefined;
|
|
302
|
+
generated: undefined;
|
|
303
|
+
}, {}, {
|
|
304
|
+
length: 191;
|
|
305
|
+
}>;
|
|
306
|
+
tenantId: import("drizzle-orm/pg-core").PgColumn<{
|
|
307
|
+
name: "tenantId";
|
|
308
|
+
tableName: string;
|
|
309
|
+
dataType: "string";
|
|
310
|
+
columnType: "PgVarchar";
|
|
311
|
+
data: string;
|
|
312
|
+
driverParam: string;
|
|
313
|
+
notNull: true;
|
|
314
|
+
hasDefault: true;
|
|
315
|
+
isPrimaryKey: false;
|
|
316
|
+
isAutoincrement: false;
|
|
317
|
+
hasRuntimeDefault: false;
|
|
318
|
+
enumValues: [string, ...string[]];
|
|
319
|
+
baseColumn: never;
|
|
320
|
+
identity: undefined;
|
|
321
|
+
generated: undefined;
|
|
322
|
+
}, {}, {
|
|
323
|
+
length: 191;
|
|
324
|
+
}>;
|
|
325
|
+
source: import("drizzle-orm/pg-core").PgColumn<{
|
|
326
|
+
name: "source";
|
|
327
|
+
tableName: string;
|
|
328
|
+
dataType: "string";
|
|
329
|
+
columnType: "PgVarchar";
|
|
330
|
+
data: string;
|
|
331
|
+
driverParam: string;
|
|
332
|
+
notNull: true;
|
|
333
|
+
hasDefault: true;
|
|
334
|
+
isPrimaryKey: false;
|
|
335
|
+
isAutoincrement: false;
|
|
336
|
+
hasRuntimeDefault: false;
|
|
337
|
+
enumValues: [string, ...string[]];
|
|
338
|
+
baseColumn: never;
|
|
339
|
+
identity: undefined;
|
|
340
|
+
generated: undefined;
|
|
341
|
+
}, {}, {
|
|
342
|
+
length: 191;
|
|
343
|
+
}>;
|
|
344
|
+
};
|
|
345
|
+
dialect: "pg";
|
|
346
|
+
}>;
|
|
347
|
+
userPermission: import("drizzle-orm/pg-core").PgTableWithColumns<{
|
|
348
|
+
name: string;
|
|
349
|
+
schema: undefined;
|
|
350
|
+
columns: {
|
|
351
|
+
userType: import("drizzle-orm/pg-core").PgColumn<{
|
|
352
|
+
name: string;
|
|
353
|
+
tableName: string;
|
|
354
|
+
dataType: "string";
|
|
355
|
+
columnType: "PgVarchar";
|
|
356
|
+
data: string;
|
|
357
|
+
driverParam: string;
|
|
358
|
+
notNull: true;
|
|
359
|
+
hasDefault: false;
|
|
360
|
+
isPrimaryKey: false;
|
|
361
|
+
isAutoincrement: false;
|
|
362
|
+
hasRuntimeDefault: false;
|
|
363
|
+
enumValues: [string, ...string[]];
|
|
364
|
+
baseColumn: never;
|
|
365
|
+
identity: undefined;
|
|
366
|
+
generated: undefined;
|
|
367
|
+
}, {}, {
|
|
368
|
+
length: 191;
|
|
369
|
+
}>;
|
|
370
|
+
userId: import("drizzle-orm/pg-core").PgColumn<{
|
|
371
|
+
name: string;
|
|
372
|
+
tableName: string;
|
|
373
|
+
dataType: "string";
|
|
374
|
+
columnType: "PgVarchar";
|
|
375
|
+
data: string;
|
|
376
|
+
driverParam: string;
|
|
377
|
+
notNull: true;
|
|
378
|
+
hasDefault: false;
|
|
379
|
+
isPrimaryKey: false;
|
|
380
|
+
isAutoincrement: false;
|
|
381
|
+
hasRuntimeDefault: false;
|
|
382
|
+
enumValues: [string, ...string[]];
|
|
383
|
+
baseColumn: never;
|
|
384
|
+
identity: undefined;
|
|
385
|
+
generated: undefined;
|
|
386
|
+
}, {}, {
|
|
387
|
+
length: 191;
|
|
388
|
+
}>;
|
|
389
|
+
permissionId: import("drizzle-orm/pg-core").PgColumn<{
|
|
390
|
+
name: string;
|
|
391
|
+
tableName: string;
|
|
392
|
+
dataType: "string";
|
|
393
|
+
columnType: "PgVarchar";
|
|
394
|
+
data: string;
|
|
395
|
+
driverParam: string;
|
|
396
|
+
notNull: true;
|
|
397
|
+
hasDefault: false;
|
|
398
|
+
isPrimaryKey: false;
|
|
399
|
+
isAutoincrement: false;
|
|
400
|
+
hasRuntimeDefault: false;
|
|
401
|
+
enumValues: [string, ...string[]];
|
|
402
|
+
baseColumn: never;
|
|
403
|
+
identity: undefined;
|
|
404
|
+
generated: undefined;
|
|
405
|
+
}, {}, {
|
|
406
|
+
length: 191;
|
|
407
|
+
}>;
|
|
408
|
+
};
|
|
409
|
+
dialect: "pg";
|
|
410
|
+
}>;
|
|
411
|
+
};
|
|
412
|
+
/** The table set the store queries — what {@link createAuthzTables} returns. */
|
|
413
|
+
export type AuthzTables = ReturnType<typeof createAuthzTables>;
|
|
414
|
+
/** The default tables (canonical names, default schema). */
|
|
415
|
+
export declare const authzTables: AuthzTables;
|
|
416
|
+
/**
|
|
417
|
+
* The idempotent Postgres DDL for a table set — `CREATE TABLE IF NOT EXISTS` + `CREATE INDEX IF
|
|
418
|
+
* NOT EXISTS`, never destructive. It is what {@link DrizzleAuthzStore.ensureSchema} runs; prefer
|
|
419
|
+
* drizzle-kit (spread the tables into your schema) when you already manage migrations with it.
|
|
420
|
+
* Table/schema names are read back off the Drizzle table config, so BYO names are honored.
|
|
421
|
+
*/
|
|
422
|
+
export declare function authzSchemaDdl(tables?: AuthzTables): string[];
|
|
423
|
+
//# sourceMappingURL=schema.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.d.ts","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,iBAAiB,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAEhE;;;GAGG;AACH,eAAO,MAAM,mBAAmB;;;;;;CAMtB,CAAC;AAEX;;;;GAIG;AACH,eAAO,MAAM,aAAa,KAAK,CAAC;AAEhC,iFAAiF;AACjF,wBAAgB,iBAAiB,CAAC,UAAU,CAAC,EAAE,UAAU,GAAG,QAAQ,CAAC,UAAU,CAAC,CAY/E;AAOD;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,iBAAiB,CAAC,IAAI,GAAE,IAAI,CAAC,iBAAiB,EAAE,YAAY,GAAG,QAAQ,CAAM;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;EA8D5F;AAED,gFAAgF;AAChF,MAAM,MAAM,WAAW,GAAG,UAAU,CAAC,OAAO,iBAAiB,CAAC,CAAC;AAE/D,4DAA4D;AAC5D,eAAO,MAAM,WAAW,EAAE,WAAiC,CAAC;AAI5D;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,GAAE,WAAyB,GAAG,MAAM,EAAE,CA4E1E"}
|
package/dist/schema.js
ADDED
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { assertSafeIdentifier } from '@dudousxd/nestjs-authz';
|
|
2
|
+
import { DEFAULT_ROLE_SOURCE } from '@dudousxd/nestjs-authz/store-kit';
|
|
3
|
+
import { getTableConfig, index, pgSchema, pgTable, primaryKey, timestamp, varchar, } from 'drizzle-orm/pg-core';
|
|
4
|
+
/**
|
|
5
|
+
* Default table names — the canonical cross-adapter names (the TypeORM, MikroORM and Prisma
|
|
6
|
+
* adapters use the same ones), so a store swap keeps the data where it is.
|
|
7
|
+
*/
|
|
8
|
+
export const DEFAULT_TABLE_NAMES = {
|
|
9
|
+
roles: 'authz_roles',
|
|
10
|
+
permissions: 'authz_permissions',
|
|
11
|
+
rolePermission: 'authz_role_permission',
|
|
12
|
+
userRole: 'authz_user_role',
|
|
13
|
+
userPermission: 'authz_user_permission',
|
|
14
|
+
};
|
|
15
|
+
/**
|
|
16
|
+
* The "no tenant" sentinel for the user-role pivot's `tenantId`. A GLOBAL (unscoped) assignment
|
|
17
|
+
* stores this empty string rather than `NULL` so the column can be part of the composite primary
|
|
18
|
+
* key with portable uniqueness (SQL treats `NULL`s as distinct). Same value as the TypeORM adapter.
|
|
19
|
+
*/
|
|
20
|
+
export const GLOBAL_TENANT = '';
|
|
21
|
+
/** Resolve BYO table names over {@link DEFAULT_TABLE_NAMES}, validating each. */
|
|
22
|
+
export function resolveTableNames(tableNames) {
|
|
23
|
+
const names = {
|
|
24
|
+
roles: tableNames?.roles ?? DEFAULT_TABLE_NAMES.roles,
|
|
25
|
+
permissions: tableNames?.permissions ?? DEFAULT_TABLE_NAMES.permissions,
|
|
26
|
+
rolePermission: tableNames?.rolePermission ?? DEFAULT_TABLE_NAMES.rolePermission,
|
|
27
|
+
userRole: tableNames?.userRole ?? DEFAULT_TABLE_NAMES.userRole,
|
|
28
|
+
userPermission: tableNames?.userPermission ?? DEFAULT_TABLE_NAMES.userPermission,
|
|
29
|
+
};
|
|
30
|
+
for (const [key, name] of Object.entries(names)) {
|
|
31
|
+
assertSafeIdentifier(name, `table name (${key})`);
|
|
32
|
+
}
|
|
33
|
+
return names;
|
|
34
|
+
}
|
|
35
|
+
// Columns are named exactly like the TypeORM entities / Prisma models (camelCase), so every
|
|
36
|
+
// adapter reads and writes the same physical schema.
|
|
37
|
+
const id = () => varchar('id', { length: 191 }).primaryKey();
|
|
38
|
+
const ref = (name) => varchar(name, { length: 191 }).notNull();
|
|
39
|
+
/**
|
|
40
|
+
* Build the five RBAC tables as Drizzle `pgTable`s. Pure — call it once at module scope and
|
|
41
|
+
* re-export the result from the schema file your `drizzle.config.ts` points at, so drizzle-kit
|
|
42
|
+
* generates the migration for you:
|
|
43
|
+
*
|
|
44
|
+
* ```ts
|
|
45
|
+
* // db/schema.ts
|
|
46
|
+
* import { createAuthzTables } from '@dudousxd/nestjs-authz-drizzle';
|
|
47
|
+
* export const authz = createAuthzTables(); // or { tableNames, schema }
|
|
48
|
+
* export const { roles: authzRoles, userRole: authzUserRole } = authz;
|
|
49
|
+
* ```
|
|
50
|
+
*
|
|
51
|
+
* Pass the SAME object to the store (`new DrizzleAuthzStore(db, { tables: authz })`) so the
|
|
52
|
+
* queries and the migration can never disagree. Index/constraint names are derived from the
|
|
53
|
+
* table names, so relocating the tables keeps them unique.
|
|
54
|
+
*/
|
|
55
|
+
export function createAuthzTables(opts = {}) {
|
|
56
|
+
const names = resolveTableNames(opts.tableNames);
|
|
57
|
+
if (opts.schema !== undefined)
|
|
58
|
+
assertSafeIdentifier(opts.schema, 'schema name');
|
|
59
|
+
// `pgSchema('public')` is rejected by drizzle — the default schema IS `pgTable`.
|
|
60
|
+
const table = (opts.schema === undefined ? pgTable : pgSchema(opts.schema).table);
|
|
61
|
+
const roles = table(names.roles, {
|
|
62
|
+
id: id(),
|
|
63
|
+
name: varchar('name', { length: 191 }).notNull().unique(`${names.roles}_name_unique`),
|
|
64
|
+
guard: varchar('guard', { length: 191 }),
|
|
65
|
+
createdAt: timestamp('createdAt', { mode: 'date' }).notNull().defaultNow(),
|
|
66
|
+
});
|
|
67
|
+
const permissions = table(names.permissions, {
|
|
68
|
+
id: id(),
|
|
69
|
+
name: varchar('name', { length: 191 }).notNull().unique(`${names.permissions}_name_unique`),
|
|
70
|
+
guard: varchar('guard', { length: 191 }),
|
|
71
|
+
createdAt: timestamp('createdAt', { mode: 'date' }).notNull().defaultNow(),
|
|
72
|
+
});
|
|
73
|
+
const rolePermission = table(names.rolePermission, { roleId: ref('roleId'), permissionId: ref('permissionId') }, (t) => [
|
|
74
|
+
primaryKey({ name: `${names.rolePermission}_pkey`, columns: [t.roleId, t.permissionId] }),
|
|
75
|
+
]);
|
|
76
|
+
const userRole = table(names.userRole, {
|
|
77
|
+
userType: ref('userType'),
|
|
78
|
+
userId: ref('userId'),
|
|
79
|
+
roleId: ref('roleId'),
|
|
80
|
+
tenantId: varchar('tenantId', { length: 191 }).notNull().default(GLOBAL_TENANT),
|
|
81
|
+
/** Where the assignment came from (`'manual'`, `'sso'`, …) — see `setUserRoles`. */
|
|
82
|
+
source: varchar('source', { length: 191 }).notNull().default(DEFAULT_ROLE_SOURCE),
|
|
83
|
+
}, (t) => [
|
|
84
|
+
primaryKey({
|
|
85
|
+
name: `${names.userRole}_pkey`,
|
|
86
|
+
columns: [t.userType, t.userId, t.roleId, t.tenantId, t.source],
|
|
87
|
+
}),
|
|
88
|
+
index(`${names.userRole}_user_idx`).on(t.userType, t.userId),
|
|
89
|
+
]);
|
|
90
|
+
const userPermission = table(names.userPermission, { userType: ref('userType'), userId: ref('userId'), permissionId: ref('permissionId') }, (t) => [
|
|
91
|
+
primaryKey({
|
|
92
|
+
name: `${names.userPermission}_pkey`,
|
|
93
|
+
columns: [t.userType, t.userId, t.permissionId],
|
|
94
|
+
}),
|
|
95
|
+
index(`${names.userPermission}_user_idx`).on(t.userType, t.userId),
|
|
96
|
+
]);
|
|
97
|
+
return { roles, permissions, rolePermission, userRole, userPermission };
|
|
98
|
+
}
|
|
99
|
+
/** The default tables (canonical names, default schema). */
|
|
100
|
+
export const authzTables = createAuthzTables();
|
|
101
|
+
const q = (identifier) => `"${identifier}"`;
|
|
102
|
+
/**
|
|
103
|
+
* The idempotent Postgres DDL for a table set — `CREATE TABLE IF NOT EXISTS` + `CREATE INDEX IF
|
|
104
|
+
* NOT EXISTS`, never destructive. It is what {@link DrizzleAuthzStore.ensureSchema} runs; prefer
|
|
105
|
+
* drizzle-kit (spread the tables into your schema) when you already manage migrations with it.
|
|
106
|
+
* Table/schema names are read back off the Drizzle table config, so BYO names are honored.
|
|
107
|
+
*/
|
|
108
|
+
export function authzSchemaDdl(tables = authzTables) {
|
|
109
|
+
const qualified = (t) => {
|
|
110
|
+
const cfg = getTableConfig(t);
|
|
111
|
+
assertSafeIdentifier(cfg.name, 'table name');
|
|
112
|
+
if (cfg.schema !== undefined)
|
|
113
|
+
assertSafeIdentifier(cfg.schema, 'schema name');
|
|
114
|
+
return {
|
|
115
|
+
name: cfg.name,
|
|
116
|
+
ref: cfg.schema ? `${q(cfg.schema)}.${q(cfg.name)}` : q(cfg.name),
|
|
117
|
+
schema: cfg.schema,
|
|
118
|
+
};
|
|
119
|
+
};
|
|
120
|
+
const roles = qualified(tables.roles);
|
|
121
|
+
const permissions = qualified(tables.permissions);
|
|
122
|
+
const rolePermission = qualified(tables.rolePermission);
|
|
123
|
+
const userRole = qualified(tables.userRole);
|
|
124
|
+
const userPermission = qualified(tables.userPermission);
|
|
125
|
+
const ddl = [];
|
|
126
|
+
const schemas = new Set([roles, permissions, rolePermission, userRole, userPermission]
|
|
127
|
+
.map((t) => t.schema)
|
|
128
|
+
.filter((s) => s !== undefined));
|
|
129
|
+
for (const schema of schemas)
|
|
130
|
+
ddl.push(`CREATE SCHEMA IF NOT EXISTS ${q(schema)}`);
|
|
131
|
+
const createTable = (ref, parts) => `CREATE TABLE IF NOT EXISTS ${ref} (${parts.join(', ')})`;
|
|
132
|
+
const vc = (column) => `"${column}" varchar(191) NOT NULL`;
|
|
133
|
+
for (const t of [roles, permissions]) {
|
|
134
|
+
ddl.push(createTable(t.ref, [
|
|
135
|
+
'"id" varchar(191) PRIMARY KEY NOT NULL',
|
|
136
|
+
vc('name'),
|
|
137
|
+
'"guard" varchar(191)',
|
|
138
|
+
'"createdAt" timestamp DEFAULT now() NOT NULL',
|
|
139
|
+
`CONSTRAINT ${q(`${t.name}_name_unique`)} UNIQUE("name")`,
|
|
140
|
+
]));
|
|
141
|
+
}
|
|
142
|
+
ddl.push(createTable(rolePermission.ref, [
|
|
143
|
+
vc('roleId'),
|
|
144
|
+
vc('permissionId'),
|
|
145
|
+
`CONSTRAINT ${q(`${rolePermission.name}_pkey`)} PRIMARY KEY("roleId","permissionId")`,
|
|
146
|
+
]));
|
|
147
|
+
ddl.push(createTable(userRole.ref, [
|
|
148
|
+
vc('userType'),
|
|
149
|
+
vc('userId'),
|
|
150
|
+
vc('roleId'),
|
|
151
|
+
`"tenantId" varchar(191) DEFAULT '' NOT NULL`,
|
|
152
|
+
`"source" varchar(191) DEFAULT '${DEFAULT_ROLE_SOURCE}' NOT NULL`,
|
|
153
|
+
`CONSTRAINT ${q(`${userRole.name}_pkey`)} PRIMARY KEY("userType","userId","roleId","tenantId","source")`,
|
|
154
|
+
]));
|
|
155
|
+
ddl.push(createTable(userPermission.ref, [
|
|
156
|
+
vc('userType'),
|
|
157
|
+
vc('userId'),
|
|
158
|
+
vc('permissionId'),
|
|
159
|
+
`CONSTRAINT ${q(`${userPermission.name}_pkey`)} PRIMARY KEY("userType","userId","permissionId")`,
|
|
160
|
+
]));
|
|
161
|
+
// Tables created before per-source assignments: add the column (existing rows become
|
|
162
|
+
// 'manual'). Widening the primary key to include "source" is a manual migration — see README.
|
|
163
|
+
ddl.push(`ALTER TABLE ${userRole.ref} ADD COLUMN IF NOT EXISTS "source" varchar(191) DEFAULT '${DEFAULT_ROLE_SOURCE}' NOT NULL`);
|
|
164
|
+
for (const t of [userRole, userPermission]) {
|
|
165
|
+
ddl.push(`CREATE INDEX IF NOT EXISTS ${q(`${t.name}_user_idx`)} ON ${t.ref} ("userType","userId")`);
|
|
166
|
+
}
|
|
167
|
+
return ddl;
|
|
168
|
+
}
|
|
169
|
+
//# sourceMappingURL=schema.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema.js","sourceRoot":"","sources":["../src/schema.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,oBAAoB,EAAE,MAAM,wBAAwB,CAAC;AAC9D,OAAO,EAAE,mBAAmB,EAAE,MAAM,kCAAkC,CAAC;AACvE,OAAO,EACL,cAAc,EACd,KAAK,EACL,QAAQ,EACR,OAAO,EACP,UAAU,EACV,SAAS,EACT,OAAO,GACR,MAAM,qBAAqB,CAAC;AAG7B;;;GAGG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG;IACjC,KAAK,EAAE,aAAa;IACpB,WAAW,EAAE,mBAAmB;IAChC,cAAc,EAAE,uBAAuB;IACvC,QAAQ,EAAE,iBAAiB;IAC3B,cAAc,EAAE,uBAAuB;CAC/B,CAAC;AAEX;;;;GAIG;AACH,MAAM,CAAC,MAAM,aAAa,GAAG,EAAE,CAAC;AAEhC,iFAAiF;AACjF,MAAM,UAAU,iBAAiB,CAAC,UAAuB;IACvD,MAAM,KAAK,GAAyB;QAClC,KAAK,EAAE,UAAU,EAAE,KAAK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EAAE,CAAC,QAAQ,CAAC;QACZ,EAAE,CAAC,cAAc,CAAC;QAClB,cAAc,CAAC,CAAC,GAAG,cAAc,CAAC,IAAI,OAAO,CAAC,uCAAuC;KACtF,CAAC,CACH,CAAC;IACF,GAAG,CAAC,IAAI,CACN,WAAW,CAAC,QAAQ,CAAC,GAAG,EAAE;QACxB,EAAE,CAAC,UAAU,CAAC;QACd,EAAE,CAAC,QAAQ,CAAC;QACZ,EAAE,CAAC,QAAQ,CAAC;QACZ,6CAA6C;QAC7C,kCAAkC,mBAAmB,YAAY;QACjE,cAAc,CAAC,CAAC,GAAG,QAAQ,CAAC,IAAI,OAAO,CAAC,gEAAgE;KACzG,CAAC,CACH,CAAC;IACF,GAAG,CAAC,IAAI,CACN,WAAW,CAAC,cAAc,CAAC,GAAG,EAAE;QAC9B,EAAE,CAAC,UAAU,CAAC;QACd,EAAE,CAAC,QAAQ,CAAC;QACZ,EAAE,CAAC,cAAc,CAAC;QAClB,cAAc,CAAC,CAAC,GAAG,cAAc,CAAC,IAAI,OAAO,CAAC,kDAAkD;KACjG,CAAC,CACH,CAAC;IACF,qFAAqF;IACrF,8FAA8F;IAC9F,GAAG,CAAC,IAAI,CACN,eAAe,QAAQ,CAAC,GAAG,4DAA4D,mBAAmB,YAAY,CACvH,CAAC;IACF,KAAK,MAAM,CAAC,IAAI,CAAC,QAAQ,EAAE,cAAc,CAAC,EAAE,CAAC;QAC3C,GAAG,CAAC,IAAI,CACN,8BAA8B,CAAC,CAAC,GAAG,CAAC,CAAC,IAAI,WAAW,CAAC,OAAO,CAAC,CAAC,GAAG,wBAAwB,CAC1F,CAAC;IACJ,CAAC;IACD,OAAO,GAAG,CAAC;AACb,CAAC"}
|
package/dist/scope.d.ts
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import type { ScopeConstraint } from '@dudousxd/nestjs-authz';
|
|
2
|
+
import type { Type } from '@nestjs/common';
|
|
3
|
+
import { type Column, type SQL, Table } from 'drizzle-orm';
|
|
4
|
+
/**
|
|
5
|
+
* The minimal scope-resolving surface shared by `Gate` and `BoundGate`. Accepting this (rather
|
|
6
|
+
* than `Gate`) lets {@link applyScope} take either a Gate (current/context user) or a
|
|
7
|
+
* `gate.forUser(...)` BoundGate without a cast. Mirrors the TypeORM/MikroORM/Prisma adapters.
|
|
8
|
+
*/
|
|
9
|
+
export interface ScopeResolver {
|
|
10
|
+
scope(entity: Type<unknown>, ability?: string): Promise<ScopeConstraint>;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Where scope `field`s resolve: a Drizzle table (fields are its property keys, e.g. `authorId`,
|
|
14
|
+
* falling back to the DB column name, e.g. `author_id`), or an explicit field → column map.
|
|
15
|
+
* An explicit map is an allowlist: a policy can never reach a column you did not list, and it
|
|
16
|
+
* lets a scope reference columns of a JOINed table.
|
|
17
|
+
*/
|
|
18
|
+
export type ScopeColumns = Table | Record<string, Column>;
|
|
19
|
+
/**
|
|
20
|
+
* Compile an ORM-neutral {@link ScopeConstraint} (`gate.scope(Entity)`) into a Drizzle `WHERE`.
|
|
21
|
+
*
|
|
22
|
+
* Returns:
|
|
23
|
+
* - `undefined` for `allow-all` — pass it straight to `.where(...)` / `and(...)`, which both
|
|
24
|
+
* ignore `undefined`, so NO predicate is added (every row is visible);
|
|
25
|
+
* - `1 = 0` for `deny-all` — an always-false predicate;
|
|
26
|
+
* - otherwise the compiled condition tree.
|
|
27
|
+
*
|
|
28
|
+
* Empty semantics match the core AST and the sibling adapters: empty `in` → no rows, empty `nin`
|
|
29
|
+
* → all rows, empty `and` → all rows, empty `or` → no rows.
|
|
30
|
+
*
|
|
31
|
+
* SAFE: fields are resolved against the table's columns (or your explicit map) — an unknown field
|
|
32
|
+
* throws instead of reaching SQL — and values go through Drizzle's operators, so they are always
|
|
33
|
+
* bound parameters (and mapped by the column's driver mapping, e.g. `Date` → timestamp).
|
|
34
|
+
*/
|
|
35
|
+
export declare function compileScope(constraint: ScopeConstraint, columns: ScopeColumns): SQL | undefined;
|
|
36
|
+
/**
|
|
37
|
+
* Ergonomic entry point: resolve the query scope for the current (context) user via the Gate and
|
|
38
|
+
* compile it against `columns`. Equivalent to `compileScope(await gate.scope(entity, ability), columns)`.
|
|
39
|
+
*
|
|
40
|
+
* ```ts
|
|
41
|
+
* const scope = await applyScope(gate, Post, posts); // posts = your pgTable
|
|
42
|
+
* const rows = await db.select().from(posts).where(and(scope, eq(posts.published, true)));
|
|
43
|
+
* ```
|
|
44
|
+
*/
|
|
45
|
+
export declare function applyScope(gate: ScopeResolver, entity: Type<unknown>, columns: ScopeColumns, ability?: string): Promise<SQL | undefined>;
|
|
46
|
+
//# sourceMappingURL=scope.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"scope.d.ts","sourceRoot":"","sources":["../src/scope.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAkB,eAAe,EAAa,MAAM,wBAAwB,CAAC;AACzF,OAAO,KAAK,EAAE,IAAI,EAAE,MAAM,gBAAgB,CAAC;AAC3C,OAAO,EACL,KAAK,MAAM,EACX,KAAK,GAAG,EACR,KAAK,EAgBN,MAAM,aAAa,CAAC;AAErB;;;;GAIG;AACH,MAAM,WAAW,aAAa;IAC5B,KAAK,CAAC,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC,CAAC;CAC1E;AAED;;;;;GAKG;AACH,MAAM,MAAM,YAAY,GAAG,KAAK,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;AAqB1D;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,YAAY,CAAC,UAAU,EAAE,eAAe,EAAE,OAAO,EAAE,YAAY,GAAG,GAAG,GAAG,SAAS,CA8ChG;AAED;;;;;;;;GAQG;AACH,wBAAsB,UAAU,CAC9B,IAAI,EAAE,aAAa,EACnB,MAAM,EAAE,IAAI,CAAC,OAAO,CAAC,EACrB,OAAO,EAAE,YAAY,EACrB,OAAO,SAAY,GAClB,OAAO,CAAC,GAAG,GAAG,SAAS,CAAC,CAE1B"}
|