/
Database.ts
119 lines (100 loc) · 3.27 KB
/
Database.ts
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
import md5 from 'md5'
import { StrictEventEmitter } from 'strict-event-emitter'
import {
InternalEntity,
InternalEntityProperty,
ModelDictionary,
PrimaryKeyType,
} from '../glossary'
type Models<Dictionary extends ModelDictionary> = Record<
string,
Map<PrimaryKeyType, InternalEntity<Dictionary, any>>
>
export type DatabaseMethodToEventFn<Method extends (...args: any[]) => any> = (
id: string,
...args: Parameters<Method>
) => void
export interface DatabaseEventsMap {
create: DatabaseMethodToEventFn<Database<any>['create']>
update: DatabaseMethodToEventFn<Database<any>['update']>
delete: DatabaseMethodToEventFn<Database<any>['delete']>
}
let callOrder = 0
export class Database<Dictionary extends ModelDictionary> {
public id: string
public events: StrictEventEmitter<DatabaseEventsMap>
private models: Models<Dictionary>
constructor(dictionary: Dictionary) {
this.events = new StrictEventEmitter()
this.models = Object.keys(dictionary).reduce<Models<Dictionary>>(
(acc, modelName) => {
acc[modelName] = new Map<string, InternalEntity<Dictionary, string>>()
return acc
},
{},
)
callOrder++
this.id = this.generateId()
}
/**
* Generates a unique MD5 hash based on the database
* module location and invocation order. Used to reproducibly
* identify a database instance among sibling instances.
*/
private generateId() {
const { stack } = new Error()
const callFrame = stack?.split('\n')[4]
const salt = `${callOrder}-${callFrame?.trim()}`
return md5(salt)
}
getModel<ModelName extends string>(name: ModelName) {
return this.models[name]
}
create<ModelName extends string>(
modelName: ModelName,
entity: InternalEntity<Dictionary, any>,
customPrimaryKey?: PrimaryKeyType,
) {
const primaryKey =
customPrimaryKey ||
(entity[entity[InternalEntityProperty.primaryKey]] as string)
this.events.emit('create', this.id, modelName, entity, customPrimaryKey)
return this.getModel(modelName).set(primaryKey, entity)
}
update<ModelName extends string>(
modelName: ModelName,
prevEntity: InternalEntity<Dictionary, any>,
nextEntity: InternalEntity<Dictionary, any>,
) {
const prevPrimaryKey =
prevEntity[prevEntity[InternalEntityProperty.primaryKey]]
const nextPrimaryKey =
nextEntity[prevEntity[InternalEntityProperty.primaryKey]]
if (nextPrimaryKey !== prevPrimaryKey) {
this.delete(modelName, prevPrimaryKey as string)
}
this.create(modelName, nextEntity, nextPrimaryKey as string)
this.events.emit('update', this.id, modelName, prevEntity, nextEntity)
}
has<ModelName extends string>(
modelName: ModelName,
primaryKey: PrimaryKeyType,
) {
return this.getModel(modelName).has(primaryKey)
}
count<ModelName extends string>(modelName: ModelName) {
return this.getModel(modelName).size
}
delete<ModelName extends string>(
modelName: ModelName,
primaryKey: PrimaryKeyType,
) {
this.getModel(modelName).delete(primaryKey)
this.events.emit('delete', this.id, modelName, primaryKey)
}
listEntities<ModelName extends string>(
modelName: ModelName,
): InternalEntity<Dictionary, ModelName>[] {
return Array.from(this.getModel(modelName).values())
}
}