@@ -97,13 +97,19 @@ more data types than SQLite, only a subset of JavaScript types are supported.
9797Attempting to write an unsupported data type to SQLite will result in an
9898exception.
9999
100- | Storage class | JavaScript to SQLite | SQLite to JavaScript |
101- | ------------- | -------------------------- | ------------------------------------- |
102- | ` NULL ` | {null} | {null} |
103- | ` INTEGER ` | {number} or {bigint} | {number} or {bigint} _ (configurable)_ |
104- | ` REAL ` | {number} | {number} |
105- | ` TEXT ` | {string} | {string} |
106- | ` BLOB ` | {TypedArray} or {DataView} | {Uint8Array} |
100+ | Storage class | JavaScript to SQLite | SQLite to JavaScript |
101+ | ------------- | --------------------------------------------------------------- | ------------------------------------- |
102+ | ` NULL ` | {null} | {null} |
103+ | ` INTEGER ` | {number}, {bigint}, or {boolean} | {number} or {bigint} _ (configurable)_ |
104+ | ` REAL ` | {number} | {number} |
105+ | ` TEXT ` | {string} | {string} |
106+ | ` BLOB ` | {TypedArray}, {DataView}, {ArrayBuffer}, or {SharedArrayBuffer} | {Uint8Array} |
107+
108+ Booleans are written as the ` INTEGER ` values ` 1 ` and ` 0 ` . Like any other
109+ ` INTEGER ` value, they are read back as {number} by default, or as {bigint}
110+ values (` 1n ` and ` 0n ` ) when reading BigInts is enabled. Writing a {bigint} that
111+ does not fit in a signed 64-bit integer throws an ` ERR_INVALID_ARG_VALUE `
112+ error.
107113
108114APIs that read values from SQLite have a configuration option that determines
109115whether ` INTEGER ` values are converted to ` number ` or ` bigint ` in JavaScript,
@@ -832,6 +838,7 @@ added:
832838-->
833839
834840* ` changeset ` {Uint8Array} A binary changeset or patchset.
841+
835842* ` options ` {Object} The configuration options for how the changes will be applied.
836843 * ` filter ` {Function} for each table affected by at least
837844 one change in the changeset, the ` filter ` callback is invoked with the
@@ -860,6 +867,7 @@ added:
860867 applying the changeset is aborted and the database is rolled back.
861868
862869 ** Default** : A function that returns ` SQLITE_CHANGESET_ABORT ` .
870+
863871* Returns: {boolean} Whether the changeset was applied successfully without being aborted.
864872
865873An exception is thrown if the database is not
@@ -985,11 +993,61 @@ times with different bound values. Parameters also offer protection against
985993[ SQL injection] [ ] attacks. For these reasons, prepared statements are preferred
986994over hand-crafted SQL strings when handling user input.
987995
996+ ### Binding parameters
997+
998+ The ` all() ` , ` get() ` , ` iterate() ` , and ` run() ` methods bind their arguments to
999+ the parameters of the prepared statement before executing it. Parameters are
1000+ either anonymous or named.
1001+
1002+ Anonymous parameters are written as ` ? ` in SQL and are bound in order from the
1003+ arguments passed to the method. The ` ?NNN ` form assigns SQLite parameter index
1004+ ` NNN ` to a placeholder. Avoid mixing numbered and named parameters because they
1005+ share parameter indexes.
1006+
1007+ ``` js
1008+ db .prepare (' SELECT ? AS a, ? AS b' ).get (' x' , 42 );
1009+ // { a: 'x', b: 42 }
1010+ db .prepare (' SELECT ?2 AS a, ?1 AS b' ).get (' first' , ' second' );
1011+ // { a: 'second', b: 'first' }
1012+ ```
1013+
1014+ Named parameters begin with one of the prefix characters ` $ ` , ` : ` , or ` @ ` in
1015+ SQL. They are bound from an object passed as the first argument. Repeating a
1016+ name in the SQL binds the same value to every occurrence.
1017+
1018+ ``` js
1019+ db .prepare (' SELECT $a AS a, $b AS b' ).get ({ $a: 1 , $b: 2 });
1020+ // { a: 1, b: 2 }
1021+ db .prepare (' SELECT :a AS a' ).get ({ ' :a' : 1 });
1022+ // { a: 1 }
1023+ db .prepare (' SELECT @a AS a' ).get ({ ' @a' : 1 });
1024+ // { a: 1 }
1025+ db .prepare (' SELECT $k AS a, $k AS b' ).get ({ k: 7 });
1026+ // { a: 7, b: 7 }
1027+ ```
1028+
1029+ The last example omits the prefix character from the object key. Bare names are
1030+ allowed by default; see [ ` statement.setAllowBareNamedParameters() ` ] [ ] for their
1031+ caveats.
1032+
1033+ Binding a key that does not name a parameter of the statement throws an
1034+ ` ERR_INVALID_STATE ` error unless unknown named parameters are ignored. See
1035+ [ ` statement.setAllowUnknownNamedParameters() ` ] [ ] .
1036+
1037+ See [ Type conversion between JavaScript and SQLite] [ ] for the values that can be
1038+ bound. Binding any other value throws an ` ERR_INVALID_ARG_TYPE ` error.
1039+
9881040### ` statement.all([namedParameters][, ...anonymousParameters]) `
9891041
9901042<!-- YAML
9911043added: v22.5.0
9921044changes:
1045+ - version: REPLACEME
1046+ pr-url: https://github.com/nodejs/node/pull/62001
1047+ description: Add support for boolean values in bound parameters.
1048+ - version: REPLACEME
1049+ pr-url: https://github.com/nodejs/node/pull/62061
1050+ description: Add support for `ArrayBuffer` and `SharedArrayBuffer` objects in bound parameters.
9931051 - version:
9941052 - v23.7.0
9951053 - v22.14.0
@@ -999,16 +1057,17 @@ changes:
9991057
10001058* ` namedParameters ` {Object} An optional object used to bind named parameters.
10011059 The keys of this object are used to configure the mapping.
1002- * ` ...anonymousParameters ` {null|number|bigint|string|Buffer|TypedArray|DataView} Zero or
1003- more values to bind to anonymous parameters.
1060+ * ` ...anonymousParameters ` {null|number|bigint|boolean| string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer}
1061+ Zero or more values to bind to anonymous parameters.
10041062* Returns: {Array} An array of objects. Each object corresponds to a row
10051063 returned by executing the prepared statement. The keys and values of each
10061064 object correspond to the column names and values of the row.
10071065
10081066This method executes a prepared statement and returns all results as an array of
10091067objects. If the prepared statement does not return any results, this method
10101068returns an empty array. The prepared statement [ parameters are bound] [ ] using
1011- the values in ` namedParameters ` and ` anonymousParameters ` .
1069+ the values in ` namedParameters ` and ` anonymousParameters ` . See
1070+ [ Binding parameters] [ ] .
10121071
10131072### ` statement.close() `
10141073
@@ -1029,7 +1088,6 @@ added:
10291088
10301089* Returns: {Array} An array of objects. Each object corresponds to a column
10311090 in the prepared statement, and contains the following properties:
1032-
10331091 * ` column ` {string|null} The unaliased name of the column in the origin
10341092 table, or ` null ` if the column is the result of an expression or subquery.
10351093 This property is the result of [ ` sqlite3_column_origin_name() ` ] [ ] .
@@ -1067,6 +1125,12 @@ execution of this prepared statement. This property is a wrapper around
10671125<!-- YAML
10681126added: v22.5.0
10691127changes:
1128+ - version: REPLACEME
1129+ pr-url: https://github.com/nodejs/node/pull/62001
1130+ description: Add support for boolean values in bound parameters.
1131+ - version: REPLACEME
1132+ pr-url: https://github.com/nodejs/node/pull/62061
1133+ description: Add support for `ArrayBuffer` and `SharedArrayBuffer` objects in bound parameters.
10701134 - version:
10711135 - v23.7.0
10721136 - v22.14.0
@@ -1076,8 +1140,8 @@ changes:
10761140
10771141* ` namedParameters ` {Object} An optional object used to bind named parameters.
10781142 The keys of this object are used to configure the mapping.
1079- * ` ...anonymousParameters ` {null|number|bigint|string|Buffer|TypedArray|DataView} Zero or
1080- more values to bind to anonymous parameters.
1143+ * ` ...anonymousParameters ` {null|number|bigint|boolean| string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer}
1144+ Zero or more values to bind to anonymous parameters.
10811145* Returns: {Object|undefined} An object corresponding to the first row returned
10821146 by executing the prepared statement. The keys and values of the object
10831147 correspond to the column names and values of the row. If no rows were returned
@@ -1086,7 +1150,8 @@ changes:
10861150This method executes a prepared statement and returns the first result as an
10871151object. If the prepared statement does not return any results, this method
10881152returns ` undefined ` . The prepared statement [ parameters are bound] [ ] using the
1089- values in ` namedParameters ` and ` anonymousParameters ` .
1153+ values in ` namedParameters ` and ` anonymousParameters ` . See
1154+ [ Binding parameters] [ ] .
10901155
10911156### ` statement.iterate([namedParameters][, ...anonymousParameters]) `
10921157
@@ -1095,6 +1160,12 @@ added:
10951160 - v23.4.0
10961161 - v22.13.0
10971162changes:
1163+ - version: REPLACEME
1164+ pr-url: https://github.com/nodejs/node/pull/62001
1165+ description: Add support for boolean values in bound parameters.
1166+ - version: REPLACEME
1167+ pr-url: https://github.com/nodejs/node/pull/62061
1168+ description: Add support for `ArrayBuffer` and `SharedArrayBuffer` objects in bound parameters.
10981169 - version:
10991170 - v23.7.0
11001171 - v22.14.0
@@ -1104,22 +1175,29 @@ changes:
11041175
11051176* ` namedParameters ` {Object} An optional object used to bind named parameters.
11061177 The keys of this object are used to configure the mapping.
1107- * ` ...anonymousParameters ` {null|number|bigint|string|Buffer|TypedArray|DataView} Zero or
1108- more values to bind to anonymous parameters.
1178+ * ` ...anonymousParameters ` {null|number|bigint|boolean| string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer}
1179+ Zero or more values to bind to anonymous parameters.
11091180* Returns: {Iterator} An iterable iterator of objects. Each object corresponds to a row
11101181 returned by executing the prepared statement. The keys and values of each
11111182 object correspond to the column names and values of the row.
11121183
11131184This method executes a prepared statement and returns an iterator of
11141185objects. If the prepared statement does not return any results, this method
11151186returns an empty iterator. The prepared statement [ parameters are bound] [ ] using
1116- the values in ` namedParameters ` and ` anonymousParameters ` .
1187+ the values in ` namedParameters ` and ` anonymousParameters ` . See
1188+ [ Binding parameters] [ ] .
11171189
11181190### ` statement.run([namedParameters][, ...anonymousParameters]) `
11191191
11201192<!-- YAML
11211193added: v22.5.0
11221194changes:
1195+ - version: REPLACEME
1196+ pr-url: https://github.com/nodejs/node/pull/62001
1197+ description: Add support for boolean values in bound parameters.
1198+ - version: REPLACEME
1199+ pr-url: https://github.com/nodejs/node/pull/62061
1200+ description: Add support for `ArrayBuffer` and `SharedArrayBuffer` objects in bound parameters.
11231201 - version:
11241202 - v23.7.0
11251203 - v22.14.0
@@ -1129,8 +1207,8 @@ changes:
11291207
11301208* ` namedParameters ` {Object} An optional object used to bind named parameters.
11311209 The keys of this object are used to configure the mapping.
1132- * ` ...anonymousParameters ` {null|number|bigint|string|Buffer|TypedArray|DataView} Zero or
1133- more values to bind to anonymous parameters.
1210+ * ` ...anonymousParameters ` {null|number|bigint|boolean| string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer}
1211+ Zero or more values to bind to anonymous parameters.
11341212* Returns: {Object}
11351213 * ` changes ` {number|bigint} The number of rows modified, inserted, or deleted
11361214 by the most recently completed ` INSERT ` , ` UPDATE ` , or ` DELETE ` statement.
@@ -1144,7 +1222,8 @@ changes:
11441222
11451223This method executes a prepared statement and returns an object summarizing the
11461224resulting changes. The prepared statement [ parameters are bound] [ ] using the
1147- values in ` namedParameters ` and ` anonymousParameters ` .
1225+ values in ` namedParameters ` and ` anonymousParameters ` . See
1226+ [ Binding parameters] [ ] .
11481227
11491228### ` statement.setAllowBareNamedParameters(enabled) `
11501229
@@ -1155,14 +1234,15 @@ added: v22.5.0
11551234* ` enabled ` {boolean} Enables or disables support for binding named parameters
11561235 without the prefix character.
11571236
1158- The names of SQLite parameters begin with a prefix character. By default,
1159- ` node:sqlite ` allows binding named parameters without this prefix character in
1160- the parameter object. With the exception of the dollar sign character, these
1161- prefix characters require extra quoting when used in object keys.
1237+ The names of SQLite parameters begin with a prefix character. However, with the
1238+ exception of the dollar sign character, these prefix characters also require
1239+ extra quoting when used in object keys.
11621240
1163- This method enables or disables support for bare named parameters, which do not
1164- require the prefix character in JavaScript code. There are several caveats to
1165- be aware of when bare named parameters are enabled:
1241+ To improve ergonomics, ` node:sqlite ` allows bare named parameters, which do not
1242+ require the prefix character in JavaScript code, by default. This method can be
1243+ used to disable that behavior, requiring the prefix character when binding.
1244+ There are several caveats to be aware of when bare named parameters are
1245+ allowed:
11661246
11671247* The prefix character is still required in SQL.
11681248* The prefix character is still allowed in JavaScript. In fact, prefixed names
@@ -1256,11 +1336,18 @@ class execute synchronously.
12561336
12571337<!-- YAML
12581338added: v24.9.0
1339+ changes:
1340+ - version: REPLACEME
1341+ pr-url: https://github.com/nodejs/node/pull/62001
1342+ description: Add support for boolean values in bound parameters.
1343+ - version: REPLACEME
1344+ pr-url: https://github.com/nodejs/node/pull/62061
1345+ description: Add support for `ArrayBuffer` and `SharedArrayBuffer` objects in bound parameters.
12591346-->
12601347
12611348* ` stringElements ` {string\[ ] } Template literal elements containing the SQL
12621349 query.
1263- * ` ...boundParameters ` {null|number|bigint|string|Buffer|TypedArray|DataView}
1350+ * ` ...boundParameters ` {null|number|bigint|boolean| string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer }
12641351 Parameter values to be bound to placeholders in the template string.
12651352* Returns: {Array} An array of objects representing the rows returned by the query.
12661353
@@ -1274,11 +1361,18 @@ called directly.
12741361
12751362<!-- YAML
12761363added: v24.9.0
1364+ changes:
1365+ - version: REPLACEME
1366+ pr-url: https://github.com/nodejs/node/pull/62001
1367+ description: Add support for boolean values in bound parameters.
1368+ - version: REPLACEME
1369+ pr-url: https://github.com/nodejs/node/pull/62061
1370+ description: Add support for `ArrayBuffer` and `SharedArrayBuffer` objects in bound parameters.
12771371-->
12781372
12791373* ` stringElements ` {string\[ ] } Template literal elements containing the SQL
12801374 query.
1281- * ` ...boundParameters ` {null|number|bigint|string|Buffer|TypedArray|DataView}
1375+ * ` ...boundParameters ` {null|number|bigint|boolean| string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer }
12821376 Parameter values to be bound to placeholders in the template string.
12831377* Returns: {Object | undefined} An object representing the first row returned by
12841378 the query, or ` undefined ` if no rows are returned.
@@ -1292,11 +1386,18 @@ called directly.
12921386
12931387<!-- YAML
12941388added: v24.9.0
1389+ changes:
1390+ - version: REPLACEME
1391+ pr-url: https://github.com/nodejs/node/pull/62001
1392+ description: Add support for boolean values in bound parameters.
1393+ - version: REPLACEME
1394+ pr-url: https://github.com/nodejs/node/pull/62061
1395+ description: Add support for `ArrayBuffer` and `SharedArrayBuffer` objects in bound parameters.
12951396-->
12961397
12971398* ` stringElements ` {string\[ ] } Template literal elements containing the SQL
12981399 query.
1299- * ` ...boundParameters ` {null|number|bigint|string|Buffer|TypedArray|DataView}
1400+ * ` ...boundParameters ` {null|number|bigint|boolean| string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer }
13001401 Parameter values to be bound to placeholders in the template string.
13011402* Returns: {Iterator} An iterator that yields objects representing the rows returned by the query.
13021403
@@ -1309,11 +1410,18 @@ called directly.
13091410
13101411<!-- YAML
13111412added: v24.9.0
1413+ changes:
1414+ - version: REPLACEME
1415+ pr-url: https://github.com/nodejs/node/pull/62001
1416+ description: Add support for boolean values in bound parameters.
1417+ - version: REPLACEME
1418+ pr-url: https://github.com/nodejs/node/pull/62061
1419+ description: Add support for `ArrayBuffer` and `SharedArrayBuffer` objects in bound parameters.
13121420-->
13131421
13141422* ` stringElements ` {string\[ ] } Template literal elements containing the SQL
13151423 query.
1316- * ` ...boundParameters ` {null|number|bigint|string|Buffer|TypedArray|DataView}
1424+ * ` ...boundParameters ` {null|number|bigint|boolean| string|Buffer|TypedArray|DataView|ArrayBuffer|SharedArrayBuffer }
13171425 Parameter values to be bound to placeholders in the template string.
13181426* Returns: {Object} An object containing information about the execution, including ` changes ` and ` lastInsertRowid ` .
13191427
@@ -1679,6 +1787,7 @@ callback function to indicate what type of operation is being authorized.
16791787 </tr >
16801788</table >
16811789
1790+ [ Binding parameters ] : #binding-parameters
16821791[ Changesets and Patchsets ] : https://www.sqlite.org/sessionintro.html#changesets_and_patchsets
16831792[ Constants Passed To The Conflict Handler ] : https://www.sqlite.org/session/c_changeset_conflict.html
16841793[ Constants Returned From The Conflict Handler ] : https://www.sqlite.org/session/c_changeset_abort.html
@@ -1727,6 +1836,8 @@ callback function to indicate what type of operation is being authorized.
17271836[ `sqlite3session_create()` ] : https://www.sqlite.org/session/sqlite3session_create.html
17281837[ `sqlite3session_delete()` ] : https://www.sqlite.org/session/sqlite3session_delete.html
17291838[ `sqlite3session_patchset()` ] : https://www.sqlite.org/session/sqlite3session_patchset.html
1839+ [ `statement.setAllowBareNamedParameters()` ] : #statementsetallowbarenamedparametersenabled
1840+ [ `statement.setAllowUnknownNamedParameters()` ] : #statementsetallowunknownnamedparametersenabled
17301841[ busy timeout ] : https://sqlite.org/c3ref/busy_timeout.html
17311842[ connection ] : https://www.sqlite.org/c3ref/sqlite3.html
17321843[ data types ] : https://www.sqlite.org/datatype3.html
0 commit comments