-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.ts
More file actions
174 lines (151 loc) · 3.52 KB
/
types.ts
File metadata and controls
174 lines (151 loc) · 3.52 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
/**
* @file types.ts
* @description Contains all shared type definitions and enums for the Stabilize ORM.
* @author ElectronSz
*/
export enum DBType {
Postgres = "postgres",
MySQL = "mysql",
SQLite = "sqlite",
}
export enum LogLevel {
Debug,
Info,
Warn,
Error,
}
export enum RelationType {
OneToOne,
OneToMany,
ManyToOne,
ManyToMany,
}
/**
* An enumeration of abstract data types that are mapped to database-specific types.
* This allows models to be defined in a database-agnostic way.
*/
export enum DataTypes {
STRING, // Maps to VARCHAR or TEXT
TEXT, // Maps to TEXT
INTEGER, // Maps to INTEGER or INT
BIGINT, // Maps to BIGINT
FLOAT, // Maps to REAL or FLOAT
DOUBLE, // Maps to DOUBLE PRECISION
DECIMAL, // Maps to DECIMAL or NUMERIC
BOOLEAN, // Maps to BOOLEAN or TINYINT/INTEGER
DATE, // Maps to DATE or TEXT
DATETIME, // Maps to TIMESTAMP, DATETIME, or TEXT
JSON, // Maps to JSON, JSONB, or TEXT
UUID, // Maps to UUID or VARCHAR(36)
BLOB, // Maps to BYTEA or BLOB
}
export interface DBConfig {
type: DBType;
connectionString: string;
retryAttempts?: number;
retryDelay?: number;
maxJitter?: number;
}
export interface CacheConfig {
enabled: boolean;
ttl: number;
redisUrl?: string;
cachePrefix?: string;
strategy?: "cache-aside" | "write-through";
}
/**
* Configuration for the logger.
*/
export interface LoggerConfig {
level?: LogLevel;
filePath?: string;
maxFileSize?: number;
maxFiles?: number;
}
export interface PoolMetrics {
activeConnections: number;
idleConnections: number;
totalConnections: number;
}
export interface QueryHint {
type: string;
value: string;
}
export interface CacheStats {
hits: number;
misses: number;
keys: number;
}
export interface Migration {
name: string;
up: string[];
down: string[];
}
export class StabilizeError extends Error {
constructor(
message: string,
public code: string,
public originalError?: Error,
) {
super(message);
this.name = "StabilizeError";
}
}
export interface DefaultExpression {
sql: string;
}
export function sqlDefault(sql: string): DefaultExpression {
return { sql };
}
export type TransactionIsolationLevel =
| "READ UNCOMMITTED"
| "READ COMMITTED"
| "REPEATABLE READ"
| "SERIALIZABLE";
export interface QueryLogEntry {
query: string;
params: any[];
durationMs: number;
timestamp: Date;
source: string;
}
export type StabilizeEvent =
| "query"
| "error"
| "migration:start"
| "migration:complete"
| "transaction:start"
| "transaction:complete"
| "transaction:error"
| "connection:open"
| "connection:close";
export type StabilizeEventHandler = (...args: any[]) => void;
export class StabilizeEmitter {
private listeners: Map<StabilizeEvent, StabilizeEventHandler[]> = new Map();
on(event: StabilizeEvent, handler: StabilizeEventHandler): void {
if (!this.listeners.has(event)) {
this.listeners.set(event, []);
}
this.listeners.get(event)!.push(handler);
}
off(event: StabilizeEvent, handler: StabilizeEventHandler): void {
const handlers = this.listeners.get(event);
if (handlers) {
const idx = handlers.indexOf(handler);
if (idx !== -1) handlers.splice(idx, 1);
}
}
emit(event: StabilizeEvent, ...args: any[]): void {
const handlers = this.listeners.get(event);
if (handlers) {
for (const handler of handlers) {
try {
handler(...args);
} catch {}
}
}
}
}
export function generateUUID(): string {
return crypto.randomUUID();
}