Skip to content

Commit

Permalink
refactor: use count query for exists / unique validators (#710)
Browse files Browse the repository at this point in the history
  • Loading branch information
mastermunj committed Jul 22, 2021
1 parent 447255e commit 7beaa79
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Bindings/Validator.ts
Expand Up @@ -157,7 +157,7 @@ class DbRowCheck {
{ pointer, errorReporter, arrayExpressionPointer, refs }: ValidationRuntimeOptions
) {
const client = this.database.connection(connection)
const query = client.from(table)
const query = client.from(table).select(1)

/**
* Convert datetime to a string
Expand Down
23 changes: 23 additions & 0 deletions test/bindings/validator.spec.ts
Expand Up @@ -77,6 +77,7 @@ test.group('Validator | exists', (group) => {
const { sql: knexSql, bindings: knexBindings } = db
.connection()
.getReadClient()
.select(1)
.from('users')
.where('id', userId)
.limit(1)
Expand Down Expand Up @@ -114,6 +115,7 @@ test.group('Validator | exists', (group) => {
const { sql: knexSql, bindings: knexBindings } = db
.connection()
.getReadClient()
.select(1)
.from('users')
.where('id', userId)
.where('username', 'nikk')
Expand Down Expand Up @@ -161,6 +163,7 @@ test.group('Validator | exists', (group) => {
const { sql: knexSql, bindings: knexBindings } = db
.connection()
.getReadClient()
.select(1)
.from('users')
.where('id', userId)
.where('username', 'nikk')
Expand Down Expand Up @@ -213,6 +216,7 @@ test.group('Validator | exists', (group) => {
const { sql: knexSql, bindings: knexBindings } = db
.connection()
.getReadClient()
.select(1)
.from('users')
.where('id', userId)
.whereIn('username', ['nikk', 'romain'])
Expand Down Expand Up @@ -260,6 +264,7 @@ test.group('Validator | exists', (group) => {
const { sql: knexSql, bindings: knexBindings } = db
.connection()
.getReadClient()
.select(1)
.from('users')
.where('id', userId)
.whereIn('username', ['nikk', 'romain'])
Expand Down Expand Up @@ -312,6 +317,7 @@ test.group('Validator | exists', (group) => {
const { sql: knexSql, bindings: knexBindings } = db
.connection()
.getReadClient()
.select(1)
.from('users')
.where('id', userId)
.whereNot('username', 'virk')
Expand Down Expand Up @@ -359,6 +365,7 @@ test.group('Validator | exists', (group) => {
const { sql: knexSql, bindings: knexBindings } = db
.connection()
.getReadClient()
.select(1)
.from('users')
.where('id', userId)
.whereNot('username', 'virk')
Expand Down Expand Up @@ -411,6 +418,7 @@ test.group('Validator | exists', (group) => {
const { sql: knexSql, bindings: knexBindings } = db
.connection()
.getReadClient()
.select(1)
.from('users')
.where('id', userId)
.whereNotIn('username', ['virk', 'nikk'])
Expand Down Expand Up @@ -458,6 +466,7 @@ test.group('Validator | exists', (group) => {
const { sql: knexSql, bindings: knexBindings } = db
.connection()
.getReadClient()
.select(1)
.from('users')
.where('id', userId)
.whereNotIn('username', ['virk', 'nikk'])
Expand Down Expand Up @@ -507,6 +516,7 @@ test.group('Validator | exists', (group) => {
const { sql: knexSql, bindings: knexBindings } = db
.connection()
.getReadClient()
.select(1)
.from('users')
.whereRaw(`lower(username) = ?`, [db.connection().knexRawQuery(`lower(?)`, ['VIRK'])])
.limit(1)
Expand Down Expand Up @@ -572,6 +582,7 @@ test.group('Validator | exists', (group) => {

const knexQuery = client
.getReadClient()
.select(1)
.from('users')
.where(
'created_at',
Expand Down Expand Up @@ -624,6 +635,7 @@ test.group('Validator | exists', (group) => {
const knexQuery = db
.connection()
.getReadClient()
.select(1)
.from('users')
.where('created_at', '2020-10-20')
.limit(1)
Expand Down Expand Up @@ -732,6 +744,7 @@ test.group('Validator | unique', (group) => {
const { sql: knexSql, bindings: knexBindings } = db
.connection()
.getReadClient()
.select(1)
.from('users')
.where('id', userId)
.where('username', 'virk')
Expand Down Expand Up @@ -779,6 +792,7 @@ test.group('Validator | unique', (group) => {
const { sql: knexSql, bindings: knexBindings } = db
.connection()
.getReadClient()
.select(1)
.from('users')
.where('id', userId)
.where('username', 'virk')
Expand Down Expand Up @@ -831,6 +845,7 @@ test.group('Validator | unique', (group) => {
const { sql: knexSql, bindings: knexBindings } = db
.connection()
.getReadClient()
.select(1)
.from('users')
.where('id', userId)
.whereIn('username', ['virk', 'nikk'])
Expand Down Expand Up @@ -878,6 +893,7 @@ test.group('Validator | unique', (group) => {
const { sql: knexSql, bindings: knexBindings } = db
.connection()
.getReadClient()
.select(1)
.from('users')
.where('id', userId)
.whereIn('username', ['virk', 'nikk'])
Expand Down Expand Up @@ -930,6 +946,7 @@ test.group('Validator | unique', (group) => {
const { sql: knexSql, bindings: knexBindings } = db
.connection()
.getReadClient()
.select(1)
.from('users')
.where('id', userId)
.whereNot('username', 'nikk')
Expand Down Expand Up @@ -977,6 +994,7 @@ test.group('Validator | unique', (group) => {
const { sql: knexSql, bindings: knexBindings } = db
.connection()
.getReadClient()
.select(1)
.from('users')
.where('id', userId)
.whereNot('username', 'nikk')
Expand Down Expand Up @@ -1029,6 +1047,7 @@ test.group('Validator | unique', (group) => {
const { sql: knexSql, bindings: knexBindings } = db
.connection()
.getReadClient()
.select(1)
.from('users')
.where('id', userId)
.whereNotIn('country_id', [1, 2])
Expand Down Expand Up @@ -1076,6 +1095,7 @@ test.group('Validator | unique', (group) => {
const { sql: knexSql, bindings: knexBindings } = db
.connection()
.getReadClient()
.select(1)
.from('users')
.where('id', userId)
.whereNotIn('country_id', [1, 2])
Expand Down Expand Up @@ -1124,6 +1144,7 @@ test.group('Validator | unique', (group) => {
const { sql: knexSql, bindings: knexBindings } = db
.connection()
.getReadClient()
.select(1)
.from('users')
.whereRaw(`lower(username) = ?`, [db.connection().knexRawQuery(`lower(?)`, ['VIRK'])])
.limit(1)
Expand Down Expand Up @@ -1199,6 +1220,7 @@ test.group('Validator | unique', (group) => {

const knexQuery = client
.getReadClient()
.select(1)
.from('users')
.where(
'created_at',
Expand Down Expand Up @@ -1255,6 +1277,7 @@ test.group('Validator | unique', (group) => {
const knexQuery = db
.connection()
.getReadClient()
.select(1)
.from('users')
.where('created_at', '2020-10-20')
.limit(1)
Expand Down

0 comments on commit 7beaa79

Please sign in to comment.