forked from nearform/sql
-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQL.d.ts
137 lines (119 loc) · 4.48 KB
/
SQL.d.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/** A tagged template containing strings and values */
interface StatementLike {
strings: string[]
values: any[]
}
interface StatementOptions {
unsafe?: boolean
}
/**
* An SQL statement tagged template
* @see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals
*/
declare class SqlStatement implements StatementLike {
constructor(strings: string[], values: any[])
/** The string components of this tagged template */
strings: string[]
/**
* Safely glues multiple SQL statements together
* @param pieces the statements to be glued
* @param separator the glue separator placed between each statement
* @example
* const sql = SQL`SELECT id FROM customers WHERE `
* sql.glue([
* sql,
* SQL`email = ${email}`
* ])
*/
glue(pieces: StatementLike[], separator: string): SqlStatement
/**
* Safely glues multiple SQL statements together
* @param pieces the statements to be glued
* @param separator the glue separator placed between each statement
* @example
* SQL.glue([
* SQL`SELECT id FROM customers WHERE `,
* SQL`email = ${email}`
* ])
* )
*/
static glue(pieces: StatementLike[], separator: string): SqlStatement
/**
* A function that accepts an array of objects and a mapper function
* It returns a clean SQL format using the object properties defined in the mapper function
* @param array the items to be mapped over
* @param mapFunc a function to transform the items in `array` before being added to the SqlStatement
* @example
* SQL`SELECT ${SQL.map([1,2,3])}`
* @example
* SQL`SELECT ${SQL.map([1,2,3], x => x ** 2)}`
*/
map<T>(array: T[], mapFunc?: (item: T) => unknown): SqlStatement
/**
* A function that accepts an array of objects and a mapper function
* It returns a clean SQL format using the object properties defined in the mapper function
* @param array the items to be mapped over
* @param mapFunc a function to transform the items in `array` before being added to the SqlStatement
* @example
* SQL`SELECT ${SQL.map([1,2,3])}`
* @example
* SQL`SELECT ${SQL.map([1,2,3], x => x ** 2)}`
*/
static map<T>(array: T[], mapFunc?: (item: T) => unknown): SqlStatement
/** Returns a formatted but unsafe statement of strings and values, useful for debugging */
get debug(): string
/** Returns a formatted statement suitable for use in PostgreSQL */
get text(): string
/** Returns a formatted statement suitable for use in MySQL */
get sql(): string
/** The value components of this tagged template */
get values(): any[]
/**
* Appends another statement onto this statement
* @deprecated Please append within template literals, e.g. SQL`SELECT * ${sql}`
* @param statement a statement to be appended onto this existing statement
* @param options allows disabling the safe template escaping while appending
* @example
* SQL`UPDATE users SET name = ${username}, email = ${email} `
* .append(SQL`SET ${dynamicName} = '2'`, { unsafe: true })
* .append(SQL`WHERE id = ${userId}`)
*/
append(statement: StatementLike, options?: StatementOptions): SqlStatement
}
declare namespace SQL {
export { SqlStatement }
/**
* Safely glues multiple SQL statements together
* @param pieces the statements to be glued
* @param separator the glue separator placed between each statement
* @example
* SQL.glue([
* SQL`SELECT id FROM customers WHERE `,
* SQL`email = ${email}`
* ])
* )
*/
export function glue(pieces: StatementLike[], separator: string): SqlStatement
/**
* A function that accepts an array of objects and a mapper function
* It returns a clean SQL format using the object properties defined in the mapper function
* @param array the items to be mapped over
* @param mapFunc a function to transform the items in `array` before being added to the SqlStatement
* @example
* SQL`SELECT ${SQL.map([1,2,3])}`
* @example
* SQL`SELECT ${SQL.map([1,2,3], x => x ** 2)}`
*/
export function map<T>(array: T[], mapFunc?: (item: T) => unknown): SqlStatement
export function unsafe<T>(value: T): { value: T }
export function quoteIdent(value: string): { value: string }
}
/**
* Create an SQL statement tagged template
* @param strings template literal string components
* @param values template literal value components
* @example
* SQL`SELECT id FROM customers WHERE name = ${userInput}`
*/
declare function SQL(strings: any, ...values: any[]): SqlStatement
export = SQL