-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFuseDB.ts
221 lines (197 loc) · 5.88 KB
/
FuseDB.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
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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import * as os from "os";
import * as path from "path";
import { ActiveQuery } from "./ActiveQuery";
import { Model } from "./Model";
import { Schema } from "./Schema";
import { SubQuery } from "./SubQuery";
import { fastHash } from "./Utils";
import { IAdapter, IInsertManyResponse } from "./adapters/Adapter";
import { FileAdapter } from "./adapters/FileAdapter";
import { MongoAdapter } from "./adapters/MongoAdapter";
export interface Options {
adapter?: IAdapter;
}
export class FuseDB {
public schema: Schema;
public serializer: string;
constructor(public model: Model<any>) {
this.schema = model["$schema"];
}
public static setup(opts: Options) {
Config.adapter = opts.adapter;
Config.adapter.close();
}
public static async checkConnection() {
if (Config.adapter) {
return Config.adapter.checkConnection();
}
}
public getAdapter(): IAdapter {
return Config.adapter;
}
public async saveRecord(): Promise<any> {
let extraValidationRequired = false;
this.schema.validate(this.model);
if (typeof this.model["onBeforeSave"] === "function") {
extraValidationRequired = true;
await this.model["onBeforeSave"]();
}
if (this.model["_id"] !== undefined) {
if (typeof this.model["onBeforeUpdate"] === "function") {
extraValidationRequired = true;
await this.model["onBeforeUpdate"]();
}
const record = this.updateRecord(extraValidationRequired);
if (typeof this.model["onAfterSave"] === "function") {
await this.model["onAfterSave"]();
}
return record;
}
if (typeof this.model["onBeforeCreate"] === "function") {
extraValidationRequired = true;
await this.model["onBeforeCreate"]();
}
const record = await this.createRecord(extraValidationRequired);
if (typeof this.model["onAfterSave"] === "function") {
await this.model["onAfterSave"]();
}
return record;
}
public async updateRecord(extraValidationRequired: boolean): Promise<any> {
const adapter = this.getAdapter();
const response = await adapter.update(
this.schema.name,
this.model["_id"],
this.schema.toDatabase(this.model, extraValidationRequired)
);
return this.model;
}
public find(opts: any): ActiveQuery<any> {
return new ActiveQuery(opts, this) as any;
}
public async drop(): Promise<boolean> {
const adapter = this.getAdapter();
await adapter.drop(this.schema.name);
return true;
}
public async query(query: ActiveQuery<any>): Promise<any[]> {
const adapter = this.getAdapter();
const response = await adapter.fetch({
collection: this.schema.name,
query: query.getQuery(),
limit: query.getLimit(),
skip: query.getSkip(),
sort: query.getSort(),
});
const subQuery = new SubQuery(this, query);
await subQuery.map(response);
return this.schema.mapValues(response);
}
private async mapResponse(
query: ActiveQuery<any>,
response: { [key: string]: any }[]
) {
const withConditions = query.getWithConditions();
if (withConditions.size === 0) {
return;
}
}
public async count(query: ActiveQuery<any>): Promise<number> {
const adapter = this.getAdapter();
const response = await adapter.count(this.schema.name, query.getQuery());
return response;
}
public async delete(query: ActiveQuery<any>): Promise<number> {
const adapter = this.getAdapter();
const response = await adapter.count(this.schema.name, query.getQuery());
return response;
}
public async remove(): Promise<number> {
const adapter = this.getAdapter();
const response = await adapter.delete(this.schema.name, this.getId());
return response;
}
public async deleteMany(query: ActiveQuery<any>): Promise<number> {
const adapter = this.getAdapter();
const response = await adapter.deleteMany(
this.schema.name,
query.getQuery()
);
return response;
}
public async updateMany(
query: ActiveQuery<any>,
data: Record<string, any>
): Promise<number> {
const adapter = this.getAdapter();
const response = await adapter.updateMany(
this.schema.name,
query.getQuery(),
data
);
return response;
}
public async insertMany(
docs: Array<Record<string, any>>
): IInsertManyResponse {
const adapter = this.getAdapter();
const response = await adapter.insertMany(this.schema.name, docs);
return response;
}
public async createRecord(extraValidationRequired?: boolean): Promise<any> {
const adapter = this.getAdapter();
if (!this.model["_id"]) {
const newId = adapter.generateId();
if (newId) {
this.model["_id"] = newId;
}
}
const response = await adapter.create(
this.schema.name,
this.schema.toDatabase(this.model, extraValidationRequired)
);
this.assignProps(response);
return this.model;
}
public assignProps(props?: any) {
if (typeof props === "object") {
for (let key in props) {
let value = props[key];
if (this.schema.contains(key)) {
this.model[key] = value;
}
}
}
}
private getId() {
return this.model["_id"];
}
public toJSON() {
const collection = this.schema.getCollection();
const json = {};
for (const key in collection) {
const props = collection[key];
if (typeof props === "object") {
if (!props.hidden) {
const hasToJson = typeof props.toJSON === "function";
json[key] = hasToJson
? props.toJSON(this.model[key])
: this.model[key];
}
} else {
json[key] = this.model[key];
}
}
return json;
}
}
const MONGO_URL = process.env.MONGO_URL || `mongodb://localhost:27017/test_db`;
export let Config: Options = {
adapter: MongoAdapter({
dbName: "test_db",
options: {
useUnifiedTopology: true,
},
url: MONGO_URL,
}),
};