Skip to content

Commit d74749c

Browse files
committed
fix: fix bug with entities with methods
1 parent 8fa319e commit d74749c

9 files changed

+44
-36
lines changed

src/repository.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@ module.exports = class Repository {
1414
this.entity = options.entity
1515
this.entityIDs = this.#getEntityIds(options)
1616
this.foreignKeys = options.foreignKeys
17-
this.knex = options.knex
1817
this.dataMapper = new DataMapper(
1918
this.entity,
2019
this.entityIDs,
2120
this.foreignKeys,
2221
options
2322
)
23+
this.knex = options.knex
2424
}
2525

2626
runner() {
@@ -237,7 +237,7 @@ module.exports = class Repository {
237237

238238
if (herbs.entity.isEntity(entity)) {
239239
const fields = Object.values(entity.prototype.meta.schema)
240-
const idFields = fields.filter(({ options }) => options.isId)
240+
const idFields = fields.filter(({ options }) => options?.isId)
241241
const idFieldsNames = idFields.map(({ name }) => name)
242242

243243
return idFieldsNames

test/unit/queries/delete.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ describe("Delete an Entity", () => {
88
id: id(Number),
99
stringTest: field(String),
1010
booleanTest: field(Boolean),
11+
aMethod: () => { },
1112
})
1213
}
1314

test/unit/queries/find.test.js

+33-31
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
const { entity, field, id } = require('@herbsjs/gotu')
22
const Repository = require('../../../src/repository')
3-
const assert = require('assert')
4-
const { RequestTokenHandler } = require('tedious/lib/token/handler')
3+
const assert = require('assert').strict
54

65
describe('Query Find', () => {
76

@@ -75,7 +74,7 @@ describe('Query Find', () => {
7574
const ret = await itemRepo.find()
7675

7776
//then
78-
assert.strictEqual(ret.length, 2)
77+
assert.equal(ret.length, 2)
7978
})
8079

8180
it('should return entities with collumn order by', async () => {
@@ -97,9 +96,9 @@ describe('Query Find', () => {
9796
const ret = await itemRepo.find({ orderBy: 'stringTest' })
9897

9998
//then
100-
assert.strictEqual(ret.length, 2)
101-
assert.deepStrictEqual(spy.select, ['id', 'string_test', 'boolean_test'])
102-
assert.deepStrictEqual(spy.orderBy, 'stringTest')
99+
assert.equal(ret.length, 2)
100+
assert.deepEqual(spy.select, ['id', 'string_test', 'boolean_test'])
101+
assert.deepEqual(spy.orderBy, 'stringTest')
103102
})
104103

105104
it('should return entities with limit', async () => {
@@ -121,9 +120,9 @@ describe('Query Find', () => {
121120
const ret = await itemRepo.find({ limit: 1 })
122121

123122
//then
124-
assert.strictEqual(ret.length, 1)
125-
assert.deepStrictEqual(spy.select, ['id', 'string_test', 'boolean_test'])
126-
assert.deepStrictEqual(spy.limit, 1)
123+
assert.equal(ret.length, 1)
124+
assert.deepEqual(spy.select, ['id', 'string_test', 'boolean_test'])
125+
assert.deepEqual(spy.limit, 1)
127126
})
128127

129128
it('should return all entities when limit is 0', async () => {
@@ -145,8 +144,8 @@ describe('Query Find', () => {
145144
const ret = await itemRepo.find({ limit: 0 })
146145

147146
//then
148-
assert.strictEqual(ret.length, 2)
149-
assert.deepStrictEqual(spy.select, ['id', 'string_test', 'boolean_test'])
147+
assert.equal(ret.length, 2)
148+
assert.deepEqual(spy.select, ['id', 'string_test', 'boolean_test'])
150149
})
151150

152151
it('should return data with offset', async () => {
@@ -168,9 +167,9 @@ describe('Query Find', () => {
168167
const ret = await itemRepo.find({ offset: 10 })
169168

170169
//then
171-
assert.strictEqual(ret.length, 2)
172-
assert.deepStrictEqual(spy.select, ['id', 'string_test', 'boolean_test'])
173-
assert.deepStrictEqual(spy.offset, 10)
170+
assert.equal(ret.length, 2)
171+
assert.deepEqual(spy.select, ['id', 'string_test', 'boolean_test'])
172+
assert.deepEqual(spy.offset, 10)
174173
})
175174

176175
it('should return entities with complex order by', async () => {
@@ -192,9 +191,9 @@ describe('Query Find', () => {
192191
const ret = await itemRepo.find({ orderBy: [{ column: 'nome', order: 'desc' }, 'email'] })
193192

194193
//then
195-
assert.strictEqual(ret.length, 2)
196-
assert.deepStrictEqual(spy.select, ['id', 'string_test', 'boolean_test'])
197-
assert.deepStrictEqual(spy.orderBy, [{ column: 'nome', order: 'desc' }, 'email'])
194+
assert.equal(ret.length, 2)
195+
assert.deepEqual(spy.select, ['id', 'string_test', 'boolean_test'])
196+
assert.deepEqual(spy.orderBy, [{ column: 'nome', order: 'desc' }, 'email'])
198197
})
199198

200199
it('should return error when order by is a empty object', async () => {
@@ -217,14 +216,17 @@ describe('Query Find', () => {
217216
const ret = await itemRepo.find({ orderBy: {} })
218217
} catch (error) {
219218
//then
220-
assert.deepStrictEqual(error, 'order by is invalid')
219+
assert.deepEqual(error, 'order by is invalid')
221220
}
222221
})
223222
})
224223

225224
context('Find with conditions', () => {
226225
const givenAnEntity = () => {
227-
const ParentEntity = entity('A Parent Entity', {})
226+
const ParentEntity = entity('A Parent Entity', {
227+
id: id(Number),
228+
stringTest: field(String),
229+
})
228230

229231
return entity('A entity', {
230232
id: id(Number),
@@ -277,13 +279,13 @@ describe('Query Find', () => {
277279
const ret = await itemRepo.find({ where: { stringTest: ["john"] } })
278280

279281
//then
280-
assert.deepStrictEqual(ret[0].toJSON(), { id: 1, stringTest: 'john', booleanTest: true, entityTest: undefined, entitiesTest: undefined })
281-
assert.deepStrictEqual(ret[1].toJSON(), { id: 2, stringTest: 'clare', booleanTest: false, entityTest: undefined, entitiesTest: undefined })
282-
assert.deepStrictEqual(spy.select, ['id', 'string_test', 'boolean_test'])
283-
assert.deepStrictEqual(spy.value, ["john"])
282+
assert.deepEqual(ret[0].toJSON(), { id: 1, stringTest: 'john', booleanTest: true, entityTest: undefined, entitiesTest: undefined })
283+
assert.deepEqual(ret[1].toJSON(), { id: 2, stringTest: 'clare', booleanTest: false, entityTest: undefined, entitiesTest: undefined })
284+
assert.deepEqual(spy.select, ['id', 'string_test', 'boolean_test'])
285+
assert.deepEqual(spy.value, ["john"])
284286
})
285287

286-
it('should return entities using foreing key', async () => {
288+
it('should return entities using foreign keys', async () => {
287289
//given
288290
let spy = {}
289291
const retFromDeb = [
@@ -303,11 +305,11 @@ describe('Query Find', () => {
303305
const ret = await itemRepo.find({ where: { fkField: 1 } })
304306

305307
//then
306-
assert.deepStrictEqual(ret[0].toJSON({ allowExtraKeys: true }), { id: 1, stringTest: 'john', booleanTest: true, entityTest: undefined, entitiesTest: undefined, fkField: "21" })
307-
assert.deepStrictEqual(ret[1].toJSON({ allowExtraKeys: true }), { id: 2, stringTest: 'clare', booleanTest: false, entityTest: undefined, entitiesTest: undefined, fkField: null })
308-
assert.deepStrictEqual(spy.select, ['id', 'string_test', 'boolean_test', 'fk_field'])
309-
assert.deepStrictEqual(spy.where, 'fk_field')
310-
assert.deepStrictEqual(spy.value, [1])
308+
assert.deepEqual(ret[0].toJSON({ allowExtraKeys: true }), { id: 1, stringTest: 'john', booleanTest: true, entityTest: undefined, entitiesTest: undefined, fkField: "21" })
309+
assert.deepEqual(ret[1].toJSON({ allowExtraKeys: true }), { id: 2, stringTest: 'clare', booleanTest: false, entityTest: undefined, entitiesTest: undefined, fkField: null })
310+
assert.deepEqual(spy.select, ['id', 'string_test', 'boolean_test', 'fk_field'])
311+
assert.deepEqual(spy.where, 'fk_field')
312+
assert.deepEqual(spy.value, [1])
311313
})
312314

313315

@@ -331,7 +333,7 @@ describe('Query Find', () => {
331333
const ret = await itemRepo.find({ where: "wrong" })
332334
} catch (error) {
333335
//then
334-
assert.deepStrictEqual(error, "condition term is invalid")
336+
assert.deepEqual(error, "condition term is invalid")
335337
}
336338
})
337339

@@ -356,7 +358,7 @@ describe('Query Find', () => {
356358
const ret = await itemRepo.find({ where: { wrong: { wrong: "wrong" } } })
357359
} catch (error) {
358360
//then
359-
assert.deepStrictEqual(error, "condition value is invalid")
361+
assert.deepEqual(error, "condition value is invalid")
360362
}
361363
})
362364
})

test/unit/queries/findAll.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ describe('Query Find All', () => {
1313
booleanTest: field(Boolean),
1414
entityTest: field(ParentEntity),
1515
entitiesTest: field([ParentEntity]),
16+
aMethod: () => { },
1617
})
1718
}
1819

test/unit/queries/findByID.test.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ describe('Query Find by ID', () => {
1313
booleanTest: field(Boolean),
1414
entityTest: field(ParentEntity),
1515
entitiesTest: field([ParentEntity]),
16+
aMethod: () => { },
1617
})
1718
}
1819

@@ -65,7 +66,7 @@ describe('Query Find by ID', () => {
6566
assert.deepStrictEqual(spy.value, [1])
6667
})
6768

68-
it('should return entities instances with foreing key', async () => {
69+
it('should return entities instances with foreign keys', async () => {
6970
//given
7071
let spy = {}
7172
const retFromDeb = [

test/unit/queries/first.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ describe('Query First', () => {
1515
booleanTest: field(Boolean),
1616
entityTest: field(ParentEntity),
1717
entitiesTest: field([ParentEntity]),
18+
aMethod: () => { },
1819
})
1920
}
2021

test/unit/queries/insert.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ describe("Insert an Entity", () => {
1212
booleanTest: field(Boolean),
1313
entityTest: field(ParentEntity),
1414
entitiesTest: field([ParentEntity]),
15+
aMethod: () => { },
1516
})
1617
}
1718

test/unit/queries/update.test.js

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ describe("Update an Entity", () => {
1212
booleanTest: field(Boolean),
1313
entityTest: field(ParentEntity),
1414
entitiesTest: field([ParentEntity]),
15+
aMethod: () => { },
1516
})
1617
}
1718

test/unit/repository.test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { entity, field, id } = require('@herbsjs/gotu')
22
const Repository = require('../../src/repository')
3-
const assert = require('assert')
3+
const assert = require('assert').strict
44
const Convention = require('../../src/convention')
55

66
describe('Repository', () => {
@@ -17,7 +17,7 @@ describe('Repository', () => {
1717

1818
it('should use custom convention when it is specified', () => {
1919
const customConvention = {
20-
doSomething() {}
20+
doSomething() { }
2121
}
2222

2323
const repositoryInstance = new Repository({

0 commit comments

Comments
 (0)