@elysiumjs/mnemosyne-drizzle 0.1.4 → 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.
Files changed (53) hide show
  1. package/CHANGELOG.md +100 -0
  2. package/README.md +51 -0
  3. package/dist/cache.d.ts +60 -0
  4. package/dist/cache.js +1 -0
  5. package/dist/commands/migration_generate.command.d.ts +11 -0
  6. package/dist/commands/migration_generate.command.js +1 -0
  7. package/dist/commands/migration_run.command.d.ts +26 -0
  8. package/dist/commands/migration_run.command.js +10 -0
  9. package/dist/commands/project.d.ts +11 -0
  10. package/dist/commands/project.js +1 -0
  11. package/dist/database.d.ts +63 -23
  12. package/dist/database.js +1 -0
  13. package/dist/drizzle.module.d.ts +40 -0
  14. package/dist/drizzle.module.js +1 -0
  15. package/dist/index.d.ts +4 -3
  16. package/dist/index.js +1 -2
  17. package/dist/migrations.d.ts +45 -0
  18. package/dist/migrations.js +2 -0
  19. package/dist/model.d.ts +8 -8
  20. package/dist/model.js +1 -0
  21. package/dist/repository.d.ts +20 -5
  22. package/dist/repository.js +1 -0
  23. package/dist/table.d.ts +47 -123
  24. package/dist/table.js +1 -0
  25. package/dist/tenancy.d.ts +28 -11
  26. package/dist/tenancy.js +1 -0
  27. package/package.json +25 -13
  28. package/.turbo/turbo-build.log +0 -9
  29. package/.turbo/turbo-typecheck.log +0 -1
  30. package/coverage/.lcov.info.01ffde0d9f956dce.tmp +0 -14
  31. package/coverage/.lcov.info.2f80a4def3a82099.tmp +0 -343
  32. package/coverage/.lcov.info.560a2daaba0934cd.tmp +0 -366
  33. package/coverage/.lcov.info.7162c9b788dc0bc6.tmp +0 -366
  34. package/coverage/.lcov.info.b57c0b452c3901e5.tmp +0 -17
  35. package/coverage/.lcov.info.c47de8626f331696.tmp +0 -17
  36. package/coverage/.lcov.info.c710a42b25b08349.tmp +0 -343
  37. package/coverage/.lcov.info.dbc8f065467fc8a2.tmp +0 -287
  38. package/coverage/.lcov.info.de180a43301f82c0.tmp +0 -35
  39. package/coverage/.lcov.info.df18163ab929ab1b.tmp +0 -357
  40. package/coverage/.lcov.info.febf711c192f5c0c.tmp +0 -17
  41. package/coverage/lcov.info +0 -357
  42. package/src/database.ts +0 -175
  43. package/src/index.ts +0 -77
  44. package/src/model.ts +0 -171
  45. package/src/repository.ts +0 -113
  46. package/src/table.ts +0 -70
  47. package/src/tenancy.ts +0 -249
  48. package/tests/database.test.ts +0 -198
  49. package/tests/model.test.ts +0 -362
  50. package/tests/setup.ts +0 -10
  51. package/tests/table.test.ts +0 -72
  52. package/tests/tenancy.test.ts +0 -212
  53. package/tsconfig.json +0 -12
@@ -1,357 +0,0 @@
1
- TN:
2
- SF:../core/dist/index.js
3
- FNF:225
4
- FNH:28
5
- DA:1,0
6
- DA:2,0
7
- DA:3,0
8
- DA:4,0
9
- DA:5,0
10
- DA:6,0
11
- DA:7,0
12
- DA:8,0
13
- DA:9,0
14
- DA:10,0
15
- DA:11,0
16
- DA:12,0
17
- DA:13,0
18
- DA:14,0
19
- DA:15,0
20
- DA:16,0
21
- DA:17,0
22
- DA:18,0
23
- DA:19,1221
24
- LF:19
25
- LH:1
26
- end_of_record
27
- TN:
28
- SF:src/database.ts
29
- FNF:9
30
- FNH:1
31
- DA:20,130
32
- DA:21,108
33
- DA:22,92
34
- DA:23,126
35
- DA:63,114
36
- DA:64,18
37
- DA:65,47
38
- DA:67,0
39
- DA:68,0
40
- DA:69,0
41
- DA:70,0
42
- DA:71,0
43
- DA:72,0
44
- DA:73,75
45
- DA:76,0
46
- DA:77,0
47
- DA:78,0
48
- DA:79,0
49
- DA:80,0
50
- DA:81,0
51
- DA:82,0
52
- DA:83,0
53
- DA:84,0
54
- DA:85,0
55
- DA:86,0
56
- DA:87,0
57
- DA:88,0
58
- DA:89,0
59
- DA:90,0
60
- DA:91,0
61
- DA:92,0
62
- DA:93,0
63
- DA:94,0
64
- DA:95,0
65
- DA:96,0
66
- DA:97,0
67
- DA:98,0
68
- DA:99,0
69
- DA:100,0
70
- DA:101,0
71
- DA:102,0
72
- DA:103,0
73
- DA:104,0
74
- DA:105,0
75
- DA:106,0
76
- DA:107,0
77
- DA:108,0
78
- DA:109,0
79
- DA:110,0
80
- DA:111,0
81
- DA:112,0
82
- DA:113,0
83
- DA:114,0
84
- DA:115,0
85
- DA:116,0
86
- DA:117,0
87
- DA:118,0
88
- DA:119,0
89
- DA:120,0
90
- DA:121,0
91
- DA:122,0
92
- DA:123,0
93
- DA:124,0
94
- DA:125,0
95
- DA:126,0
96
- DA:127,6
97
- DA:129,2
98
- DA:143,98
99
- DA:144,27
100
- DA:145,48
101
- DA:146,28
102
- DA:147,57
103
- DA:148,21
104
- DA:149,24
105
- DA:150,27
106
- DA:151,33
107
- DA:152,25
108
- DA:153,23
109
- DA:154,18
110
- DA:155,20
111
- DA:156,4
112
- DA:157,22
113
- DA:158,87
114
- DA:161,0
115
- DA:162,66
116
- DA:165,0
117
- DA:166,35
118
- DA:168,6
119
- DA:169,2
120
- DA:175,87
121
- LF:90
122
- LH:31
123
- end_of_record
124
- TN:
125
- SF:src/model.ts
126
- FNF:8
127
- FNH:7
128
- DA:18,76
129
- DA:19,53
130
- DA:21,43
131
- DA:22,45
132
- DA:33,37
133
- DA:34,50
134
- DA:35,31
135
- DA:36,15
136
- DA:37,20
137
- DA:38,15
138
- DA:39,20
139
- DA:40,16
140
- DA:41,16
141
- DA:42,0
142
- DA:43,0
143
- DA:44,0
144
- DA:45,0
145
- DA:46,0
146
- DA:47,0
147
- DA:48,0
148
- DA:49,0
149
- DA:50,0
150
- DA:51,0
151
- DA:53,2
152
- DA:69,31
153
- DA:70,13
154
- DA:71,35
155
- DA:74,13
156
- DA:75,41
157
- DA:76,42
158
- DA:77,21
159
- DA:78,40
160
- DA:79,29
161
- DA:80,33
162
- DA:81,14
163
- DA:82,43
164
- DA:83,4
165
- DA:86,21
166
- DA:87,45
167
- DA:88,21
168
- DA:89,16
169
- DA:90,112
170
- DA:92,1
171
- DA:93,4
172
- DA:94,41
173
- DA:95,55
174
- DA:97,2
175
- DA:114,38
176
- DA:115,7
177
- DA:116,9
178
- DA:117,3
179
- DA:118,70
180
- DA:148,27
181
- DA:149,7
182
- DA:150,12
183
- DA:151,3
184
- DA:152,39
185
- DA:153,43
186
- DA:158,81
187
- DA:161,47
188
- DA:162,47
189
- DA:163,2
190
- DA:165,12
191
- LF:63
192
- LH:53
193
- end_of_record
194
- TN:
195
- SF:src/table.ts
196
- FNF:2
197
- FNH:2
198
- DA:17,64
199
- DA:30,34
200
- DA:37,31
201
- DA:38,10
202
- DA:39,3
203
- DA:40,33
204
- DA:59,22
205
- DA:63,11
206
- DA:64,9
207
- DA:65,16
208
- DA:66,3
209
- DA:67,64
210
- DA:68,36
211
- DA:69,13
212
- LF:14
213
- LH:14
214
- end_of_record
215
- TN:
216
- SF:src/tenancy.ts
217
- FNF:14
218
- FNH:10
219
- DA:19,112
220
- DA:20,68
221
- DA:21,164
222
- DA:23,76
223
- DA:24,86
224
- DA:30,60
225
- DA:31,62
226
- DA:43,89
227
- DA:44,136
228
- DA:53,99
229
- DA:54,73
230
- DA:55,69
231
- DA:56,5
232
- DA:57,80
233
- DA:58,86
234
- DA:59,44
235
- DA:74,63
236
- DA:78,16
237
- DA:79,22
238
- DA:80,24
239
- DA:81,6
240
- DA:82,59
241
- DA:83,71
242
- DA:84,5
243
- DA:86,98
244
- DA:87,85
245
- DA:88,81
246
- DA:89,5
247
- DA:91,94
248
- DA:92,122
249
- DA:93,96
250
- DA:95,42
251
- DA:110,0
252
- DA:111,0
253
- DA:112,0
254
- DA:113,0
255
- DA:114,0
256
- DA:115,0
257
- DA:116,0
258
- DA:117,0
259
- DA:118,0
260
- DA:119,0
261
- DA:120,0
262
- DA:121,0
263
- DA:122,0
264
- DA:123,7
265
- DA:132,0
266
- DA:133,0
267
- DA:134,0
268
- DA:135,0
269
- DA:136,0
270
- DA:137,0
271
- DA:138,0
272
- DA:139,0
273
- DA:140,0
274
- DA:141,75
275
- DA:150,0
276
- DA:151,0
277
- DA:152,0
278
- DA:153,0
279
- DA:154,0
280
- DA:155,0
281
- DA:156,0
282
- DA:157,0
283
- DA:158,46
284
- DA:169,143
285
- DA:170,72
286
- DA:171,140
287
- DA:172,144
288
- DA:173,122
289
- DA:175,54
290
- DA:176,30
291
- DA:177,34
292
- DA:178,168
293
- DA:179,170
294
- DA:180,9
295
- DA:195,57
296
- DA:196,35
297
- DA:198,54
298
- DA:199,90
299
- DA:200,36
300
- DA:201,16
301
- DA:202,112
302
- DA:204,6
303
- DA:205,9
304
- DA:206,82
305
- DA:207,120
306
- DA:210,16
307
- DA:211,26
308
- DA:212,18
309
- DA:213,20
310
- DA:214,10
311
- DA:216,40
312
- DA:218,2
313
- DA:228,66
314
- DA:229,29
315
- DA:231,56
316
- DA:233,36
317
- DA:236,16
318
- DA:237,24
319
- DA:238,16
320
- DA:239,20
321
- DA:240,10
322
- DA:241,76
323
- DA:242,144
324
- DA:244,94
325
- DA:245,152
326
- DA:246,41
327
- DA:247,8
328
- DA:249,2
329
- LF:110
330
- LH:80
331
- end_of_record
332
- TN:
333
- SF:tests/setup.ts
334
- FNF:3
335
- FNH:1
336
- DA:1,0
337
- DA:2,0
338
- DA:3,0
339
- DA:4,0
340
- DA:5,0
341
- DA:6,30
342
- DA:7,47
343
- DA:8,19
344
- DA:9,2
345
- DA:10,2
346
- LF:10
347
- LH:5
348
- end_of_record
349
- TN:
350
- SF:../mnemosyne/dist/index.js
351
- FNF:52
352
- FNH:12
353
- DA:1,0
354
- DA:2,3199
355
- LF:2
356
- LH:1
357
- end_of_record
package/src/database.ts DELETED
@@ -1,175 +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 { CacheInterface, DatabaseCacheConfig, DatabaseDriver } from '@elysiumjs/mnemosyne';
16
- import type { DrizzleConfig } from 'drizzle-orm';
17
- import type { BunSQLDatabase } from 'drizzle-orm/bun-sql';
18
- import type { CacheConfig } from 'drizzle-orm/cache/core/types';
19
-
20
- import { AbstractDatabase, createDatabaseCacheStorage } from '@elysiumjs/mnemosyne';
21
- import { getTableName, is, Table } from 'drizzle-orm';
22
- import { drizzle } from 'drizzle-orm/bun-sql';
23
- import { Cache as DrizzleCache } from 'drizzle-orm/cache/core';
24
-
25
- // ============================================================================
26
- // Types
27
- // ============================================================================
28
-
29
- /**
30
- * Connection configuration for Drizzle ORM.
31
- *
32
- * Extends `DrizzleConfig` with connection details and optional caching.
33
- *
34
- * @author Axel Nana <axel.nana@workbud.com>
35
- * @template TSchema The Drizzle schema type.
36
- */
37
- export type DrizzleConnectionProps<
38
- TSchema extends Record<string, unknown> = Record<string, never>
39
- > = Omit<DrizzleConfig<TSchema>, 'cache'> &
40
- ({ connection: string | ({ url?: string } & Bun.SQLOptions) } | { client: Bun.SQL }) & {
41
- cache?: true | DatabaseCacheConfig;
42
- };
43
-
44
- /**
45
- * A Drizzle database connection backed by Bun's SQL driver.
46
- * @author Axel Nana <axel.nana@workbud.com>
47
- */
48
- export type DrizzleConnection = BunSQLDatabase<Record<string, never>> & { $client: Bun.SQL };
49
-
50
- // ============================================================================
51
- // DrizzleDatabaseCache
52
- // ============================================================================
53
-
54
- /**
55
- * Drizzle query cache backed by the framework's cache storage.
56
- *
57
- * Extends the Drizzle `Cache` base class and implements the
58
- * framework's `DatabaseCacheStrategy` interface so that query
59
- * results can be transparently cached and invalidated.
60
- *
61
- * @author Axel Nana <axel.nana@workbud.com>
62
- */
63
- export class DrizzleDatabaseCache extends DrizzleCache {
64
- #cache: Omit<CacheInterface, 'tags'>;
65
- private usedTablesPerKey: Record<string, string[]> = {};
66
-
67
- constructor(
68
- private readonly cacheStrategy: 'explicit' | 'all' = 'explicit',
69
- private readonly globalTtl: number = 1,
70
- storageEngine: 'redis' | 'memory' = 'redis'
71
- ) {
72
- super();
73
- this.#cache = createDatabaseCacheStorage({ storage: storageEngine });
74
- }
75
-
76
- public override strategy(): 'explicit' | 'all' {
77
- return this.cacheStrategy;
78
- }
79
-
80
- public override async get(key: string): Promise<any[] | undefined> {
81
- return (await this.#cache.get<any[]>(key)) ?? undefined;
82
- }
83
-
84
- public override async put(
85
- key: string,
86
- response: any,
87
- tables: string[],
88
- _isTag: boolean,
89
- config?: CacheConfig
90
- ): Promise<void> {
91
- await this.#cache.set(key, response, (config?.ex ?? this.globalTtl) * 1000);
92
- for (const table of tables) {
93
- const keys = this.usedTablesPerKey[table];
94
- if (keys === undefined) {
95
- this.usedTablesPerKey[table] = [key];
96
- } else {
97
- keys.push(key);
98
- }
99
- }
100
- }
101
-
102
- public override async onMutate(params: {
103
- tags?: string | string[];
104
- tables?: string | string[] | Table<any> | Table<any>[];
105
- }): Promise<void> {
106
- const tagsArray = params.tags ? (Array.isArray(params.tags) ? params.tags : [params.tags]) : [];
107
- const tablesArray = params.tables
108
- ? Array.isArray(params.tables)
109
- ? params.tables
110
- : [params.tables]
111
- : [];
112
- const keysToDelete = new Set<string>();
113
-
114
- for (const table of tablesArray) {
115
- const tableName = is(table, Table) ? getTableName(table as Table) : (table as string);
116
- const keys = this.usedTablesPerKey[tableName] ?? [];
117
- for (const key of keys) keysToDelete.add(key);
118
- }
119
-
120
- if (keysToDelete.size > 0 || tagsArray.length > 0) {
121
- await this.#cache.mdel(tagsArray);
122
- await this.#cache.mdel([...keysToDelete]);
123
- for (const table of tablesArray) {
124
- const tableName = is(table, Table) ? getTableName(table as Table) : (table as string);
125
- this.usedTablesPerKey[tableName] = [];
126
- }
127
- }
128
- }
129
- }
130
-
131
- // ============================================================================
132
- // DrizzleDatabase
133
- // ============================================================================
134
-
135
- /**
136
- * Drizzle-specific database manager.
137
- *
138
- * Implements the abstract database class with a driver that creates
139
- * Drizzle connections using Bun's built-in SQL client.
140
- *
141
- * @author Axel Nana <axel.nana@workbud.com>
142
- */
143
- class DrizzleDatabase extends AbstractDatabase<DrizzleConnection, DrizzleConnectionProps> {
144
- protected driver: DatabaseDriver<DrizzleConnection, DrizzleConnectionProps> = {
145
- createConnection(config) {
146
- const cache =
147
- config.cache === undefined
148
- ? undefined
149
- : config.cache === true
150
- ? new DrizzleDatabaseCache()
151
- : config.cache.enabled !== false
152
- ? new DrizzleDatabaseCache(
153
- config.cache.strategy,
154
- config.cache.ttl,
155
- config.cache.storage
156
- )
157
- : undefined;
158
- return drizzle({ ...config, cache }) as DrizzleConnection;
159
- },
160
-
161
- withTransaction(connection, callback) {
162
- return (connection as any).transaction((tx: any) => callback(tx));
163
- },
164
-
165
- getRawClient(connection) {
166
- return connection.$client;
167
- }
168
- };
169
- }
170
-
171
- /**
172
- * Singleton Drizzle database manager.
173
- * @author Axel Nana <axel.nana@workbud.com>
174
- */
175
- export const Database = new DrizzleDatabase();
package/src/index.ts DELETED
@@ -1,77 +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 { MnemosyneConfig } from '@elysiumjs/mnemosyne';
16
- import type { DrizzleConnectionProps } from './database';
17
-
18
- import { Application, Event } from '@elysiumjs/core';
19
- import { registerTenancyStrategy } from '@elysiumjs/mnemosyne';
20
-
21
- import { Database } from './database';
22
- import { DrizzleRLSTenancy, DrizzleSchemaTenancy } from './tenancy';
23
-
24
- // Re-export drizzle-orm/pg-core with enhanced pgTable
25
- export * from 'drizzle-orm';
26
- export * from 'drizzle-orm/pg-core';
27
- export type {
28
- ColumnsWithTable,
29
- SelectedFields,
30
- SelectedFieldsFlat,
31
- SelectedFieldsOrdered,
32
- TableConfig
33
- } from 'drizzle-orm/pg-core';
34
- export { pgTable, getTableBuilders } from './table';
35
-
36
- // Re-exports
37
- export { Database, DrizzleDatabaseCache } from './database';
38
- export type { DrizzleConnectionProps, DrizzleConnection } from './database';
39
- export {
40
- DrizzleModel,
41
- createSchemaFromDrizzle,
42
- drizzleAdapter,
43
- type DrizzleModelOptions
44
- } from './model';
45
- export { DrizzleRepository } from './repository';
46
- export {
47
- DrizzleSchemaTenancy,
48
- DrizzleRLSTenancy,
49
- wrapTenantSchema,
50
- registerTenantSchema,
51
- getTenantSchema,
52
- withRLS,
53
- setConnectionTenant,
54
- getSessionTenant,
55
- createRLSPolicy
56
- } from './tenancy';
57
-
58
- // Auto-initialize on app launch
59
- Event.once('elysium:app:launched', () => {
60
- const app = Application.instance;
61
- const config = app.getConfig<MnemosyneConfig<DrizzleConnectionProps>>('elysium:mnemosyne' as any);
62
-
63
- if (config?.database) {
64
- for (const [name, props] of Object.entries(config.database.connections)) {
65
- Database.registerConnection(name, props);
66
- }
67
- if (Database.connectionExists(config.database.default)) {
68
- Database.setDefaultConnection(config.database.default);
69
- }
70
- }
71
-
72
- if (config?.tenancy) {
73
- const strategy =
74
- config.tenancy.mode === 'rls' ? new DrizzleRLSTenancy() : new DrizzleSchemaTenancy();
75
- registerTenancyStrategy(strategy);
76
- }
77
- });