@ooneex/database 0.0.12 → 0.0.13
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/dist/index.js +206 -2
- package/dist/index.js.map +2 -2
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,4 +1,208 @@
|
|
|
1
1
|
// @bun
|
|
2
|
-
|
|
2
|
+
// src/AbstractTypeormSqliteDatabase.ts
|
|
3
|
+
class AbstractTypeormSqliteDatabase {
|
|
4
|
+
source;
|
|
5
|
+
async open(entity, database) {
|
|
6
|
+
const source = this.getSource(database);
|
|
7
|
+
if (!source.isInitialized) {
|
|
8
|
+
await source.initialize();
|
|
9
|
+
}
|
|
10
|
+
return source.getRepository(entity);
|
|
11
|
+
}
|
|
12
|
+
async close(database) {
|
|
13
|
+
const source = this.getSource(database);
|
|
14
|
+
if (source.isInitialized) {
|
|
15
|
+
await source.destroy();
|
|
16
|
+
}
|
|
17
|
+
}
|
|
18
|
+
async drop(database) {
|
|
19
|
+
const source = this.getSource(database);
|
|
20
|
+
if (source.isInitialized) {
|
|
21
|
+
await source.dropDatabase();
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
getEntityManager(database) {
|
|
25
|
+
return this.getSource(database).manager;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
// src/DatabaseException.ts
|
|
29
|
+
import { Exception } from "@ooneex/exception";
|
|
30
|
+
import { HttpStatus } from "@ooneex/http-status";
|
|
3
31
|
|
|
4
|
-
|
|
32
|
+
class DatabaseException extends Exception {
|
|
33
|
+
constructor(message, data = {}) {
|
|
34
|
+
super(message, {
|
|
35
|
+
status: HttpStatus.Code.InternalServerError,
|
|
36
|
+
data
|
|
37
|
+
});
|
|
38
|
+
this.name = "DatabaseException";
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
// src/decorators.ts
|
|
42
|
+
import { container, EContainerScope } from "@ooneex/container";
|
|
43
|
+
var decorator = {
|
|
44
|
+
database: (scope = EContainerScope.Singleton) => {
|
|
45
|
+
return (target) => {
|
|
46
|
+
container.add(target, scope);
|
|
47
|
+
};
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
// src/RedisDatabase.ts
|
|
51
|
+
var {RedisClient } = globalThis.Bun;
|
|
52
|
+
class RedisDatabase {
|
|
53
|
+
options;
|
|
54
|
+
client;
|
|
55
|
+
connectionUrl;
|
|
56
|
+
constructor(options = {}) {
|
|
57
|
+
this.options = options;
|
|
58
|
+
this.connectionUrl = options.url || Bun.env.DATABASE_REDIS_URL || "";
|
|
59
|
+
if (!this.connectionUrl) {
|
|
60
|
+
throw new DatabaseException("No Redis connection URL provided. The 'url' option must be specified or set the following environment variable: DATABASE_REDIS_URL.", {
|
|
61
|
+
...options,
|
|
62
|
+
connectionUrl: this.connectionUrl
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
this.client = new RedisClient(this.connectionUrl, {
|
|
66
|
+
connectionTimeout: options.connectionTimeout || 1e4,
|
|
67
|
+
idleTimeout: options.idleTimeout || 0,
|
|
68
|
+
autoReconnect: options.autoReconnect ?? true,
|
|
69
|
+
maxRetries: options.maxRetries || 10,
|
|
70
|
+
enableOfflineQueue: options.enableOfflineQueue ?? true,
|
|
71
|
+
enableAutoPipelining: options.enableAutoPipelining ?? true,
|
|
72
|
+
...options.tls !== undefined && { tls: options.tls }
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
getClient() {
|
|
76
|
+
return this.client;
|
|
77
|
+
}
|
|
78
|
+
async open() {
|
|
79
|
+
try {
|
|
80
|
+
if (!this.client.connected) {
|
|
81
|
+
await this.client.connect();
|
|
82
|
+
}
|
|
83
|
+
return this.client;
|
|
84
|
+
} catch (error) {
|
|
85
|
+
throw new DatabaseException(`Failed to open Redis connection: ${error instanceof Error ? error.message : String(error)}`, {
|
|
86
|
+
connectionUrl: this.connectionUrl,
|
|
87
|
+
options: this.options,
|
|
88
|
+
error
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
async close() {
|
|
93
|
+
try {
|
|
94
|
+
if (this.client.connected) {
|
|
95
|
+
this.client.close();
|
|
96
|
+
}
|
|
97
|
+
} catch (error) {
|
|
98
|
+
throw new DatabaseException(`Failed to close Redis connection: ${error instanceof Error ? error.message : String(error)}`, {
|
|
99
|
+
connectionUrl: this.connectionUrl,
|
|
100
|
+
error
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
async drop() {
|
|
105
|
+
try {
|
|
106
|
+
if (!this.client.connected) {
|
|
107
|
+
await this.open();
|
|
108
|
+
}
|
|
109
|
+
await this.client.send("FLUSHDB", []);
|
|
110
|
+
} catch (error) {
|
|
111
|
+
throw new DatabaseException(`Failed to drop Redis database: ${error instanceof Error ? error.message : String(error)}`, {
|
|
112
|
+
connectionUrl: this.connectionUrl,
|
|
113
|
+
error
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
// src/TypeormPgDatabase.ts
|
|
119
|
+
import { DataSource } from "typeorm";
|
|
120
|
+
class TypeormPgDatabase {
|
|
121
|
+
source;
|
|
122
|
+
constructor(options = {}) {
|
|
123
|
+
const url = (options.url || Bun.env.DATABASE_URL || "").trim();
|
|
124
|
+
if (!url) {
|
|
125
|
+
throw new DatabaseException("No database URL provided. Please set DATABASE_URL environment variable or provide a url in the options.", {
|
|
126
|
+
...options,
|
|
127
|
+
url
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
this.source = new DataSource({
|
|
131
|
+
synchronize: false,
|
|
132
|
+
entities: [],
|
|
133
|
+
extra: {
|
|
134
|
+
max: 10
|
|
135
|
+
},
|
|
136
|
+
...options,
|
|
137
|
+
url,
|
|
138
|
+
type: "postgres"
|
|
139
|
+
});
|
|
140
|
+
}
|
|
141
|
+
getSource() {
|
|
142
|
+
return this.source;
|
|
143
|
+
}
|
|
144
|
+
async open(entity) {
|
|
145
|
+
const source = this.getSource();
|
|
146
|
+
if (!source.isInitialized) {
|
|
147
|
+
await source.initialize();
|
|
148
|
+
}
|
|
149
|
+
return source.getRepository(entity);
|
|
150
|
+
}
|
|
151
|
+
async close() {
|
|
152
|
+
const source = this.getSource();
|
|
153
|
+
if (source.isInitialized) {
|
|
154
|
+
await source.destroy();
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
async drop() {
|
|
158
|
+
const source = this.getSource();
|
|
159
|
+
if (source.isInitialized) {
|
|
160
|
+
await source.dropDatabase();
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
getEntityManager() {
|
|
164
|
+
return this.getSource().manager;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
// src/TypeormSqliteDatabase.ts
|
|
168
|
+
import { DataSource as DataSource2 } from "typeorm";
|
|
169
|
+
class TypeormSqliteDatabase extends AbstractTypeormSqliteDatabase {
|
|
170
|
+
options;
|
|
171
|
+
constructor(options = undefined) {
|
|
172
|
+
super();
|
|
173
|
+
this.options = options;
|
|
174
|
+
}
|
|
175
|
+
getSource(database) {
|
|
176
|
+
if (!database && this.source) {
|
|
177
|
+
return this.source;
|
|
178
|
+
}
|
|
179
|
+
database = database || this.options?.database || Bun.env.SQLITE_DATABASE_PATH || "";
|
|
180
|
+
if (!database) {
|
|
181
|
+
throw new DatabaseException("No database path provided. The 'database' option must be specified with a valid file path or ':memory:' for in-memory database. Alternatively, set the SQLITE_DATABASE_PATH environment variable.", {
|
|
182
|
+
...this.options || {},
|
|
183
|
+
database
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
this.source = new DataSource2({
|
|
187
|
+
synchronize: false,
|
|
188
|
+
entities: [],
|
|
189
|
+
enableWAL: true,
|
|
190
|
+
busyErrorRetry: 2000,
|
|
191
|
+
busyTimeout: 30000,
|
|
192
|
+
...this.options || {},
|
|
193
|
+
database,
|
|
194
|
+
type: "sqlite"
|
|
195
|
+
});
|
|
196
|
+
return this.source;
|
|
197
|
+
}
|
|
198
|
+
}
|
|
199
|
+
export {
|
|
200
|
+
decorator,
|
|
201
|
+
TypeormSqliteDatabase,
|
|
202
|
+
TypeormPgDatabase,
|
|
203
|
+
RedisDatabase,
|
|
204
|
+
DatabaseException,
|
|
205
|
+
AbstractTypeormSqliteDatabase
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
//# debugId=2243286518C5C83764756E2164756E21
|
package/dist/index.js.map
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
"import { DataSource, type EntityManager, type EntityTarget, type ObjectLiteral, type Repository } from \"typeorm\";\nimport type { PostgresConnectionOptions } from \"typeorm/driver/postgres/PostgresConnectionOptions.js\";\nimport { DatabaseException } from \"./DatabaseException\";\nimport type { ITypeormDatabase } from \"./types\";\n\nexport class TypeormPgDatabase implements ITypeormDatabase {\n private source: DataSource;\n\n constructor(options: Omit<PostgresConnectionOptions, \"type\"> = {}) {\n const url = (options.url || Bun.env.DATABASE_URL || \"\").trim();\n\n if (!url) {\n throw new DatabaseException(\n \"No database URL provided. Please set DATABASE_URL environment variable or provide a url in the options.\",\n {\n ...options,\n url,\n },\n );\n }\n\n this.source = new DataSource({\n synchronize: false,\n entities: [],\n extra: {\n max: 10,\n // idleTimeoutMillis: 30000,\n },\n ...options,\n url,\n type: \"postgres\",\n });\n }\n\n public getSource(): DataSource {\n return this.source;\n }\n\n public async open<Entity extends ObjectLiteral>(entity: EntityTarget<Entity>): Promise<Repository<Entity>> {\n const source = this.getSource();\n\n if (!source.isInitialized) {\n await source.initialize();\n }\n\n return source.getRepository(entity);\n }\n\n public async close(): Promise<void> {\n const source = this.getSource();\n if (source.isInitialized) {\n await source.destroy();\n }\n }\n\n public async drop(): Promise<void> {\n const source = this.getSource();\n if (source.isInitialized) {\n await source.dropDatabase();\n }\n }\n\n public getEntityManager(): EntityManager {\n return this.getSource().manager;\n }\n}\n",
|
|
10
10
|
"import { DataSource } from \"typeorm\";\nimport type { SqliteConnectionOptions } from \"typeorm/driver/sqlite/SqliteConnectionOptions.js\";\nimport { AbstractTypeormSqliteDatabase } from \"./AbstractTypeormSqliteDatabase\";\nimport { DatabaseException } from \"./DatabaseException\";\n\nexport class TypeormSqliteDatabase extends AbstractTypeormSqliteDatabase {\n constructor(private readonly options: Omit<SqliteConnectionOptions, \"type\"> | undefined = undefined) {\n super();\n }\n\n public getSource(database?: string): DataSource {\n if (!database && this.source) {\n return this.source;\n }\n\n database = database || this.options?.database || Bun.env.SQLITE_DATABASE_PATH || \"\";\n\n if (!database) {\n throw new DatabaseException(\n \"No database path provided. The 'database' option must be specified with a valid file path or ':memory:' for in-memory database. Alternatively, set the SQLITE_DATABASE_PATH environment variable.\",\n {\n ...(this.options || {}),\n database,\n },\n );\n }\n\n this.source = new DataSource({\n synchronize: false,\n entities: [],\n enableWAL: true,\n busyErrorRetry: 2000,\n busyTimeout: 30_000,\n ...(this.options || {}),\n database,\n type: \"sqlite\",\n });\n\n return this.source;\n }\n}\n"
|
|
11
11
|
],
|
|
12
|
-
"mappings": "
|
|
13
|
-
"debugId": "
|
|
12
|
+
"mappings": ";;AAGO,MAAe,8BAA0D;AAAA,EACpE;AAAA,OAGG,KAAkC,CAC7C,QACA,UAC6B;AAAA,IAC7B,MAAM,SAAS,KAAK,UAAU,QAAQ;AAAA,IAEtC,IAAI,CAAC,OAAO,eAAe;AAAA,MACzB,MAAM,OAAO,WAAW;AAAA,IAC1B;AAAA,IAEA,OAAO,OAAO,cAAc,MAAM;AAAA;AAAA,OAGvB,MAAK,CAAC,UAAkC;AAAA,IACnD,MAAM,SAAS,KAAK,UAAU,QAAQ;AAAA,IACtC,IAAI,OAAO,eAAe;AAAA,MACxB,MAAM,OAAO,QAAQ;AAAA,IACvB;AAAA;AAAA,OAGW,KAAI,CAAC,UAAkC;AAAA,IAClD,MAAM,SAAS,KAAK,UAAU,QAAQ;AAAA,IACtC,IAAI,OAAO,eAAe;AAAA,MACxB,MAAM,OAAO,aAAa;AAAA,IAC5B;AAAA;AAAA,EAGK,gBAA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