@geekmidas/testkit 0.4.0 → 1.0.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/CHANGELOG.md +13 -0
- package/dist/Factory-BFVnMMCC.mjs.map +1 -1
- package/dist/{Factory-c16c27Y6.d.cts → Factory-BOX312yd.d.cts} +3 -2
- package/dist/Factory-BOX312yd.d.cts.map +1 -0
- package/dist/Factory-BhjUOBWN.cjs.map +1 -1
- package/dist/{Factory-BcGJjLc8.d.mts → Factory-SFupxRC2.d.mts} +2 -1
- package/dist/Factory-SFupxRC2.d.mts.map +1 -0
- package/dist/Factory.d.cts +2 -2
- package/dist/Factory.d.mts +1 -1
- package/dist/KyselyFactory-BFqVIn_0.cjs.map +1 -1
- package/dist/KyselyFactory-DMswpwji.mjs.map +1 -1
- package/dist/{KyselyFactory-uZ45h7YU.d.cts → KyselyFactory-Dy5zzV4B.d.cts} +4 -3
- package/dist/KyselyFactory-Dy5zzV4B.d.cts.map +1 -0
- package/dist/{KyselyFactory-Cj-EultY.d.mts → KyselyFactory-vAxYodck.d.mts} +3 -2
- package/dist/KyselyFactory-vAxYodck.d.mts.map +1 -0
- package/dist/KyselyFactory.d.cts +3 -3
- package/dist/KyselyFactory.d.mts +2 -2
- package/dist/{ObjectionFactory-DL4qkuF1.d.mts → ObjectionFactory-BWjB49-i.d.mts} +3 -2
- package/dist/ObjectionFactory-BWjB49-i.d.mts.map +1 -0
- package/dist/ObjectionFactory-BeFBYcan.cjs.map +1 -1
- package/dist/{ObjectionFactory-CdhzKs4f.d.cts → ObjectionFactory-CD-WFuMJ.d.cts} +4 -3
- package/dist/ObjectionFactory-CD-WFuMJ.d.cts.map +1 -0
- package/dist/ObjectionFactory-QCJ7u0Ql.mjs.map +1 -1
- package/dist/ObjectionFactory.d.cts +3 -3
- package/dist/ObjectionFactory.d.mts +2 -2
- package/dist/{PostgresKyselyMigrator-upT-hmrz.mjs → PostgresKyselyMigrator-6sE1KOni.mjs} +2 -2
- package/dist/PostgresKyselyMigrator-6sE1KOni.mjs.map +1 -0
- package/dist/{PostgresKyselyMigrator-CIx3AFSR.d.mts → PostgresKyselyMigrator-CBltSOq5.d.cts} +3 -2
- package/dist/PostgresKyselyMigrator-CBltSOq5.d.cts.map +1 -0
- package/dist/{PostgresKyselyMigrator-CfytARcA.cjs → PostgresKyselyMigrator-D6IbPq8t.cjs} +2 -2
- package/dist/PostgresKyselyMigrator-D6IbPq8t.cjs.map +1 -0
- package/dist/{PostgresKyselyMigrator-CQ3aUoy_.d.cts → PostgresKyselyMigrator-DrVWncqd.d.mts} +3 -2
- package/dist/PostgresKyselyMigrator-DrVWncqd.d.mts.map +1 -0
- package/dist/PostgresKyselyMigrator.cjs +2 -2
- package/dist/PostgresKyselyMigrator.d.cts +2 -2
- package/dist/PostgresKyselyMigrator.d.mts +2 -2
- package/dist/PostgresKyselyMigrator.mjs +2 -2
- package/dist/{PostgresMigrator-DbuJGAVy.mjs → PostgresMigrator-BjjenqSd.mjs} +2 -2
- package/dist/PostgresMigrator-BjjenqSd.mjs.map +1 -0
- package/dist/{PostgresMigrator-D5UkK1_K.d.cts → PostgresMigrator-Bres0U6E.d.cts} +2 -1
- package/dist/PostgresMigrator-Bres0U6E.d.cts.map +1 -0
- package/dist/{PostgresMigrator-DFcNdCvD.cjs → PostgresMigrator-D6dQn0x2.cjs} +2 -2
- package/dist/PostgresMigrator-D6dQn0x2.cjs.map +1 -0
- package/dist/{PostgresMigrator-DQaRxoaY.d.mts → PostgresMigrator-S-YYosAC.d.mts} +2 -1
- package/dist/PostgresMigrator-S-YYosAC.d.mts.map +1 -0
- package/dist/PostgresMigrator.cjs +1 -1
- package/dist/PostgresMigrator.d.cts +1 -1
- package/dist/PostgresMigrator.d.mts +1 -1
- package/dist/PostgresMigrator.mjs +1 -1
- package/dist/{PostgresObjectionMigrator-CZHHcCOv.d.cts → PostgresObjectionMigrator-CPfBAP7r.d.cts} +3 -2
- package/dist/PostgresObjectionMigrator-CPfBAP7r.d.cts.map +1 -0
- package/dist/{PostgresObjectionMigrator-BG6ymgnt.cjs → PostgresObjectionMigrator-DK8ODIHQ.cjs} +2 -2
- package/dist/PostgresObjectionMigrator-DK8ODIHQ.cjs.map +1 -0
- package/dist/{PostgresObjectionMigrator-D_hCcrQu.d.mts → PostgresObjectionMigrator-DVEqB5tp.d.mts} +3 -2
- package/dist/PostgresObjectionMigrator-DVEqB5tp.d.mts.map +1 -0
- package/dist/{PostgresObjectionMigrator-DPj2pOpX.mjs → PostgresObjectionMigrator-D_QxXbIN.mjs} +2 -2
- package/dist/PostgresObjectionMigrator-D_QxXbIN.mjs.map +1 -0
- package/dist/PostgresObjectionMigrator.cjs +2 -2
- package/dist/PostgresObjectionMigrator.d.cts +2 -2
- package/dist/PostgresObjectionMigrator.d.mts +2 -2
- package/dist/PostgresObjectionMigrator.mjs +2 -2
- package/dist/{VitestKyselyTransactionIsolator-D3EZZhjZ.d.cts → VitestKyselyTransactionIsolator-CduJlHoT.d.cts} +4 -3
- package/dist/VitestKyselyTransactionIsolator-CduJlHoT.d.cts.map +1 -0
- package/dist/{VitestKyselyTransactionIsolator-Dxlp1u0f.d.mts → VitestKyselyTransactionIsolator-Cswnnj0k.d.mts} +4 -3
- package/dist/VitestKyselyTransactionIsolator-Cswnnj0k.d.mts.map +1 -0
- package/dist/{VitestKyselyTransactionIsolator-EvDLk5zg.cjs → VitestKyselyTransactionIsolator-D7RRXOBa.cjs} +2 -2
- package/dist/VitestKyselyTransactionIsolator-D7RRXOBa.cjs.map +1 -0
- package/dist/{VitestKyselyTransactionIsolator-CNURW8y6.mjs → VitestKyselyTransactionIsolator-DceyIqr4.mjs} +2 -2
- package/dist/VitestKyselyTransactionIsolator-DceyIqr4.mjs.map +1 -0
- package/dist/VitestKyselyTransactionIsolator.cjs +1 -1
- package/dist/VitestKyselyTransactionIsolator.d.cts +2 -2
- package/dist/VitestKyselyTransactionIsolator.d.mts +2 -2
- package/dist/VitestKyselyTransactionIsolator.mjs +1 -1
- package/dist/{VitestObjectionTransactionIsolator-1TpsPqfG.d.cts → VitestObjectionTransactionIsolator-BXoR6xdG.d.cts} +4 -3
- package/dist/VitestObjectionTransactionIsolator-BXoR6xdG.d.cts.map +1 -0
- package/dist/{VitestObjectionTransactionIsolator-CM5KTAFA.cjs → VitestObjectionTransactionIsolator-CdLRrzNf.cjs} +2 -2
- package/dist/VitestObjectionTransactionIsolator-CdLRrzNf.cjs.map +1 -0
- package/dist/{VitestObjectionTransactionIsolator-jQFaCz0u.mjs → VitestObjectionTransactionIsolator-OF2osYY5.mjs} +2 -2
- package/dist/VitestObjectionTransactionIsolator-OF2osYY5.mjs.map +1 -0
- package/dist/{VitestObjectionTransactionIsolator-i9jIgU8Q.d.mts → VitestObjectionTransactionIsolator-x6hY5j4u.d.mts} +4 -3
- package/dist/VitestObjectionTransactionIsolator-x6hY5j4u.d.mts.map +1 -0
- package/dist/VitestObjectionTransactionIsolator.cjs +1 -1
- package/dist/VitestObjectionTransactionIsolator.d.cts +2 -2
- package/dist/VitestObjectionTransactionIsolator.d.mts +2 -2
- package/dist/VitestObjectionTransactionIsolator.mjs +1 -1
- package/dist/{VitestTransactionIsolator-BvR19bYn.d.mts → VitestTransactionIsolator-BNWJqh9f.d.mts} +3 -2
- package/dist/VitestTransactionIsolator-BNWJqh9f.d.mts.map +1 -0
- package/dist/VitestTransactionIsolator-CMfJXZP8.cjs.map +1 -1
- package/dist/{VitestTransactionIsolator-CwQaxZLP.d.cts → VitestTransactionIsolator-CSroc7Df.d.cts} +3 -2
- package/dist/VitestTransactionIsolator-CSroc7Df.d.cts.map +1 -0
- package/dist/VitestTransactionIsolator-DQ7tLqgV.mjs.map +1 -1
- package/dist/VitestTransactionIsolator.d.cts +1 -1
- package/dist/VitestTransactionIsolator.d.mts +1 -1
- package/dist/aws.cjs.map +1 -1
- package/dist/aws.d.cts +2 -0
- package/dist/aws.d.cts.map +1 -0
- package/dist/aws.d.mts +2 -0
- package/dist/aws.d.mts.map +1 -0
- package/dist/aws.mjs.map +1 -1
- package/dist/benchmark.cjs.map +1 -1
- package/dist/benchmark.d.cts +1 -0
- package/dist/benchmark.d.cts.map +1 -0
- package/dist/benchmark.d.mts +1 -0
- package/dist/benchmark.d.mts.map +1 -0
- package/dist/benchmark.mjs.map +1 -1
- package/dist/better-auth.cjs +29 -30
- package/dist/better-auth.cjs.map +1 -1
- package/dist/better-auth.d.cts +2 -2
- package/dist/better-auth.d.cts.map +1 -0
- package/dist/better-auth.d.mts.map +1 -0
- package/dist/better-auth.mjs +29 -30
- package/dist/better-auth.mjs.map +1 -1
- package/dist/directory-B-Ozljzk.mjs.map +1 -1
- package/dist/directory-BVC8g7cX.cjs.map +1 -1
- package/dist/{directory-BXavAeJZ.d.mts → directory-CVrfTq1I.d.mts} +2 -1
- package/dist/directory-CVrfTq1I.d.mts.map +1 -0
- package/dist/{directory-Mi7tdOuD.d.cts → directory-DAnMWi50.d.cts} +2 -1
- package/dist/directory-DAnMWi50.d.cts.map +1 -0
- package/dist/faker-B14IEMIN.cjs.map +1 -1
- package/dist/faker-BGKYFoCT.mjs.map +1 -1
- package/dist/{faker-DvxiCtxc.d.cts → faker-Cg76aFNO.d.cts} +3 -3
- package/dist/faker-Cg76aFNO.d.cts.map +1 -0
- package/dist/faker-DHh7xs4u.d.mts.map +1 -0
- package/dist/faker.d.cts +1 -1
- package/dist/helpers.cjs.map +1 -1
- package/dist/helpers.d.cts +1 -0
- package/dist/helpers.d.cts.map +1 -0
- package/dist/helpers.d.mts +1 -0
- package/dist/helpers.d.mts.map +1 -0
- package/dist/helpers.mjs.map +1 -1
- package/dist/kysely.cjs +3 -3
- package/dist/kysely.cjs.map +1 -1
- package/dist/kysely.d.cts +8 -7
- package/dist/kysely.d.cts.map +1 -0
- package/dist/kysely.d.mts +7 -6
- package/dist/kysely.d.mts.map +1 -0
- package/dist/kysely.mjs +3 -3
- package/dist/kysely.mjs.map +1 -1
- package/dist/logger.cjs.map +1 -1
- package/dist/logger.d.cts +1 -0
- package/dist/logger.d.cts.map +1 -0
- package/dist/logger.d.mts +1 -0
- package/dist/logger.d.mts.map +1 -0
- package/dist/logger.mjs.map +1 -1
- package/dist/objection.cjs +3 -3
- package/dist/objection.cjs.map +1 -1
- package/dist/objection.d.cts +8 -7
- package/dist/objection.d.cts.map +1 -0
- package/dist/objection.d.mts +7 -6
- package/dist/objection.d.mts.map +1 -0
- package/dist/objection.mjs +3 -3
- package/dist/objection.mjs.map +1 -1
- package/dist/os/directory.d.cts +1 -1
- package/dist/os/directory.d.mts +1 -1
- package/dist/os/index.d.cts +1 -1
- package/dist/os/index.d.mts +1 -1
- package/dist/timer.cjs.map +1 -1
- package/dist/timer.d.cts +2 -0
- package/dist/timer.d.cts.map +1 -0
- package/dist/timer.d.mts +2 -0
- package/dist/timer.d.mts.map +1 -0
- package/dist/timer.mjs.map +1 -1
- package/package.json +5 -5
- package/src/Factory.ts +72 -72
- package/src/KyselyFactory.ts +330 -330
- package/src/ObjectionFactory.ts +354 -355
- package/src/PostgresKyselyMigrator.ts +37 -37
- package/src/PostgresMigrator.ts +107 -107
- package/src/PostgresObjectionMigrator.ts +91 -91
- package/src/VitestKyselyTransactionIsolator.ts +27 -27
- package/src/VitestObjectionTransactionIsolator.ts +39 -39
- package/src/VitestTransactionIsolator.ts +196 -195
- package/src/__tests__/Factory.spec.ts +163 -155
- package/src/__tests__/KyselyFactory.spec.ts +443 -439
- package/src/__tests__/ObjectionFactory.spec.ts +563 -557
- package/src/__tests__/PostgresKyselyMigrator.spec.ts +641 -641
- package/src/__tests__/PostgresMigrator.spec.ts +341 -341
- package/src/__tests__/PostgresObjectionMigrator.spec.ts +578 -578
- package/src/__tests__/VitestObjectionTransactionIsolator.spec.ts +114 -114
- package/src/__tests__/benchmark.spec.ts +140 -0
- package/src/__tests__/better-auth.spec.ts +15 -15
- package/src/__tests__/faker.spec.ts +226 -137
- package/src/__tests__/integration.spec.ts +597 -597
- package/src/__tests__/utilities.spec.ts +211 -0
- package/src/aws.ts +104 -104
- package/src/benchmark.ts +12 -12
- package/src/better-auth.ts +286 -301
- package/src/faker.ts +153 -153
- package/src/helpers.ts +6 -6
- package/src/kysely.ts +33 -33
- package/src/logger.ts +10 -10
- package/src/objection.ts +31 -31
- package/src/os/directory.ts +11 -10
- package/src/timer.ts +1 -1
- package/test/globalSetup.ts +45 -45
- package/test/helpers.ts +189 -189
- package/test/migrations/1749664623372_user.ts +13 -13
- package/tsconfig.json +9 -0
- package/vitest.config.ts +4 -4
- package/dist/PostgresKyselyMigrator-CfytARcA.cjs.map +0 -1
- package/dist/PostgresKyselyMigrator-upT-hmrz.mjs.map +0 -1
- package/dist/PostgresMigrator-DFcNdCvD.cjs.map +0 -1
- package/dist/PostgresMigrator-DbuJGAVy.mjs.map +0 -1
- package/dist/PostgresObjectionMigrator-BG6ymgnt.cjs.map +0 -1
- package/dist/PostgresObjectionMigrator-DPj2pOpX.mjs.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-CNURW8y6.mjs.map +0 -1
- package/dist/VitestKyselyTransactionIsolator-EvDLk5zg.cjs.map +0 -1
- package/dist/VitestObjectionTransactionIsolator-CM5KTAFA.cjs.map +0 -1
- package/dist/VitestObjectionTransactionIsolator-jQFaCz0u.mjs.map +0 -1
package/src/better-auth.ts
CHANGED
|
@@ -1,325 +1,310 @@
|
|
|
1
1
|
import {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
createAdapterFactory,
|
|
3
|
+
type DBAdapterDebugLogOption,
|
|
4
|
+
type Where,
|
|
5
5
|
} from 'better-auth/adapters';
|
|
6
6
|
|
|
7
7
|
interface MemoryAdapterConfig {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
debugLogs?: DBAdapterDebugLogOption;
|
|
9
|
+
usePlural?: boolean;
|
|
10
|
+
initialData?: Record<string, any[]>;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
13
|
class MemoryStore {
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
14
|
+
constructor(
|
|
15
|
+
initialData?: Record<string, any[]>,
|
|
16
|
+
private readonly data: Map<string, any> = new Map(),
|
|
17
|
+
) {
|
|
18
|
+
if (initialData) {
|
|
19
|
+
for (const [model, records] of Object.entries(initialData)) {
|
|
20
|
+
const modelData = new Map();
|
|
21
|
+
for (const record of records) {
|
|
22
|
+
modelData.set(record.id, { ...record });
|
|
23
|
+
}
|
|
24
|
+
this.data.set(model, modelData);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
getModel(modelName: string): Map<string, any> {
|
|
30
|
+
if (!this.data.has(modelName)) {
|
|
31
|
+
this.data.set(modelName, new Map());
|
|
32
|
+
}
|
|
33
|
+
return this.data.get(modelName)!;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
clear() {
|
|
37
|
+
this.data.clear();
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
getAllData() {
|
|
41
|
+
const result: Record<string, any[]> = {};
|
|
42
|
+
for (const [model, records] of this.data.entries()) {
|
|
43
|
+
result[model] = Array.from(records.values());
|
|
44
|
+
}
|
|
45
|
+
return result;
|
|
46
|
+
}
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
function matchesWhere(record: any, where?: Where[]): boolean {
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
50
|
+
if (!where || where.length === 0) return true;
|
|
51
|
+
|
|
52
|
+
let result: boolean | null = null;
|
|
53
|
+
|
|
54
|
+
for (const condition of where) {
|
|
55
|
+
const { field, value, operator = 'eq', connector = 'AND' } = condition;
|
|
56
|
+
const recordValue = record[field];
|
|
57
|
+
|
|
58
|
+
let matches = false;
|
|
59
|
+
|
|
60
|
+
switch (operator) {
|
|
61
|
+
case 'eq':
|
|
62
|
+
matches = recordValue === value;
|
|
63
|
+
break;
|
|
64
|
+
case 'ne':
|
|
65
|
+
matches = recordValue !== value;
|
|
66
|
+
break;
|
|
67
|
+
case 'lt':
|
|
68
|
+
matches = value != null && recordValue < value;
|
|
69
|
+
break;
|
|
70
|
+
case 'lte':
|
|
71
|
+
matches = value != null && recordValue <= value;
|
|
72
|
+
break;
|
|
73
|
+
case 'gt':
|
|
74
|
+
matches = value != null && recordValue > value;
|
|
75
|
+
break;
|
|
76
|
+
case 'gte':
|
|
77
|
+
matches = value != null && recordValue >= value;
|
|
78
|
+
break;
|
|
79
|
+
case 'in':
|
|
80
|
+
matches =
|
|
81
|
+
Array.isArray(value) && (value as unknown[]).includes(recordValue);
|
|
82
|
+
break;
|
|
83
|
+
case 'not_in':
|
|
84
|
+
matches =
|
|
85
|
+
Array.isArray(value) && !(value as unknown[]).includes(recordValue);
|
|
86
|
+
break;
|
|
87
|
+
case 'contains':
|
|
88
|
+
matches =
|
|
89
|
+
typeof recordValue === 'string' &&
|
|
90
|
+
typeof value === 'string' &&
|
|
91
|
+
recordValue.includes(value);
|
|
92
|
+
break;
|
|
93
|
+
case 'starts_with':
|
|
94
|
+
matches =
|
|
95
|
+
typeof recordValue === 'string' &&
|
|
96
|
+
typeof value === 'string' &&
|
|
97
|
+
recordValue.startsWith(value);
|
|
98
|
+
break;
|
|
99
|
+
case 'ends_with':
|
|
100
|
+
matches =
|
|
101
|
+
typeof recordValue === 'string' &&
|
|
102
|
+
typeof value === 'string' &&
|
|
103
|
+
recordValue.endsWith(value);
|
|
104
|
+
break;
|
|
105
|
+
default:
|
|
106
|
+
matches = recordValue === value;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
// Apply connector logic
|
|
110
|
+
if (result === null) {
|
|
111
|
+
result = matches;
|
|
112
|
+
} else if (connector === 'OR') {
|
|
113
|
+
result = result || matches;
|
|
114
|
+
} else {
|
|
115
|
+
// Default: AND
|
|
116
|
+
result = result && matches;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
return result ?? true;
|
|
121
121
|
}
|
|
122
122
|
type SortBy = {
|
|
123
|
-
|
|
124
|
-
|
|
123
|
+
field: string;
|
|
124
|
+
direction: 'asc' | 'desc';
|
|
125
125
|
};
|
|
126
126
|
|
|
127
127
|
function applySorting(records: any[], sortBy?: SortBy): any[] {
|
|
128
|
-
|
|
128
|
+
if (!sortBy) return records;
|
|
129
129
|
|
|
130
|
-
|
|
130
|
+
const { field, direction } = sortBy;
|
|
131
131
|
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
132
|
+
return records.sort((a, b) => {
|
|
133
|
+
const aVal = a[field];
|
|
134
|
+
const bVal = b[field];
|
|
135
135
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
136
|
+
if (aVal === bVal) return 0;
|
|
137
|
+
if (aVal == null) return direction === 'asc' ? -1 : 1;
|
|
138
|
+
if (bVal == null) return direction === 'asc' ? 1 : -1;
|
|
139
139
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
140
|
+
const comparison = aVal < bVal ? -1 : 1;
|
|
141
|
+
return direction === 'asc' ? comparison : -comparison;
|
|
142
|
+
});
|
|
143
143
|
}
|
|
144
144
|
|
|
145
145
|
export const memoryAdapter = (
|
|
146
|
-
|
|
147
|
-
|
|
146
|
+
config: MemoryAdapterConfig = {},
|
|
147
|
+
store = new Map(),
|
|
148
148
|
) => {
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
const modelData = storeInstance.getModel(modelName);
|
|
311
|
-
|
|
312
|
-
return Array.from(modelData.values()).filter((record) =>
|
|
313
|
-
matchesWhere(record, where),
|
|
314
|
-
).length;
|
|
315
|
-
},
|
|
316
|
-
}),
|
|
317
|
-
});
|
|
318
|
-
|
|
319
|
-
// Add utility methods to the adapter
|
|
320
|
-
return Object.assign(adapterInstance, {
|
|
321
|
-
clear: () => store.clear(),
|
|
322
|
-
getAllData: () => storeInstance.getAllData(),
|
|
323
|
-
getStore: () => store,
|
|
324
|
-
});
|
|
149
|
+
const storeInstance = new MemoryStore(config.initialData, store);
|
|
150
|
+
|
|
151
|
+
const adapterInstance = createAdapterFactory({
|
|
152
|
+
config: {
|
|
153
|
+
adapterId: 'memory-adapter',
|
|
154
|
+
adapterName: 'Memory Adapter',
|
|
155
|
+
usePlural: config.usePlural ?? false,
|
|
156
|
+
debugLogs: config.debugLogs ?? false,
|
|
157
|
+
supportsJSON: true,
|
|
158
|
+
supportsDates: true,
|
|
159
|
+
supportsBooleans: true,
|
|
160
|
+
supportsNumericIds: false,
|
|
161
|
+
},
|
|
162
|
+
|
|
163
|
+
// Note: The framework automatically transforms field names in both directions:
|
|
164
|
+
// - Input data is already transformed before reaching the adapter (e.g., email -> email_address)
|
|
165
|
+
// - Output data is automatically transformed after the adapter returns (e.g., email_address -> email)
|
|
166
|
+
// Therefore, we should NOT call transformInput or transformOutput ourselves.
|
|
167
|
+
adapter: ({ debugLog, getModelName, transformWhereClause }) => ({
|
|
168
|
+
create: async ({ data, model }) => {
|
|
169
|
+
debugLog('CREATE', { model, data });
|
|
170
|
+
const modelName = getModelName(model);
|
|
171
|
+
const modelData = storeInstance.getModel(modelName);
|
|
172
|
+
|
|
173
|
+
// Data is already transformed by the framework - store directly
|
|
174
|
+
const record = data as Record<string, unknown>;
|
|
175
|
+
if (!record.id) {
|
|
176
|
+
record.id = crypto.randomUUID();
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
const id = record.id as string;
|
|
180
|
+
modelData.set(id, { ...record });
|
|
181
|
+
return modelData.get(id);
|
|
182
|
+
},
|
|
183
|
+
|
|
184
|
+
findOne: async ({ where, model }) => {
|
|
185
|
+
debugLog('FIND_ONE', { model, where });
|
|
186
|
+
const modelName = getModelName(model);
|
|
187
|
+
const modelData = storeInstance.getModel(modelName);
|
|
188
|
+
const transformedWhere = transformWhereClause({ model, where });
|
|
189
|
+
|
|
190
|
+
for (const record of modelData.values()) {
|
|
191
|
+
if (matchesWhere(record, transformedWhere)) {
|
|
192
|
+
return record;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return null;
|
|
196
|
+
},
|
|
197
|
+
|
|
198
|
+
findMany: async ({ where, model, limit, offset, sortBy }) => {
|
|
199
|
+
debugLog('FIND_MANY', { model, where });
|
|
200
|
+
|
|
201
|
+
const modelName = getModelName(model);
|
|
202
|
+
const modelData = storeInstance.getModel(modelName);
|
|
203
|
+
const transformedWhere = transformWhereClause({ model, where });
|
|
204
|
+
|
|
205
|
+
let results = Array.from(modelData.values()).filter((record) =>
|
|
206
|
+
matchesWhere(record, transformedWhere),
|
|
207
|
+
);
|
|
208
|
+
|
|
209
|
+
if (sortBy) {
|
|
210
|
+
results = applySorting(results, sortBy);
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
if (offset) {
|
|
214
|
+
results = results.slice(offset);
|
|
215
|
+
}
|
|
216
|
+
if (limit) {
|
|
217
|
+
results = results.slice(0, limit);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
return results;
|
|
221
|
+
},
|
|
222
|
+
|
|
223
|
+
update: async ({ where, update, model }) => {
|
|
224
|
+
debugLog('UPDATE', { model, where });
|
|
225
|
+
|
|
226
|
+
const modelName = getModelName(model);
|
|
227
|
+
const modelData = storeInstance.getModel(modelName);
|
|
228
|
+
const transformedWhere = transformWhereClause({ model, where });
|
|
229
|
+
|
|
230
|
+
for (const [id, record] of modelData.entries()) {
|
|
231
|
+
if (matchesWhere(record, transformedWhere)) {
|
|
232
|
+
// Update data is already transformed by the framework
|
|
233
|
+
const updated = { ...record, ...update };
|
|
234
|
+
modelData.set(id, updated);
|
|
235
|
+
return updated;
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
return null;
|
|
239
|
+
},
|
|
240
|
+
|
|
241
|
+
updateMany: async ({ where, update, model }) => {
|
|
242
|
+
debugLog('UPDATE_MANY', { model, where });
|
|
243
|
+
const modelName = getModelName(model);
|
|
244
|
+
const modelData = storeInstance.getModel(modelName);
|
|
245
|
+
const transformedWhere = transformWhereClause({ model, where });
|
|
246
|
+
|
|
247
|
+
let count = 0;
|
|
248
|
+
|
|
249
|
+
for (const [id, record] of modelData.entries()) {
|
|
250
|
+
if (matchesWhere(record, transformedWhere)) {
|
|
251
|
+
// Update data is already transformed by the framework
|
|
252
|
+
modelData.set(id, { ...record, ...update });
|
|
253
|
+
count++;
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
return count;
|
|
257
|
+
},
|
|
258
|
+
|
|
259
|
+
delete: async ({ where, model }) => {
|
|
260
|
+
debugLog('DELETE', { model, where });
|
|
261
|
+
const modelName = getModelName(model);
|
|
262
|
+
const modelData = storeInstance.getModel(modelName);
|
|
263
|
+
const transformedWhere = transformWhereClause({ model, where });
|
|
264
|
+
|
|
265
|
+
for (const [id, record] of modelData.entries()) {
|
|
266
|
+
if (matchesWhere(record, transformedWhere)) {
|
|
267
|
+
modelData.delete(id);
|
|
268
|
+
return;
|
|
269
|
+
}
|
|
270
|
+
}
|
|
271
|
+
},
|
|
272
|
+
|
|
273
|
+
deleteMany: async ({ where, model }) => {
|
|
274
|
+
debugLog('DELETE_MANY', { model, where });
|
|
275
|
+
const modelName = getModelName(model);
|
|
276
|
+
const modelData = storeInstance.getModel(modelName);
|
|
277
|
+
const transformedWhere = transformWhereClause({ model, where });
|
|
278
|
+
|
|
279
|
+
const toDelete: string[] = [];
|
|
280
|
+
for (const [id, record] of modelData.entries()) {
|
|
281
|
+
if (matchesWhere(record, transformedWhere)) {
|
|
282
|
+
toDelete.push(id);
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
for (const id of toDelete) {
|
|
287
|
+
modelData.delete(id);
|
|
288
|
+
}
|
|
289
|
+
return toDelete.length;
|
|
290
|
+
},
|
|
291
|
+
|
|
292
|
+
count: async ({ where, model }) => {
|
|
293
|
+
const modelName = getModelName(model);
|
|
294
|
+
const modelData = storeInstance.getModel(modelName);
|
|
295
|
+
const transformedWhere = transformWhereClause({ model, where });
|
|
296
|
+
|
|
297
|
+
return Array.from(modelData.values()).filter((record) =>
|
|
298
|
+
matchesWhere(record, transformedWhere),
|
|
299
|
+
).length;
|
|
300
|
+
},
|
|
301
|
+
}),
|
|
302
|
+
});
|
|
303
|
+
|
|
304
|
+
// Add utility methods to the adapter
|
|
305
|
+
return Object.assign(adapterInstance, {
|
|
306
|
+
clear: () => store.clear(),
|
|
307
|
+
getAllData: () => storeInstance.getAllData(),
|
|
308
|
+
getStore: () => store,
|
|
309
|
+
});
|
|
325
310
|
};
|