-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
267 lines (251 loc) · 6.95 KB
/
Copy pathindex.ts
File metadata and controls
267 lines (251 loc) · 6.95 KB
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
142
143
144
145
146
147
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
/**
* @file stabilize.ts
* @description The main entry point for the Stabilize ORM, tying together the client, cache, and repositories.
* @author ElectronSz
*/
import { Cache } from "./cache";
import { DBClient } from "./client";
import { type Logger, StabilizeLogger } from "./logger";
import { QueryBuilder } from "./query-builder";
import { Repository } from "./repository";
import {
runMigrations,
generateMigration,
type Migration,
mapDataTypeToSql,
} from "./migrations";
import {
autoMigrate,
defineSeed,
runSeeds,
resetDatabase,
type SeedDefinition,
} from "./auto-migrate";
import {
type DBConfig,
type CacheConfig,
type LoggerConfig,
DBType,
DataTypes,
StabilizeError,
type PoolMetrics,
type QueryHint,
RelationType,
type CacheStats,
LogLevel,
type DefaultExpression,
sqlDefault,
type TransactionIsolationLevel,
type QueryLogEntry,
type StabilizeEvent,
type StabilizeEventHandler,
StabilizeEmitter,
generateUUID,
} from "./types";
import { defineModel, MetadataStorage } from "./model";
import type { Hook } from "./hooks";
export class Stabilize {
public client: DBClient;
private cache: Cache | null;
private logger: Logger;
public events: StabilizeEmitter;
constructor(
config: DBConfig,
cacheConfig: CacheConfig = { enabled: false, ttl: 60 },
loggerConfig: LoggerConfig = {},
existingClient?: DBClient,
) {
this.logger = new StabilizeLogger(loggerConfig);
this.client = existingClient || new DBClient(config, this.logger);
this.cache = existingClient
? null
: cacheConfig.enabled
? new Cache(cacheConfig, this.logger)
: null;
this.events = new StabilizeEmitter();
this.events.emit("connection:open", config.type);
}
/**
* Gets a repository for a given model, used to perform CRUD operations.
* @param model The model class, defined using `defineModel`.
* @returns A new `Repository` instance for the specified model.
* @example
* ```
* const stabilize = new Stabilize(dbConfig);
* const userRepository = stabilize.getRepository(User);
*
* const user = await userRepository.findOne(1);
* console.log(user);
* ```
*/
getRepository<T>(model: new (...args: any[]) => T): Repository<T> {
const cacheConfig = this.cache ? this.cache.config : undefined;
return new Repository(this.client, model, cacheConfig, this.logger);
}
/**
* Executes a callback within a database transaction, ensuring all operations are atomic.
* The callback receives a transactional `DBClient` instance that must be passed to
* repository methods to ensure they are part of the same transaction.
*
* @param callback The async function to execute. It receives a `txClient` as its only argument.
* @returns The result of the callback function.
* @example
* ```
* const userRepo = stabilize.getRepository(User);
* const profileRepo = stabilize.getRepository(Profile);
*
* try {
* await stabilize.transaction(async (txClient) => {
* const newUser = await userRepo.create({ name: 'Ciniso Dlamini' }, {}, txClient);
* await profileRepo.create({ userId: newUser.id, bio: 'A new bio' }, {}, txClient);
* });
* console.log('User and profile created successfully.');
* } catch (error) {
* console.error('Transaction failed, everything was rolled back.', error);
* }
* ```
*/
async transaction<T>(
callback: (txClient: DBClient) => Promise<T>,
): Promise<T> {
return this.client.transaction(callback);
}
/**
* Retrieves statistics from the cache, if it is enabled.
* @returns A promise that resolves to an object containing cache hits, misses, and total keys.
* @example
* ```
* const stats = await stabilize.getCacheStats();
* console.log(`Cache Hits: ${stats.hits}, Misses: ${stats.misses}`);
* ```
*/
async getCacheStats(): Promise<CacheStats> {
if (!this.cache) {
return { hits: 0, misses: 0, keys: 0 };
}
return this.cache.getStats();
}
/**
* Closes the database connection and disconnects the cache client for a graceful shutdown.
* @example
* ```
* await stabilize.close();
* console.log('Connections closed.');
* ```
*/
async close() {
this.events.emit("connection:close");
await this.client.close();
if (this.cache) {
await this.cache.disconnect();
}
}
async healthCheck(): Promise<{
status: string;
database: string;
latencyMs: number;
cacheStatus: string;
}> {
const start = performance.now();
try {
const results = await this.client.query("SELECT 1 AS ok");
const cacheStatus = this.cache
? (await this.cache.get("healthcheck"))
? "connected"
: "connected (miss)"
: "disabled";
return {
status: results.length > 0 ? "healthy" : "unhealthy",
database: this.client.config.type,
latencyMs: Number((performance.now() - start).toFixed(2)),
cacheStatus,
};
} catch (error) {
return {
status: "unhealthy",
database: this.client.config.type,
latencyMs: Number((performance.now() - start).toFixed(2)),
cacheStatus: "unknown",
};
}
}
async rawQuery<T = any>(query: string, params: any[] = []): Promise<T[]> {
return this.client.query<T>(query, params);
}
async rawExec(
query: string,
params: any[] = [],
): Promise<{ affectedRows: number }> {
return this.client.queryExec(query, params);
}
async migrate(config: DBConfig, migrations: Migration[]) {
return runMigrations(config, migrations);
}
async autoMigrate(models: any | any[]) {
return autoMigrate(this.client, models);
}
async seed(seeds?: any[]) {
if (seeds) {
return runSeeds(this.client, seeds);
}
return runSeeds(this.client);
}
async reset(models: any | any[]) {
return resetDatabase(this.client, models);
}
async poolStats(): Promise<{ active: number; idle: number; total: number }> {
const raw = this.client as any;
if (raw.totalCount !== undefined) {
return { active: 0, idle: 0, total: raw.totalCount };
}
if (raw._allConnections && raw._allConnections.length !== undefined) {
return {
active: raw._allConnections.length,
idle: raw._freeConnections?.length ?? 0,
total: raw._allConnections.length,
};
}
return { active: -1, idle: -1, total: -1 };
}
}
export {
Repository,
DBClient,
QueryBuilder,
Cache,
StabilizeLogger,
DBType,
DataTypes,
LogLevel,
RelationType,
MetadataStorage,
mapDataTypeToSql,
StabilizeError,
runMigrations,
generateMigration,
defineModel,
autoMigrate,
sqlDefault,
defineSeed,
runSeeds,
resetDatabase,
StabilizeEmitter,
generateUUID,
};
export type {
Migration,
DBConfig,
CacheConfig,
LoggerConfig,
QueryHint,
PoolMetrics,
CacheStats,
Logger,
Hook,
DefaultExpression,
SeedDefinition,
TransactionIsolationLevel,
QueryLogEntry,
StabilizeEvent,
StabilizeEventHandler,
};