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

Class Entity

packages/repository/src/model.ts:440–512  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

438 * Base class for entities which have unique ids
439 */
440export class Entity extends Model implements Persistable {
441 /**
442 * Get the names of identity properties (primary keys).
443 */
444 static getIdProperties(): string[] {
445 return this.definition.idProperties();
446 }
447
448 /**
449 * Get the identity value for a given entity instance or entity data object.
450 *
451 * @param entityOrData - The data object for which to determine the identity
452 * value.
453 */
454 static getIdOf(entityOrData: AnyObject): any {
455 if (typeof entityOrData.getId === 'function') {
456 return entityOrData.getId();
457 }
458
459 const idName = this.getIdProperties()[0];
460 return entityOrData[idName];
461 }
462
463 /**
464 * Get the identity value. If the identity is a composite key, returns
465 * an object.
466 */
467 getId(): any {
468 const definition = (this.constructor as typeof Entity).definition;
469 const idProps = definition.idProperties();
470 if (idProps.length === 1) {
471 return (this as AnyObject)[idProps[0]];
472 }
473 if (!idProps.length) {
474 throw new Error(
475 `Invalid Entity ${this.constructor.name}:` +
476 'missing primary key (id) property',
477 );
478 }
479 return this.getIdObject();
480 }
481
482 /**
483 * Get the identity as an object, such as `{id: 1}` or
484 * `{schoolId: 1, studentId: 2}`
485 */
486 getIdObject(): Object {
487 const definition = (this.constructor as typeof Entity).definition;
488 const idProps = definition.idProperties();
489 const idObj = {} as any;
490 for (const idProp of idProps) {
491 idObj[idProp] = (this as AnyObject)[idProp];
492 }
493 return idObj;
494 }
495
496 /**
497 * Build the where object for the given id

Callers 1

book.entity.tsFile · 0.85

Implementers 1

SequelizeModelextensions/sequelize/src/sequelize/seq

Calls

no outgoing calls

Tested by

no test coverage detected