This repository has been archived by the owner on Jul 9, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.ts
196 lines (155 loc) · 6.29 KB
/
model.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
import { Class } from '@travetto/registry';
import { BindUtil, SchemaValidator, DEFAULT_VIEW } from '@travetto/schema';
import { QueryVerifierService } from './query';
import { Injectable } from '@travetto/di';
import { ModelOptions } from './types';
import { ModelCore, Query, BulkState, ModelQuery, PageableModelQuery } from '../model';
import { ModelSource } from './source';
import { ModelRegistry } from './registry';
import { AppEnv, deepAssign } from '@travetto/base';
function getClass<T>(o: T) {
return o.constructor as Class<T>;
}
@Injectable({ target: ModelService })
export class ModelService extends ModelSource {
constructor(private source: ModelSource, private queryService: QueryVerifierService) {
super();
}
postConstruct() {
// Cannot block on registry since this is an injectable (circular dep)
// Call but ignore return
this.init();
}
async init() {
await ModelRegistry.init();
if (AppEnv.watch) {
if (this.source.onSchemaChange) {
ModelRegistry.onSchemaChange(this.source.onSchemaChange.bind(this.source));
}
if (this.source.onChange) {
ModelRegistry.on(this.source.onChange.bind(this.source));
}
}
}
convert<T extends ModelCore>(cls: Class<T>, o: T) {
const config = ModelRegistry.get(cls);
let cons = cls;
if (config && config.subtypes && !!o.type) {
cons = config.subtypes[o.type];
}
if (o instanceof cons) {
return o;
} else {
return BindUtil.bindSchema(cons, new cons(), o);
}
}
async prePersist<T extends ModelCore>(cls: Class<T>, o: T): Promise<T>;
async prePersist<T extends ModelCore>(cls: Class<T>, o: Partial<T>, view: string): Promise<Partial<T>>;
async prePersist<T extends ModelCore>(cls: Class<T>, o: Partial<T> | T, view: string = DEFAULT_VIEW) {
if (o.prePersist) {
o.prePersist();
}
let res = await SchemaValidator.validate(o, view);
res = await this.source.prePersist(cls, res);
return res as T;
}
postLoad<T extends ModelCore>(cls: Class<T>, o: T): T;
postLoad<T extends ModelCore>(cls: Class<T>, o: Partial<T>, view: string): Partial<T>;
postLoad<T extends ModelCore>(cls: Class<T>, o: T) {
o = this.source.postLoad(cls, o) as T;
o = this.convert(cls, o);
if (o.postLoad) {
o.postLoad();
}
return o;
}
async query<T extends ModelCore, U = T>(cls: Class<T>, query: Query<T>) {
this.queryService.verify(cls, query);
const res = await this.source.query<T, U>(cls, query);
return res.map(o => this.postLoad(cls, o as any as T));
}
async getAllByQuery<T extends ModelCore>(cls: Class<T>, query: PageableModelQuery<T> = {}) {
this.queryService.verify(cls, query);
const config = ModelRegistry.get(cls) as ModelOptions<T>;
if (!query.sort && config.defaultSort) {
query.sort = config.defaultSort;
}
const res = await this.source.getAllByQuery(cls, query);
return res.map(o => this.postLoad(cls, o));
}
getCountByQuery<T extends ModelCore>(cls: Class<T>, query: ModelQuery<T> = {}) {
this.queryService.verify(cls, query);
return this.source.getCountByQuery(cls, query);
}
async getByQuery<T extends ModelCore>(cls: Class<T>, query: PageableModelQuery<T> = {}, failOnMany: boolean = true) {
this.queryService.verify(cls, query);
const res = await this.source.getByQuery(cls, query, failOnMany);
return this.postLoad(cls, res);
}
async saveOrUpdate<T extends ModelCore>(cls: Class<T>, o: T, query: ModelQuery<T>) {
this.queryService.verify(cls, query);
const res = await this.getAllByQuery(getClass(o), { ...query, limit: 2 });
if (res.length === 1) {
o = deepAssign(res[0], o);
return await this.update(cls, o);
} else if (res.length === 0) {
return await this.save(cls, o);
}
throw new Error(`Too many already exist: ${res.length}`);
}
async getById<T extends ModelCore>(cls: Class<T>, id: string) {
const res = await this.source.getById(cls, id);
return this.postLoad(cls, res);
}
deleteById<T extends ModelCore>(cls: Class<T>, id: string) {
return this.source.deleteById(cls, id);
}
deleteByQuery<T extends ModelCore>(cls: Class<T>, query: ModelQuery<T> = {}) {
this.queryService.verify(cls, query);
return this.source.deleteByQuery(cls, query);
}
async save<T extends ModelCore>(cls: Class<T>, o: T) {
o = await this.prePersist(cls, o);
const res = await this.source.save(cls, o);
return this.postLoad(cls, res);
}
async saveAll<T extends ModelCore>(cls: Class<T>, objs: T[]) {
objs = await Promise.all(objs.map(o => this.prePersist(cls, o)));
const res = await this.source.saveAll(cls, objs);
return res.map(x => this.postLoad(cls, x));
}
async update<T extends ModelCore>(cls: Class<T>, o: T) {
o = await this.prePersist(cls, o);
const res = await this.source.update(cls, o);
return this.postLoad(cls, res);
}
updateAllByQuery<T extends ModelCore>(cls: Class<T>, query: ModelQuery<T>, data: Partial<T>) {
this.queryService.verify(cls, query);
return this.source.updateAllByQuery(cls, query, data);
}
updatePartial<T extends ModelCore>(cls: Class<T>, model: Partial<T>) {
return this.source.updatePartial(cls, model);
}
async updatePartialByQuery<T extends ModelCore>(cls: Class<T>, query: ModelQuery<T>, body: Partial<T>) {
this.queryService.verify(cls, query);
// Do not do pre-persist, because we don't know what we would be validating
const res = await this.source.updatePartialByQuery(cls, query, body);
return this.postLoad(cls, res);
}
async updatePartialView<T extends ModelCore>(cls: Class<T>, o: Partial<T>, view: string) {
o = await this.prePersist(cls, o, view);
const partial = BindUtil.bindSchema(cls, {}, o, view);
const res = await this.updatePartial(cls, partial);
return this.postLoad(cls, res);
}
async updatePartialViewByQuery<T extends ModelCore>(cls: Class<T>, o: Partial<T>, view: string, query: ModelQuery<T>) {
this.queryService.verify(cls, query);
o = await this.prePersist(cls, o, view);
const partial = BindUtil.bindSchema(cls, {}, o, view);
const res = await this.updatePartialByQuery(cls, query, partial);
return res;
}
bulkProcess<T extends ModelCore>(cls: Class<T>, state: BulkState<T>) {
return this.source.bulkProcess(cls, state);
}
}