How to use the @loopback/repository.juggler.DataSource function in @loopback/repository

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-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 / examples / todo / src / controllers / model-admin.controller.ts View on Github external
async function getDataSourceForConnectionString(
  app: TodoListApplication,
  connectionString: string,
) {
  // To keep our proof-of-concept simple, we create a new datasource instance
  // for each new discovery request.
  // In a real application, we should probably re-use datasource instances
  // sharing the same connection string.
  const dsName = `db-${++dbCounter}`;
  console.log('Connecting to %j - datasource %j', connectionString, dsName);
  const ds = new juggler.DataSource({
    name: dsName,
    connector: require('loopback-connector-mysql'),
    url: connectionString,
  });
  await ds.connect();
  app.dataSource(ds, dsName);
  assert.equal(ds.name, dsName);
  return ds;
}
github strongloop / loopback-next / packages / boot / src / __tests__ / acceptance / model-api.booter.acceptance.ts View on Github external
async function givenAppWithDataSource() {
    app = new BooterApp({
      rest: givenHttpServerConfig(),
    });
    app.dataSource(new juggler.DataSource({connector: 'memory'}), 'db');
  }
github strongloop / loopback-next / packages / rest-crud / src / __tests__ / acceptance / default-model-crud-rest.acceptance.ts View on Github external
async function setupTestScenario() {
    const db = new juggler.DataSource({connector: 'memory'});

    const ProductRepository = defineCrudRepositoryClass(Product);

    repo = new ProductRepository(db);

    const CrudRestController = defineCrudRestController<
      Product,
      typeof Product.prototype.id,
      'id'
    >(Product, {basePath: '/products'});

    class ProductController extends CrudRestController {
      constructor() {
        super(repo);
      }
    }
github strongloop / loopback-next / examples / todo-list / src / __tests__ / helpers.ts View on Github external
testdb,
    async () => todoRepo,
    async () => todoListImageRepo,
  );

  const todoListImageRepo: TodoListImageRepository = new TodoListImageRepository(
    testdb,
    async () => todoListRepo,
  );

  await todoRepo.deleteAll();
  await todoListRepo.deleteAll();
  await todoListImageRepo.deleteAll();
}

export const testdb: juggler.DataSource = new juggler.DataSource({
  name: 'db',
  connector: 'memory',
});
github strongloop / loopback-next / packages / repository-tests / src / crud-test-suite.ts View on Github external
withCrudCtx(function setupGlobalDataSource(ctx: CrudTestContext) {
        ctx.dataSource = new juggler.DataSource(ctx.dataSourceOptions);
      }),
    );