@@ -467,15 +467,68 @@ added: v24.9.0
467467 ** Default:** ` 1000 ` .
468468* Returns: {SQLTagStore} A new SQL tag store for caching prepared statements.
469469
470- Creates a new ` SQLTagStore ` , which is an LRU ( Least Recently Used) cache for
471- storing prepared statements. This allows for the efficient reuse of prepared
472- statements by tagging them with a unique identifier.
470+ Creates a new [ ` SQLTagStore ` ] [ ] , which is a Least Recently Used (LRU ) cache
471+ for storing prepared statements. This allows for the efficient reuse of
472+ prepared statements by tagging them with a unique identifier.
473473
474474When a tagged SQL literal is executed, the ` SQLTagStore ` checks if a prepared
475- statement for that specific SQL string already exists in the cache. If it does,
476- the cached statement is used. If not, a new prepared statement is created,
477- executed, and then stored in the cache for future use. This mechanism helps to
478- avoid the overhead of repeatedly parsing and preparing the same SQL statements.
475+ statement for the corresponding SQL query string already exists in the cache.
476+ If it does, the cached statement is used. If not, a new prepared statement is
477+ created, executed, and then stored in the cache for future use. This mechanism
478+ helps to avoid the overhead of repeatedly parsing and preparing the same SQL
479+ statements.
480+
481+ Tagged statements bind the placeholder values from the template literal as
482+ parameters to the underlying prepared statement. For example:
483+
484+ ``` js
485+ sqlTagStore .get ` SELECT ${ value} ` ;
486+ ```
487+
488+ is equivalent to:
489+
490+ ``` js
491+ db .prepare (' SELECT ?' ).get (value);
492+ ```
493+
494+ However, in the first example, the tag store will cache the underlying prepared
495+ statement for future use.
496+
497+ > ** Note:** The ` ${value} ` syntax in tagged statements _ binds_ a parameter to
498+ > the prepared statement. This differs from its behavior in _ untagged_ template
499+ > literals, where it performs string interpolation.
500+ >
501+ > ``` js
502+ > // This a safe example of binding a parameter to a tagged statement.
503+ > sqlTagStore .run ` INSERT INTO t1 (id) VALUES (${ id} )` ;
504+ >
505+ > // This is an *unsafe* example of an untagged template string.
506+ > // `id` is interpolated into the query text as a string.
507+ > // This can lead to SQL injection and data corruption.
508+ > db .run (` INSERT INTO t1 (id) VALUES (${ id} )` );
509+ > ` ` `
510+
511+ The tag store will match a statement from the cache if the query strings
512+ (including the positions of any bound placeholders) are identical.
513+
514+ ` ` ` js
515+ // The following statements will match in the cache:
516+ sqlTagStore .get ` SELECT * FROM t1 WHERE id = ${ id} AND active = 1` ;
517+ sqlTagStore .get ` SELECT * FROM t1 WHERE id = ${ 12345 } AND active = 1` ;
518+
519+ // The following statements will not match, as the query strings
520+ // and bound placeholders differ:
521+ sqlTagStore .get ` SELECT * FROM t1 WHERE id = ${ id} AND active = 1` ;
522+ sqlTagStore .get ` SELECT * FROM t1 WHERE id = 12345 AND active = 1` ;
523+
524+ // The following statements will not match, as matches are case-sensitive:
525+ sqlTagStore .get ` SELECT * FROM t1 WHERE id = ${ id} AND active = 1` ;
526+ sqlTagStore .get ` select * from t1 where id = ${ id} and active = 1` ;
527+ ```
528+
529+ The only way of binding parameters in tagged statements is with the ` ${value} `
530+ syntax. Do not add parameter binding placeholders (` ? ` etc.) to the SQL query
531+ string itself.
479532
480533``` mjs
481534import { DatabaseSync } from ' node:sqlite' ;
@@ -886,65 +939,86 @@ added: v24.9.0
886939This class represents a single LRU (Least Recently Used) cache for storing
887940prepared statements.
888941
889- Instances of this class are created via the database.createTagStore() method,
890- not by using a constructor. The store caches prepared statements based on the
891- provided SQL query string. When the same query is seen again, the store
942+ Instances of this class are created via the [ ` database.createTagStore() ` ] [ ]
943+ method, not by using a constructor. The store caches prepared statements based
944+ on the provided SQL query string. When the same query is seen again, the store
892945retrieves the cached statement and safely applies the new values through
893946parameter binding, thereby preventing attacks like SQL injection.
894947
895948The cache has a maxSize that defaults to 1000 statements, but a custom size can
896- be provided (e.g., database.createTagStore(100)). All APIs exposed by this
949+ be provided (e.g., ` database.createTagStore(100) ` ). All APIs exposed by this
897950class execute synchronously.
898951
899- ### ` sqlTagStore.all(sqlTemplate [, ...values ]) `
952+ ### ` sqlTagStore.all(stringElements [, ...boundParameters ]) `
900953
901954<!-- YAML
902955added: v24.9.0
903956-->
904957
905- * ` sqlTemplate ` {Template Literal} A template literal containing the SQL query.
906- * ` ...values ` {any} Values to be interpolated into the template literal.
958+ * ` stringElements ` {string\[ ] } Template literal elements containing the SQL
959+ query.
960+ * ` ...boundParameters ` {null|number|bigint|string|Buffer|TypedArray|DataView}
961+ Parameter values to be bound to placeholders in the template string.
907962* Returns: {Array} An array of objects representing the rows returned by the query.
908963
909- Executes the given SQL query and returns all resulting rows as an array of objects.
964+ Executes the given SQL query and returns all resulting rows as an array of
965+ objects.
910966
911- ### ` sqlTagStore.get(sqlTemplate[, ...values]) `
967+ This function is intended to be used as a template literal tag, not to be
968+ called directly.
969+
970+ ### ` sqlTagStore.get(stringElements[, ...boundParameters]) `
912971
913972<!-- YAML
914973added: v24.9.0
915974-->
916975
917- * ` sqlTemplate ` {Template Literal} A template literal containing the SQL query.
918- * ` ...values ` {any} Values to be interpolated into the template literal.
976+ * ` stringElements ` {string\[ ] } Template literal elements containing the SQL
977+ query.
978+ * ` ...boundParameters ` {null|number|bigint|string|Buffer|TypedArray|DataView}
979+ Parameter values to be bound to placeholders in the template string.
919980* Returns: {Object | undefined} An object representing the first row returned by
920981 the query, or ` undefined ` if no rows are returned.
921982
922983Executes the given SQL query and returns the first resulting row as an object.
923984
924- ### ` sqlTagStore.iterate(sqlTemplate[, ...values]) `
985+ This function is intended to be used as a template literal tag, not to be
986+ called directly.
987+
988+ ### ` sqlTagStore.iterate(stringElements[, ...boundParameters]) `
925989
926990<!-- YAML
927991added: v24.9.0
928992-->
929993
930- * ` sqlTemplate ` {Template Literal} A template literal containing the SQL query.
931- * ` ...values ` {any} Values to be interpolated into the template literal.
994+ * ` stringElements ` {string\[ ] } Template literal elements containing the SQL
995+ query.
996+ * ` ...boundParameters ` {null|number|bigint|string|Buffer|TypedArray|DataView}
997+ Parameter values to be bound to placeholders in the template string.
932998* Returns: {Iterator} An iterator that yields objects representing the rows returned by the query.
933999
9341000Executes the given SQL query and returns an iterator over the resulting rows.
9351001
936- ### ` sqlTagStore.run(sqlTemplate[, ...values]) `
1002+ This function is intended to be used as a template literal tag, not to be
1003+ called directly.
1004+
1005+ ### ` sqlTagStore.run(stringElements[, ...boundParameters]) `
9371006
9381007<!-- YAML
9391008added: v24.9.0
9401009-->
9411010
942- * ` sqlTemplate ` {Template Literal} A template literal containing the SQL query.
943- * ` ...values ` {any} Values to be interpolated into the template literal.
1011+ * ` stringElements ` {string\[ ] } Template literal elements containing the SQL
1012+ query.
1013+ * ` ...boundParameters ` {null|number|bigint|string|Buffer|TypedArray|DataView}
1014+ Parameter values to be bound to placeholders in the template string.
9441015* Returns: {Object} An object containing information about the execution, including ` changes ` and ` lastInsertRowid ` .
9451016
9461017Executes the given SQL query, which is expected to not return any rows (e.g., INSERT, UPDATE, DELETE).
9471018
1019+ This function is intended to be used as a template literal tag, not to be
1020+ called directly.
1021+
9481022### ` sqlTagStore.size() `
9491023
9501024<!-- YAML
@@ -961,7 +1035,7 @@ A read-only property that returns the number of prepared statements currently in
9611035added: v24.9.0
9621036-->
9631037
964- * Returns : {integer} The maximum number of prepared statements the cache can hold.
1038+ * Type : {integer}
9651039
9661040A read-only property that returns the maximum number of prepared statements the cache can hold.
9671041
@@ -971,25 +1045,17 @@ A read-only property that returns the maximum number of prepared statements the
9711045added: v24.9.0
9721046-->
9731047
974- * {DatabaseSync} The ` DatabaseSync ` instance that created this ` SQLTagStore ` .
1048+ * Type: {DatabaseSync}
9751049
9761050A read-only property that returns the ` DatabaseSync ` object associated with this ` SQLTagStore ` .
9771051
978- ### ` sqlTagStore.reset() `
979-
980- <!-- YAML
981- added: v24.9.0
982- -->
983-
984- Resets the LRU cache, clearing all stored prepared statements.
985-
9861052### ` sqlTagStore.clear() `
9871053
9881054<!-- YAML
9891055added: v24.9.0
9901056-->
9911057
992- An alias for ` sqlTagStore.reset() ` .
1058+ Resets the LRU cache, clearing all stored prepared statements .
9931059
9941060### Type conversion between JavaScript and SQLite
9951061
@@ -1331,7 +1397,9 @@ callback function to indicate what type of operation is being authorized.
13311397[ `SQLITE_DETERMINISTIC` ] : https://www.sqlite.org/c3ref/c_deterministic.html
13321398[ `SQLITE_DIRECTONLY` ] : https://www.sqlite.org/c3ref/c_deterministic.html
13331399[ `SQLITE_MAX_FUNCTION_ARG` ] : https://www.sqlite.org/limits.html#max_function_arg
1400+ [ `SQLTagStore` ] : #class-sqltagstore
13341401[ `database.applyChangeset()` ] : #databaseapplychangesetchangeset-options
1402+ [ `database.createTagStore()` ] : #databasecreatetagstoremaxsize
13351403[ `database.setAuthorizer()` ] : #databasesetauthorizercallback
13361404[ `sqlite3_backup_finish()` ] : https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupfinish
13371405[ `sqlite3_backup_init()` ] : https://www.sqlite.org/c3ref/backup_finish.html#sqlite3backupinit
0 commit comments