@elysiumjs/mnemosyne-drizzle 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/CHANGELOG.md +60 -0
- package/dist/cache.d.ts +60 -0
- package/dist/cache.js +1 -0
- package/dist/commands/migration_generate.command.js +1 -1
- package/dist/commands/migration_run.command.d.ts +13 -0
- package/dist/commands/migration_run.command.js +6 -11
- package/dist/commands/project.d.ts +11 -0
- package/dist/commands/project.js +1 -0
- package/dist/database.d.ts +55 -23
- package/dist/database.js +1 -1
- package/dist/drizzle.module.d.ts +2 -2
- package/dist/drizzle.module.js +1 -1
- package/dist/index.d.ts +2 -2
- package/dist/index.js +1 -1
- package/dist/migrations.d.ts +12 -0
- package/dist/migrations.js +2 -1
- package/dist/model.d.ts +5 -5
- package/dist/repository.d.ts +20 -5
- package/dist/repository.js +1 -1
- package/dist/tenancy.d.ts +25 -8
- package/dist/tenancy.js +1 -1
- package/package.json +19 -9
- package/.omc/state/agent-replay-3920b579-6d8e-4f5f-924a-319202936685.jsonl +0 -7
- package/.omc/state/sessions/3920b579-6d8e-4f5f-924a-319202936685/last-tool-error-state.json +0 -7
- package/.omc/state/sessions/3920b579-6d8e-4f5f-924a-319202936685/mission-state.json +0 -93
- package/.omc/state/sessions/3920b579-6d8e-4f5f-924a-319202936685/pre-tool-advisory-throttle.json +0 -18
- package/.omc/state/sessions/3920b579-6d8e-4f5f-924a-319202936685/subagent-tracking-state.json +0 -61
- package/.turbo/turbo-build.log +0 -3
- package/scripts/minify.ts +0 -10
- package/src/commands/migration_generate.command.ts +0 -101
- package/src/commands/migration_run.command.ts +0 -160
- package/src/database.ts +0 -205
- package/src/drizzle.module.ts +0 -97
- package/src/index.ts +0 -47
- package/src/migrations.ts +0 -100
- package/src/model.ts +0 -178
- package/src/repository.ts +0 -113
- package/src/table.ts +0 -70
- package/src/tenancy.ts +0 -249
- package/tests/drizzle.module.test.ts +0 -201
- package/tests/migrations.test.ts +0 -123
- package/tests/model.test.ts +0 -362
- package/tests/setup.ts +0 -0
- package/tests/table.test.ts +0 -72
- package/tests/tenancy.test.ts +0 -212
- package/tsconfig.json +0 -12
package/tests/tenancy.test.ts
DELETED
|
@@ -1,212 +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 { afterAll, afterEach, beforeAll, describe, expect, it, jest, mock, spyOn } from 'bun:test';
|
|
16
|
-
import * as d from 'drizzle-orm/pg-core';
|
|
17
|
-
|
|
18
|
-
import { Application, Service } from '@elysiumjs/core';
|
|
19
|
-
|
|
20
|
-
import { Database } from '../src/database';
|
|
21
|
-
import { DrizzleModel } from '../src/model';
|
|
22
|
-
import { pgTable } from '../src/table';
|
|
23
|
-
import {
|
|
24
|
-
createRLSPolicy,
|
|
25
|
-
DrizzleRLSTenancy,
|
|
26
|
-
DrizzleSchemaTenancy,
|
|
27
|
-
getTenantSchema,
|
|
28
|
-
registerTenantSchema,
|
|
29
|
-
wrapTenantSchema
|
|
30
|
-
} from '../src/tenancy';
|
|
31
|
-
|
|
32
|
-
// ============================================================================
|
|
33
|
-
// Mock setup
|
|
34
|
-
// ============================================================================
|
|
35
|
-
|
|
36
|
-
mock.module('drizzle-orm/bun-sql', () => ({
|
|
37
|
-
drizzle: mock(() => ({ mockDrizzleInstance: true }))
|
|
38
|
-
}));
|
|
39
|
-
|
|
40
|
-
beforeAll(() => {
|
|
41
|
-
const mockContext = {
|
|
42
|
-
getStore: mock(() => new Map()),
|
|
43
|
-
run: mock((_: any, callback: any) => callback())
|
|
44
|
-
};
|
|
45
|
-
Object.defineProperty(Application, 'context', {
|
|
46
|
-
get: () => mockContext,
|
|
47
|
-
configurable: true
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
// ============================================================================
|
|
52
|
-
// Tests
|
|
53
|
-
// ============================================================================
|
|
54
|
-
|
|
55
|
-
describe('Tenancy', () => {
|
|
56
|
-
afterEach(() => {
|
|
57
|
-
jest.clearAllMocks();
|
|
58
|
-
});
|
|
59
|
-
|
|
60
|
-
afterAll(() => {
|
|
61
|
-
mock.restore();
|
|
62
|
-
});
|
|
63
|
-
|
|
64
|
-
describe('getTenantSchema', () => {
|
|
65
|
-
it('should return a PgSchema for the tenant', () => {
|
|
66
|
-
const schema = getTenantSchema('schema-test-tenant');
|
|
67
|
-
expect(schema).toBeDefined();
|
|
68
|
-
expect(schema.schemaName).toBe('schema-test-tenant');
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
it('should return the same schema for the same tenant', () => {
|
|
72
|
-
const schema1 = getTenantSchema('reuse-schema-tenant');
|
|
73
|
-
const schema2 = getTenantSchema('reuse-schema-tenant');
|
|
74
|
-
expect(schema1).toBe(schema2);
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
|
|
78
|
-
describe('registerTenantSchema', () => {
|
|
79
|
-
it('should register and return a new tenant schema', () => {
|
|
80
|
-
const pgSchemaSpy = spyOn(d, 'pgSchema');
|
|
81
|
-
|
|
82
|
-
const schema = registerTenantSchema('register-tenant');
|
|
83
|
-
|
|
84
|
-
expect(pgSchemaSpy).toHaveBeenCalledWith('register-tenant');
|
|
85
|
-
expect(schema).toBeDefined();
|
|
86
|
-
});
|
|
87
|
-
|
|
88
|
-
it('should not re-register existing tenant schemas', () => {
|
|
89
|
-
const pgSchemaSpy = spyOn(d, 'pgSchema');
|
|
90
|
-
|
|
91
|
-
registerTenantSchema('already-registered');
|
|
92
|
-
pgSchemaSpy.mockClear();
|
|
93
|
-
|
|
94
|
-
const schema = registerTenantSchema('already-registered');
|
|
95
|
-
|
|
96
|
-
expect(pgSchemaSpy).not.toHaveBeenCalled();
|
|
97
|
-
expect(schema).toBeDefined();
|
|
98
|
-
});
|
|
99
|
-
});
|
|
100
|
-
|
|
101
|
-
describe('wrapTenantSchema', () => {
|
|
102
|
-
it('should return a regular pgTable for the public tenant', () => {
|
|
103
|
-
const pgTableSpy = spyOn(d, 'pgTable');
|
|
104
|
-
const cols = { id: d.uuid().primaryKey().defaultRandom() };
|
|
105
|
-
|
|
106
|
-
const table = wrapTenantSchema('public', 'wrap_public', cols);
|
|
107
|
-
|
|
108
|
-
expect(pgTableSpy).toHaveBeenCalledWith('wrap_public', cols);
|
|
109
|
-
expect(table).toBeDefined();
|
|
110
|
-
});
|
|
111
|
-
|
|
112
|
-
it('should return a schema-qualified table for non-public tenants', () => {
|
|
113
|
-
const cols = { id: d.uuid().primaryKey().defaultRandom() };
|
|
114
|
-
|
|
115
|
-
const table = wrapTenantSchema('tenant-wrap', 'wrap_test', cols);
|
|
116
|
-
|
|
117
|
-
expect(table).toBeDefined();
|
|
118
|
-
});
|
|
119
|
-
|
|
120
|
-
it('should cache tenant tables', () => {
|
|
121
|
-
const cols = { id: d.uuid().primaryKey().defaultRandom() };
|
|
122
|
-
|
|
123
|
-
const table1 = wrapTenantSchema('cached-wrap', 'cache_wrap_test', cols);
|
|
124
|
-
const table2 = wrapTenantSchema('cached-wrap', 'cache_wrap_test', cols);
|
|
125
|
-
|
|
126
|
-
expect(table1).toBe(table2);
|
|
127
|
-
});
|
|
128
|
-
});
|
|
129
|
-
|
|
130
|
-
describe('DrizzleSchemaTenancy', () => {
|
|
131
|
-
it('should have mode set to schema', () => {
|
|
132
|
-
const strategy = new DrizzleSchemaTenancy();
|
|
133
|
-
expect(strategy.mode).toBe('schema');
|
|
134
|
-
});
|
|
135
|
-
|
|
136
|
-
it('should resolve a table to a tenant-scoped table', () => {
|
|
137
|
-
const cols = {
|
|
138
|
-
id: d.uuid().primaryKey().defaultRandom(),
|
|
139
|
-
name: d.varchar().notNull()
|
|
140
|
-
};
|
|
141
|
-
const table = pgTable('schema_strategy_test', cols);
|
|
142
|
-
|
|
143
|
-
const strategy = new DrizzleSchemaTenancy();
|
|
144
|
-
const resolved = strategy.resolveTable(table, 'strategy-tenant');
|
|
145
|
-
|
|
146
|
-
expect(resolved).toBeDefined();
|
|
147
|
-
expect(resolved).not.toBe(table);
|
|
148
|
-
});
|
|
149
|
-
|
|
150
|
-
it('should pass through callback in withIsolation', async () => {
|
|
151
|
-
const strategy = new DrizzleSchemaTenancy();
|
|
152
|
-
const result = await strategy.withIsolation(null, 'tenant', async () => 'test-result');
|
|
153
|
-
expect(result).toBe('test-result');
|
|
154
|
-
});
|
|
155
|
-
});
|
|
156
|
-
|
|
157
|
-
describe('DrizzleRLSTenancy', () => {
|
|
158
|
-
it('should have mode set to rls', () => {
|
|
159
|
-
const strategy = new DrizzleRLSTenancy();
|
|
160
|
-
expect(strategy.mode).toBe('rls');
|
|
161
|
-
});
|
|
162
|
-
|
|
163
|
-
it('should return the same table in resolveTable', () => {
|
|
164
|
-
const table = d.pgTable('rls_test', {
|
|
165
|
-
id: d.uuid().primaryKey().defaultRandom()
|
|
166
|
-
});
|
|
167
|
-
|
|
168
|
-
const strategy = new DrizzleRLSTenancy();
|
|
169
|
-
const resolved = strategy.resolveTable(table, 'any-tenant');
|
|
170
|
-
|
|
171
|
-
expect(resolved).toBe(table);
|
|
172
|
-
});
|
|
173
|
-
|
|
174
|
-
it('should wrap callback in a transaction with set_config', async () => {
|
|
175
|
-
const mockTx = {
|
|
176
|
-
execute: mock(() => Promise.resolve())
|
|
177
|
-
};
|
|
178
|
-
const mockConnection = {
|
|
179
|
-
transaction: mock(async (fn: any) => fn(mockTx))
|
|
180
|
-
};
|
|
181
|
-
|
|
182
|
-
const strategy = new DrizzleRLSTenancy();
|
|
183
|
-
const result = await strategy.withIsolation(
|
|
184
|
-
mockConnection,
|
|
185
|
-
'rls-tenant',
|
|
186
|
-
async () => 'rls-result'
|
|
187
|
-
);
|
|
188
|
-
|
|
189
|
-
expect(mockConnection.transaction).toHaveBeenCalled();
|
|
190
|
-
expect(mockTx.execute).toHaveBeenCalled();
|
|
191
|
-
expect(result).toBe('rls-result');
|
|
192
|
-
});
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
describe('createRLSPolicy', () => {
|
|
196
|
-
it('should create an RLS policy with default settings', () => {
|
|
197
|
-
const policy = createRLSPolicy('users');
|
|
198
|
-
|
|
199
|
-
expect(policy).toBeDefined();
|
|
200
|
-
expect(policy.name).toBe('users_tenant_isolation');
|
|
201
|
-
expect(policy.for).toBe('all');
|
|
202
|
-
expect(policy.to).toBe('public');
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
it('should create an RLS policy with custom column and policy name', () => {
|
|
206
|
-
const policy = createRLSPolicy('orders', 'org_id', 'orders_org_policy');
|
|
207
|
-
|
|
208
|
-
expect(policy).toBeDefined();
|
|
209
|
-
expect(policy.name).toBe('orders_org_policy');
|
|
210
|
-
});
|
|
211
|
-
});
|
|
212
|
-
});
|
package/tsconfig.json
DELETED