-
Notifications
You must be signed in to change notification settings - Fork 57
/
index.d.ts
250 lines (245 loc) · 7.19 KB
/
index.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/**
* **Use SQL databases from k6 tests.**
*
* xk6-sql is a [Grafana k6 extension](https://grafana.com/docs/k6/latest/extensions/) that enables
* the use of SQL databases in [k6](https://grafana.com/docs/k6/latest/) tests.
*
* In order to use the `xk6-sql` API, in addition to the `k6/x/sql` module,
* it is also necessary to import at least one driver module.
* The default export of the driver module is a driver identifier symbol,
* which should be passed as a parameter of the `open()` function.
*
* The driver module is typically available at `k6/x/sql/driver/FOO`,
* where `FOO` is the name of the driver.
*
* @example
* ```ts file=examples/example.js
* import sql from "k6/x/sql";
*
* // the actual database driver should be used instead of ramsql
* import driver from "k6/x/sql/driver/ramsql";
*
* const db = sql.open(driver, "roster_db");
*
* export function setup() {
* db.exec(`
* CREATE TABLE IF NOT EXISTS roster
* (
* id INTEGER PRIMARY KEY AUTOINCREMENT,
* given_name VARCHAR NOT NULL,
* family_name VARCHAR NOT NULL
* );
* `);
* }
*
* export function teardown() {
* db.close();
* }
*
* export default function () {
* let result = db.exec(`
* INSERT INTO roster
* (given_name, family_name)
* VALUES
* ('Peter', 'Pan'),
* ('Wendy', 'Darling'),
* ('Tinker', 'Bell'),
* ('James', 'Hook');
* `);
* console.log(`${result.rowsAffected()} rows inserted`);
*
* let rows = db.query("SELECT * FROM roster WHERE given_name = $1;", "Peter");
* for (const row of rows) {
* console.log(`${row.family_name}, ${row.given_name}`);
* }
* }
* ```
*
* @module sql
*/
export as namespace sql;
/**
* Open a database specified by database driver identifier Symbol and a driver-specific data source name,
* usually consisting of at least a database name and connection information.
*
* @param dirverID driver identification symbol, the default export of the driver module
* @param dataSourceName driver-specific data source name, like a database name
*
* @example
* ```ts file=examples/example.js
* import sql from "k6/x/sql";
*
* // the actual database driver should be used instead of ramsql
* import driver from "k6/x/sql/driver/ramsql";
*
* const db = sql.open(driver, "roster_db");
* ```
*/
export function open(dirverID: Symbol, dataSourceName: String): Database;
/**
* Database is a database handle representing a pool of zero or more underlying connections.
*
* @example
* ```ts file=examples/example.js
* import sql from "k6/x/sql";
*
* // the actual database driver should be used instead of ramsql
* import driver from "k6/x/sql/driver/ramsql";
*
* const db = sql.open(driver, "roster_db");
* ```
*/
export interface Database {
/**
* Close the database and prevents new queries from starting.
*
* Close waits for all queries that have started processing on the server to finish.
*
* @example
* ```ts file=examples/example.js
* import sql from "k6/x/sql";
*
* // the actual database driver should be used instead of ramsql
* import driver from "k6/x/sql/driver/ramsql";
*
* const db = sql.open(driver, "roster_db");
*
* export function teardown() {
* db.close();
* }
* ```
*/
close(): void;
/**
* Execute a query without returning any rows.
*
* @param query the query to execute
* @param args placeholder parameters in the query
* @returns summary of the executed SQL commands
* @example
* ```ts file=examples/example.js
* import sql from "k6/x/sql";
*
* // the actual database driver should be used instead of ramsql
* import driver from "k6/x/sql/driver/ramsql";
*
* const db = sql.open(driver, "roster_db");
*
* export function setup() {
* db.exec(`
* CREATE TABLE IF NOT EXISTS roster
* (
* id INTEGER PRIMARY KEY AUTOINCREMENT,
* given_name VARCHAR NOT NULL,
* family_name VARCHAR NOT NULL
* );
* `);
*
* let result = db.exec(`
* INSERT INTO roster
* (given_name, family_name)
* VALUES
* ('Peter', 'Pan'),
* ('Wendy', 'Darling'),
* ('Tinker', 'Bell'),
* ('James', 'Hook');
* `);
* console.log(`${result.rowsAffected()} rows inserted`);
* }
* ```
*/
exec(query: string, ...args: any[]): Result;
/**
* Query executes a query that returns rows, typically a SELECT.
* @param query the query to execute
* @param args placeholder parameters in the query
* @returns rows of the query result
* @example
* ```ts file=examples/example.js
* import sql from "k6/x/sql";
*
* // the actual database driver should be used instead of ramsql
* import driver from "k6/x/sql/driver/ramsql";
*
* const db = sql.open(driver, "roster_db");
*
* export default function () {
* let rows = db.query("SELECT * FROM roster WHERE given_name = $1;", "Peter");
* for (const row of results) {
* console.log(`${row.family_name}, ${row.given_name}`);
* }
* }
* ```
*/
query(query: string, ...args: any[]): Row[];
}
/**
* An object containing a single row of the query result.
*/
export interface Row {
/**
* Each column has a property with the same name as the column name.
* The value of the property contains the value of the given column in the current row.
*
* The value of the property is automatically mapped to the corresponding JavaScript type.
*
* @example
* ```ts file=examples/example.js
* import sql from "k6/x/sql";
*
* // the actual database driver should be used instead of ramsql
* import driver from "k6/x/sql/driver/ramsql";
*
* const db = sql.open(driver, "roster_db");
*
* export default function () {
* let rows = db.query("SELECT * FROM roster WHERE given_name = $1;", "Peter");
* for (const row of results) {
* console.log(`${row.family_name}, ${row.given_name}`);
* }
* }
* ```
*/
[key: string]: unknown;
}
/**
* A Result summarizes an executed SQL command.
* @example
* ```ts file=examples/example.js
* import sql from "k6/x/sql";
*
* // the actual database driver should be used instead of ramsql
* import driver from "k6/x/sql/driver/ramsql";
*
* const db = sql.open(driver, "roster_db");
*
* export function setup() {
* let result = db.exec(`
* INSERT INTO roster
* (given_name, family_name)
* VALUES
* ('Peter', 'Pan'),
* ('Wendy', 'Darling'),
* ('Tinker', 'Bell'),
* ('James', 'Hook');
* `);
* console.log(`${result.rowsAffected()} rows inserted`);
* }
* ```
*/
export interface Result {
/**
* Returns the integer generated by the database
* in response to a command. Typically this will be from an
* "auto increment" column when inserting a new row. Not all
* databases support this feature, and the syntax of such
* statements varies.
*/
lastInsertId(): number;
/**
* Returns the number of rows affected by an
* update, insert, or delete. Not every database or database
* driver may support this.
*/
rowsAffected(): number;
}