Skip to content

Commit

Permalink
feat(repository): adds support to identify entity ids automatically
Browse files Browse the repository at this point in the history
added support to identify entity ids automatically

fix #35
  • Loading branch information
eduardo-vortx committed Jan 15, 2022
1 parent a46434a commit d6e2fd2
Show file tree
Hide file tree
Showing 30 changed files with 1,329 additions and 1,619 deletions.
2,573 changes: 1,098 additions & 1,475 deletions package-lock.json

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@
"license": "MIT",
"homepage": "https://github.com/herbsjs/herbs2knex#readme",
"dependencies": {
"@herbsjs/gotu": "^1.0.0",
"@herbsjs/suma": "^1.1.0",
"knex": "^0.95.11"
"@herbsjs/gotu": "^1.1.0",
"@herbsjs/suma": "^1.2.0",
"knex": "^0.95.15"
},
"devDependencies": {
"@babel/core": "^7.15.8",
Expand Down
24 changes: 18 additions & 6 deletions src/repository.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
const Convention = require("./convention")
const DataMapper = require("./dataMapper")
const { checker } = require('@herbsjs/suma')
const { BaseEntity } = require("@herbsjs/gotu/src/baseEntity")

const dependency = { convention: Convention }
const dependency = {
convention: Convention,
DataMapper
}

module.exports = class Repository {
constructor(options) {
Expand All @@ -14,10 +18,10 @@ module.exports = class Repository {
? `${this.schema}.${this.table}`
: `${this.table}`
this.entity = options.entity
this.entityIDs = options.ids
this.entityIDs = this.#getEntityIds(options)
this.foreignKeys = options.foreignKeys
this.knex = options.knex
this.dataMapper = new DataMapper(this.entity, this.entityIDs, this.foreignKeys)
this.dataMapper = new di.DataMapper(this.entity, this.entityIDs, this.foreignKeys)
}

runner() {
Expand Down Expand Up @@ -230,9 +234,17 @@ module.exports = class Repository {
return ret === 1
}

#getEntityIds({ entity, ids }) {
if (ids) return ids

if (entity && entity.prototype instanceof BaseEntity) {
const fields = Object.values(entity.prototype.meta.schema)
const idFields = fields.filter(({ options }) => options.isId)
const idFieldsNames = idFields.map(({ name }) => name)

}


return idFieldsNames
}

return []
}
}
7 changes: 3 additions & 4 deletions test/integration/mssql/delete.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { entity, field } = require('@herbsjs/gotu')
const Repository = require('../../src/repository')
const { entity, field, id } = require('@herbsjs/gotu')
const Repository = require('../../../src/repository')
const db = require('./db')
const connection = require('../connection')
const assert = require('assert')
Expand Down Expand Up @@ -61,7 +61,7 @@ describe('Delete an Entity', () => {

const givenAnEntity = () => {
return entity('A entity', {
id: field(Number),
id: id(Number),
stringTest: field(String),
booleanTest: field(Boolean)
})
Expand All @@ -84,7 +84,6 @@ describe('Delete an Entity', () => {
entity: anEntity,
table,
database,
ids: ['id'],
knex: connection
})
const aModifiedInstance = givenAnModifiedEntity()
Expand Down
7 changes: 3 additions & 4 deletions test/integration/mssql/find.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { entity, field } = require('@herbsjs/gotu')
const Repository = require('../../src/repository')
const { entity, field, id } = require('@herbsjs/gotu')
const Repository = require('../../../src/repository')
const db = require('./db')
const connection = require('../connection')
const assert = require('assert')
Expand Down Expand Up @@ -55,7 +55,7 @@ describe('Query Find', () => {

const givenAnEntity = () => {
return entity('A entity', {
id: field(Number),
id: id(Number),
stringTest: field(String),
booleanTest: field(Boolean)
})
Expand All @@ -68,7 +68,6 @@ describe('Query Find', () => {
entity: anEntity,
table,
database,
ids: ['id'],
knex: connection
})
const injection = {}
Expand Down
7 changes: 3 additions & 4 deletions test/integration/mssql/findByID.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { entity, field } = require('@herbsjs/gotu')
const Repository = require('../../src/repository')
const { entity, field, id } = require('@herbsjs/gotu')
const Repository = require('../../../src/repository')
const db = require('./db')
const connection = require('../connection')
const assert = require('assert')
Expand Down Expand Up @@ -56,7 +56,7 @@ describe('Query Find by ID', () => {

const givenAnEntity = () => {
return entity('A entity', {
id: field(Number),
id: id(Number),
stringTest: field(String),
booleanTest: field(Boolean)
})
Expand All @@ -69,7 +69,6 @@ describe('Query Find by ID', () => {
entity: anEntity,
table,
database,
ids: ['id'],
knex: connection
})
const injection = {}
Expand Down
8 changes: 3 additions & 5 deletions test/integration/mssql/first.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { entity, field } = require('@herbsjs/gotu')
const Repository = require('../../src/repository')
const { entity, field, id } = require('@herbsjs/gotu')
const Repository = require('../../../src/repository')
const db = require('./db')
const connection = require('../connection')
const assert = require('assert')
Expand Down Expand Up @@ -55,7 +55,7 @@ describe('Query First', () => {

const givenAnEntity = () => {
return entity('A entity', {
id: field(Number),
id: id(Number),
stringTest: field(String),
booleanTest: field(Boolean)
})
Expand All @@ -68,7 +68,6 @@ describe('Query First', () => {
entity: anEntity,
table,
database,
ids: ['id'],
knex: connection
})
const injection = {}
Expand All @@ -91,7 +90,6 @@ describe('Query First', () => {
entity: anEntity,
table,
database,
ids: ['id'],
knex: connection
})
const injection = {}
Expand Down
5 changes: 2 additions & 3 deletions test/integration/mssql/insert.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { entity, field } = require('@herbsjs/gotu')
const { entity, field, id } = require('@herbsjs/gotu')
const Repository = require('../../../src/repository')
const db = require('./db')
const connection = require('../connection')
Expand Down Expand Up @@ -57,7 +57,7 @@ describe('Persist Entity', () => {

const givenAnEntity = () => {
return entity('A entity', {
id: field(Number),
id: id(Number),
stringTest: field(String),
booleanTest: field(Boolean)
})
Expand All @@ -80,7 +80,6 @@ describe('Persist Entity', () => {
entity: anEntity,
table,
database,
ids: ['id'],
knex: connection
})
const aModifiedInstance = givenAnModifiedEntity()
Expand Down
7 changes: 3 additions & 4 deletions test/integration/mssql/update.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { entity, field } = require('@herbsjs/gotu')
const Repository = require('../../src/repository')
const { entity, field, id } = require('@herbsjs/gotu')
const Repository = require('../../../src/repository')
const db = require('./db')
const connection = require('../connection')
const assert = require('assert')
Expand Down Expand Up @@ -61,7 +61,7 @@ describe('Persist Entity', () => {

const givenAnEntity = () => {
return entity('A entity', {
id: field(Number),
id: id(Number),
stringTest: field(String),
booleanTest: field(Boolean)
})
Expand All @@ -84,7 +84,6 @@ describe('Persist Entity', () => {
entity: anEntity,
table,
database,
ids: ['id'],
knex: connection
})
const aModifiedInstance = givenAnModifiedEntity()
Expand Down
7 changes: 3 additions & 4 deletions test/integration/mysql/delete.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { entity, field } = require('@herbsjs/gotu')
const Repository = require('../../src/repository')
const { entity, field, id } = require('@herbsjs/gotu')
const Repository = require('../../../src/repository')
const db = require('./db')
const connection = require('../connection')
const assert = require('assert')
Expand Down Expand Up @@ -54,7 +54,7 @@ describe('Delete an Entity', () => {

const givenAnEntity = () => {
return entity('A entity', {
id: field(Number),
id: id(Number),
stringTest: field(String),
booleanTest: field(Boolean)
})
Expand All @@ -77,7 +77,6 @@ describe('Delete an Entity', () => {
entity: anEntity,
table,
database,
ids: ['id'],
knex: connection
})
const aModifiedInstance = givenAnModifiedEntity()
Expand Down
7 changes: 3 additions & 4 deletions test/integration/mysql/find.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { entity, field } = require('@herbsjs/gotu')
const Repository = require('../../src/repository')
const { entity, field, id } = require('@herbsjs/gotu')
const Repository = require('../../../src/repository')
const db = require('./db')
const connection = require('../connection')
const assert = require('assert')
Expand Down Expand Up @@ -48,7 +48,7 @@ describe('Query Find', () => {

const givenAnEntity = () => {
return entity('A entity', {
id: field(Number),
id: id(Number),
stringTest: field(String),
booleanTest: field(Boolean)
})
Expand All @@ -61,7 +61,6 @@ describe('Query Find', () => {
entity: anEntity,
table,
database,
ids: ['id'],
knex: connection
})
const injection = {}
Expand Down
7 changes: 3 additions & 4 deletions test/integration/mysql/findByID.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { entity, field } = require('@herbsjs/gotu')
const Repository = require('../../src/repository')
const { entity, field, id } = require('@herbsjs/gotu')
const Repository = require('../../../src/repository')
const db = require('./db')
const connection = require('../connection')
const assert = require('assert')
Expand Down Expand Up @@ -49,7 +49,7 @@ describe('Query Find by ID', () => {

const givenAnEntity = () => {
return entity('A entity', {
id: field(Number),
id: id(Number),
stringTest: field(String),
booleanTest: field(Boolean)
})
Expand All @@ -62,7 +62,6 @@ describe('Query Find by ID', () => {
entity: anEntity,
table,
database,
ids: ['id'],
knex: connection
})
const injection = {}
Expand Down
8 changes: 3 additions & 5 deletions test/integration/mysql/first.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { entity, field } = require('@herbsjs/gotu')
const Repository = require('../../src/repository')
const { entity, field, id } = require('@herbsjs/gotu')
const Repository = require('../../../src/repository')
const db = require('./db')
const connection = require('../connection')
const assert = require('assert')
Expand Down Expand Up @@ -48,7 +48,7 @@ describe('Query First', () => {

const givenAnEntity = () => {
return entity('A entity', {
id: field(Number),
id: id(Number),
stringTest: field(String),
booleanTest: field(Boolean)
})
Expand All @@ -61,7 +61,6 @@ describe('Query First', () => {
entity: anEntity,
table,
database,
ids: ['id'],
knex: connection
})
const injection = {}
Expand All @@ -84,7 +83,6 @@ describe('Query First', () => {
entity: anEntity,
table,
database,
ids: ['id'],
knex: connection
})
const injection = {}
Expand Down
5 changes: 2 additions & 3 deletions test/integration/mysql/insert.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { entity, field } = require('@herbsjs/gotu')
const Repository = require('../../src/repository')
const { entity, field, id } = require('@herbsjs/gotu')
const Repository = require('../../../src/repository')
const db = require('./db')
const connection = require('../connection')
const assert = require('assert')
Expand Down Expand Up @@ -74,7 +74,6 @@ describe('Persist Entity', () => {
entity: anEntity,
table,
database,
ids: ['id'],
knex: connection
})
const aModifiedInstance = givenAnModifiedEntity()
Expand Down
7 changes: 3 additions & 4 deletions test/integration/mysql/update.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { entity, field } = require('@herbsjs/gotu')
const Repository = require('../../src/repository')
const { entity, field, id } = require('@herbsjs/gotu')
const Repository = require('../../../src/repository')
const db = require('./db')
const connection = require('../connection')
const assert = require('assert')
Expand Down Expand Up @@ -68,7 +68,7 @@ describe('Persist Entity', () => {

const givenAnEntity = () => {
return entity('A entity', {
id: field(Number),
id: id(Number),
stringTest: field(String),
booleanTest: field(Boolean)
})
Expand All @@ -91,7 +91,6 @@ describe('Persist Entity', () => {
entity: anEntity,
table,
database,
ids: ['id'],
knex: connection
})
const aModifiedInstance = givenAnModifiedEntity()
Expand Down
5 changes: 2 additions & 3 deletions test/integration/pg/delete.test.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { entity, field } = require('@herbsjs/gotu')
const { entity, field, id } = require('@herbsjs/gotu')
const Repository = require('../../../src/repository')
const db = require('./db')
const connection = require('../connection')
Expand Down Expand Up @@ -43,7 +43,7 @@ describe('Delete an Entity', () => {

const givenAnEntity = () => {
return entity('A entity', {
id: field(Number),
id: id(Number),
stringTest: field(String),
booleanTest: field(Boolean)
})
Expand All @@ -66,7 +66,6 @@ describe('Delete an Entity', () => {
entity: anEntity,
table,
schema,
ids: ['id'],
knex: connection
})
const aModifiedInstance = givenAnModifiedEntity()
Expand Down
Loading

0 comments on commit d6e2fd2

Please sign in to comment.