| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -473,15 +473,68 @@ added: v24.9.0 | |||
| 473 | 473 | **Default:** `1000`. | |
| 474 | 474 | * Returns: {SQLTagStore} A new SQL tag store for caching prepared statements. | |
| 475 | 475 | ||
| 476 | - Creates a new `SQLTagStore`, which is an LRU (Least Recently Used) cache for | ||
| 477 | - storing prepared statements. This allows for the efficient reuse of prepared | ||
| 478 | - statements by tagging them with a unique identifier. | ||
| 476 | + Creates a new [`SQLTagStore`][], which is a Least Recently Used (LRU) cache | ||
| 477 | + for storing prepared statements. This allows for the efficient reuse of | ||
| 478 | + prepared statements by tagging them with a unique identifier. | ||
| 479 | 479 | ||
| 480 | 480 | When a tagged SQL literal is executed, the `SQLTagStore` checks if a prepared | |
| 481 | - statement for that specific SQL string already exists in the cache. If it does, | ||
| 482 | - the cached statement is used. If not, a new prepared statement is created, | ||
| 483 | - executed, and then stored in the cache for future use. This mechanism helps to | ||
| 484 | - avoid the overhead of repeatedly parsing and preparing the same SQL statements. | ||
| 481 | + statement for the corresponding SQL query string already exists in the cache. | ||
| 482 | + If it does, the cached statement is used. If not, a new prepared statement is | ||
| 483 | + created, executed, and then stored in the cache for future use. This mechanism | ||
| 484 | + helps to avoid the overhead of repeatedly parsing and preparing the same SQL | ||
| 485 | + statements. | ||
| 486 | + | ||
| 487 | + Tagged statements bind the placeholder values from the template literal as | ||
| 488 | + parameters to the underlying prepared statement. For example: | ||
| 489 | + | ||
| 490 | + ```js | ||
| 491 | + sqlTagStore.get`SELECT ${value}`; | ||
| 492 | + ``` | ||
| 493 | + | ||
| 494 | + is equivalent to: | ||
| 495 | + | ||
| 496 | + ```js | ||
| 497 | + db.prepare('SELECT ?').get(value); | ||
| 498 | + ``` | ||
| 499 | + | ||
| 500 | + However, in the first example, the tag store will cache the underlying prepared | ||
| 501 | + statement for future use. | ||
| 502 | + | ||
| 503 | + > **Note:** The `${value}` syntax in tagged statements _binds_ a parameter to | ||
| 504 | + > the prepared statement. This differs from its behavior in _untagged_ template | ||
| 505 | + > literals, where it performs string interpolation. | ||
| 506 | + > | ||
| 507 | + > ```js | ||
| 508 | + > // This a safe example of binding a parameter to a tagged statement. | ||
| 509 | + > sqlTagStore.run`INSERT INTO t1 (id) VALUES (${id})`; | ||
| 510 | + > | ||
| 511 | + > // This is an *unsafe* example of an untagged template string. | ||
| 512 | + > // `id` is interpolated into the query text as a string. | ||
| 513 | + > // This can lead to SQL injection and data corruption. | ||
| 514 | + > db.run(`INSERT INTO t1 (id) VALUES (${id})`); | ||
| 515 | + > ``` | ||
| 516 | + | ||
| 517 | + The tag store will match a statement from the cache if the query strings | ||
| 518 | + (including the positions of any bound placeholders) are identical. | ||
| 519 | + | ||
| 520 | + ```js | ||
| 521 | + // The following statements will match in the cache: | ||
| 522 | + sqlTagStore.get`SELECT * FROM t1 WHERE id = ${id} AND active = 1`; | ||
| 523 | + sqlTagStore.get`SELECT * FROM t1 WHERE id = ${12345} AND active = 1`; | ||
| 524 | + | ||
| 525 | + // The following statements will not match, as the query strings | ||
| 526 | + // and bound placeholders differ: | ||
| 527 | + sqlTagStore.get`SELECT * FROM t1 WHERE id = ${id} AND active = 1`; | ||
| 528 | + sqlTagStore.get`SELECT * FROM t1 WHERE id = 12345 AND active = 1`; | ||
| 529 | + | ||
| 530 | + // The following statements will not match, as matches are case-sensitive: | ||
| 531 | + sqlTagStore.get`SELECT * FROM t1 WHERE id = ${id} AND active = 1`; | ||
| 532 | + sqlTagStore.get`select * from t1 where id = ${id} and active = 1`; | ||
| 533 | + ``` | ||
| 534 | + | ||
| 535 | + The only way of binding parameters in tagged statements is with the `${value}` | ||
| 536 | + syntax. Do not add parameter binding placeholders (`?` etc.) to the SQL query | ||
| 537 | + string itself. | ||
| 485 | 538 | ||
| 486 | 539 | ```mjs | |
| 487 | 540 | import { DatabaseSync } from 'node:sqlite'; | |
@@ -941,65 +994,86 @@ added: v24.9.0 | |||
| 941 | 994 | This class represents a single LRU (Least Recently Used) cache for storing | |
| 942 | 995 | prepared statements. | |
| 943 | 996 | ||
| 944 | - Instances of this class are created via the database.createTagStore() method, | ||
| 945 | - not by using a constructor. The store caches prepared statements based on the | ||
| 946 | - provided SQL query string. When the same query is seen again, the store | ||
| 997 | + Instances of this class are created via the [`database.createTagStore()`][] | ||
| 998 | + method, not by using a constructor. The store caches prepared statements based | ||
| 999 | + on the provided SQL query string. When the same query is seen again, the store | ||
| 947 | 1000 | retrieves the cached statement and safely applies the new values through | |
| 948 | 1001 | parameter binding, thereby preventing attacks like SQL injection. | |
| 949 | 1002 | ||
| 950 | 1003 | The cache has a maxSize that defaults to 1000 statements, but a custom size can | |
| 951 | - be provided (e.g., database.createTagStore(100)). All APIs exposed by this | ||
| 1004 | + be provided (e.g., `database.createTagStore(100)`). All APIs exposed by this | ||
| 952 | 1005 | class execute synchronously. | |
| 953 | 1006 | ||
| 954 | - ### `sqlTagStore.all(sqlTemplate[, ...values])` | ||
| 1007 | + ### `sqlTagStore.all(stringElements[, ...boundParameters])` | ||
| 955 | 1008 | ||
| 956 | 1009 | <!-- YAML | |
| 957 | 1010 | added: v24.9.0 | |
| 958 | 1011 | --> | |
| 959 | 1012 | ||
| 960 | - * `sqlTemplate` {Template Literal} A template literal containing the SQL query. | ||
| 961 | - * `...values` {any} Values to be interpolated into the template literal. | ||
| 1013 | + * `stringElements` {string\[]} Template literal elements containing the SQL | ||
| 1014 | + query. | ||
| 1015 | + * `...boundParameters` {null|number|bigint|string|Buffer|TypedArray|DataView} | ||
| 1016 | + Parameter values to be bound to placeholders in the template string. | ||
| 962 | 1017 | * Returns: {Array} An array of objects representing the rows returned by the query. | |
| 963 | 1018 | ||
| 964 | - Executes the given SQL query and returns all resulting rows as an array of objects. | ||
| 1019 | + Executes the given SQL query and returns all resulting rows as an array of | ||
| 1020 | + objects. | ||
| 965 | 1021 | ||
| 966 | - ### `sqlTagStore.get(sqlTemplate[, ...values])` | ||
| 1022 | + This function is intended to be used as a template literal tag, not to be | ||
| 1023 | + called directly. | ||
| 1024 | + | ||
| 1025 | + ### `sqlTagStore.get(stringElements[, ...boundParameters])` | ||
| 967 | 1026 | ||
| 968 | 1027 | <!-- YAML | |
| 969 | 1028 | added: v24.9.0 | |
| 970 | 1029 | --> | |
| 971 | 1030 | ||
| 972 | - * `sqlTemplate` {Template Literal} A template literal containing the SQL query. | ||
| 973 | - * `...values` {any} Values to be interpolated into the template literal. | ||
| 1031 | + * `stringElements` {string\[]} Template literal elements containing the SQL | ||
| 1032 | + query. | ||
| 1033 | + * `...boundParameters` {null|number|bigint|string|Buffer|TypedArray|DataView} | ||
| 1034 | + Parameter values to be bound to placeholders in the template string. | ||
| 974 | 1035 | * Returns: {Object | undefined} An object representing the first row returned by | |
| 975 | 1036 | the query, or `undefined` if no rows are returned. | |
| 976 | 1037 | ||
| 977 | 1038 | Executes the given SQL query and returns the first resulting row as an object. | |
| 978 | 1039 | ||
| 979 | - ### `sqlTagStore.iterate(sqlTemplate[, ...values])` | ||
| 1040 | + This function is intended to be used as a template literal tag, not to be | ||
| 1041 | + called directly. | ||
| 1042 | + | ||
| 1043 | + ### `sqlTagStore.iterate(stringElements[, ...boundParameters])` | ||
| 980 | 1044 | ||
| 981 | 1045 | <!-- YAML | |
| 982 | 1046 | added: v24.9.0 | |
| 983 | 1047 | --> | |
| 984 | 1048 | ||
| 985 | - * `sqlTemplate` {Template Literal} A template literal containing the SQL query. | ||
| 986 | - * `...values` {any} Values to be interpolated into the template literal. | ||
| 1049 | + * `stringElements` {string\[]} Template literal elements containing the SQL | ||
| 1050 | + query. | ||
| 1051 | + * `...boundParameters` {null|number|bigint|string|Buffer|TypedArray|DataView} | ||
| 1052 | + Parameter values to be bound to placeholders in the template string. | ||
| 987 | 1053 | * Returns: {Iterator} An iterator that yields objects representing the rows returned by the query. | |
| 988 | 1054 | ||
| 989 | 1055 | Executes the given SQL query and returns an iterator over the resulting rows. | |
| 990 | 1056 | ||
| 991 | - ### `sqlTagStore.run(sqlTemplate[, ...values])` | ||
| 1057 | + This function is intended to be used as a template literal tag, not to be | ||
| 1058 | + called directly. | ||
| 1059 | + | ||
| 1060 | + ### `sqlTagStore.run(stringElements[, ...boundParameters])` | ||
| 992 | 1061 | ||
| 993 | 1062 | <!-- YAML | |
| 994 | 1063 | added: v24.9.0 | |
| 995 | 1064 | --> | |
| 996 | 1065 | ||
| 997 | - * `sqlTemplate` {Template Literal} A template literal containing the SQL query. | ||
| 998 | - * `...values` {any} Values to be interpolated into the template literal. | ||
| 1066 | + * `stringElements` {string\[]} Template literal elements containing the SQL | ||
| 1067 | + query. | ||
| 1068 | + * `...boundParameters` {null|number|bigint|string|Buffer|TypedArray|DataView} | ||
| 1069 | + Parameter values to be bound to placeholders in the template string. | ||
| 999 | 1070 | * Returns: {Object} An object containing information about the execution, including `changes` and `lastInsertRowid`. | |
| 1000 | 1071 | ||
| 1001 | 1072 | Executes the given SQL query, which is expected to not return any rows (e.g., INSERT, UPDATE, DELETE). | |
| 1002 | 1073 | ||
| 1074 | + This function is intended to be used as a template literal tag, not to be | ||
| 1075 | + called directly. | ||
| 1076 | + | ||
| 1003 | 1077 | ### `sqlTagStore.size()` | |
| 1004 | 1078 | ||
| 1005 | 1079 | <!-- YAML | |
@@ -1016,7 +1090,7 @@ A read-only property that returns the number of prepared statements currently in | |||
| 1016 | 1090 | added: v24.9.0 | |
| 1017 | 1091 | --> | |
| 1018 | 1092 | ||
| 1019 | - * Returns: {integer} The maximum number of prepared statements the cache can hold. | ||
| 1093 | + * Type: {integer} | ||
| 1020 | 1094 | ||
| 1021 | 1095 | A read-only property that returns the maximum number of prepared statements the cache can hold. | |
| 1022 | 1096 | ||
@@ -1026,25 +1100,17 @@ A read-only property that returns the maximum number of prepared statements the | |||
| 1026 | 1100 | added: v24.9.0 | |
| 1027 | 1101 | --> | |
| 1028 | 1102 | ||
| 1029 | - * {DatabaseSync} The `DatabaseSync` instance that created this `SQLTagStore`. | ||
| 1103 | + * Type: {DatabaseSync} | ||
| 1030 | 1104 | ||
| 1031 | 1105 | A read-only property that returns the `DatabaseSync` object associated with this `SQLTagStore`. | |
| 1032 | 1106 | ||
| 1033 | - ### `sqlTagStore.reset()` | ||
| 1034 | - | ||
| 1035 | - <!-- YAML | ||
| 1036 | - added: v24.9.0 | ||
| 1037 | - --> | ||
| 1038 | - | ||
| 1039 | - Resets the LRU cache, clearing all stored prepared statements. | ||
| 1040 | - | ||
| 1041 | 1107 | ### `sqlTagStore.clear()` | |
| 1042 | 1108 | ||
| 1043 | 1109 | <!-- YAML | |
| 1044 | 1110 | added: v24.9.0 | |
| 1045 | 1111 | --> | |
| 1046 | 1112 | ||
| 1047 | - An alias for `sqlTagStore.reset()`. | ||
| 1113 | + Resets the LRU cache, clearing all stored prepared statements. | ||
| 1048 | 1114 | ||
| 1049 | 1115 | ### Type conversion between JavaScript and SQLite | |
| 1050 | 1116 | ||
@@ -1386,7 +1452,9 @@ callback function to indicate what type of operation is being authorized. | |||
| 1386 | 1452 | [`SQLITE_DETERMINISTIC`]: https://www.sqlite.org/c3ref/c_deterministic.html | |
| 1387 | 1453 | [`SQLITE_DIRECTONLY`]: https://www.sqlite.org/c3ref/c_deterministic.html | |
| 1388 | 1454 | [`SQLITE_MAX_FUNCTION_ARG`]: https://www.sqlite.org/limits.html#max_function_arg | |
| 1455 | + [`SQLTagStore`]: #class-sqltagstore | ||
| 1389 | 1456 | [`database.applyChangeset()`]: #databaseapplychangesetchangeset-options | |
| 1457 | + [`database.createTagStore()`]: #databasecreatetagstoremaxsize | ||
| 1390 | 1458 | [`database.setAuthorizer()`]: #databasesetauthorizercallback | |
| 1391 | 1459 | [`sqlite3_backup_finish()`]: https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupfinish | |
| 1392 | 1460 | [`sqlite3_backup_init()`]: https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupinit | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -96,8 +96,6 @@ const customTypesMap = { | |||
| 96 | 96 | 'EncapsulatedBits': 'webcrypto.html#class-encapsulatedbits', | |
| 97 | 97 | 'EncapsulatedKey': 'webcrypto.html#class-encapsulatedkey', | |
| 98 | 98 | 'SubtleCrypto': 'webcrypto.html#class-subtlecrypto', | |
| 99 | - 'Template Literal': | ||
| 100 | - `${jsDocPrefix}Reference/Template_literals`, | ||
| 101 | 99 | 'RsaOaepParams': 'webcrypto.html#class-rsaoaepparams', | |
| 102 | 100 | 'AesCtrParams': 'webcrypto.html#class-aesctrparams', | |
| 103 | 101 | 'AesCbcParams': 'webcrypto.html#class-aescbcparams', | |
| Back | FazBrowse Home | New Git URL |
0 commit comments