-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
legacy-juggler-bridge.ts
237 lines (206 loc) · 7.15 KB
/
legacy-juggler-bridge.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
// Copyright IBM Corp. 2017,2018. All Rights Reserved.
// Node module: @loopback/repository
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
import * as legacy from 'loopback-datasource-juggler';
import * as assert from 'assert';
import {isPromiseLike} from '@loopback/context';
import {
DataObject,
Options,
AnyObject,
Command,
NamedParameters,
PositionalParameters,
} from '../common-types';
import {Entity, ModelDefinition} from '../model';
import {Filter, Where} from '../query';
import {EntityCrudRepository} from './repository';
export namespace juggler {
export import DataSource = legacy.DataSource;
export import ModelBase = legacy.ModelBase;
export import ModelBaseClass = legacy.ModelBaseClass;
export import PersistedModel = legacy.PersistedModel;
export import PersistedModelClass = legacy.PersistedModelClass;
}
/**
* This is a bridge to the legacy DAO class. The function mixes DAO methods
* into a model class and attach it to a given data source
* @param modelClass {} Model class
* @param ds {DataSource} Data source
* @returns {} The new model class with DAO (CRUD) operations
*/
export function bindModel<T extends juggler.ModelBaseClass>(
modelClass: T,
ds: juggler.DataSource,
): T {
const boundModelClass = class extends modelClass {};
boundModelClass.attachTo(ds);
return boundModelClass;
}
/**
* Ensure the value is a promise
* @param p Promise or void
*/
/* tslint:disable-next-line:no-any */
function ensurePromise<T>(p: legacy.PromiseOrVoid<T>): Promise<T> {
if (p && isPromiseLike(p)) {
// Juggler uses promise-like Bluebird instead of native Promise
// implementation. We need to convert the promise returned by juggler
// methods to proper native Promise instance.
return Promise.resolve(p);
} else {
return Promise.reject(new Error('The value should be a Promise: ' + p));
}
}
/**
* Default implementation of CRUD repository using legacy juggler model
* and data source
*/
export class DefaultCrudRepository<T extends Entity, ID>
implements EntityCrudRepository<T, ID> {
modelClass: juggler.PersistedModelClass;
/**
* Constructor of DefaultCrudRepository
* @param modelClass Legacy model class
* @param dataSource Legacy data source
*/
constructor(
// entityClass should have type "typeof T", but that's not supported by TSC
public entityClass: typeof Entity & {prototype: T},
public dataSource: juggler.DataSource,
) {
const definition = entityClass.definition;
assert(
!!definition,
`Entity ${entityClass.name} must have valid model definition.`,
);
assert(
definition.idProperties().length > 0,
`Entity ${entityClass.name} must have at least one id/pk property.`,
);
this.setupPersistedModel(definition);
}
// Create an internal legacy Model attached to the datasource
private setupPersistedModel(definition: ModelDefinition) {
const dataSource = this.dataSource;
const model = dataSource.getModel(definition.name);
if (model) {
// The backing persisted model has been already defined.
this.modelClass = model as typeof juggler.PersistedModel;
return;
}
// We need to convert property definitions from PropertyDefinition
// to plain data object because of a juggler limitation
const properties: {[name: string]: object} = {};
for (const p in definition.properties) {
properties[p] = Object.assign({}, definition.properties[p]);
}
this.modelClass = dataSource.createModel<juggler.PersistedModelClass>(
definition.name,
properties,
definition.settings,
);
this.modelClass.attachTo(dataSource);
}
async create(entity: Partial<T>, options?: Options): Promise<T> {
const model = await ensurePromise(this.modelClass.create(entity, options));
return this.toEntity(model);
}
async createAll(entities: Partial<T>[], options?: Options): Promise<T[]> {
const models = await ensurePromise(
this.modelClass.create(entities, options),
);
return this.toEntities(models as DataObject<T>[]);
}
save(entity: T, options?: Options): Promise<T | null> {
const idName = this.modelClass.definition.idName();
let id;
if (typeof entity.getId === 'function') {
id = entity.getId();
} else {
id = entity[idName];
}
if (id == null) {
return this.create(entity, options);
} else {
return this.replaceById(id, entity, options).then(
result => (result ? this.toEntity(entity) : null),
);
}
}
async find(filter?: Filter, options?: Options): Promise<T[]> {
const models = await ensurePromise(this.modelClass.find(filter, options));
return this.toEntities(models);
}
async findOne(filter?: Filter, options?: Options): Promise<T> {
const model = await ensurePromise(this.modelClass.findOne(filter, options));
return this.toEntity(model);
}
async findById(id: ID, filter?: Filter, options?: Options): Promise<T> {
const model = await ensurePromise(
this.modelClass.findById(id, filter, options),
);
if (!model) {
throw new Error(`no ${this.modelClass.name} found with id "${id}"`);
}
return this.toEntity(model);
}
update(entity: T, options?: Options): Promise<boolean> {
return this.updateById(entity.getId(), entity, options);
}
delete(entity: T, options?: Options): Promise<boolean> {
return this.deleteById(entity.getId(), options);
}
updateAll(
data: Partial<T>,
where?: Where,
options?: Options,
): Promise<number> {
return ensurePromise(this.modelClass.updateAll(where, data, options)).then(
result => result.count,
);
}
updateById(id: ID, data: Partial<T>, options?: Options): Promise<boolean> {
const idProp = this.modelClass.definition.idName();
const where = {} as Where;
where[idProp] = id;
return this.updateAll(data, where, options).then(count => count > 0);
}
replaceById(id: ID, data: Partial<T>, options?: Options): Promise<boolean> {
return ensurePromise(this.modelClass.replaceById(id, data, options)).then(
result => !!result,
);
}
deleteAll(where?: Where, options?: Options): Promise<number> {
return ensurePromise(this.modelClass.deleteAll(where, options)).then(
result => result.count,
);
}
deleteById(id: ID, options?: Options): Promise<boolean> {
return ensurePromise(this.modelClass.deleteById(id, options)).then(
result => result.count > 0,
);
}
count(where?: Where, options?: Options): Promise<number> {
return ensurePromise(this.modelClass.count(where, options));
}
exists(id: ID, options?: Options): Promise<boolean> {
return ensurePromise(this.modelClass.exists(id, options));
}
async execute(
command: Command,
// tslint:disable:no-any
parameters: NamedParameters | PositionalParameters,
options?: Options,
): Promise<AnyObject> {
/* istanbul ignore next */
throw new Error('Not implemented');
}
protected toEntity(model: DataObject<T>): T {
return new this.entityClass(model.toObject()) as T;
}
protected toEntities(models: DataObject<T>[]): T[] {
return models.map(m => this.toEntity(m));
}
}