@@ -43,18 +43,36 @@ export type Methods =
43
43
| "type" ;
44
44
45
45
export class Client extends EventEmitter {
46
- protected dbUrl : string ;
47
- protected options : ClientOptions | undefined ;
48
- protected tableName : string | undefined ;
46
+ /**
47
+ * PostgreSQL database url
48
+ * @type {string }
49
+ */
50
+
51
+ public dbUrl : string ;
52
+
53
+ /**
54
+ * Client options
55
+ * @type {ClientOptions | undefined }
56
+ */
57
+
58
+ public options : ClientOptions | undefined ;
59
+
60
+ /**
61
+ * Default table name to use
62
+ * @type {string | undefined }
63
+ */
64
+
65
+ tableName : string | undefined ;
49
66
50
67
/**
51
- * The `pg` client instance for your database.
68
+ * The `pg` client instance for your database
69
+ * @type {PgClient }
52
70
*/
53
71
54
72
public client : PgClient ;
55
73
56
74
/**
57
- * Whether the database has been connected or not.
75
+ * Whether the database has been connected or not
58
76
* @see connect
59
77
* @see end
60
78
*/
@@ -71,37 +89,51 @@ export class Client extends EventEmitter {
71
89
}
72
90
73
91
/**
74
- * Connects to the database specified when creating the client.
75
- * @returns the client
92
+ * Connects to the database
93
+ * @returns { Promise<Client> } the client
76
94
* @example await db.connect();
77
95
*/
78
96
79
- public async connect ( ) {
97
+ public async connect ( ) : Promise < Client > {
80
98
await this . client . connect ( ) ;
81
99
this . connected = true ;
100
+
101
+ /**
102
+ * Emitted when the database has been connected
103
+ * @event Client#ready
104
+ * @param {Client } client the client
105
+ */
106
+
82
107
this . emit ( "ready" , this ) ;
83
108
return this ;
84
109
}
85
110
86
111
/**
87
- * Ends the connection to the database specified when creating the client.
88
- * @returns the client
112
+ * Ends the connection to the database
113
+ * @returns { Promise<Client> } the client
89
114
* @example await db.end();
90
115
*/
91
116
92
- public async end ( ) {
117
+ public async end ( ) : Promise < Client > {
93
118
await this . client . end ( ) ;
94
119
this . connected = false ;
120
+
121
+ /**
122
+ * Emitted when the database connection has been ended
123
+ * @event Client#end
124
+ * @param {Client } client the client
125
+ */
126
+
95
127
this . emit ( "end" , this ) ;
96
128
return this ;
97
129
}
98
130
99
131
/**
100
- * Fetches data from a key in the database.
101
- * @param key any string as a key, allows dot notation
102
- * @param options any options to be added to the request
103
- * @returns the data requested
104
- * @alias get
132
+ * Fetches data from a key in the database
133
+ * @param { string } key any string as a key, allows dot notation
134
+ * @param { Options } options any options to be added to the request
135
+ * @returns { Promise<any> } the data requested
136
+ * @alias Client# get
105
137
* @example const data = await db.fetch("users.1234567890.inventory");
106
138
*/
107
139
@@ -111,25 +143,24 @@ export class Client extends EventEmitter {
111
143
}
112
144
113
145
/**
114
- * Fetches data from a key in the database.
115
- * @param key any string as a key, allows dot notation
116
- * @param options any options to be added to the request
117
- * @returns the data requested
118
- * @alias get
146
+ * Fetches data from a key in the database
147
+ * @param { string } key any string as a key, allows dot notation
148
+ * @param { options } options any options to be added to the request
149
+ * @returns { Promise<any> } the data requested
150
+ * @alias Client#fetch
119
151
* @example const data = await db.fetch("users.1234567890.inventory");
120
152
*/
121
153
122
154
public async get ( key : string , ops ?: Options ) : Promise < any > {
123
- if ( ! key ) throw new TypeError ( "No key specified." ) ;
124
- return await this . arbitrate ( fetch , { id : key , ops : ops || { } } ) ;
155
+ return await this . fetch ( key , ops ) ;
125
156
}
126
157
127
158
/**
128
- * Sets new data based on a key in the database.
129
- * @param key any string as a key, allows dot notation
159
+ * Sets new data based on a key in the database
160
+ * @param { string } key any string as a key, allows dot notation
130
161
* @param value value of the data to be set
131
- * @param options any options to be added to the request
132
- * @returns the updated data
162
+ * @param { Options } options any options to be added to the request
163
+ * @returns { Promise<any> } the updated data
133
164
* @example const data = await db.set("users.1234567890.level", 100);
134
165
*/
135
166
@@ -143,11 +174,11 @@ export class Client extends EventEmitter {
143
174
}
144
175
145
176
/**
146
- * Adds a number to a key in the database. If no existing number, it will add to 0.
147
- * @param key any string as a key, allows dot notation
177
+ * Adds a number to a key in the database. If no existing number, it will add to 0
178
+ * @param { string } key any string as a key, allows dot notation
148
179
* @param value value to add
149
- * @param options any options to be added to the request
150
- * @returns the updated data
180
+ * @param { Options } options any options to be added to the request
181
+ * @returns { Promise<any> } the updated data
151
182
* @example const data = await db.add("users.1234567890.level", 1);
152
183
*/
153
184
@@ -162,11 +193,11 @@ export class Client extends EventEmitter {
162
193
}
163
194
164
195
/**
165
- * Subtracts a number to a key in the database. If no existing number, it will subtract to 0.
166
- * @param key any string as a key, allows dot notation
196
+ * Subtracts a number to a key in the database. If no existing number, it will subtract to 0
197
+ * @param { string } key any string as a key, allows dot notation
167
198
* @param value value to subtract
168
- * @param options any options to be added to the request
169
- * @returns the updated data
199
+ * @param { Options } options any options to be added to the request
200
+ * @returns { Promise<any> } the updated data
170
201
* @example const data = await db.subtract("users.1234567890.level", 10);
171
202
*/
172
203
@@ -181,11 +212,11 @@ export class Client extends EventEmitter {
181
212
}
182
213
183
214
/**
184
- * Push into an array in the database based on the key. If no existing array, it will create one.
185
- * @param key any string as a key, allows dot notation
215
+ * Push into an array in the database based on the key. If no existing array, it will create one
216
+ * @param { string } key any string as a key, allows dot notation
186
217
* @param value value to push
187
- * @param options any options to be added to the request
188
- * @returns the updated data
218
+ * @param { Options } options any options to be added to the request
219
+ * @returns { Promise<any> } the updated data
189
220
* @example const data = await db.push("users.1234567890.inventory", "Slice of Cheese");
190
221
*/
191
222
@@ -201,24 +232,24 @@ export class Client extends EventEmitter {
201
232
}
202
233
203
234
/**
204
- * Delete an object (or property) in the database.
205
- * @param key any string as a key, allows dot notation
206
- * @param options any options to be added to the request
207
- * @returns `true` if success, if not found `false`
235
+ * Delete an object (or property) in the database
236
+ * @param { string } key any string as a key, allows dot notation
237
+ * @param { Options } options any options to be added to the request
238
+ * @returns { boolean } `true` if success, if not found `false`
208
239
* @example await db.delete("users.1234567890");
209
240
*/
210
241
211
- public async delete ( key : string , ops ?: Options ) : Promise < any > {
242
+ public async delete ( key : string , ops ?: Options ) : Promise < boolean > {
212
243
if ( ! key ) throw new TypeError ( "No key specified." ) ;
213
244
return await this . arbitrate ( del , { id : key , ops : ops || { } } ) ;
214
245
}
215
246
216
247
/**
217
- * Returns a boolean indicating whether an element with the specified key exists or not.
218
- * @param key any string as a key, allows dot notation
219
- * @param options any options to be added to the request
220
- * @returns boolean
221
- * @alias includes
248
+ * Returns a boolean indicating whether an element with the specified key exists or not
249
+ * @param { string } key any string as a key, allows dot notation
250
+ * @param { Options } options any options to be added to the request
251
+ * @returns { boolean } boolean
252
+ * @alias Client# includes
222
253
* @example const data = await db.has("users.1234567890");
223
254
*/
224
255
@@ -229,23 +260,21 @@ export class Client extends EventEmitter {
229
260
230
261
/**
231
262
* Returns a boolean indicating whether an element with the specified key exists or not.
232
- * @param key any string as a key, allows dot notation
233
- * @param options any options to be added to the request
234
- * @returns boolean
235
- * @alias has
263
+ * @param { string } key any string as a key, allows dot notation
264
+ * @param { Options } options any options to be added to the request
265
+ * @returns { Promise<boolean> } boolean
266
+ * @alias Client# has
236
267
* @example const data = await db.has("users.1234567890");
237
268
*/
238
269
239
270
public async includes ( key : string , ops ?: Options ) : Promise < boolean > {
240
- if ( ! key ) throw new TypeError ( "No key specified." ) ;
241
- return await this . arbitrate ( has , { id : key , ops : ops || { } } ) ;
271
+ return await this . has ( key , ops ) ;
242
272
}
243
273
244
274
/**
245
275
* Deletes all rows from the entire active table.
246
276
* Note: This does not delete the table itself. To delete the table itself along with the rows, use `drop()`.
247
- * @returns amount of rows deleted
248
- *
277
+ * @returns {Promise<number> } amount of rows deleted
249
278
* @example const data = await db.clear();
250
279
*/
251
280
@@ -255,19 +284,19 @@ export class Client extends EventEmitter {
255
284
256
285
/**
257
286
* Deletes the entire active table.
258
- * @returns void
287
+ * @returns { Promise<void> } void
259
288
* @example await db.drop();
260
289
*/
261
290
262
- public async drop ( ) {
291
+ public async drop ( ) : Promise < void > {
263
292
return await this . arbitrate ( drop , { ops : { } } , this . tableName ) ;
264
293
}
265
294
266
295
/**
267
296
* Fetches the entire active table
268
- * @param options any options to be added to the request
269
- * @returns entire table as an object
270
- * @alias fetchAll
297
+ * @param { Options } options any options to be added to the request
298
+ * @returns { Promise<any> } entire table as an object
299
+ * @alias Client# fetchAll
271
300
* @example const data = await db.all();
272
301
*/
273
302
@@ -277,9 +306,9 @@ export class Client extends EventEmitter {
277
306
278
307
/**
279
308
* Fetches the entire active table
280
- * @param options any options to be added to the request
281
- * @returns entire table as an object
282
- * @alias all
309
+ * @param { Options } options any options to be added to the request
310
+ * @returns { Promise<any> } entire table as an object
311
+ * @alias Client# all
283
312
* @example const data = await db.all();
284
313
*/
285
314
@@ -288,10 +317,10 @@ export class Client extends EventEmitter {
288
317
}
289
318
290
319
/**
291
- * Used to get the type of the value.
292
- * @param key any string as a key, allows dot notation
293
- * @param options any options to be added to the request
294
- * @returns type from `typeof`
320
+ * Used to get the type of the value
321
+ * @param { string } key any string as a key, allows dot notation
322
+ * @param { Options } options any options to be added to the request
323
+ * @returns { Promise<"bigint" | "boolean" | "function" | "number" | "object" | "string" | "symbol" | "undefined"> } type from `typeof`
295
324
*/
296
325
297
326
public async type (
@@ -311,6 +340,13 @@ export class Client extends EventEmitter {
311
340
return await this . arbitrate ( type , { id : key , ops : ops || { } } ) ;
312
341
}
313
342
343
+ /**
344
+ * @private Arbitrate
345
+ * @param {PgClient } client
346
+ * @param {Params } params
347
+ * @param {Options } options
348
+ */
349
+
314
350
private async arbitrate (
315
351
method : ( client : PgClient , params : Params , ops : Options ) => any ,
316
352
params : Params ,
0 commit comments