├── .eslintignore
├── .eslintrc.cjs
├── .gitignore
├── .npmrc
├── .prettierignore
├── .prettierrc
├── .vscode
└── settings.json
├── README.md
├── documentation
├── README.md
├── mysql.md
├── pg.md
└── sqlite.md
├── package.json
├── pnpm-lock.yaml
├── sql
├── nanoid.sql
└── search-path.sql
├── src
├── expressions.ts
├── functions.ts
├── index.ts
├── internals.ts
├── mysql
│ ├── functions.ts
│ └── index.ts
├── operators.ts
├── pg
│ ├── citext
│ │ ├── README.md
│ │ ├── custom-types.ts
│ │ └── index.ts
│ ├── constants.ts
│ ├── cube
│ │ ├── README.md
│ │ ├── constants.ts
│ │ ├── custom-types.ts
│ │ ├── functions.ts
│ │ ├── index.ts
│ │ └── operators.ts
│ ├── custom-types.ts
│ ├── functions.ts
│ ├── fuzzystrmatch
│ │ ├── README.md
│ │ ├── functions.ts
│ │ └── index.ts
│ ├── index.ts
│ ├── internals.ts
│ ├── operators.ts
│ ├── pgcrypto
│ │ ├── README.md
│ │ ├── functions.ts
│ │ └── index.ts
│ ├── pgtrgm
│ │ ├── README.md
│ │ ├── index.ts
│ │ └── operators.ts
│ ├── postgis
│ │ ├── README.md
│ │ ├── constants.ts
│ │ ├── custom-types.ts
│ │ ├── functions.ts
│ │ ├── index.ts
│ │ └── operators.ts
│ ├── utilities.ts
│ ├── validation.ts
│ └── values.ts
├── sqlite
│ ├── functions.ts
│ └── index.ts
├── utilities.ts
└── values.ts
├── tsconfig.json
├── tsup.config.ts
└── typedoc.json
/.eslintignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | node_modules
3 | /dist
4 | .env
5 | .env.*
6 | !.env.example
7 |
8 | # Ignore files for PNPM, NPM and YARN
9 | pnpm-lock.yaml
10 | package-lock.json
11 | yarn.lock
12 |
--------------------------------------------------------------------------------
/.eslintrc.cjs:
--------------------------------------------------------------------------------
1 | module.exports = {
2 | root: true,
3 | extends: ['eslint:recommended', 'plugin:@typescript-eslint/recommended', 'prettier'],
4 | parser: '@typescript-eslint/parser',
5 | plugins: ['@typescript-eslint', 'eslint-plugin-drizzle'],
6 | parserOptions: {
7 | sourceType: 'module',
8 | ecmaVersion: 2020,
9 | },
10 | env: {
11 | browser: true,
12 | es2017: true,
13 | node: true,
14 | },
15 | rules: {
16 | 'curly': ['error', 'all'],
17 | '@typescript-eslint/consistent-type-imports': 'error',
18 | '@typescript-eslint/no-unused-vars': [
19 | 'error',
20 | { ignoreRestSiblings: true, destructuredArrayIgnorePattern: '^_' },
21 | ],
22 | },
23 | };
24 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | node_modules
2 | dist
3 |
--------------------------------------------------------------------------------
/.npmrc:
--------------------------------------------------------------------------------
1 | engine-strict=true
2 | resolution-mode=highest
3 |
--------------------------------------------------------------------------------
/.prettierignore:
--------------------------------------------------------------------------------
1 | .DS_Store
2 | node_modules
3 | /dist
4 | .env
5 | .env.*
6 | !.env.example
7 |
8 | # Ignore files for PNPM, NPM and YARN
9 | pnpm-lock.yaml
10 | package-lock.json
11 | yarn.lock
12 |
--------------------------------------------------------------------------------
/.prettierrc:
--------------------------------------------------------------------------------
1 | {
2 | "useTabs": true,
3 | "tabWidth": 2,
4 | "semi": true,
5 | "singleQuote": true,
6 | "trailingComma": "es5",
7 | "printWidth": 100,
8 | "plugins": [
9 | "./node_modules/prettier-plugin-jsdoc/dist/index.js"
10 | ],
11 | "pluginSearchDirs": [
12 | "."
13 | ],
14 | "proseWrap": "always",
15 | "overrides": [
16 | {
17 | "files": [
18 | "*.md"
19 | ],
20 | "options": {
21 | "useTabs": false
22 | }
23 | }
24 | ],
25 | "bracketSpacing": true,
26 | "bracketSameLine": false,
27 | "arrowParens": "always",
28 | "svelteIndentScriptAndStyle": true,
29 | "svelteAllowShorthand": true,
30 | "jsdocDescriptionWithDot": true,
31 | "jsdocPreferCodeFences": true,
32 | "jsdocCommentLineStrategy": "multiline",
33 | "tsdoc": true,
34 | "quoteProps": "consistent",
35 | "embeddedLanguageFormatting": "auto",
36 | "htmlWhitespaceSensitivity": "ignore"
37 | }
--------------------------------------------------------------------------------
/.vscode/settings.json:
--------------------------------------------------------------------------------
1 | {
2 | "editor.formatOnPaste": false,
3 | "editor.formatOnSave": true,
4 | "editor.defaultFormatter": "esbenp.prettier-vscode",
5 | "editor.codeActionsOnSave": {
6 | "source.organizeImports": "explicit",
7 | "source.fixAll": "explicit"
8 | },
9 | "markdownlint.config": {
10 | "default": true,
11 | "MD033": false
12 | },
13 | "[sql]": {
14 | "editor.formatOnSave": false,
15 | "editor.formatOnPaste": false,
16 | "editor.formatOnType": false
17 | },
18 | "typescript.tsdk": "node_modules/typescript/lib",
19 | "typescript.preferences.quoteStyle": "single"
20 | }
21 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 |
Drizzle ORM Helpers
2 |
3 |
4 | ⚠️ Use at your own risk ⚠️
5 |
6 |
7 | 🚧 This package is under construction, expect some things to be broken! 🚧
8 |
9 |
10 |
11 |
12 | This is a collection of unofficial typescript-friendly helpers for use with Drizzle ORM. Provided
13 | items include common SQL/dialect-specific values, functions, operators, and column types. Note that
14 | most the work is currently oriented towards Postgres and that most "solutions" provided by this
15 | package should be seen as provisory, in wait of being replaced by implementations provided
16 | officially and with more stability through Drizzle-ORM package(s).
17 |
18 | ## Documentation
19 |
20 | A crude auto-generated
21 | [**documentation** is available here](https://github.com/iolyd/drizzle-orm-helpers/blob/main/documentation/README.md).
22 |
23 | ## Examples
24 |
25 | ### Aggregating translations
26 |
27 | ```ts
28 | const APP_LANGUAGES = ['fr', 'en', 'es'] as const;
29 | type AppLanguage = (typeof APP_LANGUAGES)[number]; // 'fr' | 'en' | 'es';
30 |
31 | const languages = pgTable('languages', {
32 | lang: textenum('lang', { enum: APP_LANGUAGES }),
33 | });
34 |
35 | const projects = pgTable('projects', {
36 | id: text('id')
37 | .default(nanoid({ size: 12 }))
38 | .primaryKey(),
39 | createdById: text('created_by_id').references(() => users.id, {
40 | onDelete: 'cascade',
41 | onUpdate: 'cascade',
42 | }),
43 | });
44 |
45 | const projectsTranslations = pgTable(
46 | 'projects_t',
47 | {
48 | id: text('id')
49 | .references(() => projects.id, { onDelete: 'cascade', onUpdate: 'cascade' })
50 | .notNull(),
51 | lang: textenum('lang', { enum: APP_LANGUAGES })
52 | .references(() => languages.lang, { onDelete: 'cascade', onUpdate: 'cascade' })
53 | .notNull(),
54 | title: text('title'),
55 | description: text('description'),
56 | },
57 | (table) => {
58 | return {
59 | pk: primaryKey({ columns: [table.id, table.lang] }),
60 | };
61 | }
62 | );
63 |
64 | /**
65 | * Build a json object aggregating translations with languages as keys and joined columns as nested
66 | * properties.
67 | */
68 | export function aggTranslations(selection: TSelection) {
69 | return jsonObjectAgg(languages.lang, jsonBuildObject(selection));
70 | }
71 |
72 | /**
73 | * Join translations through the languages table.
74 | */
75 | export function joinTranslations<
76 | TSelect extends PgSelect,
77 | TTranslations extends
78 | | (AnyTable & LangColumn)
79 | | (Subquery & LangColumn),
80 | >(select: TSelect, translations: TTranslations, on: SQL) {
81 | return select
82 | .leftJoin(languages, $true)
83 | .leftJoin(translations, and(on, eq(languages.lang, translations.lang)));
84 | }
85 |
86 | const projectsWithTranslations = await joinTranslations(
87 | db
88 | .select({
89 | ...getColumns(projects),
90 | translations: aggTranslations(getColumns(projectsTranslations)),
91 | })
92 | .from(projects),
93 | projectsTranslations,
94 | eq(projects.id, projectsTranslations.id)
95 | );
96 | ```
97 |
98 | Would return aggregated data with expected types as:
99 |
100 | ```json
101 | [
102 | {
103 | id: string,
104 | created_by_id: string,
105 | translations: {
106 | fr: {
107 | id: string,
108 | lang: string,
109 | title?: string,
110 | description?: string
111 | },
112 | en: {
113 | id: string,
114 | lang: string,
115 | title?: string,
116 | description?: string
117 | }
118 | es: {
119 | id: string,
120 | lang: string,
121 | title?: string,
122 | description?: string
123 | }
124 | }
125 | },
126 | //...
127 | ]
128 | ```
129 |
--------------------------------------------------------------------------------
/documentation/README.md:
--------------------------------------------------------------------------------
1 | # src
2 |
3 | ## Table of Contents
4 |
5 | - [Type Aliases](#type-aliases)
6 | - [AnySelect](#anyselect)
7 | - [InferColumnType\](#infercolumntypet)
8 | - [InferColumns\](#infercolumnst)
9 | - [InferData\](#inferdatat)
10 | - [InferNameOrAlias\](#infernameoraliast)
11 | - [Schema](#schema)
12 | - [Select](#select)
13 | - [SubqueryWithSelection\](#subquerywithselectiontselection-tname)
14 | - [WithSubqueryWithSelection\](#withsubquerywithselectiontselection-talias)
15 | - [Variables](#variables)
16 | - [$currentTimestamp](#currenttimestamp)
17 | - [$false](#false)
18 | - [$null](#null)
19 | - [$true](#true)
20 | - [Functions](#functions)
21 | - [$boolean()](#boolean)
22 | - [add()](#add)
23 | - [cases()](#cases)
24 | - [coalesce()](#coalesce)
25 | - [distinct()](#distinct)
26 | - [divide()](#divide)
27 | - [getColumns()](#getcolumns)
28 | - [getNameOrAlias()](#getnameoralias)
29 | - [greatest()](#greatest)
30 | - [least()](#least)
31 | - [multiply()](#multiply)
32 | - [nullIf()](#nullif)
33 | - [paginate()](#paginate)
34 | - [subtract()](#subtract)
35 |
36 | ## Type Aliases
37 |
38 |
39 |
40 | ### AnySelect
41 |
42 | ```ts
43 | type AnySelect: SetOptional;
44 | ```
45 |
46 | Dialect agnostic AnySelect.
47 |
48 | #### See
49 |
50 | - AnyPgSelect
51 | - AnyMySqlSelect
52 | - AnySQLiteSelect
53 |
54 | ---
55 |
56 |
57 |
58 | ### InferColumnType\
59 |
60 | ```ts
61 | type InferColumnType: AnyColumn["_"], "data" | "dataType">>;
62 | ```
63 |
64 | Infer type of table column.
65 |
66 | #### Type parameters
67 |
68 | | Type parameter |
69 | | :------------------------------------------------------------- |
70 | | `T` _extends_ (...`config`: `never`\[]) => `ColumnBuilderBase` |
71 |
72 | ---
73 |
74 |
75 |
76 | ### InferColumns\
77 |
78 | ```ts
79 | type InferColumns: T extends Table ? T["_"]["columns"] : T extends View | Subquery | WithSubquery | AnySelect ? T["_"]["selectedFields"] : never;
80 | ```
81 |
82 | Infer table columns or (sub)query fields.
83 |
84 | #### Type parameters
85 |
86 | | Type parameter |
87 | | :------------------------------------ |
88 | | `T` _extends_ |
89 | | \| `Table` |
90 | | \| `View` |
91 | | \| `Subquery` |
92 | | \| `WithSubquery` |
93 | | \| [`AnySelect`](README.md#anyselect) |
94 |
95 | ---
96 |
97 |
98 |
99 | ### InferData\
100 |
101 | ```ts
102 | type InferData: T extends Table ? InferSelectModel : T extends Column ? T["_"]["notNull"] extends true ? T["_"]["data"] : T["_"]["data"] | null : T extends View | Subquery ? T["_"]["selectedFields"] : T extends SQL ? U : T extends SQL.Aliased ? U : unknown;
103 | ```
104 |
105 | Infer any SQL wrapper's expected return data type.
106 |
107 | #### Type parameters
108 |
109 | | Type parameter |
110 | | :------------------------- |
111 | | `T` _extends_ `SQLWrapper` |
112 |
113 | ---
114 |
115 |
116 |
117 | ### InferNameOrAlias\
118 |
119 | ```ts
120 | type InferNameOrAlias: T extends Table | View | Column ? T["_"]["name"] : T extends Subquery | WithSubquery ? T["_"]["alias"] : T extends AnySelect ? T["_"]["tableName"] : T extends SQL.Aliased ? T["fieldAlias"] : T extends Placeholder ? T["name"] : undefined;
121 | ```
122 |
123 | Infer a table's name or a (sub)query's alias.
124 |
125 | #### Type parameters
126 |
127 | | Type parameter |
128 | | :------------------------- |
129 | | `T` _extends_ `SQLWrapper` |
130 |
131 | ---
132 |
133 |
134 |
135 | ### Schema
136 |
137 | ```ts
138 | type Schema: PgSchema | MySqlSchema;
139 | ```
140 |
141 | Dialect-agnostic schema. Excludes SQLite.
142 |
143 | ---
144 |
145 |
146 |
147 | ### Select
148 |
149 | ```ts
150 | type Select: SetOptional;
151 | ```
152 |
153 | Dialect agnostic select.
154 |
155 | #### See
156 |
157 | - PgSelect.
158 | - MySqlSelect
159 | - SQLiteSelect
160 |
161 | ---
162 |
163 |
164 |
165 | ### SubqueryWithSelection\
166 |
167 | ```ts
168 | type SubqueryWithSelection: MySqlSubqueryWithSelection | PgSubqueryWithSelection | SQLiteSubqueryWithSelection;
169 | ```
170 |
171 | Dialect-agnostic subquery with selection.
172 |
173 | #### Type parameters
174 |
175 | | Type parameter |
176 | | :---------------------------------------- |
177 | | `TSelection` _extends_ `ColumnsSelection` |
178 | | `TName` _extends_ `string` |
179 |
180 | ---
181 |
182 |
183 |
184 | ### WithSubqueryWithSelection\
185 |
186 | ```ts
187 | type WithSubqueryWithSelection: PgWithSubqueryWithSelection | SQLiteWithSubqueryWithSelection | MySqlWithSubqueryWithSelection;
188 | ```
189 |
190 | Dialect-agnostic with subquery with selection.
191 |
192 | #### Type parameters
193 |
194 | | Type parameter |
195 | | :---------------------------------------- |
196 | | `TSelection` _extends_ `ColumnsSelection` |
197 | | `TAlias` _extends_ `string` |
198 |
199 | ## Variables
200 |
201 |
202 |
203 | ### $currentTimestamp
204 |
205 | ```ts
206 | const $currentTimestamp: SQL;
207 | ```
208 |
209 | #### Example
210 |
211 | ```sql
212 | current_timestamp();
213 | ```
214 |
215 | ---
216 |
217 |
218 |
219 | ### $false
220 |
221 | ```ts
222 | const $false: SQL;
223 | ```
224 |
225 | SQL template false value.
226 |
227 | ---
228 |
229 |
230 |
231 | ### $null
232 |
233 | ```ts
234 | const $null: SQL;
235 | ```
236 |
237 | SQL template null value.
238 |
239 | ---
240 |
241 |
242 |
243 | ### $true
244 |
245 | ```ts
246 | const $true: SQL;
247 | ```
248 |
249 | SQL template true value.
250 |
251 | ## Functions
252 |
253 |
254 |
255 | ### $boolean()
256 |
257 | ```ts
258 | function $boolean(value: T): SQL;
259 | ```
260 |
261 | SQL template boolean value.
262 |
263 | #### Type parameters
264 |
265 | | Type parameter |
266 | | :---------------------- |
267 | | `T` _extends_ `boolean` |
268 |
269 | #### Parameters
270 |
271 | | Parameter | Type |
272 | | :-------- | :--- |
273 | | `value` | `T` |
274 |
275 | #### Returns
276 |
277 | `SQL`\<`T`>
278 |
279 | ---
280 |
281 |
282 |
283 | ### add()
284 |
285 | ```ts
286 | function add(...values: T): SQL : T[number]>;
287 | ```
288 |
289 | Add values.
290 |
291 | #### Type parameters
292 |
293 | | Type parameter |
294 | | :------------------------------------------ |
295 | | `T` _extends_ (`number` \| `SQLWrapper`)\[] |
296 |
297 | #### Parameters
298 |
299 | | Parameter | Type |
300 | | :---------- | :--- |
301 | | ...`values` | `T` |
302 |
303 | #### Returns
304 |
305 | `SQL`\<`T`\[`number`] _extends_ `SQLWrapper` ? [`InferData`](README.md#inferdatat)\<`any`\[`any`]> :
306 | `T`\[`number`]>
307 |
308 | ---
309 |
310 |
311 |
312 | ### cases()
313 |
314 | ```ts
315 | function cases(conditionals: C, fallback?: F): SQL;
316 | ```
317 |
318 | Case condition chain.
319 |
320 | #### Type parameters
321 |
322 | | Type parameter | Value |
323 | | :-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | :--------------------------------------------------- |
324 | | `C` _extends_ (`undefined` \| \[`SQLWrapper`, `unknown`])\[] | - |
325 | | `F` | - |
326 | | `T` | `NonUndefinable`\<`TupleToUnion`\<`C`>> |
327 | | `R` | `T` _extends_ \[`T0`, `T1`] ? `T0` _extends_ `SQL`\< |
328 | | \| `null` | |
329 | | \| `false` | |
330 | | \| `0` | |
331 | | \| `"0"` | |
332 | | \| `"f"` | |
333 | | \| `"F"`> ? `never` : `T1` _extends_ `SQLWrapper` ? [`InferData`](README.md#inferdatat)\<`T1`\<`T1`>> : `T1` : `never` \| `F` _extends_ `void` ? `never` : `F` _extends_ `SQLWrapper` ? [`InferData`](README.md#inferdatat)\<`F`\<`F`>> : `F` | |
334 |
335 | #### Parameters
336 |
337 | | Parameter | Type |
338 | | :------------- | :--- |
339 | | `conditionals` | `C` |
340 | | `fallback`? | `F` |
341 |
342 | #### Returns
343 |
344 | `SQL`\<`R`>
345 |
346 | #### Examples
347 |
348 | ```ts
349 | cases([[eq(thing, other), 2]], 3);
350 | ```
351 |
352 | ```sql
353 | CASE
354 | WHEN thing = other THEN 2
355 | ELSE 3
356 | END;
357 | ```
358 |
359 | #### Todo
360 |
361 | Implement smarter typing to identify confirmable early returns with truthy conditions.
362 |
363 | ---
364 |
365 |
366 |
367 | ### coalesce()
368 |
369 | ```ts
370 | function coalesce(...values: [...T[]]): SQL>;
371 | ```
372 |
373 | #### Type parameters
374 |
375 | | Type parameter |
376 | | :------------------------- |
377 | | `T` _extends_ `unknown`\[] |
378 |
379 | #### Parameters
380 |
381 | | Parameter | Type |
382 | | :---------- | :---------- |
383 | | ...`values` | \[`...T[]`] |
384 |
385 | #### Returns
386 |
387 | `SQL`\<`CoalesceSQL`\<`T`, `true`, `never`>>
388 |
389 | #### See
390 |
391 | [https://www.postgresql.org/docs/current/functions-conditional.html#FUNCTIONS-COALESCE-NVL-IFNULL](https://www.postgresql.org/docs/current/functions-conditional.html#FUNCTIONS-COALESCE-NVL-IFNULL)
392 |
393 | ---
394 |
395 |
396 |
397 | ### distinct()
398 |
399 | ```ts
400 | function distinct(statement: T): SQL>;
401 | ```
402 |
403 | Distinct keyword.
404 |
405 | #### Type parameters
406 |
407 | | Type parameter |
408 | | :------------------------- |
409 | | `T` _extends_ `SQLWrapper` |
410 |
411 | #### Parameters
412 |
413 | | Parameter | Type |
414 | | :---------- | :--- |
415 | | `statement` | `T` |
416 |
417 | #### Returns
418 |
419 | `SQL`\<[`InferData`](README.md#inferdatat)\<`T`>>
420 |
421 | ---
422 |
423 |
424 |
425 | ### divide()
426 |
427 | ```ts
428 | function divide(
429 | ...values: T
430 | ): SQL : T[number]>;
431 | ```
432 |
433 | Divide values.
434 |
435 | #### Type parameters
436 |
437 | | Type parameter |
438 | | :------------------------------------------ |
439 | | `T` _extends_ (`number` \| `SQLWrapper`)\[] |
440 |
441 | #### Parameters
442 |
443 | | Parameter | Type |
444 | | :---------- | :--- |
445 | | ...`values` | `T` |
446 |
447 | #### Returns
448 |
449 | `SQL`\<`T`\[`number`] _extends_ `SQLWrapper` ? [`InferData`](README.md#inferdatat)\<`any`\[`any`]> :
450 | `T`\[`number`]>
451 |
452 | ---
453 |
454 |
455 |
456 | ### getColumns()
457 |
458 | ```ts
459 | function getColumns(table: T): InferColumns;
460 | ```
461 |
462 | Should replace `getTableColumns` to allow for more input versatility.
463 |
464 | #### Type parameters
465 |
466 | | Type parameter |
467 | | :---------------------------------------------------------------- |
468 | | `T` _extends_ |
469 | | \| `Table`\<`TableConfig`\<`Column`\<`any`, `object`, `object`>>> |
470 | | \| `View`\<`string`, `boolean`, `ColumnsSelection`> |
471 | | \| [`AnySelect`](README.md#anyselect) |
472 | | \| `Subquery`\<`string`, `ColumnsSelection`> |
473 | | \| `WithSubquery`\<`string`, `ColumnsSelection`> |
474 |
475 | #### Parameters
476 |
477 | | Parameter | Type |
478 | | :-------- | :--- |
479 | | `table` | `T` |
480 |
481 | #### Returns
482 |
483 | [`InferColumns`](README.md#infercolumnst)\<`T`>
484 |
485 | #### See
486 |
487 | [drizzle-team/drizzle-orm#1789](https://github.com/drizzle-team/drizzle-orm/pull/1789)
488 |
489 | ---
490 |
491 |
492 |
493 | ### getNameOrAlias()
494 |
495 | ```ts
496 | function getNameOrAlias(query: T): InferNameOrAlias;
497 | ```
498 |
499 | Get a table's name or a (sub)query's alias.
500 |
501 | #### Type parameters
502 |
503 | | Type parameter |
504 | | :------------------------- |
505 | | `T` _extends_ `SQLWrapper` |
506 |
507 | #### Parameters
508 |
509 | | Parameter | Type |
510 | | :-------- | :--- |
511 | | `query` | `T` |
512 |
513 | #### Returns
514 |
515 | [`InferNameOrAlias`](README.md#infernameoraliast)\<`T`>
516 |
517 | ---
518 |
519 |
520 |
521 | ### greatest()
522 |
523 | ```ts
524 | function greatest(
525 | ...values: [...T[]]
526 | ): SQL<
527 | {
528 | [I in string | number | symbol]: T[I] extends SQLWrapper ? InferData : T[I];
529 | }[number]
530 | >;
531 | ```
532 |
533 | #### Type parameters
534 |
535 | | Type parameter |
536 | | :------------------------- |
537 | | `T` _extends_ `unknown`\[] |
538 |
539 | #### Parameters
540 |
541 | | Parameter | Type |
542 | | :---------- | :---------- |
543 | | ...`values` | \[`...T[]`] |
544 |
545 | #### Returns
546 |
547 | `SQL`\<\{ \[I in string | number | symbol]: T\[I\] extends SQLWrapper ? InferData\ :
548 | T\[I\] }\[`number`]>
549 |
550 | #### See
551 |
552 | [https://www.postgresql.org/docs/current/functions-conditional.html#FUNCTIONS-GREATEST-LEAST](https://www.postgresql.org/docs/current/functions-conditional.html#FUNCTIONS-GREATEST-LEAST)
553 |
554 | ---
555 |
556 |
557 |
558 | ### least()
559 |
560 | ```ts
561 | function least(
562 | ...values: [...T[]]
563 | ): SQL<
564 | {
565 | [I in string | number | symbol]: T[I] extends SQLWrapper ? InferData : T[I];
566 | }[number]
567 | >;
568 | ```
569 |
570 | #### Type parameters
571 |
572 | | Type parameter |
573 | | :------------------------- |
574 | | `T` _extends_ `unknown`\[] |
575 |
576 | #### Parameters
577 |
578 | | Parameter | Type |
579 | | :---------- | :---------- |
580 | | ...`values` | \[`...T[]`] |
581 |
582 | #### Returns
583 |
584 | `SQL`\<\{ \[I in string | number | symbol]: T\[I\] extends SQLWrapper ? InferData\ :
585 | T\[I\] }\[`number`]>
586 |
587 | #### See
588 |
589 | [https://www.postgresql.org/docs/current/functions-conditional.html#FUNCTIONS-GREATEST-LEAST](https://www.postgresql.org/docs/current/functions-conditional.html#FUNCTIONS-GREATEST-LEAST)
590 |
591 | ---
592 |
593 |
594 |
595 | ### multiply()
596 |
597 | ```ts
598 | function multiply(
599 | ...values: T
600 | ): SQL : T[number]>;
601 | ```
602 |
603 | Multiply values.
604 |
605 | #### Type parameters
606 |
607 | | Type parameter |
608 | | :------------------------------------------ |
609 | | `T` _extends_ (`number` \| `SQLWrapper`)\[] |
610 |
611 | #### Parameters
612 |
613 | | Parameter | Type |
614 | | :---------- | :--- |
615 | | ...`values` | `T` |
616 |
617 | #### Returns
618 |
619 | `SQL`\<`T`\[`number`] _extends_ `SQLWrapper` ? [`InferData`](README.md#inferdatat)\<`any`\[`any`]> :
620 | `T`\[`number`]>
621 |
622 | ---
623 |
624 |
625 |
626 | ### nullIf()
627 |
628 | ```ts
629 | function nullIf(value: V, condition: C): SQL;
630 | ```
631 |
632 | Return null if value meets condition. Useful to coalesce to something else.
633 |
634 | #### Type parameters
635 |
636 | | Type parameter |
637 | | :------------------------- |
638 | | `V` _extends_ `SQLWrapper` |
639 | | `C` |
640 |
641 | #### Parameters
642 |
643 | | Parameter | Type |
644 | | :---------- | :--- |
645 | | `value` | `V` |
646 | | `condition` | `C` |
647 |
648 | #### Returns
649 |
650 | `SQL`\<`null` | `V`>
651 |
652 | #### See
653 |
654 | [https://www.postgresql.org/docs/current/functions-conditional.html#FUNCTIONS-NULLIF](https://www.postgresql.org/docs/current/functions-conditional.html#FUNCTIONS-NULLIF)
655 |
656 | ---
657 |
658 |
659 |
660 | ### paginate()
661 |
662 | ```ts
663 | function paginate(qb: T, __namedParameters: object): PgSelect | MySqlSelect | SQLiteSelect;
664 | ```
665 |
666 | Paginate a query.
667 |
668 | #### Type parameters
669 |
670 | | Type parameter |
671 | | :----------------------------------------- |
672 | | `T` _extends_ [`Select`](README.md#select) |
673 |
674 | #### Parameters
675 |
676 | | Parameter | Type |
677 | | :------------------------ | :------- |
678 | | `qb` | `T` |
679 | | `__namedParameters` | `object` |
680 | | `__namedParameters.page` | `number` |
681 | | `__namedParameters.size`? | `number` |
682 |
683 | #### Returns
684 |
685 | `PgSelect` | `MySqlSelect` | `SQLiteSelect`
686 |
687 | ---
688 |
689 |
690 |
691 | ### subtract()
692 |
693 | ```ts
694 | function subtract(
695 | ...values: T
696 | ): SQL : T[number]>;
697 | ```
698 |
699 | Subtract values.
700 |
701 | #### Type parameters
702 |
703 | | Type parameter |
704 | | :------------------------------------------ |
705 | | `T` _extends_ (`number` \| `SQLWrapper`)\[] |
706 |
707 | #### Parameters
708 |
709 | | Parameter | Type |
710 | | :---------- | :--- |
711 | | ...`values` | `T` |
712 |
713 | #### Returns
714 |
715 | `SQL`\<`T`\[`number`] _extends_ `SQLWrapper` ? [`InferData`](README.md#inferdatat)\<`any`\[`any`]> :
716 | `T`\[`number`]>
717 |
--------------------------------------------------------------------------------
/documentation/mysql.md:
--------------------------------------------------------------------------------
1 | # src/mysql
2 |
3 | ## Table of Contents
4 |
5 | - [Functions](#functions)
6 | - [random()](#random)
7 |
8 | ## Functions
9 |
10 |
11 |
12 | ### random()
13 |
14 | ```ts
15 | function random(): SQL;
16 | ```
17 |
18 | MySQL random function.
19 |
20 | #### Returns
21 |
22 | `SQL`\<`number`>
23 |
24 | Random number between 0 and 1.
25 |
26 | #### Example
27 |
28 | ```sql
29 | rand();
30 | ```
31 |
--------------------------------------------------------------------------------
/documentation/pg.md:
--------------------------------------------------------------------------------
1 | # src/pg
2 |
3 | ## Table of Contents
4 |
5 | - [Type Aliases](#type-aliases)
6 | - [IntervalUnit](#intervalunit)
7 | - [RangeBoundType](#rangeboundtype)
8 | - [RangeValue\](#rangevaluet)
9 | - [Regconfig](#regconfig)
10 | - [RegconfigString](#regconfigstring)
11 | - [Variables](#variables)
12 | - [$empty](#empty)
13 | - [$emptyArray](#emptyarray)
14 | - [$emptyJsonArray](#emptyjsonarray)
15 | - [$emptyJsonObject](#emptyjsonobject)
16 | - [$nullArray](#nullarray)
17 | - [INTERVAL_UNITS](#interval_units)
18 | - [INTERVAL_UNITS_ARR_ORDERED](#interval_units_arr_ordered)
19 | - [RANGE_BOUND_TYPES](#range_bound_types)
20 | - [RANGE_EMPTY](#range_empty)
21 | - [REGCONFIGS](#regconfigs)
22 | - [Functions](#functions)
23 | - [age()](#age)
24 | - [arrayAgg()](#arrayagg)
25 | - [boolAnd()](#booland)
26 | - [boolOr()](#boolor)
27 | - [citext()](#citext)
28 | - [contained()](#contained)
29 | - [contains()](#contains)
30 | - [cube()](#cube)
31 | - [cubeDim()](#cubedim)
32 | - [cubeDistance()](#cubedistance)
33 | - [cubeEnlarge()](#cubeenlarge)
34 | - [cubeInter()](#cubeinter)
35 | - [cubeIsPoint()](#cubeispoint)
36 | - [cubeLowerLeftCoord()](#cubelowerleftcoord)
37 | - [cubeSubset()](#cubesubset)
38 | - [cubeUnion()](#cubeunion)
39 | - [cubeUpperRightCoord()](#cubeupperrightcoord)
40 | - [daitch_mokotoff()](#daitch_mokotoff)
41 | - [daterange()](#daterange)
42 | - [daterangeSchema()](#daterangeschema)
43 | - [difference()](#difference)
44 | - [distance()](#distance)
45 | - [extract()](#extract)
46 | - [generatedTsvector()](#generatedtsvector)
47 | - [geography()](#geography)
48 | - [geometry()](#geometry)
49 | - [getCurrentTsConfig()](#getcurrenttsconfig)
50 | - [intrange()](#intrange)
51 | - [intrangeSchema()](#intrangeschema)
52 | - [isEmpty()](#isempty)
53 | - [jsonAgg()](#jsonagg)
54 | - [jsonAggBuildObject()](#jsonaggbuildobject)
55 | - [jsonBuildObject()](#jsonbuildobject)
56 | - [jsonObjectAgg()](#jsonobjectagg)
57 | - [jsonStripNulls()](#jsonstripnulls)
58 | - [jsonbBuildObject()](#jsonbbuildobject)
59 | - [jsonbObjectAgg()](#jsonbobjectagg)
60 | - [makeCube()](#makecube)
61 | - [nanoid()](#nanoid)
62 | - [now()](#now)
63 | - [numrange()](#numrange)
64 | - [numrangeSchema()](#numrangeschema)
65 | - [overlaps()](#overlaps)
66 | - [random()](#random)
67 | - [regconfig()](#regconfig-1)
68 | - [rowToJson()](#rowtojson)
69 | - [setweight()](#setweight)
70 | - [similar()](#similar)
71 | - [soundex()](#soundex)
72 | - [textenum()](#textenum)
73 | - [toExcluded()](#toexcluded)
74 | - [toInterval()](#tointerval)
75 | - [toJson()](#tojson)
76 | - [toJsonb()](#tojsonb)
77 | - [toRange()](#torange)
78 | - [toTsquery()](#totsquery)
79 | - [toTsvector()](#totsvector)
80 | - [ts()](#ts)
81 | - [tsrange()](#tsrange)
82 | - [tsrangeSchema()](#tsrangeschema)
83 | - [tsvector()](#tsvector)
84 |
85 | ## Type Aliases
86 |
87 |
88 |
89 | ### IntervalUnit
90 |
91 | ```ts
92 | type IntervalUnit: ValueOf;
93 | ```
94 |
95 | ---
96 |
97 |
98 |
99 | ### RangeBoundType
100 |
101 | ```ts
102 | type RangeBoundType: ValueOf;
103 | ```
104 |
105 | ---
106 |
107 |
108 |
109 | ### RangeValue\
110 |
111 | ```ts
112 | type RangeValue: object;
113 | ```
114 |
115 | #### Type parameters
116 |
117 | | Type parameter | Value |
118 | | :------------- | :----- |
119 | | `T` | `void` |
120 |
121 | #### Type declaration
122 |
123 | | Member | Type |
124 | | :------ | :------------ |
125 | | `lower` | `T` \| `null` |
126 | | `upper` | `T` \| `null` |
127 |
128 | ---
129 |
130 |
131 |
132 | ### Regconfig
133 |
134 | ```ts
135 | type Regconfig: ValueOf;
136 | ```
137 |
138 | ---
139 |
140 |
141 |
142 | ### RegconfigString
143 |
144 | ```ts
145 | type RegconfigString: Regconfig | string & NonNullable;
146 | ```
147 |
148 | ## Variables
149 |
150 |
151 |
152 | ### $empty
153 |
154 | ```ts
155 | const $empty: SQL<[] | "'empty'">;
156 | ```
157 |
158 | Postgres value returned for empty ranges.
159 |
160 | ---
161 |
162 |
163 |
164 | ### $emptyArray
165 |
166 | ```ts
167 | const $emptyArray: SQL<[]>;
168 | ```
169 |
170 | Empty SQL array (not json typed)
171 |
172 | ---
173 |
174 |
175 |
176 | ### $emptyJsonArray
177 |
178 | ```ts
179 | const $emptyJsonArray: SQL<[never]>;
180 | ```
181 |
182 | Empty array as SQL json.
183 |
184 | ---
185 |
186 |
187 |
188 | ### $emptyJsonObject
189 |
190 | ```ts
191 | const $emptyJsonObject: SQL