Skip to content

Commit 7ceb936

Browse files
committed
refactor(index): jsdoc fixes & un-protect members
1 parent 776083c commit 7ceb936

File tree

2 files changed

+107
-69
lines changed

2 files changed

+107
-69
lines changed

.prettierignore

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
node_modules
2+
*.log
3+
*.logs
24
dist

src/index.ts

+105-69
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,36 @@ export type Methods =
4343
| "type";
4444

4545
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;
4966

5067
/**
51-
* The `pg` client instance for your database.
68+
* The `pg` client instance for your database
69+
* @type {PgClient}
5270
*/
5371

5472
public client: PgClient;
5573

5674
/**
57-
* Whether the database has been connected or not.
75+
* Whether the database has been connected or not
5876
* @see connect
5977
* @see end
6078
*/
@@ -71,37 +89,51 @@ export class Client extends EventEmitter {
7189
}
7290

7391
/**
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
7694
* @example await db.connect();
7795
*/
7896

79-
public async connect() {
97+
public async connect(): Promise<Client> {
8098
await this.client.connect();
8199
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+
82107
this.emit("ready", this);
83108
return this;
84109
}
85110

86111
/**
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
89114
* @example await db.end();
90115
*/
91116

92-
public async end() {
117+
public async end(): Promise<Client> {
93118
await this.client.end();
94119
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+
95127
this.emit("end", this);
96128
return this;
97129
}
98130

99131
/**
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
105137
* @example const data = await db.fetch("users.1234567890.inventory");
106138
*/
107139

@@ -111,25 +143,24 @@ export class Client extends EventEmitter {
111143
}
112144

113145
/**
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
119151
* @example const data = await db.fetch("users.1234567890.inventory");
120152
*/
121153

122154
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);
125156
}
126157

127158
/**
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
130161
* @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
133164
* @example const data = await db.set("users.1234567890.level", 100);
134165
*/
135166

@@ -143,11 +174,11 @@ export class Client extends EventEmitter {
143174
}
144175

145176
/**
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
148179
* @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
151182
* @example const data = await db.add("users.1234567890.level", 1);
152183
*/
153184

@@ -162,11 +193,11 @@ export class Client extends EventEmitter {
162193
}
163194

164195
/**
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
167198
* @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
170201
* @example const data = await db.subtract("users.1234567890.level", 10);
171202
*/
172203

@@ -181,11 +212,11 @@ export class Client extends EventEmitter {
181212
}
182213

183214
/**
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
186217
* @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
189220
* @example const data = await db.push("users.1234567890.inventory", "Slice of Cheese");
190221
*/
191222

@@ -201,24 +232,24 @@ export class Client extends EventEmitter {
201232
}
202233

203234
/**
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`
208239
* @example await db.delete("users.1234567890");
209240
*/
210241

211-
public async delete(key: string, ops?: Options): Promise<any> {
242+
public async delete(key: string, ops?: Options): Promise<boolean> {
212243
if (!key) throw new TypeError("No key specified.");
213244
return await this.arbitrate(del, { id: key, ops: ops || {} });
214245
}
215246

216247
/**
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
222253
* @example const data = await db.has("users.1234567890");
223254
*/
224255

@@ -229,23 +260,21 @@ export class Client extends EventEmitter {
229260

230261
/**
231262
* 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
236267
* @example const data = await db.has("users.1234567890");
237268
*/
238269

239270
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);
242272
}
243273

244274
/**
245275
* Deletes all rows from the entire active table.
246276
* 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
249278
* @example const data = await db.clear();
250279
*/
251280

@@ -255,19 +284,19 @@ export class Client extends EventEmitter {
255284

256285
/**
257286
* Deletes the entire active table.
258-
* @returns void
287+
* @returns {Promise<void>} void
259288
* @example await db.drop();
260289
*/
261290

262-
public async drop() {
291+
public async drop(): Promise<void> {
263292
return await this.arbitrate(drop, { ops: {} }, this.tableName);
264293
}
265294

266295
/**
267296
* 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
271300
* @example const data = await db.all();
272301
*/
273302

@@ -277,9 +306,9 @@ export class Client extends EventEmitter {
277306

278307
/**
279308
* 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
283312
* @example const data = await db.all();
284313
*/
285314

@@ -288,10 +317,10 @@ export class Client extends EventEmitter {
288317
}
289318

290319
/**
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`
295324
*/
296325

297326
public async type(
@@ -311,6 +340,13 @@ export class Client extends EventEmitter {
311340
return await this.arbitrate(type, { id: key, ops: ops || {} });
312341
}
313342

343+
/**
344+
* @private Arbitrate
345+
* @param {PgClient} client
346+
* @param {Params} params
347+
* @param {Options} options
348+
*/
349+
314350
private async arbitrate(
315351
method: (client: PgClient, params: Params, ops: Options) => any,
316352
params: Params,

0 commit comments

Comments
 (0)