Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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},
);
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;
}
async function givenAppWithDataSource() {
app = new BooterApp({
rest: givenHttpServerConfig(),
});
app.dataSource(new juggler.DataSource({connector: 'memory'}), 'db');
}
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);
}
}
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',
});
withCrudCtx(function setupGlobalDataSource(ctx: CrudTestContext) {
ctx.dataSource = new juggler.DataSource(ctx.dataSourceOptions);
}),
);