Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
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');
});
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',
]);
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},
);
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
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) {
// 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;
}
}
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,
);
}
};
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(
) {
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,
);
}
};
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})