Skip to content

Working with SQLite in TypeScript

SQLite database file handling with table loading and saving capabilities.

Terminal window
npm install fairspec

The SQLite plugin provides:

  • loadSqliteTable - Load tables from SQLite databases
  • saveSqliteTable - Save tables to SQLite databases
  • SqlitePlugin - 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 schema
import { loadSqliteTable } from "fairspec"
// Load a table from SQLite database
const table = await loadSqliteTable({
data: "data.db",
format: {
name: "sqlite",
tableName: "products"
}
})
// Load from a specific path
const table = await loadSqliteTable({
data: "/path/to/database.db",
format: {
name: "sqlite",
tableName: "orders"
}
})
import { saveSqliteTable } from "fairspec"
// Save table to SQLite database
await saveSqliteTable(table, {
path: "output.db",
format: {
name: "sqlite",
tableName: "results"
}
})
// Overwrite existing table
await saveSqliteTable(table, {
path: "output.db",
format: {
name: "sqlite",
tableName: "results"
},
overwrite: true
})

Table schemas are automatically inferred from SQLite table definitions:

// Field types are automatically detected from database schema
const table = await loadSqliteTable({
data: "shop.db",
format: {
name: "sqlite",
tableName: "products"
}
})
// Types like INTEGER, TEXT, REAL are mapped to appropriate Table Schema types

When saving, the plugin automatically creates the table structure:

import { saveSqliteTable } from "fairspec"
// Creates a new database file with the specified table
await saveSqliteTable(table, {
path: "new-database.db",
format: {
name: "sqlite",
tableName: "my_data"
}
})

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" }
}
}
})