MCPcopy Create free account
hub / github.com/loopbackio/loopback-next / toObject

Method toObject

packages/repository/src/model.ts:381–420  ·  view source on GitHub ↗

* Convert to a plain object as DTO * * If `ignoreUnknownProperty` is set to false, convert all properties in the * model instance, otherwise only convert the ones defined in the model * definitions. * * See function `asObject` for each property's conversion rules.

(options?: Options)

Source from the content-addressed store, hash-verified

379 * See function `asObject` for each property's conversion rules.
380 */
381 toObject(options?: Options): Object {
382 const def = (this.constructor as typeof Model).definition;
383 const obj: AnyObject = {};
384
385 if (options?.ignoreUnknownProperties === false) {
386 const hiddenProperties: string[] = def?.settings.hiddenProperties || [];
387 for (const p in this) {
388 if (!hiddenProperties.includes(p)) {
389 const val = (this as AnyObject)[p];
390 obj[p] = asObject(val, options);
391 }
392 }
393 return obj;
394 }
395
396 if (def?.relations) {
397 for (const r in def.relations) {
398 const relName = def.relations[r].name;
399 if (relName in this) {
400 obj[relName] = asObject((this as AnyObject)[relName], {
401 ...options,
402 ignoreUnknownProperties: false,
403 });
404 }
405 }
406 }
407
408 const props = def.properties;
409 const keys = Object.keys(props);
410
411 for (const i in keys) {
412 const propertyName = keys[i];
413 const val = (this as AnyObject)[propertyName];
414
415 if (val === undefined) continue;
416 obj[propertyName] = asObject(val, options);
417 }
418
419 return obj;
420 }
421
422 constructor(data?: DataObject<Model>) {
423 Object.assign(this, data);

Callers 13

toJSONMethod · 0.95
asObjectFunction · 0.45
toEntityMethod · 0.45
saveMethod · 0.45
toEntityMethod · 0.45
model.unit.tsFile · 0.45
createRetrieveSuiteFunction · 0.45
freeformPropertiesSuiteFunction · 0.45
transactionSuiteFunction · 0.45

Calls 2

asObjectFunction · 0.85
keysMethod · 0.65

Tested by

no test coverage detected