Skip to content

Commit d53755c

Browse files
authored
Improve doc comments (#207)
* Improve doc comments ahead of new version
1 parent 16bd8a7 commit d53755c

File tree

3 files changed

+145
-37
lines changed

3 files changed

+145
-37
lines changed

src/db.ts

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,14 @@ export class DB {
142142
* const rows = db.query<[string, number]>("SELECT name, age FROM people WHERE city = ?", [city]);
143143
* // rows = [["Peter Parker", 21], ...]
144144
* ```
145+
*
146+
* ```typescript
147+
* const rows = db.query<[string, number]>(
148+
* "SELECT name, age FROM people WHERE city = :city",
149+
* { city },
150+
* );
151+
* // rows = [["Peter Parker", 21], ...]
152+
* ```
145153
*/
146154
query<R extends Row = Row>(
147155
sql: string,
@@ -168,6 +176,14 @@ export class DB {
168176
* const rows = db.queryEntries<{ name: string, age: number }>("SELECT name, age FROM people");
169177
* // rows = [{ name: "Peter Parker", age: 21 }, ...]
170178
* ```
179+
*
180+
* ```typescript
181+
* const rows = db.queryEntries<{ name: string, age: number }>(
182+
* "SELECT name, age FROM people WHERE age >= :minAge",
183+
* { minAge },
184+
* );
185+
* // rows = [{ name: "Peter Parker", age: 21 }, ...]
186+
* ```
171187
*/
172188
queryEntries<O extends RowObject = RowObject>(
173189
sql: string,
@@ -205,15 +221,15 @@ export class DB {
205221
* to specify precise types for returned data and
206222
* query parameters.
207223
*
208-
* The first type parameter `R` indicates the tuple type
209-
* for rows returned by the query.
224+
* + The first type parameter `R` indicates the tuple type
225+
* for rows returned by the query.
210226
*
211-
* The second type parameter `O` indicates the record type
212-
* for rows returned as entries (mappings from column names
213-
* to values).
227+
* + The second type parameter `O` indicates the record type
228+
* for rows returned as entries (mappings from column names
229+
* to values).
214230
*
215-
* The third type parameter `P` indicates the type this query
216-
* accepts as parameters.
231+
* + The third type parameter `P` indicates the type this query
232+
* accepts as parameters.
217233
*
218234
* Note, that the correctness of those types must
219235
* be guaranteed by the caller of this function.
@@ -226,7 +242,9 @@ export class DB {
226242
* { name: string, age: number },
227243
* { city: string },
228244
* >("SELECT name, age FROM people WHERE city = :city");
245+
*
229246
* // use query ...
247+
*
230248
* query.finalize();
231249
* ```
232250
*/
@@ -318,9 +336,9 @@ export class DB {
318336
* the database is no longer used to avoid leaking
319337
* open file descriptors.
320338
*
321-
* If `force` is specified, any active `PreparedQuery`
322-
* will be finalized. Otherwise, this throws if there
323-
* are active queries.
339+
* If `force = true` is passed, any non-finalized
340+
* `PreparedQuery` objects will be finalized. Otherwise,
341+
* this throws if there are active queries.
324342
*
325343
* `close` may safely be called multiple
326344
* times.

src/error.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { getStr } from "./wasm.ts";
33
import { Status } from "./constants.ts";
44

55
/**
6-
* Errors which can be thrown while interacting with
6+
* Errors which can occur while interacting with
77
* a database.
88
*/
99
export class SqliteError extends Error {

src/query.ts

Lines changed: 116 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,23 @@ export type QueryParameterSet =
8989
* Name of a column in a database query.
9090
*/
9191
export interface ColumnName {
92+
/**
93+
* Name of the returned column.
94+
*/
9295
name: string;
96+
/**
97+
* Name of the database column that stores
98+
* the data returned from this query.
99+
*
100+
* This might be different from `name` if a
101+
* columns was renamed using e.g. as in
102+
* `SELECT foo AS bar FROM table`.
103+
*/
93104
originName: string;
105+
/**
106+
* Name of the table that stores the data
107+
* returned from this query.
108+
*/
94109
tableName: string;
95110
}
96111

@@ -118,10 +133,7 @@ export class PreparedQuery<
118133
private _finalized: boolean;
119134

120135
/**
121-
* A prepared query which can be executed many
122-
* times.
123-
*
124-
* The constructor should never be used directly.
136+
* This constructor should never be used directly.
125137
* Instead a prepared query can be obtained by
126138
* calling `DB.prepareQuery`.
127139
*/
@@ -321,21 +333,32 @@ export class PreparedQuery<
321333
* rows into memory and hence allows to process a large
322334
* number of rows.
323335
*
324-
* # Example:
336+
* Calling `iter`, `all`, or `first` invalidates any iterators
337+
* previously returned from this prepared query.
338+
*
339+
* # Examples
340+
*
325341
* ```typescript
326342
* const query = db.prepareQuery<[number, string]>("SELECT id, name FROM people");
327343
* for (const [id, name] of query.iter()) {
328344
* // ...
329345
* }
330346
* ```
331347
*
332-
* Calling `iter` invalidates any iterators previously returned
333-
* from this prepared query. Using an invalidated iterator is a bug.
334-
*
335348
* To avoid SQL injection, user-provided values
336349
* should always be passed to the database through
337350
* a query parameter.
338351
*
352+
* ```typescript
353+
* const query = db.prepareQuery("SELECT id FROM people WHERE name = ?");
354+
* preparedQuery.iter([name]);
355+
* ```
356+
*
357+
* ```typescript
358+
* const query = db.prepareQuery("SELECT id FROM people WHERE name = :name");
359+
* preparedQuery.iter({ name });
360+
* ```
361+
*
339362
* See `QueryParameterSet` for documentation on
340363
* how values can be bound to SQL statements.
341364
*
@@ -357,6 +380,15 @@ export class PreparedQuery<
357380
/**
358381
* Like `iter` except each row is returned
359382
* as an object containing key-value pairs.
383+
*
384+
* # Examples
385+
*
386+
* ```typescript
387+
* const query = db.prepareQuery<_, { id: number, name: string }>("SELECT id, name FROM people");
388+
* for (const { id, name } of query.iter()) {
389+
* // ...
390+
* }
391+
* ```
360392
*/
361393
iterEntries(params?: P): RowsIterator<O> {
362394
this.iter(params);
@@ -401,14 +433,28 @@ export class PreparedQuery<
401433
* and returns an array containing all resulting
402434
* rows.
403435
*
404-
* Calling `all` invalidates any iterators
405-
* previously returned by calls to `iter`.
406-
* Using an invalidated iterator is a bug.
436+
* # Examples
437+
*
438+
* ```typescript
439+
* const query = db.prepareQuery<[number, string]>("SELECT id, name FROM people");
440+
* const rows = query.all();
441+
* // [[1, "Peter"], ...]
442+
* ```
407443
*
408444
* To avoid SQL injection, user-provided values
409445
* should always be passed to the database through
410446
* a query parameter.
411447
*
448+
* ```typescript
449+
* const query = db.prepareQuery("SELECT id FROM people WHERE name = ?");
450+
* preparedQuery.all([name]);
451+
* ```
452+
*
453+
* ```typescript
454+
* const query = db.prepareQuery("SELECT id FROM people WHERE name = :name");
455+
* preparedQuery.all({ name });
456+
* ```
457+
*
412458
* See `QueryParameterSet` for documentation on
413459
* how values can be bound to SQL statements.
414460
*
@@ -432,6 +478,14 @@ export class PreparedQuery<
432478
/**
433479
* Like `all` except each row is returned
434480
* as an object containing key-value pairs.
481+
*
482+
* # Examples
483+
*
484+
* ```typescript
485+
* const query = db.prepareQuery<_, { id: number, name: string }>("SELECT id, name FROM people");
486+
* const rows = query.all();
487+
* // [{ id: 1, name: "Peter" }, ...]
488+
* ```
435489
*/
436490
allEntries(params?: P): Array<O> {
437491
return this.all(params).map((row) => this.makeRowObject(row));
@@ -443,14 +497,34 @@ export class PreparedQuery<
443497
* `undefined` when there are no rows returned
444498
* by the query.
445499
*
446-
* Calling `first` invalidates any iterators
447-
* previously returned by calls to `iter`.
448-
* Using an invalidated iterator is a bug.
500+
* # Examples
501+
*
502+
* ```typescript
503+
* const query = db.prepareQuery<[number, string]>("SELECT id, name FROM people");
504+
* const person = query.first();
505+
* // [1, "Peter"]
506+
* ```
507+
*
508+
* ```typescript
509+
* const query = db.prepareQuery("SELECT id, name FROM people WHERE name = ?");
510+
* const person = query.first(["not a name"]);
511+
* // undefined
512+
* ```
449513
*
450514
* To avoid SQL injection, user-provided values
451515
* should always be passed to the database through
452516
* a query parameter.
453517
*
518+
* ```typescript
519+
* const query = db.prepareQuery("SELECT id FROM people WHERE name = ?");
520+
* preparedQuery.first([name]);
521+
* ```
522+
*
523+
* ```typescript
524+
* const query = db.prepareQuery("SELECT id FROM people WHERE name = :name");
525+
* preparedQuery.first({ name });
526+
* ```
527+
*
454528
* See `QueryParameterSet` for documentation on
455529
* how values can be bound to SQL statements.
456530
*
@@ -479,14 +553,22 @@ export class PreparedQuery<
479553
/**
480554
* Like `first` except the row is returned
481555
* as an object containing key-value pairs.
556+
*
557+
* # Examples
558+
*
559+
* ```typescript
560+
* const query = db.prepareQuery<_, { id: number, name: string }>("SELECT id, name FROM people");
561+
* const person = query.first();
562+
* // { id: 1, name: "Peter" }
563+
* ```
482564
*/
483565
firstEntry(params?: P): O | undefined {
484566
const row = this.first(params);
485567
return row === undefined ? undefined : this.makeRowObject(row);
486568
}
487569

488570
/**
489-
* Deprecated, prefer `first`.
571+
* **Deprecated:** prefer `first`.
490572
*/
491573
one(params?: P): R {
492574
const rows = this.all(params);
@@ -501,7 +583,7 @@ export class PreparedQuery<
501583
}
502584

503585
/**
504-
* Deprecated, prefer `firstEntry`.
586+
* **Deprecated:** prefer `firstEntry`.
505587
*/
506588
oneEntry(params?: P): O {
507589
return this.makeRowObject(this.one(params));
@@ -516,16 +598,23 @@ export class PreparedQuery<
516598
* rows returned by a query are not needed or
517599
* the query does not return any rows.
518600
*
519-
* Calling `execute` invalidates any iterators
520-
* previously returned by calls to `iter`.
521-
* Using an invalidated iterator is a bug.
601+
* # Examples
522602
*
523-
* To avoid SQL injection, user-provided values
524-
* should always be passed to the database through
525-
* a query parameter.
603+
* ```typescript
604+
* const query = db.prepareQuery<_, _, [string]>("INSERT INTO people (name) VALUES (?)");
605+
* query.execute(["Peter"]);
606+
* ```
607+
*
608+
* ```typescript
609+
* const query = db.prepareQuery<_, _, { name: string }>("INSERT INTO people (name) VALUES (:name)");
610+
* query.execute({ name: "Peter" });
611+
* ```
526612
*
527613
* See `QueryParameterSet` for documentation on
528614
* how values can be bound to SQL statements.
615+
*
616+
* See `QueryParameter` for documentation on how
617+
* values are returned from the database.
529618
*/
530619
execute(params?: P) {
531620
this.startQuery(params);
@@ -544,10 +633,11 @@ export class PreparedQuery<
544633
* to avoid leaking resources.
545634
*
546635
* After a prepared query has been finalized,
547-
* trying to call `iter`, `all`, `one`,
548-
* `execute`, or `columns`, or using iterators which where
549-
* previously obtained from the finalized query
550-
* is a bug.
636+
* calls to `iter`, `all`, `first`, `execute`,
637+
* or `columns` will fail.
638+
*
639+
* Using iterators which were previously returned
640+
* from the finalized query will fail.
551641
*
552642
* `finalize` may safely be called multiple
553643
* times.

0 commit comments

Comments
 (0)