@elysiumjs/mnemosyne 0.1.2 → 0.2.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/.turbo/turbo-build.log +8 -0
- package/CHANGELOG.md +34 -0
- package/README.md +19 -16
- package/dist/cache.d.ts +130 -0
- package/dist/database.d.ts +66 -0
- package/dist/index.d.ts +9 -0
- package/dist/index.js +2 -0
- package/dist/interfaces.d.ts +350 -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 +179 -0
- package/package.json +8 -9
- package/src/cache.ts +21 -20
- package/src/database.ts +13 -50
- package/src/index.ts +2 -12
- package/src/keyv-redis.ts +39 -26
- package/src/mnemosyne.module.ts +83 -0
- package/src/tenancy.ts +12 -29
- package/tests/cache.test.ts +51 -10
- package/tests/database.test.ts +37 -173
- package/tests/mnemosyne.module.test.ts +214 -0
- package/tests/setup.ts +0 -13
- package/bunfig.toml +0 -19
|
@@ -0,0 +1,214 @@
|
|
|
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 { TestingApp } from '@elysiumjs/core';
|
|
16
|
+
|
|
17
|
+
import { Application, RedisModule, Service, ServiceNotFoundError } from '@elysiumjs/core';
|
|
18
|
+
import { afterAll, afterEach, beforeAll, beforeEach, describe, expect, it, mock } from 'bun:test';
|
|
19
|
+
|
|
20
|
+
import * as pkg from '../src';
|
|
21
|
+
import { Cache, MnemosyneModule } from '../src';
|
|
22
|
+
import { AbstractDatabase } from '../src/database';
|
|
23
|
+
import { KeyvRedis } from '../src/keyv-redis';
|
|
24
|
+
import { getTenancyConfig, getTenancyStrategy } from '../src/tenancy';
|
|
25
|
+
|
|
26
|
+
const store = new Map<string, string>();
|
|
27
|
+
const originalRedisClient = Bun.RedisClient;
|
|
28
|
+
const ClientMock = mock(function (_url: string, _options?: object) {
|
|
29
|
+
return {
|
|
30
|
+
close: mock(),
|
|
31
|
+
get: mock(async (k: string) => store.get(k) ?? null),
|
|
32
|
+
set: mock(async (k: string, v: string) => {
|
|
33
|
+
store.set(k, v);
|
|
34
|
+
return 'OK';
|
|
35
|
+
})
|
|
36
|
+
};
|
|
37
|
+
});
|
|
38
|
+
beforeAll(() => {
|
|
39
|
+
Bun.RedisClient = ClientMock as unknown as typeof Bun.RedisClient;
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
afterAll(() => {
|
|
43
|
+
Bun.RedisClient = originalRedisClient;
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const redis = RedisModule.with({
|
|
47
|
+
default: 'cache',
|
|
48
|
+
connections: { cache: { url: 'redis://cache:6379' } }
|
|
49
|
+
});
|
|
50
|
+
|
|
51
|
+
class TestDatabase extends AbstractDatabase<any, any> {
|
|
52
|
+
protected driver = {
|
|
53
|
+
createConnection: mock(),
|
|
54
|
+
withTransaction: mock(),
|
|
55
|
+
getRawClient: mock()
|
|
56
|
+
} as any;
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
describe('MnemosyneModule', () => {
|
|
60
|
+
let app: TestingApp | undefined;
|
|
61
|
+
|
|
62
|
+
beforeEach(() => {
|
|
63
|
+
store.clear();
|
|
64
|
+
ClientMock.mockClear();
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
afterEach(async () => {
|
|
68
|
+
await app?.shutdown();
|
|
69
|
+
app = undefined;
|
|
70
|
+
});
|
|
71
|
+
|
|
72
|
+
it('registers nothing on import', () => {
|
|
73
|
+
expect(Service.exists('mnemosyne.cache.redis')).toBe(false);
|
|
74
|
+
expect(Service.exists('mnemosyne.cache.memory')).toBe(false);
|
|
75
|
+
expect(() => Cache.redis).toThrow(ServiceNotFoundError);
|
|
76
|
+
expect(() => Cache.memory).toThrow(ServiceNotFoundError);
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
it('exports the module', () => {
|
|
80
|
+
expect(pkg.MnemosyneModule).toBe(MnemosyneModule);
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
it('serves Cache.redis without opening a client until first use', async () => {
|
|
84
|
+
app = await Application.testing({ modules: [redis, MnemosyneModule] });
|
|
85
|
+
await app.get('mnemosyne.cache.redis');
|
|
86
|
+
const cache = Cache.redis;
|
|
87
|
+
expect(cache).toBeDefined();
|
|
88
|
+
expect(ClientMock).not.toHaveBeenCalled();
|
|
89
|
+
|
|
90
|
+
await cache.set('a', 'b');
|
|
91
|
+
expect(ClientMock).toHaveBeenCalledTimes(1);
|
|
92
|
+
expect(await cache.get('a')).toBe('b');
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
it('resolves the redis client through the async path on a lazy boot', async () => {
|
|
96
|
+
app = await Application.testing({ modules: [redis, MnemosyneModule], boot: 'lazy' });
|
|
97
|
+
await app.get('mnemosyne.cache.redis');
|
|
98
|
+
expect(await Cache.redis.get('missing')).toBeUndefined();
|
|
99
|
+
});
|
|
100
|
+
|
|
101
|
+
it('uses the configured redis connection', async () => {
|
|
102
|
+
app = await Application.testing({
|
|
103
|
+
modules: [
|
|
104
|
+
RedisModule.with({
|
|
105
|
+
default: 'other',
|
|
106
|
+
connections: { other: { url: 'redis://other:6379' } }
|
|
107
|
+
}),
|
|
108
|
+
MnemosyneModule.with({ cache: { redis: { connection: 'other' } } })
|
|
109
|
+
]
|
|
110
|
+
});
|
|
111
|
+
await app.get('mnemosyne.cache.redis');
|
|
112
|
+
await Cache.redis.set('a', 'b');
|
|
113
|
+
expect(ClientMock).toHaveBeenCalledWith('redis://other:6379', {});
|
|
114
|
+
});
|
|
115
|
+
|
|
116
|
+
it('Cache.memory uses the configured TTL', async () => {
|
|
117
|
+
app = await Application.testing({
|
|
118
|
+
modules: [MnemosyneModule.with({ cache: { memory: { ttl: 40, lruSize: 10 } } })]
|
|
119
|
+
});
|
|
120
|
+
await app.get('mnemosyne.cache.memory');
|
|
121
|
+
await Cache.memory.set('k', 'v');
|
|
122
|
+
expect(await Cache.memory.get('k')).toBe('v');
|
|
123
|
+
|
|
124
|
+
await Bun.sleep(80);
|
|
125
|
+
expect(await Cache.memory.get('k')).toBeUndefined();
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it('merges the defaults into a partial config', async () => {
|
|
129
|
+
app = await Application.testing({
|
|
130
|
+
modules: [MnemosyneModule.with({ cache: { memory: { ttl: 1000, lruSize: 10 } } })]
|
|
131
|
+
});
|
|
132
|
+
await app.get('mnemosyne.cache.memory');
|
|
133
|
+
expect(Service.exists('mnemosyne.cache.memory')).toBe(true);
|
|
134
|
+
expect(Service.exists('mnemosyne.cache.redis')).toBe(true);
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
it('registers no redis cache when it is set to false', async () => {
|
|
138
|
+
app = await Application.testing({
|
|
139
|
+
modules: [MnemosyneModule.with({ cache: { redis: false } })]
|
|
140
|
+
});
|
|
141
|
+
await app.get('mnemosyne.cache.memory');
|
|
142
|
+
|
|
143
|
+
expect(Service.exists('mnemosyne.cache.redis')).toBe(false);
|
|
144
|
+
expect(() => Cache.redis).toThrow(ServiceNotFoundError);
|
|
145
|
+
await Cache.memory.set('k', 'v');
|
|
146
|
+
expect(await Cache.memory.get('k')).toBe('v');
|
|
147
|
+
});
|
|
148
|
+
|
|
149
|
+
it('registers no memory cache when it is set to false', async () => {
|
|
150
|
+
app = await Application.testing({
|
|
151
|
+
modules: [redis, MnemosyneModule.with({ cache: { memory: false } })]
|
|
152
|
+
});
|
|
153
|
+
await app.get('mnemosyne.cache.redis');
|
|
154
|
+
|
|
155
|
+
expect(Service.exists('mnemosyne.cache.memory')).toBe(false);
|
|
156
|
+
expect(() => Cache.memory).toThrow(ServiceNotFoundError);
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it('disconnect leaves the shared redis client open', async () => {
|
|
160
|
+
app = await Application.testing({ modules: [redis, MnemosyneModule] });
|
|
161
|
+
const client = (await app.get('redis.connection.cache')) as { close: ReturnType<typeof mock> };
|
|
162
|
+
const adapter = new KeyvRedis({ connection: 'cache' });
|
|
163
|
+
|
|
164
|
+
await adapter.set('a', 'b');
|
|
165
|
+
await adapter.disconnect();
|
|
166
|
+
|
|
167
|
+
expect(client.close).not.toHaveBeenCalled();
|
|
168
|
+
expect(ClientMock).toHaveBeenCalledTimes(1);
|
|
169
|
+
expect(await adapter.get('a')).toBe('b');
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
describe('tenancy', () => {
|
|
173
|
+
it('falls back to schema mode when no config is registered', async () => {
|
|
174
|
+
app = await Application.testing({ modules: [MnemosyneModule] });
|
|
175
|
+
expect(getTenancyConfig()).toEqual({ mode: 'schema' });
|
|
176
|
+
expect(getTenancyStrategy()).toBeNull();
|
|
177
|
+
});
|
|
178
|
+
|
|
179
|
+
it('reads the registered tenancy config', async () => {
|
|
180
|
+
app = await Application.testing({
|
|
181
|
+
modules: [MnemosyneModule],
|
|
182
|
+
overrides: { 'mnemosyne.tenancy.config': { mode: 'rls' } }
|
|
183
|
+
});
|
|
184
|
+
expect(getTenancyConfig()).toEqual({ mode: 'rls' });
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
it('reads the registered tenancy strategy', async () => {
|
|
188
|
+
const strategy = { name: 'test' };
|
|
189
|
+
app = await Application.testing({
|
|
190
|
+
modules: [MnemosyneModule],
|
|
191
|
+
overrides: { 'mnemosyne.tenancy.strategy': strategy }
|
|
192
|
+
});
|
|
193
|
+
expect(getTenancyStrategy()).toBe(strategy as any);
|
|
194
|
+
});
|
|
195
|
+
});
|
|
196
|
+
|
|
197
|
+
describe('AbstractDatabase', () => {
|
|
198
|
+
it('throws ServiceNotFoundError for a missing connection', async () => {
|
|
199
|
+
app = await Application.testing({ modules: [MnemosyneModule] });
|
|
200
|
+
expect(() => new TestDatabase().getConnection('missing')).toThrow(ServiceNotFoundError);
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
it('returns a registered connection', async () => {
|
|
204
|
+
const connection = { id: 1 };
|
|
205
|
+
app = await Application.testing({
|
|
206
|
+
modules: [MnemosyneModule],
|
|
207
|
+
overrides: { 'db.connection.main': connection }
|
|
208
|
+
});
|
|
209
|
+
const db = new TestDatabase();
|
|
210
|
+
expect(db.connectionExists('main')).toBe(true);
|
|
211
|
+
expect(db.getConnection('main')).toBe(connection);
|
|
212
|
+
});
|
|
213
|
+
});
|
|
214
|
+
});
|
package/tests/setup.ts
CHANGED
|
@@ -11,16 +11,3 @@
|
|
|
11
11
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
12
|
// See the License for the specific language governing permissions and
|
|
13
13
|
// limitations under the License.
|
|
14
|
-
|
|
15
|
-
import 'reflect-metadata';
|
|
16
|
-
|
|
17
|
-
import { mock } from 'bun:test';
|
|
18
|
-
|
|
19
|
-
// Mock dependencies
|
|
20
|
-
|
|
21
|
-
mock.module('node:async_hooks', () => ({
|
|
22
|
-
AsyncLocalStorage: class {
|
|
23
|
-
public run = mock((_map, callback) => callback());
|
|
24
|
-
public disable = mock();
|
|
25
|
-
}
|
|
26
|
-
}));
|
package/bunfig.toml
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
[test]
|
|
2
|
-
# The root directory of all unit tests
|
|
3
|
-
root = "./tests"
|
|
4
|
-
# A file loaded right before running `bun test`
|
|
5
|
-
preload = ["./tests/setup.ts"]
|
|
6
|
-
# Enable code coverage by default
|
|
7
|
-
coverage = true
|
|
8
|
-
# Requires 90% line coverage, 70% function coverage, and 80% statement coverage
|
|
9
|
-
coverageThreshold = { line = 0.9, function = 0.7, statement = 0.8 }
|
|
10
|
-
# Tests files are included in the coverage statistics
|
|
11
|
-
coverageSkipTestFiles = true
|
|
12
|
-
# Use lcov as the coverage reporter
|
|
13
|
-
coverageReporter = ["lcov", "text"]
|
|
14
|
-
# Path the output coverage results
|
|
15
|
-
coverageDir = "./coverage"
|
|
16
|
-
|
|
17
|
-
[run]
|
|
18
|
-
# Always use bun runtime for every `bun run` commands
|
|
19
|
-
bun = true
|