How to use @loopback/repository - 10 common examples

To help you get started, we’ve selected a few @loopback/repository examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github strongloop / loopback-next / packages / repository-json-schema / src / __tests__ / integration / build-schema.integration.ts View on Github external
it('uses title from model metadata instead of model name', () => {
      @model({title: 'MyCustomer'})
      class Customer {}

      const schema = modelToJsonSchema(Customer, {
        // trigger build of a custom title
        partial: true,
      });

      expect(schema.title).to.equal('MyCustomerPartial');
    });
github strongloop / loopback-next / packages / boot / src / __tests__ / unit / bootstrapper.unit.ts View on Github external
describe('boot-strapper unit tests', () => {
  // RepositoryMixin is added to avoid warning message printed logged to console
  // due to the fact that RepositoryBooter is a default Booter loaded via BootMixin.
  class BootApp extends BootMixin(RepositoryMixin(Application)) {}

  let app: BootApp;
  let bootstrapper: Bootstrapper;
  const booterKey = `${BootBindings.BOOTER_PREFIX}.TestBooter`;
  const anotherBooterKey = `${BootBindings.BOOTER_PREFIX}.AnotherBooter`;

  beforeEach(getApplication);
  beforeEach(getBootStrapper);

  it('finds and runs registered booters', async () => {
    const ctx = await bootstrapper.boot();
    const booterInst = await ctx.get(booterKey);
    expect(booterInst.phasesCalled).to.eql([
      'TestBooter:configure',
      'TestBooter:load',
    ]);
github strongloop / loopback-next / packages / repository-tests / src / crud / transactions.suite.ts View on Github external
it('should not use transaction with another repository', async () => {
          const ds2Options = Object.assign({}, dataSourceOptions);
          ds2Options.name = 'anotherDataSource';
          ds2Options.database = ds2Options.database + '_new';
          const ds2 = new juggler.DataSource(ds2Options);
          const anotherRepo = new repositoryClass(Product, ds2);
          await ds2.automigrate(Product.name);

          tx = await repo.beginTransaction(IsolationLevel.READ_COMMITTED);

          // we should reject this call with a clear error message
          // stating that transaction doesn't belong to the repository
          // and that only local transactions are supported
          // expect(
          //   await anotherRepo.create({name: 'Pencil'}, {transaction: tx}),
          // ).to.throw(/some error/);
          // see https://github.com/strongloop/loopback-next/issues/3483
          const created = await anotherRepo.create(
            {name: 'Pencil'},
            {transaction: tx},
          );
github strongloop / loopback-next / packages / repository-tests / src / crud / transactions.suite.ts View on Github external
it('should not use transaction with another repository', async () => {
          const ds2Options = Object.assign({}, dataSourceOptions);
          ds2Options.name = 'anotherDataSource';
          ds2Options.database = ds2Options.database + '_new';
          const ds2 = new juggler.DataSource(ds2Options);
          const anotherRepo = new repositoryClass(Product, ds2);
          await ds2.automigrate(Product.name);

          tx = await repo.beginTransaction(IsolationLevel.READ_COMMITTED);

          // we should reject this call with a clear error message
          // stating that transaction doesn't belong to the repository
          // and that only local transactions are supported
          // expect(
          //   await anotherRepo.create({name: 'Pencil'}, {transaction: tx}),
          // ).to.throw(/some error/);
          // see https://github.com/strongloop/loopback-next/issues/3483
          const created = await anotherRepo.create(
            {name: 'Pencil'},
            {transaction: tx},
          );
          expect(created.toObject()).to.have.properties('id', 'name');
          expect(created.id).to.be.ok();

          // for now, LB ignores the transaction so the instance
github strongloop / loopback-next / packages / rest / src / __tests__ / acceptance / validation / validation.acceptance.ts View on Github external
describe('Validation at REST level', () => {
  let app: RestApplication;
  let client: Client;

  @model()
  class Product {
    @property()
    id: number;

    @property({required: true})
    name: string;

    // NOTE(rfeng): We have to add `type: 'string'` to `@property` as
    // `string | null` removes TypeScript design:type reflection
    @property({required: false, type: 'string', jsonSchema: {nullable: true}})
    description?: string | null;

    @property({required: true, jsonSchema: {range: [0, 100]}})
    price: number;

    constructor(data: Partial) {
github strongloop / loopback-next / packages / boot / src / __tests__ / fixtures / application.ts View on Github external
// Node module: @loopback/boot
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT

import {ApplicationConfig} from '@loopback/core';
import {RepositoryMixin} from '@loopback/repository';
import {RestApplication} from '@loopback/rest';
import {ServiceMixin} from '@loopback/service-proxy';
import {BootMixin} from '../..';

// Force package.json to be copied to `dist` by `tsc`
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import * as pkg from './package.json';

export class BooterApp extends BootMixin(
  ServiceMixin(RepositoryMixin(RestApplication)),
) {
  constructor(options?: ApplicationConfig) {
    super(options);
    this.projectRoot = __dirname;
  }
}
github strongloop / loopback-next / packages / repository-tests / src / crud / relations / fixtures / repositories / address.repository.ts View on Github external
constructor(
      db: juggler.DataSource,
      customerRepositoryGetter: Getter,
    ) {
      super(Address, db);
      // create a belongsto relation from this public method
      const customerMeta = this.entityClass.definition.relations['customer'];
      this.customer = createBelongsToAccessor(
        customerMeta as BelongsToDefinition,
        customerRepositoryGetter,
        this,
      );
    }
  };
github strongloop / loopback-next / packages / repository-tests / src / crud / relations / fixtures / repositories / customer.repository.ts View on Github external
constructor(
      db: juggler.DataSource,
      orderRepositoryGetter: Getter,
      addressRepositoryGetter: Getter,
    ) {
      super(Customer, db);
      const ordersMeta = this.entityClass.definition.relations['orders'];
      // create a has-many relation through this public method
      this.orders = createHasManyRepositoryFactory(
        ordersMeta as HasManyDefinition,
        orderRepositoryGetter,
      );

      const addressMeta = this.entityClass.definition.relations['address'];
      this.address = createHasOneRepositoryFactory(
        addressMeta as HasOneDefinition,
        addressRepositoryGetter,
      );
      const customersMeta = this.entityClass.definition.relations['customers'];
      this.customers = createHasManyRepositoryFactory(
        customersMeta as HasManyDefinition,
        Getter.fromValue(this),
      );
      const parentMeta = this.entityClass.definition.relations['parent'];
      this.parent = createBelongsToAccessor(
github strongloop / loopback-next / packages / repository-tests / src / crud / relations / fixtures / repositories / customer.repository.ts View on Github external
) {
      super(Customer, db);
      const ordersMeta = this.entityClass.definition.relations['orders'];
      // create a has-many relation through this public method
      this.orders = createHasManyRepositoryFactory(
        ordersMeta as HasManyDefinition,
        orderRepositoryGetter,
      );

      const addressMeta = this.entityClass.definition.relations['address'];
      this.address = createHasOneRepositoryFactory(
        addressMeta as HasOneDefinition,
        addressRepositoryGetter,
      );
      const customersMeta = this.entityClass.definition.relations['customers'];
      this.customers = createHasManyRepositoryFactory(
        customersMeta as HasManyDefinition,
        Getter.fromValue(this),
      );
      const parentMeta = this.entityClass.definition.relations['parent'];
      this.parent = createBelongsToAccessor(
        parentMeta as BelongsToDefinition,
        Getter.fromValue(this),
        this,
      );
    }
  };
github strongloop / loopback-next / packages / repository-tests / src / crud / replace-by-id.suite.ts View on Github external
export function createSuiteForReplaceById(
  dataSourceOptions: DataSourceOptions,
  repositoryClass: CrudRepositoryCtor,
  features: CrudFeatures,
) {
  @model()
  class Product extends Entity {
    @property({
      type: features.idType,
      id: true,
      generated: true,
      description: 'The unique identifier for a product',
    })
    id: MixedIdType;

    // cloudant needs this property to do replacement method
    // see cloudant README file for more details
    @property({type: 'string', required: false})
    _rev: string;

    @property({type: 'string', required: true})
    name: string;

    @property({type: 'string', required: false})