GeezerVison is showing github.com — sqlite: aggregate constants in a single property · nodejs/node@0ab36e1
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: doc/api/sqlite.md +12-4Lines changed: 12 additions & 4 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -472,11 +472,19 @@ exception. | 472 | 472 | | `TEXT` | {string} | | 473 | 473 | | `BLOB` | {Uint8Array} | | 474 | 474 |
| 475 | | -## SQLite constants | | 475 | +## `sqlite.constants` | 476 | 476 |
| 477 | | -The following constants are exported by the `node:sqlite` module. | | 477 | +<!-- YAML | | 478 | +added: REPLACEME | | 479 | +--> | | 480 | + | | 481 | +* {Object} | | 482 | + | | 483 | +An object containing commonly used constants for SQLite operations. | | 484 | + | | 485 | +### SQLite constants | 478 | 486 |
| 479 | | -### SQLite Session constants | | 487 | +The following constants are exported by the `sqlite.constants` object. | 480 | 488 |
| 481 | 489 | #### Conflict-resolution constants | 482 | 490 |
| @@ -497,7 +505,7 @@ The following constants are meant for use with [`database.applyChangeset()`](#da | 497 | 505 | </tr> | 498 | 506 | <tr> | 499 | 507 | <td><code>SQLITE_CHANGESET_ABORT</code></td> | 500 | | - <td>Abort when a change encounters a conflict and roll back databsase.</td> | | 508 | + <td>Abort when a change encounters a conflict and roll back database.</td> | 501 | 509 | </tr> | 502 | 510 | </table> | 503 | 511 |
| |
Collapse file Copy file name to clipboardExpand all lines: src/node_sqlite.cc +10-3Lines changed: 10 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -1658,6 +1658,12 @@ void Session::Delete() { | 1658 | 1658 | session_ = nullptr; | 1659 | 1659 | } | 1660 | 1660 |
| | 1661 | +void DefineConstants(Local<Object> target) { | | 1662 | + NODE_DEFINE_CONSTANT(target, SQLITE_CHANGESET_OMIT); | | 1663 | + NODE_DEFINE_CONSTANT(target, SQLITE_CHANGESET_REPLACE); | | 1664 | + NODE_DEFINE_CONSTANT(target, SQLITE_CHANGESET_ABORT); | | 1665 | +} | | 1666 | + | 1661 | 1667 | static void Initialize(Local<Object> target, | 1662 | 1668 | Local<Value> unused, | 1663 | 1669 | Local<Context> context, | @@ -1668,6 +1674,9 @@ static void Initialize(Local<Object> target, | 1668 | 1674 | NewFunctionTemplate(isolate, DatabaseSync::New); | 1669 | 1675 | db_tmpl->InstanceTemplate()->SetInternalFieldCount( | 1670 | 1676 | DatabaseSync::kInternalFieldCount); | | 1677 | + Local<Object> constants = Object::New(isolate); | | 1678 | + | | 1679 | + DefineConstants(constants); | 1671 | 1680 |
| 1672 | 1681 | SetProtoMethod(isolate, db_tmpl, "open", DatabaseSync::Open); | 1673 | 1682 | SetProtoMethod(isolate, db_tmpl, "close", DatabaseSync::Close); | @@ -1690,9 +1699,7 @@ static void Initialize(Local<Object> target, | 1690 | 1699 | "StatementSync", | 1691 | 1700 | StatementSync::GetConstructorTemplate(env)); | 1692 | 1701 |
| 1693 | | - NODE_DEFINE_CONSTANT(target, SQLITE_CHANGESET_OMIT); | 1694 | | - NODE_DEFINE_CONSTANT(target, SQLITE_CHANGESET_REPLACE); | 1695 | | - NODE_DEFINE_CONSTANT(target, SQLITE_CHANGESET_ABORT); | | 1702 | + target->Set(context, OneByteString(isolate, "constants"), constants).Check(); | 1696 | 1703 | } | 1697 | 1704 |
| 1698 | 1705 | } // namespace sqlite | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-sqlite-session.js +4-12Lines changed: 4 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 3 | 3 | require('../common'); | 4 | 4 | const { | 5 | 5 | DatabaseSync, | 6 | | - SQLITE_CHANGESET_OMIT, | 7 | | - SQLITE_CHANGESET_REPLACE, | 8 | | - SQLITE_CHANGESET_ABORT | | 6 | + constants, | 9 | 7 | } = require('node:sqlite'); | 10 | 8 | const { test, suite } = require('node:test'); | 11 | 9 |
| @@ -165,7 +163,7 @@ suite('conflict resolution', () => { | 165 | 163 | test('database.applyChangeset() - conflict with SQLITE_CHANGESET_ABORT', (t) => { | 166 | 164 | const { database2, changeset } = prepareConflict(); | 167 | 165 | const result = database2.applyChangeset(changeset, { | 168 | | - onConflict: SQLITE_CHANGESET_ABORT | | 166 | + onConflict: constants.SQLITE_CHANGESET_ABORT | 169 | 167 | }); | 170 | 168 | // When changeset is aborted due to a conflict, applyChangeset should return false | 171 | 169 | t.assert.strictEqual(result, false); | @@ -177,7 +175,7 @@ suite('conflict resolution', () => { | 177 | 175 | test('database.applyChangeset() - conflict with SQLITE_CHANGESET_REPLACE', (t) => { | 178 | 176 | const { database2, changeset } = prepareConflict(); | 179 | 177 | const result = database2.applyChangeset(changeset, { | 180 | | - onConflict: SQLITE_CHANGESET_REPLACE | | 178 | + onConflict: constants.SQLITE_CHANGESET_REPLACE | 181 | 179 | }); | 182 | 180 | // Not aborted due to conflict, so should return true | 183 | 181 | t.assert.strictEqual(result, true); | @@ -189,7 +187,7 @@ suite('conflict resolution', () => { | 189 | 187 | test('database.applyChangeset() - conflict with SQLITE_CHANGESET_OMIT', (t) => { | 190 | 188 | const { database2, changeset } = prepareConflict(); | 191 | 189 | const result = database2.applyChangeset(changeset, { | 192 | | - onConflict: SQLITE_CHANGESET_OMIT | | 190 | + onConflict: constants.SQLITE_CHANGESET_OMIT | 193 | 191 | }); | 194 | 192 | // Not aborted due to conflict, so should return true | 195 | 193 | t.assert.strictEqual(result, true); | @@ -199,12 +197,6 @@ suite('conflict resolution', () => { | 199 | 197 | }); | 200 | 198 | }); | 201 | 199 |
| 202 | | -test('session related constants are defined', (t) => { | 203 | | - t.assert.strictEqual(SQLITE_CHANGESET_OMIT, 0); | 204 | | - t.assert.strictEqual(SQLITE_CHANGESET_REPLACE, 1); | 205 | | - t.assert.strictEqual(SQLITE_CHANGESET_ABORT, 2); | 206 | | -}); | 207 | | - | 208 | 200 | test('database.createSession() - filter changes', (t) => { | 209 | 201 | const database1 = new DatabaseSync(':memory:'); | 210 | 202 | const database2 = new DatabaseSync(':memory:'); | |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-sqlite.js +7-1Lines changed: 7 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 2 | 2 | const { spawnPromisified } = require('../common'); | 3 | 3 | const tmpdir = require('../common/tmpdir'); | 4 | 4 | const { join } = require('node:path'); | 5 | | -const { DatabaseSync } = require('node:sqlite'); | | 5 | +const { DatabaseSync, constants } = require('node:sqlite'); | 6 | 6 | const { suite, test } = require('node:test'); | 7 | 7 | let cnt = 0; | 8 | 8 |
| @@ -85,6 +85,12 @@ test('in-memory databases are supported', (t) => { | 85 | 85 | ); | 86 | 86 | }); | 87 | 87 |
| | 88 | +test('sqlite constants are defined', (t) => { | | 89 | + t.assert.strictEqual(constants.SQLITE_CHANGESET_OMIT, 0); | | 90 | + t.assert.strictEqual(constants.SQLITE_CHANGESET_REPLACE, 1); | | 91 | + t.assert.strictEqual(constants.SQLITE_CHANGESET_ABORT, 2); | | 92 | +}); | | 93 | + | 88 | 94 | test('PRAGMAs are supported', (t) => { | 89 | 95 | const db = new DatabaseSync(nextDb()); | 90 | 96 | t.after(() => { db.close(); }); | |
Collapse file Copy file name to clipboardExpand all lines: typings/internalBinding/constants.d.ts +5Lines changed: 5 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -130,6 +130,11 @@ export interface ConstantsBinding { | 130 | 130 | PRIORITY_HIGHEST: -20; | 131 | 131 | }; | 132 | 132 | }; | | 133 | + sqlite: { | | 134 | + SQLITE_CHANGESET_OMIT: 0; | | 135 | + SQLITE_CHANGESET_REPLACE: 1; | | 136 | + SQLITE_CHANGESET_ABORT: 2; | | 137 | + }; | 133 | 138 | fs: { | 134 | 139 | UV_FS_SYMLINK_DIR: 1; | 135 | 140 | UV_FS_SYMLINK_JUNCTION: 2; | |
|