Skip to content

Commit

Permalink
Merge pull request #133 from AthennaIO/develop
Browse files Browse the repository at this point in the history
add way to disable ORM validations from connection
  • Loading branch information
jlenon7 authored Jan 9, 2024
2 parents 8d05511 + d9e32c7 commit 7daf44a
Show file tree
Hide file tree
Showing 8 changed files with 70 additions and 5 deletions.
3 changes: 2 additions & 1 deletion configurer/database
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@ export default {
},

fake: {
driver: 'fake'
driver: 'fake',
validations: false
}
}
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/database",
"version": "4.26.0",
"version": "4.27.0",
"description": "The Athenna database handler for SQL/NoSQL.",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down
18 changes: 18 additions & 0 deletions src/models/builders/ModelQueryBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1044,6 +1044,16 @@ export class ModelQueryBuilder<
return this
}

/**
* Validate if connection is able to run ORM
* validations or not.
*/
private isNotAbleToValidate() {
const connection = this.schema.getModelConnection()

return !Config.get(`database.connections.${connection}.validations`)
}

/**
* Set the internal selected properties and soft delete
* queries.
Expand Down Expand Up @@ -1073,6 +1083,10 @@ export class ModelQueryBuilder<
* can be created in database.
*/
private validateNullable(data: any) {
if (this.isNotAbleToValidate()) {
return
}

if (!this.isToValidateNullable) {
return
}
Expand All @@ -1097,6 +1111,10 @@ export class ModelQueryBuilder<
* can be created in database.
*/
private async validateUnique(data: any, isUpdate = false) {
if (this.isNotAbleToValidate()) {
return
}

if (!this.isToValidateUnique) {
return
}
Expand Down
9 changes: 9 additions & 0 deletions src/models/schemas/ModelSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ export class ModelSchema<M extends BaseModel = any> {
return this.Model.table()
}

/**
* Get the model driver name.
*/
public getModelDriverName() {
const connection = this.getModelConnection()

return Config.get(`database.connections.${connection}.driver`)
}

/**
* Get the model driver.
*/
Expand Down
3 changes: 2 additions & 1 deletion tests/fixtures/config/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ export default {

connections: {
fake: {
driver: 'fake'
driver: 'fake',
validations: true
},
mysql: {
driver: 'mysql'
Expand Down
21 changes: 21 additions & 0 deletions tests/unit/models/builders/ModelQueryBuilderTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,27 @@ export default class ModelQueryBuilderTest {
)
}

@Test()
public async shouldNotBeAbleToValidateIsNullableIfConnectionValidationsAreTurnedOf({ assert }: Context) {
Config.set('database.connections.fake.validations', false)

Mock.when(Database.driver, 'find').resolve({ id: '3' })

await assert.doesNotReject(
() => User.query().setAttributes(false).uniqueValidation(false).create(),
NullableValueException
)
}

@Test()
public async shouldNotBeAbleToValidateIsUniqueIfConnectionValidationsAreTurnedOf({ assert }: Context) {
Config.set('database.connections.fake.validations', false)

Mock.when(Database.driver, 'find').resolve({ id: '3' })

await assert.doesNotReject(() => User.query().uniqueValidation(true).create(), UniqueValueException)
}

@Test()
public async shouldBeAbleToCreateDataAndSetDefaultTimestamps({ assert }: Context) {
const dataToCreate = { name: 'New User' }
Expand Down
15 changes: 15 additions & 0 deletions tests/unit/models/schemas/ModelSchemaTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,21 @@ export default class ModelSchemaTest {
assert.deepEqual(connection, 'fake')
}

@Test()
public async shouldBeAbleToGetModelDriverName({ assert }: Context) {
Config.set('database.default', 'fake')
Config.set('database.connections.fake.driver', 'fake')

class User extends BaseModel {
@Column()
public id: string
}

const driver = new ModelSchema(User).getModelDriverName()

assert.deepEqual(driver, 'fake')
}

@Test()
public async shouldBeAbleToGetModelColumnByProperty({ assert }: Context) {
class User extends BaseModel {
Expand Down

0 comments on commit 7daf44a

Please sign in to comment.