-
-
Notifications
You must be signed in to change notification settings - Fork 198
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: allow unique and exists validations to perform case insensitive…
… search
- Loading branch information
1 parent
62a1ba5
commit 761f823
Showing
5 changed files
with
622 additions
and
69 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,12 @@ | ||
/* | ||
* @adonisjs/lucid | ||
* | ||
* (c) Harminder Virk <[email protected]> | ||
* (c) AdonisJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import type { FieldContext } from '@vinejs/vine/types' | ||
import type { ApplicationService } from '@adonisjs/core/types' | ||
|
||
import { Database } from '../src/database/main.js' | ||
|
@@ -16,7 +15,7 @@ import { QueryClient } from '../src/query_client/index.js' | |
import { BaseModel } from '../src/orm/base_model/index.js' | ||
import { DatabaseTestUtils } from '../src/test_utils/database.js' | ||
import type { DatabaseConfig, DbQueryEventNode } from '../src/types/database.js' | ||
import { DatabaseQueryBuilderContract } from '../src/types/querybuilder.js' | ||
import { VineDbSearchCallback, VineDbSearchOptions } from '../src/types/vine.js' | ||
|
||
/** | ||
* Extending AdonisJS types | ||
|
@@ -40,43 +39,40 @@ declare module '@adonisjs/core/test_utils' { | |
* Extending VineJS schema types | ||
*/ | ||
declare module '@vinejs/vine' { | ||
interface VineLucidBindings { | ||
interface VineLucidBindings<ValueType> { | ||
/** | ||
* Ensure the value is unique inside the database by table and column name. | ||
* Optionally, you can define a filter to narrow down the query. | ||
*/ | ||
unique(options: VineDbSearchOptions): this | ||
|
||
/** | ||
* Ensure the value is unique inside the database by self | ||
* executing a query. | ||
* | ||
* - The callback must return "true", if the value is unique (does not exist). | ||
* - The callback must return "false", if the value is not unique (already exists). | ||
*/ | ||
unique(callback: (db: Database, value: string, field: FieldContext) => Promise<boolean>): this | ||
unique(callback: VineDbSearchCallback<ValueType>): this | ||
|
||
/** | ||
* Ensure the value is unique inside the database by table and column name. | ||
* Ensure the value exists inside the database by table and column name. | ||
* Optionally, you can define a filter to narrow down the query. | ||
*/ | ||
unique(options: { | ||
table: string | ||
column?: string | ||
filter?: ( | ||
db: DatabaseQueryBuilderContract, | ||
value: unknown, | ||
field: FieldContext | ||
) => Promise<void> | ||
}): this | ||
exists(options: VineDbSearchOptions): this | ||
|
||
/** | ||
* Ensure the value is exists inside the database by self | ||
* Ensure the value exists inside the database by self | ||
* executing a query. | ||
* | ||
* - The callback must return "false", if the value exists. | ||
* - The callback must return "true", if the value does not exist. | ||
*/ | ||
exists(callback: (db: Database, value: string, field: FieldContext) => Promise<boolean>): this | ||
exists(callback: VineDbSearchCallback<ValueType>): this | ||
} | ||
|
||
interface VineNumber extends VineLucidBindings {} | ||
|
||
interface VineString extends VineLucidBindings {} | ||
interface VineNumber extends VineLucidBindings<number> {} | ||
interface VineString extends VineLucidBindings<string> {} | ||
} | ||
|
||
/** | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* @adonisjs/lucid | ||
* | ||
* (c) AdonisJS | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
import type { FieldContext } from '@vinejs/vine/types' | ||
import type { Database } from '../database/main.js' | ||
import type { DatabaseQueryBuilderContract } from './querybuilder.js' | ||
|
||
/** | ||
* Options for the unique and the exists validations | ||
*/ | ||
export type VineDbSearchOptions = { | ||
/** | ||
* Database table for the query | ||
*/ | ||
table: string | ||
|
||
/** | ||
* The column against which to search the value | ||
*/ | ||
column: string | ||
|
||
/** | ||
* Specify a custom connection for the query | ||
*/ | ||
connection?: string | ||
|
||
/** | ||
* Enable to perform a case insensitive search on the column. The | ||
* current value and the existing value in the database will be | ||
* lowercased using the "lower" function | ||
* | ||
* https://www.sqlite.org/lang_corefunc.html#lower | ||
* https://docs.aws.amazon.com/redshift/latest/dg/r_LOWER.html | ||
* https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_lower | ||
* https://www.postgresql.org/docs/9.1/functions-string.html | ||
* https://docs.microsoft.com/en-us/sql/t-sql/functions/lower-transact-sql?view=sql-server-ver15 | ||
* https://coderwall.com/p/6yhsuq/improve-case-insensitive-queries-in-postgres-using-smarter-indexes | ||
*/ | ||
caseInsensitive?: boolean | ||
|
||
/** | ||
* Apply a custom filter to the query builder | ||
*/ | ||
filter?: ( | ||
db: DatabaseQueryBuilderContract, | ||
value: string, | ||
field: FieldContext | ||
) => void | Promise<void> | ||
} | ||
|
||
/** | ||
* Callback to self execute the query for the unique and the | ||
* exists validations | ||
*/ | ||
export type VineDbSearchCallback<ValueType> = ( | ||
db: Database, | ||
value: ValueType, | ||
field: FieldContext | ||
) => Promise<boolean> |
Oops, something went wrong.