diff --git a/package-lock.json b/package-lock.json index d807a2b..4770ff6 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "@athenna/database", - "version": "5.56.0", + "version": "5.57.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "@athenna/database", - "version": "5.56.0", + "version": "5.57.0", "license": "MIT", "dependencies": { "@faker-js/faker": "^8.4.1" diff --git a/package.json b/package.json index 0a94b08..a67cd31 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@athenna/database", - "version": "5.56.0", + "version": "5.57.0", "description": "The Athenna database handler for SQL/NoSQL.", "license": "MIT", "author": "João Lenon ", diff --git a/src/models/BaseModel.ts b/src/models/BaseModel.ts index d760bc1..8797530 100644 --- a/src/models/BaseModel.ts +++ b/src/models/BaseModel.ts @@ -546,7 +546,11 @@ export class BaseModel { Object.keys(copied).forEach(key => { const value = this[key] - if (Is.Array(value) && value[0] && ORIGINAL_SYMBOL in value[0]) { + if ( + Is.Array(value) && + Is.Object(value[0]) && + ORIGINAL_SYMBOL in value[0] + ) { return } diff --git a/src/models/relations/HasOneThrough/HasOneThroughRelation.ts b/src/models/relations/HasOneThrough/HasOneThroughRelation.ts index 6fea71f..96b5634 100644 --- a/src/models/relations/HasOneThrough/HasOneThroughRelation.ts +++ b/src/models/relations/HasOneThrough/HasOneThroughRelation.ts @@ -36,10 +36,7 @@ export class HasOneThroughRelation { models: BaseModel[], relation: HasOneThroughOptions ): Promise { - const result = await HasManyThroughRelation.loadAll( - models, - relation as any - ) + const result = await HasManyThroughRelation.loadAll(models, relation as any) return result.map(m => { const arr = (m[relation.property] as any[]) || [] diff --git a/tests/fixtures/migrations/2022_10_08_0000020_create_through_tables_postgres.ts b/tests/fixtures/migrations/2022_10_08_0000020_create_through_tables_postgres.ts index 576836b..77d9d45 100644 --- a/tests/fixtures/migrations/2022_10_08_0000020_create_through_tables_postgres.ts +++ b/tests/fixtures/migrations/2022_10_08_0000020_create_through_tables_postgres.ts @@ -45,11 +45,7 @@ export class ThroughTablesMigration extends BaseMigration { await db.createTable('sale_items', table => { table.increments('id') table.integer('quantity').defaultTo(1) - table - .integer('appointmentId') - .unsigned() - .references('id') - .inTable('appointments') + table.integer('appointmentId').unsigned().references('id').inTable('appointments') table.integer('saleId').unsigned().references('id').inTable('sales') }) } diff --git a/tests/unit/models/BaseModelTest.ts b/tests/unit/models/BaseModelTest.ts index 8fe173d..042bc61 100644 --- a/tests/unit/models/BaseModelTest.ts +++ b/tests/unit/models/BaseModelTest.ts @@ -563,6 +563,20 @@ export default class BaseModelTest { assert.isTrue(user.isPersisted()) } + @Test() + public async shouldBeAbleToSetOriginalWhenModelHasAnArrayOfPrimitivesValue({ assert }: Context) { + const user = new User() + + user.id = '1' + user.name = 'lenon' + user.metadata4 = { name: 'lenon', age: 30, address: { city: 'foo', state: 'bar' } } as any + ;(user as any).images = ['https://example.com/a.png', 'https://example.com/b.png'] + + assert.doesNotThrow(() => user.setOriginal()) + assert.isTrue(user.isPersisted()) + assert.isFalse(user.isDirty()) + } + @Test() public async shouldBeAbleToValidateThatAModelIsDirty({ assert }: Context) { const user = new User() diff --git a/tests/unit/models/relations/HasOneThrough/HasOneThroughRelationTest.ts b/tests/unit/models/relations/HasOneThrough/HasOneThroughRelationTest.ts index 3a78e9d..4354dda 100644 --- a/tests/unit/models/relations/HasOneThrough/HasOneThroughRelationTest.ts +++ b/tests/unit/models/relations/HasOneThrough/HasOneThroughRelationTest.ts @@ -35,9 +35,7 @@ export default class HasOneThroughRelationTest { { id: 1, name: 'South' }, { id: 2, name: 'North' } ]) - await pg.table('members').createMany([ - { id: 1, name: 'lenon', regionId: 1 } - ]) + await pg.table('members').createMany([{ id: 1, name: 'lenon', regionId: 1 }]) await pg.table('articles').createMany([ { id: 1, title: 'first', memberId: 1 }, { id: 2, title: 'second', memberId: 1 }