-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
141 lines (130 loc) · 5.03 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
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
47
48
49
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { inject, Module } from "djinject";
import { UndoLogPublicImpl, UndoLogSetupPublicImpl } from "./impl";
import { Assertions } from "./assertions";
import { Connection } from "./sqlite3";
import { Delta, UndoLog } from "./undo-log";
import { UndoLogAssertions } from "./undo-log-assertions";
import { AssertionsImpl } from "./impl/assertions";
import { UndoLogImpl } from "./impl/undo-log";
import { UndoLogAssertionsImpl } from "./impl/undo-log-assertions";
import { ActionServices } from "./utils/action-services";
import { ChannelServices } from "./utils/channel-services";
import { ConfigurationServices } from "./utils/configuration-services";
import { DatabaseDefinitionServices } from "./utils/database-definition-services";
import { DatabaseManipulationServices } from "./utils/database-manipulation-services";
import { DatabaseQueryServices } from "./utils/database-query-services";
import { DatabaseUtilitiesServices } from "./utils/database-utilities-services";
import { SetupServices } from "./utils/setup-services";
import { TableServices } from "./utils/table-services";
import { TeardownServices } from "./utils/teardown-services";
import { ActionServicesImpl } from "./impl/utils/action-services";
import { ChannelServicesImpl } from "./impl/utils/channel-services";
import { ConfigurationServicesImpl } from "./impl/utils/configuration-services";
import { DatabaseDefinitionServicesImpl } from "./impl/utils/database-definition-services";
import { DatabaseManipulationServicesImpl } from "./impl/utils/database-manipulation-services";
import { DatabaseQueryServicesImpl } from "./impl/utils/database-query-services";
import { DatabaseUtilitiesServicesImpl } from "./impl/utils/database-utilities-services";
import { SetupServicesImpl } from "./impl/utils/setup-services";
import { TableServicesImpl } from "./impl/utils/table-services";
import { TeardownServicesImpl } from "./impl/utils/teardown-services";
export interface UndoLogServices {
connection: Connection;
installations: {
forceForeignKeys: boolean;
prefix: string;
setup: SetupServices;
teardown: TeardownServices;
}
internals: {
configs: ConfigurationServices;
tables: TableServices;
channels: ChannelServices;
actions: ActionServices;
logFactory: () => UndoLog;
}
databases: {
definitions: DatabaseDefinitionServices;
manipulations: DatabaseManipulationServices;
queries: DatabaseQueryServices;
utils: DatabaseUtilitiesServices;
}
api: {
setup: UndoLogPublicSetup;
logFactory: (channelId: number) => UndoLogPublic;
}
}
export interface UndoLogTestServices extends UndoLogServices {
tests: {
assertions: Assertions;
logAssertions: UndoLogAssertions;
}
}
function module(
connection: Connection,
prefix: string
): Module<UndoLogServices, UndoLogServices> {
const module: Module<UndoLogServices, UndoLogServices> = {
connection: () => connection,
installations: {
prefix: () => prefix,
forceForeignKeys: () => true,
setup: srv => new SetupServicesImpl(srv),
teardown: srv => new TeardownServicesImpl(srv),
},
internals: {
configs: srv => new ConfigurationServicesImpl(srv),
tables: srv => new TableServicesImpl(srv),
channels: srv => new ChannelServicesImpl(srv),
actions: srv => new ActionServicesImpl(srv),
logFactory: (srv) => () => new UndoLogImpl(srv),
},
databases: {
definitions: srv => new DatabaseDefinitionServicesImpl(srv),
manipulations: srv => new DatabaseManipulationServicesImpl(srv),
queries: srv => new DatabaseQueryServicesImpl(srv),
utils: () => new DatabaseUtilitiesServicesImpl(),
},
api: {
setup: (srv) => new UndoLogSetupPublicImpl(srv),
logFactory: (srv) => (channelId: number) => new UndoLogPublicImpl(srv, channelId),
}
};
return module;
}
export function createUndoLogServices(connection: Connection, prefix: string) {
return inject(module(connection, prefix)) as UndoLogServices;
}
function testModule(
connection: Connection,
prefix: string
): Module<UndoLogTestServices> {
return {
...module(connection, prefix),
tests: {
assertions: (srv) => new AssertionsImpl(srv),
logAssertions: (srv) => new UndoLogAssertionsImpl(srv),
},
};
}
export function createTestServices(connection: Connection, prefix: string) {
return inject(testModule(connection, prefix)) as UndoLogTestServices;
}
export type InitializeMultipleOptions = Record<number, string | string[]>;
export type InitializeMultipleResult = Record<number, UndoLogPublic>;
export interface UndoLogPublicSetup {
initializeSingle(channelId?: number): Promise<UndoLogPublic>;
initializeMultiple(
options: InitializeMultipleOptions
): Promise<InitializeMultipleResult>;
}
export interface UndoLogConstructorOptions {
tablePrefix: string;
channelId: number;
}
export interface UndoLogPublic {
trackWithin(action: () => Promise<void>, category?: string): Promise<void>;
canUndo(): Promise<boolean>;
canRedo(): Promise<boolean>;
undo(): Promise<Delta[]>;
redo(): Promise<Delta[]>;
}