1
|
|
|
import Database from "./Database.js"; |
2
|
|
|
import Table from "./Table.js"; |
3
|
|
|
import Connection from "./Connection"; |
4
|
|
|
import Index from "./Table/Index"; |
5
|
|
|
import {ConnectionInterface, DatabaseInterface, StoreInterface} from "../JeloquentInterfaces"; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* |
9
|
|
|
*/ |
10
|
|
|
class Store implements StoreInterface { |
11
|
|
|
|
12
|
|
|
classInstances: object; |
13
|
|
|
|
14
|
|
|
numberOfModelCreated: number; |
15
|
|
|
|
16
|
|
|
private connections: Map<string, ConnectionInterface>; |
17
|
|
|
|
18
|
|
|
private databases: Map<string, DatabaseInterface>; |
19
|
|
|
|
20
|
|
|
private useConnectionName: string; |
21
|
|
|
|
22
|
|
|
private useDatabase: string; |
23
|
|
|
|
24
|
|
|
constructor() { |
25
|
|
|
this.classInstances = {}; |
26
|
|
|
this.databases = new Map(); |
27
|
|
|
this.connections = new Map(); |
28
|
|
|
this.numberOfModelCreated = 0; |
29
|
|
|
this.useDatabase = 'default'; |
30
|
|
|
this.useConnectionName = 'default'; |
31
|
|
|
globalThis.Store = this; |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
add(database: DatabaseInterface): void { |
35
|
|
|
this.databases.set(database.name, database); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
addConnection(connection: ConnectionInterface, name = 'default'): void { |
39
|
|
|
this.connections.set(name, connection); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
connection(): ConnectionInterface|null { |
43
|
|
|
return this.connections.get(this.useConnectionName) ?? null; |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
database(): DatabaseInterface|null { |
47
|
|
|
return this.databases.get(this.useDatabase) ?? null; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
use(storeName = 'default'): void { |
51
|
|
|
this.useDatabase = storeName; |
52
|
|
|
this.databases.get(this.useDatabase)?.setIndexes(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
useConnection(name = 'default'): void { |
56
|
|
|
this.useConnectionName = name; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @deprecated |
61
|
|
|
*/ |
62
|
|
|
useConnections(name:string): void { |
63
|
|
|
this.useConnection(name); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
|
68
|
|
|
export { |
69
|
|
|
Store, |
70
|
|
|
Database, |
71
|
|
|
Table, |
72
|
|
|
Connection, |
73
|
|
|
Index, |
74
|
|
|
}; |