### Specifying Driver Typings for DB Connection (TypeScript) Source: https://github.com/kriasoft/node-sqlite/blob/master/README.md Illustrates how to provide specific typings for the underlying database driver and statement objects when opening a database connection using the `open` function. This example uses generics to specify `sqlite3.Database` and `sqlite3.Statement` types, assuming `@types/sqlite3` is installed. ```typescript // Assuming you have @types/sqlite3 installed import sqlite3 from 'sqlite3' // sqlite3.Database, sqlite3.Statement is the default if no explicit generic is specified await open({ filename: ':memory' }) ``` -------------------------------- ### Configuring Database Migrations (TypeScript) Source: https://github.com/kriasoft/node-sqlite/blob/master/README.md Shows the available configuration options for the `db.migrate()` method. Options include forcing migration re-application (`force`), specifying the migrations table name (`table`), and customizing the path to migration files (`migrationsPath`). This snippet defines the structure for the options object. ```typescript await db.migrate({ /** * If true, will force the migration API to rollback and re-apply the latest migration over * again each time when Node.js app launches. */ force?: boolean /** * Migrations table name. Default is 'migrations' */ table?: string /** * Path to the migrations folder. Default is `path.join(process.cwd(), 'migrations')` */ migrationsPath?: string }) ``` -------------------------------- ### Getting SQLite Database Driver - TypeScript Source: https://github.com/kriasoft/node-sqlite/blob/master/docs/interfaces/_src_interfaces_.isqlite.config.md This code snippet demonstrates how to import the `sqlite3` library and obtain the `Database` class, which serves as the database driver required by the `ISqlite.Config` interface. This driver is essential for establishing a connection to the sqlite database. ```TypeScript import sqlite from 'sqlite3'\n\nconst driver = sqlite.Database ``` -------------------------------- ### Initial Schema Migration (Up/Down) - SQL Source: https://github.com/kriasoft/node-sqlite/blob/master/migrations/README.md This SQL script defines the initial database schema. The 'Up' section creates the 'Category' and 'Post' tables with primary keys, foreign key constraints, check constraints, and an index. It also inserts initial data into 'Category'. The 'Down' section provides the SQL commands to drop the index and tables, reverting the schema to its state before this migration. ```sql -------------------------------------------------------------------------------- -- Up -------------------------------------------------------------------------------- CREATE TABLE Category ( id INTEGER PRIMARY KEY, name TEXT NOT NULL ); CREATE TABLE Post ( id INTEGER PRIMARY KEY, categoryId INTEGER NOT NULL, title TEXT NOT NULL, isPublished NUMERIC NOT NULL DEFAULT 0, CONSTRAINT Post_fk_categoryId FOREIGN KEY (categoryId) REFERENCES Category (id) ON UPDATE CASCADE ON DELETE CASCADE, CONSTRAINT Post_ck_isPublished CHECK (isPublished IN (0, 1)) ); CREATE INDEX Post_ix_categoryId ON Post (categoryId); INSERT INTO Category (id, name) VALUES (1, 'Test'); -------------------------------------------------------------------------------- -- Down -------------------------------------------------------------------------------- DROP INDEX Post_ix_categoryId; DROP TABLE Post; DROP TABLE Category; ``` -------------------------------- ### Add Test Table Migration (Up/Down) - SQL Source: https://github.com/kriasoft/node-sqlite/blob/master/migrations/README.md This SQL script introduces a new table for a specific feature. The 'Up' section creates a simple 'Test' table with a primary key and a text column. The 'Down' section provides the SQL command to drop the 'Test' table, reverting the schema changes made by the 'Up' script. ```sql -------------------------------------------------------------------------------- -- Up -------------------------------------------------------------------------------- CREATE TABLE Test ( id INTEGER PRIMARY KEY, name TEXT NOT NULL ); -------------------------------------------------------------------------------- -- Down -------------------------------------------------------------------------------- DROP TABLE Test; ``` -------------------------------- ### Querying SQLite with Tagged Templates (JavaScript) Source: https://github.com/kriasoft/node-sqlite/blob/master/README.md Demonstrates executing a simple SELECT query using `sql-template-strings` with ES6 tagged templates. This approach helps prevent SQL injection by properly escaping parameters. Requires the `sql-template-strings` and `sqlite` modules. ```javascript import SQL from 'sql-template-strings' const book = 'harry potter'; const author = 'J. K. Rowling'; const data = await db.all(SQL`SELECT author FROM books WHERE name = ${book} AND author = ${author}`); ``` -------------------------------- ### Using Generics for Single Row Result (TypeScript) Source: https://github.com/kriasoft/node-sqlite/blob/master/README.md Shows how to define an interface representing the expected row structure and use it as a generic parameter for the `db.get` method. This provides static type checking and IDE autocompletion for the properties of the returned single row object. ```typescript interface Row { col: string } // result will be of type Row, allowing Typescript supported IDEs to autocomplete on the properties! const result = await db.get('SELECT col FROM tbl WHERE col = ?', 'test') ``` -------------------------------- ### Using Generics for Multiple Row Results (TypeScript) Source: https://github.com/kriasoft/node-sqlite/blob/master/README.md Illustrates how to define an interface for a single row and use an array of that interface (`Row[]`) as a generic parameter for the `db.all` method. This provides type information for the array of rows returned and allows for type-safe iteration and access to row properties. ```typescript interface Row { col: string } // Result is an array of rows, you can now have array-autocompletion data const result = await db.all('SELECT col FROM tbl') result.each((row) => { // row should have type information now! }) ``` -------------------------------- ### Opening a SQLite Database (Node.js) Source: https://github.com/kriasoft/node-sqlite/blob/master/CHANGELOG.md This snippet demonstrates how to initialize a database connection using the `sqlite.open` function. It takes the database file path as an argument and returns a database object, allowing for managing multiple database connections. ```Javascript sqlite.open('db.sqlite') ``` -------------------------------- ### Running Database Migrations (Node.js) Source: https://github.com/kriasoft/node-sqlite/blob/master/CHANGELOG.md This snippet illustrates how to execute database migrations using the `migrate` method of a database object. The `{ force: 'last' }` option forces the re-application of the last migration, useful for development or testing. ```Javascript db.migrate({ force: 'last' }) ``` -------------------------------- ### Iterating Query Results with each (JavaScript) Source: https://github.com/kriasoft/node-sqlite/blob/master/docs/classes/_src_database_.database.md Demonstrates how to use the `each` method to asynchronously iterate over query results. It executes the SQL query with parameters and calls a callback function for each row fetched. The method resolves when all rows have been processed. ```JavaScript await db.each('SELECT * FROM x WHERE y = ?', 'z', (err, row) => { \n // row contains the row data\n // each() resolves when there are no more rows to fetch\n}) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.