@elysiumjs/mnemosyne 0.1.0 → 0.1.2
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/bunfig.toml +19 -0
- package/package.json +5 -5
- package/src/index.ts +6 -16
- package/src/tenancy.ts +1 -1
- package/.turbo/turbo-build.log +0 -8
- package/.turbo/turbo-typecheck.log +0 -1
- package/coverage/.lcov.info.2cbccf8710225f8a.tmp +0 -301
- package/coverage/.lcov.info.80cb03b14eb4a841.tmp +0 -644
- package/coverage/lcov.info +0 -549
- package/dist/cache.d.ts +0 -113
- package/dist/database.d.ts +0 -82
- package/dist/index.d.ts +0 -17
- package/dist/index.js +0 -2
- package/dist/interfaces.d.ts +0 -350
- package/dist/keyv-redis.d.ts +0 -84
- package/dist/model.d.ts +0 -68
- package/dist/repository.d.ts +0 -96
- package/dist/tenancy.d.ts +0 -180
package/dist/tenancy.d.ts
DELETED
|
@@ -1,180 +0,0 @@
|
|
|
1
|
-
import type { Context } from '@elysiumjs/core';
|
|
2
|
-
import type { TenancyStrategy } from './interfaces';
|
|
3
|
-
import { Middleware } from '@elysiumjs/core';
|
|
4
|
-
/**
|
|
5
|
-
* The tenancy mode for the application.
|
|
6
|
-
* - `schema`: Each tenant has their own Postgres schema.
|
|
7
|
-
* - `rls`: Row-Level Security with a shared schema and a tenant column.
|
|
8
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
9
|
-
*/
|
|
10
|
-
export type TenancyMode = 'schema' | 'rls';
|
|
11
|
-
/**
|
|
12
|
-
* Global tenancy configuration for the application.
|
|
13
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
14
|
-
*/
|
|
15
|
-
export interface TenancyConfig {
|
|
16
|
-
/**
|
|
17
|
-
* The tenancy mode to use.
|
|
18
|
-
*/
|
|
19
|
-
mode: TenancyMode;
|
|
20
|
-
/**
|
|
21
|
-
* Configuration specific to RLS mode.
|
|
22
|
-
*/
|
|
23
|
-
rls?: {
|
|
24
|
-
/**
|
|
25
|
-
* The PostgreSQL session variable used to store the current tenant.
|
|
26
|
-
* @default 'app.current_tenant'
|
|
27
|
-
*/
|
|
28
|
-
sessionVariable?: string;
|
|
29
|
-
/**
|
|
30
|
-
* The default column name used for tenant isolation in RLS policies.
|
|
31
|
-
* @default 'tenant_id'
|
|
32
|
-
*/
|
|
33
|
-
defaultColumn?: string;
|
|
34
|
-
};
|
|
35
|
-
/**
|
|
36
|
-
* Configuration specific to schema-based tenancy.
|
|
37
|
-
*/
|
|
38
|
-
schema?: {
|
|
39
|
-
/**
|
|
40
|
-
* Whether to automatically create tenant schemas on first use.
|
|
41
|
-
*/
|
|
42
|
-
autoCreate?: boolean;
|
|
43
|
-
};
|
|
44
|
-
}
|
|
45
|
-
/**
|
|
46
|
-
* Per-model tenancy configuration, used to control how a specific model
|
|
47
|
-
* interacts with the tenancy system.
|
|
48
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
49
|
-
*/
|
|
50
|
-
export interface ModelTenancyConfig {
|
|
51
|
-
/**
|
|
52
|
-
* Whether tenancy is enabled for this model.
|
|
53
|
-
*/
|
|
54
|
-
enabled: boolean;
|
|
55
|
-
/**
|
|
56
|
-
* Override the global tenancy mode for this model.
|
|
57
|
-
*/
|
|
58
|
-
mode?: TenancyMode;
|
|
59
|
-
/**
|
|
60
|
-
* The column name used for RLS tenant isolation in this model.
|
|
61
|
-
*/
|
|
62
|
-
column?: string;
|
|
63
|
-
/**
|
|
64
|
-
* Custom RLS policy name for this model.
|
|
65
|
-
*/
|
|
66
|
-
policyName?: string;
|
|
67
|
-
}
|
|
68
|
-
/**
|
|
69
|
-
* Gets the current global tenancy configuration.
|
|
70
|
-
*
|
|
71
|
-
* Reads from the 'elysium:mnemosyne' app config key. Returns a default
|
|
72
|
-
* configuration if the app is not yet initialized or no tenancy config is set.
|
|
73
|
-
*
|
|
74
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
75
|
-
* @returns The current tenancy configuration.
|
|
76
|
-
*/
|
|
77
|
-
export declare const getConfig: () => TenancyConfig;
|
|
78
|
-
/**
|
|
79
|
-
* Registers the tenancy strategy to use for table resolution and isolation.
|
|
80
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
81
|
-
* @param strategy The tenancy strategy to register.
|
|
82
|
-
*/
|
|
83
|
-
export declare const registerTenancyStrategy: (strategy: TenancyStrategy<unknown, unknown>) => void;
|
|
84
|
-
/**
|
|
85
|
-
* Gets the currently registered tenancy strategy.
|
|
86
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
87
|
-
* @returns The current tenancy strategy, or `null` if none is registered.
|
|
88
|
-
*/
|
|
89
|
-
export declare const getTenancyStrategy: () => TenancyStrategy<unknown, unknown> | null;
|
|
90
|
-
/**
|
|
91
|
-
* Gets the current tenant from the async context.
|
|
92
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
93
|
-
* @returns The current tenant identifier, or `null` if not set.
|
|
94
|
-
*/
|
|
95
|
-
export declare const getCurrentTenant: () => string | null;
|
|
96
|
-
/**
|
|
97
|
-
* Runs a callback within the context of a specific tenant.
|
|
98
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
99
|
-
* @param tenant The tenant identifier to set.
|
|
100
|
-
* @param callback The function to execute within the tenant context.
|
|
101
|
-
* @returns The return value of the callback.
|
|
102
|
-
*/
|
|
103
|
-
export declare const withTenant: <T>(tenant: string, callback: () => T) => T;
|
|
104
|
-
/**
|
|
105
|
-
* Base class for tenant-aware middleware.
|
|
106
|
-
*
|
|
107
|
-
* The middleware extracts the tenant identifier from an HTTP header and
|
|
108
|
-
* sets `ctx.tenant`. For RLS mode, route handlers should use the driver's
|
|
109
|
-
* isolation mechanism to wrap their queries in a tenant-scoped transaction.
|
|
110
|
-
*
|
|
111
|
-
* @example
|
|
112
|
-
* ```typescript
|
|
113
|
-
* class OrganizationMiddleware extends TenantMiddleware {
|
|
114
|
-
* protected header = 'x-organization-id';
|
|
115
|
-
* protected allowPublic = false;
|
|
116
|
-
*
|
|
117
|
-
* protected async validateTenant(orgId: string): Promise<boolean> {
|
|
118
|
-
* return Organization.exists(orgId);
|
|
119
|
-
* }
|
|
120
|
-
* }
|
|
121
|
-
* ```
|
|
122
|
-
*
|
|
123
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
124
|
-
*/
|
|
125
|
-
export declare abstract class TenantMiddleware extends Middleware {
|
|
126
|
-
/**
|
|
127
|
-
* The HTTP header name from which the tenant ID is read.
|
|
128
|
-
*/
|
|
129
|
-
protected header: string;
|
|
130
|
-
/**
|
|
131
|
-
* Whether requests without a tenant ID are allowed (defaulting to 'public').
|
|
132
|
-
*/
|
|
133
|
-
protected allowPublic: boolean;
|
|
134
|
-
/**
|
|
135
|
-
* The database connection name used for tenant operations.
|
|
136
|
-
*/
|
|
137
|
-
protected connectionName: string;
|
|
138
|
-
/**
|
|
139
|
-
* Validates that a tenant identifier is legitimate.
|
|
140
|
-
* Override this to check against your data store.
|
|
141
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
142
|
-
* @param _tenant The tenant identifier to validate.
|
|
143
|
-
* @returns `true` if the tenant is valid, `false` otherwise.
|
|
144
|
-
*/
|
|
145
|
-
protected validateTenant(_tenant: string): Promise<boolean>;
|
|
146
|
-
/**
|
|
147
|
-
* Hook called after a tenant has been successfully resolved.
|
|
148
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
149
|
-
* @param _tenant The resolved tenant identifier.
|
|
150
|
-
* @param _ctx The request context.
|
|
151
|
-
*/
|
|
152
|
-
protected onTenantResolved(_tenant: string, _ctx: Context): Promise<void>;
|
|
153
|
-
/**
|
|
154
|
-
* Called when the tenant cannot be determined or is invalid.
|
|
155
|
-
* Throws an appropriate error by default.
|
|
156
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
157
|
-
* @param reason Whether the tenant is 'missing' or 'invalid'.
|
|
158
|
-
* @param _ctx The request context.
|
|
159
|
-
*/
|
|
160
|
-
protected onTenantError(reason: 'missing' | 'invalid', _ctx: Context): never;
|
|
161
|
-
/**
|
|
162
|
-
* Extracts and validates the tenant from the incoming request.
|
|
163
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
164
|
-
* @param ctx The request context.
|
|
165
|
-
*/
|
|
166
|
-
onRequest(ctx: Context): Promise<any>;
|
|
167
|
-
}
|
|
168
|
-
/**
|
|
169
|
-
* A simple tenant middleware that allows public access by default.
|
|
170
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
171
|
-
*/
|
|
172
|
-
export declare class SimpleTenantMiddleware extends TenantMiddleware {
|
|
173
|
-
}
|
|
174
|
-
/**
|
|
175
|
-
* A strict tenant middleware that rejects requests without a tenant ID.
|
|
176
|
-
* @author Axel Nana <axel.nana@workbud.com>
|
|
177
|
-
*/
|
|
178
|
-
export declare class StrictTenantMiddleware extends TenantMiddleware {
|
|
179
|
-
protected allowPublic: boolean;
|
|
180
|
-
}
|