Working with SQLite in TypeScript
SQLite database file handling with table loading and saving capabilities.
Installation
Section titled “Installation”npm install fairspecGetting Started
Section titled “Getting Started”The SQLite plugin provides:
loadSqliteTable- Load tables from SQLite databasessaveSqliteTable- Save tables to SQLite databasesSqlitePlugin- Plugin for framework integration
For example:
import { loadSqliteTable } from "fairspec"
const table = await loadSqliteTable({ data: "database.db", format: { name: "sqlite", tableName: "users" }})// column types will be automatically inferred from database schemaBasic Usage
Section titled “Basic Usage”Loading SQLite Tables
Section titled “Loading SQLite Tables”import { loadSqliteTable } from "fairspec"
// Load a table from SQLite databaseconst table = await loadSqliteTable({ data: "data.db", format: { name: "sqlite", tableName: "products" }})
// Load from a specific pathconst table = await loadSqliteTable({ data: "/path/to/database.db", format: { name: "sqlite", tableName: "orders" }})Saving SQLite Tables
Section titled “Saving SQLite Tables”import { saveSqliteTable } from "fairspec"
// Save table to SQLite databaseawait saveSqliteTable(table, { path: "output.db", format: { name: "sqlite", tableName: "results" }})
// Overwrite existing tableawait saveSqliteTable(table, { path: "output.db", format: { name: "sqlite", tableName: "results" }, overwrite: true})Advanced Features
Section titled “Advanced Features”Schema Inference
Section titled “Schema Inference”Table schemas are automatically inferred from SQLite table definitions:
// Field types are automatically detected from database schemaconst table = await loadSqliteTable({ data: "shop.db", format: { name: "sqlite", tableName: "products" }})// Types like INTEGER, TEXT, REAL are mapped to appropriate Table Schema typesCreating New Tables
Section titled “Creating New Tables”When saving, the plugin automatically creates the table structure:
import { saveSqliteTable } from "fairspec"
// Creates a new database file with the specified tableawait saveSqliteTable(table, { path: "new-database.db", format: { name: "sqlite", tableName: "my_data" }})Working with Table Schema
Section titled “Working with Table Schema”You can provide a custom Table Schema when saving:
import { saveSqliteTable } from "fairspec"
await saveSqliteTable(table, { path: "output.db", format: { name: "sqlite", tableName: "customers" }, tableSchema: { properties: { id: { type: "integer" }, name: { type: "string" }, email: { type: "string" } } }})