diff --git a/packages/repository-tests/src/crud/create-retrieve.suite.ts b/packages/repository-tests/src/crud/create-retrieve.suite.ts index ac8706a7e657..c9feb42c1ccf 100644 --- a/packages/repository-tests/src/crud/create-retrieve.suite.ts +++ b/packages/repository-tests/src/crud/create-retrieve.suite.ts @@ -12,9 +12,9 @@ import { property, } from '@loopback/repository'; import {expect, toJSON} from '@loopback/testlab'; -import {MixedIdType} from '../helpers.repository-tests'; import { deleteAllModelsInDefaultDataSource, + MixedIdType, withCrudCtx, } from '../helpers.repository-tests'; import { @@ -89,7 +89,7 @@ export function createRetrieveSuite( const pens = await repo.create({name: 'Pens', categoryId: 1}); const pencils = await repo.create({name: 'Pencils', categoryId: 2}); const products = await findByForeignKeys(repo, 'categoryId', [1]); - expect(products).deepEqual([pens]); + expect(toJSON(products)).deepEqual(toJSON([pens])); expect(products).to.not.containDeep(pencils); }); @@ -97,7 +97,7 @@ export function createRetrieveSuite( const pens = await repo.create({name: 'Pens', categoryId: 1}); const pencils = await repo.create({name: 'Pencils', categoryId: 2}); const products = await findByForeignKeys(repo, 'categoryId', [1, 2]); - expect(products).deepEqual([pens, pencils]); + expect(toJSON(products)).deepEqual(toJSON([pens, pencils])); }); }); } diff --git a/packages/repository-tests/src/crud/nested-model-properties.suite.ts b/packages/repository-tests/src/crud/nested-model-properties.suite.ts index e6c2d9722d8a..a3fe2a7c0bbb 100644 --- a/packages/repository-tests/src/crud/nested-model-properties.suite.ts +++ b/packages/repository-tests/src/crud/nested-model-properties.suite.ts @@ -14,8 +14,8 @@ import { import {expect, toJSON} from '@loopback/testlab'; import { deleteAllModelsInDefaultDataSource, - withCrudCtx, MixedIdType, + withCrudCtx, } from '../helpers.repository-tests'; import { CrudFeatures, @@ -91,6 +91,7 @@ export function nestedModelsPropertiesSuite( @property({ id: true, generated: true, + useDefaultIdType: true, }) id: MixedIdType; diff --git a/packages/repository-tests/src/crud/relations/acceptance/belongs-to.relation.acceptance.ts b/packages/repository-tests/src/crud/relations/acceptance/belongs-to.relation.acceptance.ts index 0e23888c83d9..278e323b3b8c 100644 --- a/packages/repository-tests/src/crud/relations/acceptance/belongs-to.relation.acceptance.ts +++ b/packages/repository-tests/src/crud/relations/acceptance/belongs-to.relation.acceptance.ts @@ -91,7 +91,7 @@ export function belongsToRelationAcceptance( description: 'Order that is shipped Tuesday morning', }); const result = await orderRepo.shipment(order.id); - expect(result).to.deepEqual(shipment); + expect(toJSON(result)).to.deepEqual(toJSON(shipment)); }); it('returns undefined if the source instance does not have the foreign key', async () => { diff --git a/packages/repository-tests/src/crud/relations/acceptance/has-many.relation.acceptance.ts b/packages/repository-tests/src/crud/relations/acceptance/has-many.relation.acceptance.ts index cdca52274d93..f37c32b4b99d 100644 --- a/packages/repository-tests/src/crud/relations/acceptance/has-many.relation.acceptance.ts +++ b/packages/repository-tests/src/crud/relations/acceptance/has-many.relation.acceptance.ts @@ -310,7 +310,7 @@ export function hasManyRelationAcceptance( }); const childsParent = await getParentCustomer(child.id); expect(_.pick(childsParent, ['id', 'name'])).to.eql( - _.pick(parent, ['id', 'name']), + toJSON(_.pick(parent, ['id', 'name'])), ); }); diff --git a/packages/repository-tests/src/crud/relations/acceptance/multi-relations-inclusion-resolver.relation.acceptance.ts b/packages/repository-tests/src/crud/relations/acceptance/multi-relations-inclusion-resolver.relation.acceptance.ts index 4bde2b0774ae..126144e94043 100644 --- a/packages/repository-tests/src/crud/relations/acceptance/multi-relations-inclusion-resolver.relation.acceptance.ts +++ b/packages/repository-tests/src/crud/relations/acceptance/multi-relations-inclusion-resolver.relation.acceptance.ts @@ -16,6 +16,8 @@ import { withCrudCtx, } from '../../../helpers.repository-tests'; import { + Address, + AddressRepository, Customer, CustomerRepository, Order, @@ -36,6 +38,7 @@ export function hasManyInclusionResolverAcceptance( ); function suite() { before(deleteAllModelsInDefaultDataSource); + let addressRepo: AddressRepository; let customerRepo: CustomerRepository; let orderRepo: OrderRepository; @@ -43,14 +46,18 @@ export function hasManyInclusionResolverAcceptance( withCrudCtx(async function setupRepository(ctx: CrudTestContext) { // this helper should create the inclusion resolvers and also // register inclusion resolvers for us - ({customerRepo, orderRepo} = givenBoundCrudRepositories( + ({customerRepo, orderRepo, addressRepo} = givenBoundCrudRepositories( ctx.dataSource, repositoryClass, features, )); expect(customerRepo.orders.inclusionResolver).to.be.Function(); - await ctx.dataSource.automigrate([Customer.name, Order.name]); + await ctx.dataSource.automigrate([ + Customer.name, + Order.name, + Address.name, + ]); }), ); @@ -63,7 +70,14 @@ export function hasManyInclusionResolverAcceptance( const parent = await customerRepo.create({name: 'parent'}); const customer = await customerRepo.create({ name: 'customer', - parentId: parent.id, + //parentId: parent.id, + }); + const address = await addressRepo.create({ + street: '8200 Warden', + city: 'Markham', + province: 'On', + zipcode: '8200', + customerId: parent.id, }); const order = await orderRepo.create({ description: 'an order', @@ -71,7 +85,7 @@ export function hasManyInclusionResolverAcceptance( }); const result = await customerRepo.find({ - include: [{relation: 'orders'}, {relation: 'customers'}], + include: [{relation: 'orders'}, {relation: 'address'}], }); const expected = [ { @@ -84,16 +98,11 @@ export function hasManyInclusionResolverAcceptance( shipmentInfo: features.emptyValue, }, ], - customers: [ - { - ...customer, - parentId: parent.id, - }, - ], + address: address, }, { ...customer, - parentId: parent.id, // doesn't have any related models + parentId: features.emptyValue, //parent.id, // doesn't have any related models }, ]; expect(toJSON(result)).to.deepEqual(toJSON(expected)); diff --git a/packages/repository-tests/src/crud/relations/fixtures/models/address.model.ts b/packages/repository-tests/src/crud/relations/fixtures/models/address.model.ts index fdaa46032580..47524d77e1d0 100644 --- a/packages/repository-tests/src/crud/relations/fixtures/models/address.model.ts +++ b/packages/repository-tests/src/crud/relations/fixtures/models/address.model.ts @@ -19,6 +19,7 @@ export class Address extends Entity { @property({ id: true, generated: true, + useDefaltIdType: true, }) id: MixedIdType; @property({ diff --git a/packages/repository-tests/src/crud/relations/fixtures/models/customer.model.ts b/packages/repository-tests/src/crud/relations/fixtures/models/customer.model.ts index 6d189eb850ff..07b4104f770a 100644 --- a/packages/repository-tests/src/crud/relations/fixtures/models/customer.model.ts +++ b/packages/repository-tests/src/crud/relations/fixtures/models/customer.model.ts @@ -24,6 +24,7 @@ export class Customer extends Entity { @property({ id: true, generated: true, + useDefaltIdType: true, }) id: MixedIdType; diff --git a/packages/repository-tests/src/crud/relations/fixtures/models/order.model.ts b/packages/repository-tests/src/crud/relations/fixtures/models/order.model.ts index 226b1fb4fb1a..b1045586a200 100644 --- a/packages/repository-tests/src/crud/relations/fixtures/models/order.model.ts +++ b/packages/repository-tests/src/crud/relations/fixtures/models/order.model.ts @@ -20,6 +20,7 @@ export class Order extends Entity { @property({ id: true, generated: true, + useDefaultIdType: true, }) id: MixedIdType; diff --git a/packages/repository-tests/src/crud/relations/fixtures/models/shipment.model.ts b/packages/repository-tests/src/crud/relations/fixtures/models/shipment.model.ts index 2005226e7714..d5559aecc3a4 100644 --- a/packages/repository-tests/src/crud/relations/fixtures/models/shipment.model.ts +++ b/packages/repository-tests/src/crud/relations/fixtures/models/shipment.model.ts @@ -19,6 +19,7 @@ export class Shipment extends Entity { @property({ id: true, generated: true, + useDefaltIdType: true, }) id: MixedIdType; diff --git a/packages/repository-tests/src/crud/replace-by-id.suite.ts b/packages/repository-tests/src/crud/replace-by-id.suite.ts index 2af747b9649c..ddbbe3f7857a 100644 --- a/packages/repository-tests/src/crud/replace-by-id.suite.ts +++ b/packages/repository-tests/src/crud/replace-by-id.suite.ts @@ -35,6 +35,7 @@ export function createSuiteForReplaceById( type: features.idType, id: true, generated: true, + useDefaultIdType: true, description: 'The unique identifier for a product', }) id: MixedIdType; diff --git a/packages/repository/src/__tests__/unit/decorator/model-and-relation.decorator.unit.ts b/packages/repository/src/__tests__/unit/decorator/model-and-relation.decorator.unit.ts index bcd60a86dcf7..19d3b307c97b 100644 --- a/packages/repository/src/__tests__/unit/decorator/model-and-relation.decorator.unit.ts +++ b/packages/repository/src/__tests__/unit/decorator/model-and-relation.decorator.unit.ts @@ -192,7 +192,12 @@ describe('model decorator', () => { column: 'QTY', }, }); - expect(meta.id).to.eql({type: 'string', id: true, generated: true}); + expect(meta.id).to.eql({ + type: 'string', + id: true, + generated: true, + useDefaultIdType: false, + }); expect(meta.isShipped).to.eql({type: Boolean}); }); diff --git a/packages/repository/src/model.ts b/packages/repository/src/model.ts index e6009c101ef7..6663a8bbb6a3 100644 --- a/packages/repository/src/model.ts +++ b/packages/repository/src/model.ts @@ -106,6 +106,16 @@ export class ModelDefinition { const definition = (definitionOrType as PropertyDefinition).type ? (definitionOrType as PropertyDefinition) : {type: definitionOrType}; + + if ( + definition.id === true && + definition.generated === true && + definition.type !== undefined && + definition.useDefaultIdType === undefined + ) { + definition.useDefaultIdType = false; + } + this.properties[name] = definition; return this; }