QueryAction.ts74 lines · main
| 1 | import type { SafeSqlFragment } from '../pg-format' |
| 2 | import { IQueryFilter, QueryFilter } from './QueryFilter' |
| 3 | import type { Dictionary, QueryTable } from './types' |
| 4 | |
| 5 | export interface IQueryAction { |
| 6 | count: () => IQueryFilter |
| 7 | delete: (options?: { returning: boolean }) => IQueryFilter |
| 8 | insert: (values: Array<Dictionary<any>>, options?: { returning: boolean }) => IQueryFilter |
| 9 | select: (columns?: SafeSqlFragment) => IQueryFilter |
| 10 | update: (value: Dictionary<any>, options?: { returning: boolean }) => IQueryFilter |
| 11 | truncate: (options?: { returning: boolean }) => IQueryFilter |
| 12 | } |
| 13 | |
| 14 | export class QueryAction implements IQueryAction { |
| 15 | constructor(protected table: QueryTable) {} |
| 16 | |
| 17 | /** |
| 18 | * Performs a COUNT on the table. |
| 19 | */ |
| 20 | count() { |
| 21 | return new QueryFilter(this.table, { action: 'count' }) |
| 22 | } |
| 23 | |
| 24 | /** |
| 25 | * Performs a DELETE on the table. |
| 26 | * |
| 27 | * @param options.returning If `true`, return the deleted row(s) in the response. |
| 28 | */ |
| 29 | delete(options?: { returning: boolean; enumArrayColumns?: Array<string> }) { |
| 30 | return new QueryFilter(this.table, { action: 'delete' }, options) |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Performs an INSERT into the table. |
| 35 | * |
| 36 | * @param values The values to insert. |
| 37 | * @param options.returning If `true`, return the inserted row(s) in the response. |
| 38 | */ |
| 39 | insert( |
| 40 | values: Array<Dictionary<any>>, |
| 41 | options?: { returning: boolean; enumArrayColumns?: Array<string> } |
| 42 | ) { |
| 43 | return new QueryFilter(this.table, { action: 'insert', actionValue: values }, options) |
| 44 | } |
| 45 | |
| 46 | /** |
| 47 | * Performs vertical filtering with SELECT. |
| 48 | * |
| 49 | * @param columns the query columns, by default set to '*'. |
| 50 | */ |
| 51 | select(columns?: SafeSqlFragment) { |
| 52 | return new QueryFilter(this.table, { action: 'select', actionValue: columns }) |
| 53 | } |
| 54 | |
| 55 | /** |
| 56 | * Performs an UPDATE on the table. |
| 57 | * |
| 58 | * @param value The value to update. |
| 59 | * @param options.returning If `true`, return the updated row(s) in the response. |
| 60 | */ |
| 61 | update( |
| 62 | value: Dictionary<any>, |
| 63 | options?: { returning: boolean; enumArrayColumns?: Array<string> } |
| 64 | ) { |
| 65 | return new QueryFilter(this.table, { action: 'update', actionValue: value }, options) |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Performs a TRUNCATE on the table |
| 70 | */ |
| 71 | truncate(options?: { returning: boolean; enumArrayColumns?: Array<string> }) { |
| 72 | return new QueryFilter(this.table, { action: 'truncate' }, options) |
| 73 | } |
| 74 | } |