-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDbProcessor.ts
163 lines (125 loc) · 5.57 KB
/
DbProcessor.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
import * as Util from "util";
import * as Url from "url";
import * as FS from "fs";
import * as Path from "path";
import * as mkdirp from "mkdirp";
import * as QueryString from "querystring";
import * as Uuid from "uuid";
import {IDbProcessor} from "./IDbProcessor";
import {DbCommand} from "./DbCommand";
import {Config} from "./Config";
import {Search} from "./Search";
import {PersistenceError} from "./PersistenceError";
export class DbProcessor implements IDbProcessor {
private config: Config;
constructor(config: Config) {
this.config = config;
}
public getSingle(dbCommand: DbCommand, callback: (data: string, err?: string) => any): void {
FS.exists(dbCommand.getEntityPath(), (exists: boolean) => {
if (!exists) return callback(null);
FS.readFile(dbCommand.getEntityPath(), (err: any, entityData: Buffer) => {
if (err) return callback(null, err);
callback(entityData.toString());
});
});
}
public getMany(dbCommand: DbCommand, search: Search, callback: (data: Array<string>, dataErrors: Array<PersistenceError>, err?: any) => any): void {
var errors = new Array<PersistenceError>();
FS.exists(dbCommand.getEntityRootPath(), (exists: boolean) => {
if (!exists) return callback(null, errors);
FS.readdir(dbCommand.getEntityRootPath(), (err: any, filenames: Array<string>) => {
if (err) return callback(null, errors, err);
var results = new Array<string>();
var readFile = (index: number = 0) => {
var filename = filenames[index];
try {
var relativeFilePath = Path.join(dbCommand.getEntityRootPath(), filename);
FS.readFile(relativeFilePath, (err: any, buffer: Buffer) => {
if (err) throw Error(err);
var entityData = buffer.toString();
if (search == null || search.test(JSON.parse(entityData)))
results.push(entityData);
if (index < (filenames.length - 1))
readFile(++index);
else
callback(results, errors);
});
}
catch (err) {
var id = (new RegExp("(.+)\.[^\.]+$").exec(filename))[1];
errors.push(new PersistenceError(id, err.toString()));
}
};
readFile();
});
});
}
public saveSingle(dbCommand: DbCommand, entity: any, callback: (data: string, err?: any) => any): void {
dbCommand.entityId = (dbCommand.entityId || entity.id || Uuid.v1());
entity.id = dbCommand.entityId;
mkdirp(dbCommand.getEntityRootPath(), (err: any) => {
if (err) return callback(null, err);
var entityData = JSON.stringify(entity);
FS.writeFile(dbCommand.getEntityPath(), entityData, (err: any) => {
if (err)
throw Error(err);
callback(entityData);
});
});
}
public saveMany(dbCommand: DbCommand, entities: Array<any>, callback: (data: Array<string>, errors: Array<PersistenceError>) => any): void {
var count = entities.length;
var createdEntities = new Array<any>();
var errors = new Array<PersistenceError>();
if (count == 0) return callback(createdEntities, errors);
var save = (index: number = 0) => {
var entity = entities[index];
try {
this.saveSingle(dbCommand.forEntityId(entity.id), entity, (createdEntityData: string) => {
createdEntities.push(createdEntityData);
if (index < (count - 1))
save(index + 1);
else
callback(createdEntities, errors);
});
}
catch (err) {
errors.push(new PersistenceError(entity.id, err));
}
};
save();
}
public deleteSingle(dbCommand: DbCommand, callback: (err?: string) => any): void {
var entityPath = dbCommand.getEntityPath();
FS.exists(entityPath, (exists: boolean) => {
if (!exists) return callback();
FS.unlink(entityPath, (err: any) => {
if (err) return callback(err);
callback();
});
});
}
public deleteMany(dbCommand: DbCommand, ids: Array<string>, callback: (deletedIds: Array<string>, errors: Array<PersistenceError>) => any): void {
var count = ids.length;
var deletedIds = new Array<any>();
var errors = new Array<PersistenceError>();
if (count == 0) return callback(deletedIds, errors);
var save = (index: number = 0) => {
var id = ids[index];
try {
this.deleteSingle(dbCommand.forEntityId(id), () => {
deletedIds.push(id);
if (index < (count - 1))
save(index + 1);
else
callback(deletedIds, errors);
});
}
catch (err) {
errors.push(new PersistenceError(id, err));
}
};
save();
}
}