@elysiumjs/mnemosyne 0.1.2 → 0.3.1
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 +90 -0
- package/README.md +19 -16
- package/dist/cache.d.ts +134 -0
- package/dist/database.d.ts +127 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +2 -0
- package/dist/interfaces.d.ts +360 -0
- package/dist/keyv-redis.d.ts +88 -0
- package/dist/mnemosyne.module.d.ts +44 -0
- package/dist/model.d.ts +68 -0
- package/dist/repository.d.ts +96 -0
- package/dist/tenancy.d.ts +201 -0
- package/package.json +21 -12
- package/bunfig.toml +0 -19
- package/src/cache.ts +0 -218
- package/src/database.ts +0 -151
- package/src/index.ts +0 -47
- package/src/interfaces.ts +0 -449
- package/src/keyv-redis.ts +0 -313
- package/src/model.ts +0 -189
- package/src/repository.ts +0 -137
- package/src/tenancy.ts +0 -291
- package/tests/cache.test.ts +0 -464
- package/tests/database.test.ts +0 -249
- package/tests/model.test.ts +0 -234
- package/tests/repository.test.ts +0 -177
- package/tests/setup.ts +0 -26
- package/tsconfig.json +0 -12
package/src/tenancy.ts
DELETED
|
@@ -1,291 +0,0 @@
|
|
|
1
|
-
// Copyright (c) 2026-present Workbud Technologies Inc. All rights reserved.
|
|
2
|
-
//
|
|
3
|
-
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
-
// you may not use this file except in compliance with the License.
|
|
5
|
-
// You may obtain a copy of the License at
|
|
6
|
-
//
|
|
7
|
-
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
-
//
|
|
9
|
-
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
-
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
-
// See the License for the specific language governing permissions and
|
|
13
|
-
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
import type { Context } from '@elysiumjs/core';
|
|
16
|
-
import type { TenancyStrategy } from './interfaces';
|
|
17
|
-
|
|
18
|
-
import { Application, Middleware } from '@elysiumjs/core';
|
|
19
|
-
|
|
20
|
-
// ============================================================================
|
|
21
|
-
// Types
|
|
22
|
-
// ============================================================================
|
|
23
|
-
|
|
24
|
-
/**
|
|
25
|
-
* The tenancy mode for the application.
|
|
26
|
-
* - `schema`: Each tenant has their own Postgres schema.
|
|
27
|
-
* - `rls`: Row-Level Security with a shared schema and a tenant column.
|
|
28
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
29
|
-
*/
|
|
30
|
-
export type TenancyMode = 'schema' | 'rls';
|
|
31
|
-
|
|
32
|
-
/**
|
|
33
|
-
* Global tenancy configuration for the application.
|
|
34
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
35
|
-
*/
|
|
36
|
-
export interface TenancyConfig {
|
|
37
|
-
/**
|
|
38
|
-
* The tenancy mode to use.
|
|
39
|
-
*/
|
|
40
|
-
mode: TenancyMode;
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Configuration specific to RLS mode.
|
|
44
|
-
*/
|
|
45
|
-
rls?: {
|
|
46
|
-
/**
|
|
47
|
-
* The PostgreSQL session variable used to store the current tenant.
|
|
48
|
-
* @default 'app.current_tenant'
|
|
49
|
-
*/
|
|
50
|
-
sessionVariable?: string;
|
|
51
|
-
|
|
52
|
-
/**
|
|
53
|
-
* The default column name used for tenant isolation in RLS policies.
|
|
54
|
-
* @default 'tenant_id'
|
|
55
|
-
*/
|
|
56
|
-
defaultColumn?: string;
|
|
57
|
-
};
|
|
58
|
-
|
|
59
|
-
/**
|
|
60
|
-
* Configuration specific to schema-based tenancy.
|
|
61
|
-
*/
|
|
62
|
-
schema?: {
|
|
63
|
-
/**
|
|
64
|
-
* Whether to automatically create tenant schemas on first use.
|
|
65
|
-
*/
|
|
66
|
-
autoCreate?: boolean;
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
|
|
70
|
-
/**
|
|
71
|
-
* Per-model tenancy configuration, used to control how a specific model
|
|
72
|
-
* interacts with the tenancy system.
|
|
73
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
74
|
-
*/
|
|
75
|
-
export interface ModelTenancyConfig {
|
|
76
|
-
/**
|
|
77
|
-
* Whether tenancy is enabled for this model.
|
|
78
|
-
*/
|
|
79
|
-
enabled: boolean;
|
|
80
|
-
|
|
81
|
-
/**
|
|
82
|
-
* Override the global tenancy mode for this model.
|
|
83
|
-
*/
|
|
84
|
-
mode?: TenancyMode;
|
|
85
|
-
|
|
86
|
-
/**
|
|
87
|
-
* The column name used for RLS tenant isolation in this model.
|
|
88
|
-
*/
|
|
89
|
-
column?: string;
|
|
90
|
-
|
|
91
|
-
/**
|
|
92
|
-
* Custom RLS policy name for this model.
|
|
93
|
-
*/
|
|
94
|
-
policyName?: string;
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
// ============================================================================
|
|
98
|
-
// Internal State
|
|
99
|
-
// ============================================================================
|
|
100
|
-
|
|
101
|
-
let _strategy: TenancyStrategy<unknown, unknown> | null = null;
|
|
102
|
-
|
|
103
|
-
// ============================================================================
|
|
104
|
-
// Configuration
|
|
105
|
-
// ============================================================================
|
|
106
|
-
|
|
107
|
-
/**
|
|
108
|
-
* Gets the current global tenancy configuration.
|
|
109
|
-
*
|
|
110
|
-
* Reads from the 'elysium:mnemosyne' app config key. Returns a default
|
|
111
|
-
* configuration if the app is not yet initialized or no tenancy config is set.
|
|
112
|
-
*
|
|
113
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
114
|
-
* @returns The current tenancy configuration.
|
|
115
|
-
*/
|
|
116
|
-
export const getTenancyConfig = (): TenancyConfig => {
|
|
117
|
-
const app = Application.instance;
|
|
118
|
-
if (!app) {
|
|
119
|
-
return { mode: 'schema' };
|
|
120
|
-
}
|
|
121
|
-
const config = app.getConfig<{ tenancy?: TenancyConfig }>('elysium:mnemosyne' as any);
|
|
122
|
-
return config?.tenancy ?? { mode: 'schema' };
|
|
123
|
-
};
|
|
124
|
-
|
|
125
|
-
// ============================================================================
|
|
126
|
-
// Strategy Registry
|
|
127
|
-
// ============================================================================
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Registers the tenancy strategy to use for table resolution and isolation.
|
|
131
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
132
|
-
* @param strategy The tenancy strategy to register.
|
|
133
|
-
*/
|
|
134
|
-
export const registerTenancyStrategy = (strategy: TenancyStrategy<unknown, unknown>): void => {
|
|
135
|
-
_strategy = strategy;
|
|
136
|
-
};
|
|
137
|
-
|
|
138
|
-
/**
|
|
139
|
-
* Gets the currently registered tenancy strategy.
|
|
140
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
141
|
-
* @returns The current tenancy strategy, or `null` if none is registered.
|
|
142
|
-
*/
|
|
143
|
-
export const getTenancyStrategy = (): TenancyStrategy<unknown, unknown> | null => {
|
|
144
|
-
return _strategy;
|
|
145
|
-
};
|
|
146
|
-
|
|
147
|
-
// ============================================================================
|
|
148
|
-
// Context Management
|
|
149
|
-
// ============================================================================
|
|
150
|
-
|
|
151
|
-
/**
|
|
152
|
-
* Gets the current tenant from the async context.
|
|
153
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
154
|
-
* @returns The current tenant identifier, or `null` if not set.
|
|
155
|
-
*/
|
|
156
|
-
export const getCurrentTenant = (): string | null => {
|
|
157
|
-
if (!Application.instance || !Application.context.getStore) {
|
|
158
|
-
return null;
|
|
159
|
-
}
|
|
160
|
-
const store = Application.context.getStore();
|
|
161
|
-
return (store?.get('tenant') ?? null) as string | null;
|
|
162
|
-
};
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Runs a callback within the context of a specific tenant.
|
|
166
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
167
|
-
* @param tenant The tenant identifier to set.
|
|
168
|
-
* @param callback The function to execute within the tenant context.
|
|
169
|
-
* @returns The return value of the callback.
|
|
170
|
-
*/
|
|
171
|
-
export const withTenant = <T>(tenant: string, callback: () => T): T => {
|
|
172
|
-
const currentStore = Application.context.getStore();
|
|
173
|
-
const newStore = new Map(currentStore);
|
|
174
|
-
newStore.set('tenant', tenant);
|
|
175
|
-
return Application.context.run(newStore, callback);
|
|
176
|
-
};
|
|
177
|
-
|
|
178
|
-
// ============================================================================
|
|
179
|
-
// TenantMiddleware Base Class
|
|
180
|
-
// ============================================================================
|
|
181
|
-
|
|
182
|
-
/**
|
|
183
|
-
* Base class for tenant-aware middleware.
|
|
184
|
-
*
|
|
185
|
-
* The middleware extracts the tenant identifier from an HTTP header and
|
|
186
|
-
* sets `ctx.tenant`. For RLS mode, route handlers should use the driver's
|
|
187
|
-
* isolation mechanism to wrap their queries in a tenant-scoped transaction.
|
|
188
|
-
*
|
|
189
|
-
* @example
|
|
190
|
-
* ```typescript
|
|
191
|
-
* class OrganizationMiddleware extends TenantMiddleware {
|
|
192
|
-
* protected header = 'x-organization-id';
|
|
193
|
-
* protected allowPublic = false;
|
|
194
|
-
*
|
|
195
|
-
* protected async validateTenant(orgId: string): Promise<boolean> {
|
|
196
|
-
* return Organization.exists(orgId);
|
|
197
|
-
* }
|
|
198
|
-
* }
|
|
199
|
-
* ```
|
|
200
|
-
*
|
|
201
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
202
|
-
*/
|
|
203
|
-
export abstract class TenantMiddleware extends Middleware {
|
|
204
|
-
/**
|
|
205
|
-
* The HTTP header name from which the tenant ID is read.
|
|
206
|
-
*/
|
|
207
|
-
protected header: string = 'x-tenant-id';
|
|
208
|
-
|
|
209
|
-
/**
|
|
210
|
-
* Whether requests without a tenant ID are allowed (defaulting to 'public').
|
|
211
|
-
*/
|
|
212
|
-
protected allowPublic: boolean = true;
|
|
213
|
-
|
|
214
|
-
/**
|
|
215
|
-
* The database connection name used for tenant operations.
|
|
216
|
-
*/
|
|
217
|
-
protected connectionName: string = 'default';
|
|
218
|
-
|
|
219
|
-
/**
|
|
220
|
-
* Validates that a tenant identifier is legitimate.
|
|
221
|
-
* Override this to check against your data store.
|
|
222
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
223
|
-
* @param _tenant The tenant identifier to validate.
|
|
224
|
-
* @returns `true` if the tenant is valid, `false` otherwise.
|
|
225
|
-
*/
|
|
226
|
-
protected async validateTenant(_tenant: string): Promise<boolean> {
|
|
227
|
-
return true;
|
|
228
|
-
}
|
|
229
|
-
|
|
230
|
-
/**
|
|
231
|
-
* Hook called after a tenant has been successfully resolved.
|
|
232
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
233
|
-
* @param _tenant The resolved tenant identifier.
|
|
234
|
-
* @param _ctx The request context.
|
|
235
|
-
*/
|
|
236
|
-
protected async onTenantResolved(_tenant: string, _ctx: Context): Promise<void> {}
|
|
237
|
-
|
|
238
|
-
/**
|
|
239
|
-
* Called when the tenant cannot be determined or is invalid.
|
|
240
|
-
* Throws an appropriate error by default.
|
|
241
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
242
|
-
* @param reason Whether the tenant is 'missing' or 'invalid'.
|
|
243
|
-
* @param _ctx The request context.
|
|
244
|
-
*/
|
|
245
|
-
protected onTenantError(reason: 'missing' | 'invalid', _ctx: Context): never {
|
|
246
|
-
if (reason === 'missing') {
|
|
247
|
-
throw new Error('Tenant ID required');
|
|
248
|
-
} else {
|
|
249
|
-
throw new Error('Invalid tenant');
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
|
|
253
|
-
/**
|
|
254
|
-
* Extracts and validates the tenant from the incoming request.
|
|
255
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
256
|
-
* @param ctx The request context.
|
|
257
|
-
*/
|
|
258
|
-
public async onRequest(ctx: Context): Promise<any> {
|
|
259
|
-
const tenantId = ctx.request.headers.get(this.header);
|
|
260
|
-
|
|
261
|
-
if (!tenantId || tenantId === 'public') {
|
|
262
|
-
if (this.allowPublic) {
|
|
263
|
-
(ctx as any).tenant = 'public';
|
|
264
|
-
return;
|
|
265
|
-
}
|
|
266
|
-
return this.onTenantError('missing', ctx);
|
|
267
|
-
}
|
|
268
|
-
|
|
269
|
-
const isValid = await this.validateTenant(tenantId);
|
|
270
|
-
if (!isValid) {
|
|
271
|
-
return this.onTenantError('invalid', ctx);
|
|
272
|
-
}
|
|
273
|
-
|
|
274
|
-
(ctx as any).tenant = tenantId;
|
|
275
|
-
await this.onTenantResolved(tenantId, ctx);
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
|
|
279
|
-
/**
|
|
280
|
-
* A simple tenant middleware that allows public access by default.
|
|
281
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
282
|
-
*/
|
|
283
|
-
export class SimpleTenantMiddleware extends TenantMiddleware {}
|
|
284
|
-
|
|
285
|
-
/**
|
|
286
|
-
* A strict tenant middleware that rejects requests without a tenant ID.
|
|
287
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
288
|
-
*/
|
|
289
|
-
export class StrictTenantMiddleware extends TenantMiddleware {
|
|
290
|
-
protected allowPublic = false;
|
|
291
|
-
}
|