-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
96 lines (76 loc) · 2.79 KB
/
index.js
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
import { Model, ModelItem } from "./src/model.js";
import { SQLBuilder } from "./src/sql.js";
import {
DBDriver,
MySQLDBDriver,
RawFunctionDBDriver,
SQLiteDBDriver,
} from "./src/multisupport.js";
export class NodeORM {
static dbdrivers = [MySQLDBDriver, SQLiteDBDriver, RawFunctionDBDriver];
static defaultDB = undefined; // Will fallback to env reading
static envObject = process.env;
static async extractFromEnv() {
if (
typeof this.envObject["DB_CONNECTION"] !== "string" ||
this.envObject["DB_CONNECTION"].length == 0
) {
throw new Error(
"When using environment. no 'DB_CONNECTION' env variable or it isn't a string."
);
}
for (const dbdriver of this.dbdrivers) {
try {
if (!(dbdriver.prototype instanceof DBDriver)) continue;
const data = await dbdriver.extractFromEnv(this.envObject); // It should return an array, because that array is going to be 'extracted' into dbdriver.make(...here) directly.
if (!Array.isArray(data)) continue;
return await dbdriver.make(...data);
} catch (error) {}
}
throw new Error(
`Unsupported database type: ${this.envObject["DB_CONNECTION"]}`
);
}
static async determine(dbinstance) {
dbinstance = dbinstance || this.defaultDB;
// If the given dbinstance is a promise, resolve it
if (dbinstance instanceof Promise) dbinstance = await dbinstance;
// Return the given DBDriver
if (dbinstance instanceof DBDriver) return dbinstance;
// Search for a DBDriver that can handle the given.
for (const dbdriver of this.dbdrivers) {
const made = await dbdriver.make(dbinstance);
if (made instanceof DBDriver) {
if (!dbinstance && !this.defaultDB) this.defaultDB = made; // Prevent creating other connections using multiple initialize().
return made;
}
}
// None of the above, fallback to env reading...
return await this.extractFromEnv();
}
}
export async function initialize(connection, ...models) {
if (connection instanceof Promise) connection = await connection;
if (connection.prototype instanceof Model) {
// All of arguments are models?
models.push(connection);
connection = undefined;
}
const conn = await NodeORM.determine(connection);
for (const model of models) {
if (!(model.prototype instanceof Model)) {
continue;
}
await model.init(conn);
}
return conn;
}
export {
MySQLDBDriver,
SQLiteDBDriver,
RawFunctionDBDriver,
DBDriver,
Model,
ModelItem,
SQLBuilder,
};