@elysiumjs/mnemosyne 0.2.0 → 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/src/cache.ts DELETED
@@ -1,219 +0,0 @@
1
- // Copyright (c) 2025-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 { KeyvStoreAdapter } from 'keyv';
16
-
17
- import { Service, ServiceNotFoundError } from '@elysiumjs/core';
18
- import { createCache } from 'cache-manager';
19
- import { Keyv } from 'keyv';
20
- import { alphabetical } from 'radash';
21
-
22
- /** Name of the global service backing `Cache.redis`. */
23
- export const REDIS_CACHE_SERVICE = 'mnemosyne.cache.redis';
24
-
25
- /** Name of the global service backing `Cache.memory`. */
26
- export const MEMORY_CACHE_SERVICE = 'mnemosyne.cache.memory';
27
-
28
- /**
29
- * Interface for a cache storage.
30
- * @author Axel Nana <axel.nana@workbud.com>
31
- */
32
- export interface CacheInterface {
33
- /**
34
- * Retrieves the cached data for a given key.
35
- * @typeParam T The type of the cached data.
36
- *
37
- * @param key The key of the cached data.
38
- */
39
- get<T>(key: string): Promise<T | undefined>;
40
-
41
- /**
42
- * Retrieves the cached data for a given list of keys.
43
- * @typeParam T The type of the cached data.
44
- *
45
- * @param keys The list of cached data keys.
46
- */
47
- mget<T>(keys: string[]): Promise<Array<T | undefined>>;
48
-
49
- /**
50
- * Gets the TTL of the cached data with the given key.
51
- * @param key The key of the cached data.
52
- * @returns The TTL of the cached data, or `undefined` if the data does not exist.
53
- */
54
- ttl(key: string): Promise<number | undefined>;
55
-
56
- /**
57
- * Caches data under the given key and an optional TTL.
58
- * @typeParam T The type of the data to cache.
59
- *
60
- * @param key The key of the cached data.
61
- * @param data The data to cache.
62
- * @param ttl The optional TTL of the cached data. If not defined, the data is cached forever.
63
- * @returns The cached data.
64
- */
65
- set<T>(key: string, data: T, ttl?: number): Promise<T>;
66
-
67
- /**
68
- * Caches data under the given list of keys and an optional TTL.
69
- * @typeParam T The type of the data to cache.
70
- *
71
- * @param data The list of data to cache.
72
- * @returns The list of cached data.
73
- */
74
- mset<T>(
75
- data: Array<{
76
- key: string;
77
- value: T;
78
- ttl?: number;
79
- }>
80
- ): Promise<
81
- Array<{
82
- key: string;
83
- value: T;
84
- ttl?: number;
85
- }>
86
- >;
87
-
88
- /**
89
- * Deletes the cached data with the given key.
90
- * @typeParam T The type of the data to cache.
91
- *
92
- * @param key The key of the cached data.
93
- * @returns `true` if the data was deleted, `false` otherwise.
94
- */
95
- del(key: string): Promise<boolean>;
96
-
97
- /**
98
- * Deletes the cached data with the given list of keys.
99
- * @typeParam T The type of the data to cache.
100
- *
101
- * @param keys The list of cached data keys.
102
- * @returns `true` if the data was deleted, `false` otherwise.
103
- */
104
- mdel(keys: string[]): Promise<boolean>;
105
-
106
- /**
107
- * Deletes all cached data.
108
- * @returns `true` if the data was deleted, `false` otherwise.
109
- */
110
- clear(): Promise<boolean>;
111
-
112
- /**
113
- * Creates a sub-cache with the given tags.
114
- * @param tags The list of tags for the sub-cache.
115
- */
116
- tags(...tags: string[]): Omit<CacheInterface, 'tags'>;
117
- }
118
-
119
- /**
120
- * Makes a cache interface wrapping the given store.
121
- * @author Axel Nana <axel.nana@workburd.com>
122
- * @param store The cache storage to use in the interface.
123
- * @returns A new `CacheInterface`.
124
- */
125
- export const makeCacheInterface = (store: KeyvStoreAdapter): CacheInterface => {
126
- const base = createCache({
127
- stores: [
128
- new Keyv({
129
- store,
130
- namespace: 'cache'
131
- })
132
- ]
133
- }) as Omit<CacheInterface, 'tags'>;
134
-
135
- const tagsCache = new Map<string, Omit<CacheInterface, 'tags'>>();
136
-
137
- return {
138
- ...base,
139
- tags(...tags) {
140
- const namespace = `cache__${alphabetical(tags, (item) => item).join('__')}`;
141
- let cache = tagsCache.get(namespace);
142
-
143
- if (!cache) {
144
- cache = createCache({
145
- stores: [
146
- new Keyv({
147
- store,
148
- namespace
149
- })
150
- ]
151
- }) as Omit<CacheInterface, 'tags'>;
152
-
153
- tagsCache.set(namespace, cache);
154
- }
155
-
156
- return cache;
157
- }
158
- };
159
- };
160
-
161
- const getCache = (name: string): CacheInterface => {
162
- const cache = Service.get<CacheInterface>(name);
163
- if (!cache) throw new ServiceNotFoundError(name);
164
- return cache;
165
- };
166
-
167
- /**
168
- * Static access to the caches provided by the `MnemosyneModule`.
169
- * @author Axel Nana <axel.nana@workbud.com>
170
- */
171
- export abstract class Cache {
172
- /**
173
- * A weak cache storage for simple non-intrusive caching of data.
174
- * @author Axel Nana <axel.nana@workbud.com>
175
- */
176
- public static readonly weak = Object.seal({
177
- /**
178
- * INTERNAL USAGE ONLY.
179
- * @private
180
- */
181
- map: new WeakMap<object, unknown>(),
182
-
183
- /**
184
- * Gets the cached data for a given context.
185
- * @param ctx The cache context.
186
- * @returns The cached data.
187
- */
188
- get<T extends object>(ctx: object): T {
189
- if (!this.map.has(ctx)) this.map.set(ctx, {});
190
- return this.map.get(ctx) as T;
191
- },
192
-
193
- /**
194
- * Deletes the cached data for a given context.
195
- * @param ctx The cache context.
196
- */
197
- del(ctx: object): void {
198
- this.map.delete(ctx);
199
- }
200
- });
201
-
202
- /**
203
- * Redis-based cache storage.
204
- * @author Axel Nana <axel.nana@workbud.com>
205
- * @throws {ServiceNotFoundError} If the `MnemosyneModule` is not loaded or has no redis cache.
206
- */
207
- public static get redis(): CacheInterface {
208
- return getCache(REDIS_CACHE_SERVICE);
209
- }
210
-
211
- /**
212
- * Memory-based cache storage.
213
- * @author Axel Nana <axel.nana@workbud.com>
214
- * @throws {ServiceNotFoundError} If the `MnemosyneModule` is not loaded or has no memory cache.
215
- */
216
- public static get memory(): CacheInterface {
217
- return getCache(MEMORY_CACHE_SERVICE);
218
- }
219
- }
package/src/database.ts DELETED
@@ -1,114 +0,0 @@
1
- // Copyright (c) 2025-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 } from './cache';
16
- import type { DatabaseCacheConfig, DatabaseDriver } from './interfaces';
17
-
18
- import { Application, Service, ServiceNotFoundError } from '@elysiumjs/core';
19
-
20
- import { Cache } from './cache';
21
-
22
- /**
23
- * Creates a cache storage instance tagged for database use.
24
- * Drivers can use this to build their cache backends.
25
- * @author Axel Nana <axel.nana@workbud.com>
26
- * @param config The database cache configuration.
27
- * @returns A cache interface tagged with 'database'.
28
- */
29
- export const createDatabaseCacheStorage = (
30
- config: DatabaseCacheConfig
31
- ): Omit<CacheInterface, 'tags'> => {
32
- return Cache[config.storage ?? 'redis'].tags('database');
33
- };
34
-
35
- /**
36
- * Abstract database manager.
37
- *
38
- * Retrieves named database connections using a driver. Connections are registered as
39
- * global services `db.connection.{name}` by the driver modules.
40
- *
41
- * @author Axel Nana <axel.nana@workbud.com>
42
- * @template TConnection The connection type managed by the driver.
43
- * @template TConnectionConfig The configuration used to create a connection.
44
- */
45
- export abstract class AbstractDatabase<TConnection, TConnectionConfig> {
46
- /**
47
- * The database driver used to create and manage connections.
48
- */
49
- protected abstract driver: DatabaseDriver<TConnection, TConnectionConfig>;
50
-
51
- /**
52
- * Creates a service name for a database connection. Driver modules register their
53
- * connections (and the `default` alias) under this name.
54
- * @author Axel Nana <axel.nana@workbud.com>
55
- * @param name The name of the connection.
56
- * @returns A service name for the connection.
57
- */
58
- public static getConnectionName(name: string): string {
59
- return `db.connection.${name}`;
60
- }
61
-
62
- /**
63
- * Retrieves the connection with the given name.
64
- *
65
- * For the default connection, checks the async context for a
66
- * transaction handle (`db:tx`) before falling back to the service container.
67
- * @throws {ServiceNotFoundError} If no connection with that name is registered.
68
- *
69
- * @author Axel Nana <axel.nana@workbud.com>
70
- * @param name The connection name.
71
- * @returns The connection with the given name.
72
- */
73
- public getConnection(name: string = 'default'): TConnection {
74
- if (name === 'default') {
75
- const tx = Application.context.getStore()?.get('db:tx') as TConnection | undefined;
76
- if (tx) return tx;
77
- }
78
-
79
- const serviceName = AbstractDatabase.getConnectionName(name);
80
- const connection = Service.get<TConnection>(serviceName);
81
- if (connection === null) throw new ServiceNotFoundError(serviceName);
82
-
83
- return connection;
84
- }
85
-
86
- /**
87
- * Checks if a connection with the given name exists.
88
- * @author Axel Nana <axel.nana@workbud.com>
89
- * @param name The name of the connection to check.
90
- * @returns `true` if the connection exists, `false` otherwise.
91
- */
92
- public connectionExists(name: string): boolean {
93
- return Service.exists(AbstractDatabase.getConnectionName(name));
94
- }
95
-
96
- /**
97
- * Retrieves the default connection.
98
- * @author Axel Nana <axel.nana@workbud.com>
99
- * @returns The default connection.
100
- */
101
- public getDefaultConnection(): TConnection {
102
- return this.getConnection('default');
103
- }
104
-
105
- /**
106
- * Returns the raw underlying client for the given connection.
107
- * @author Axel Nana <axel.nana@workbud.com>
108
- * @param name The connection name.
109
- * @returns The raw client instance.
110
- */
111
- public getRawClient(name: string = 'default'): unknown {
112
- return this.driver.getRawClient(this.getConnection(name));
113
- }
114
- }
package/src/index.ts DELETED
@@ -1,37 +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
- export type {
16
- ColumnMetadata,
17
- DatabaseCacheConfig,
18
- DatabaseCacheStrategy,
19
- DatabaseDriver,
20
- IdType,
21
- ModelAdapter,
22
- ModelClass,
23
- RepositoryClass,
24
- RepositoryInterface,
25
- TenancyStrategy
26
- } from './interfaces';
27
-
28
- export * from './database';
29
- export * from './model';
30
- export * from './repository';
31
-
32
- export * from './tenancy';
33
- export type { TenancyConfig, TenancyMode, ModelTenancyConfig } from './tenancy';
34
-
35
- export { Cache, type CacheInterface, MEMORY_CACHE_SERVICE, REDIS_CACHE_SERVICE } from './cache';
36
- export * from './mnemosyne.module';
37
- export * from './keyv-redis';