S42-Core SQLite
SQLIte
- SQLIte
- Overview
- Purpose
- Key Features
- Methods
constructor(props: TypeSQLiteConnection)
close()
createTable(tableName: string, schema: TypeTableSchema)
dropTable(tableName: string)
delete(tableName: string, where: string, params: any[])
insert(tableName: string, data: { [key: string]: any })
select(tableName: string, where: string, params: any[])
- Use Cases
Overview
The SQLIte
class provides a simple interface for interacting with a SQLite database using Node.js. It supports both file-based and in-memory databases and provides basic methods for creating, dropping, inserting, deleting, and selecting data from tables.
Purpose
The primary goal of the SQLIte
class is to provide a convenient and type-safe wrapper around SQLite operations, allowing developers to interact with a SQLite database in a structured and secure manner.
Key Features
- Flexible Database Initialization: Supports both file-based and in-memory SQLite databases.
- CRUD Operations: Provides methods for creating tables, inserting data, deleting data, and selecting data from tables.
- Secure Query Handling: Uses parameterized queries to prevent SQL injection.
- Error Handling: Includes robust error handling to manage database operations safely.
Methods
constructor(props: TypeSQLiteConnection)
Initializes a new SQLite connection based on the provided configuration.
- props: An object specifying the connection type (‘file’ or ‘memory’) and an optional filename for file-based databases.
Usage
close()
Closes the SQLite database connection.
Usage
createTable(tableName: string, schema: TypeTableSchema)
Creates a new table with the specified name and schema.
- tableName: The name of the table to create.
- schema: An object representing the schema of the table, where the keys are column names and the values are column types.
Usage
dropTable(tableName: string)
Drops the table with the specified name if it exists.
- tableName: The name of the table to drop.
Usage
delete(tableName: string, where: string, params: any[])
Deletes rows from the specified table that match the given condition.
- tableName: The name of the table from which to delete rows.
- where: A SQL WHERE clause specifying the condition for deletion.
- params: An array of parameters to replace placeholders in the WHERE clause.
Usage
insert(tableName: string, data: { [key: string]: any })
Inserts a new row into the specified table with the given data.
- tableName: The name of the table into which to insert the data.
- data: An object representing the data to insert, where the keys are column names and the values are the values to insert.
Usage
select(tableName: string, where: string, params: any[])
Selects rows from the specified table that match the given condition.
- tableName: The name of the table from which to select rows.
- where: A SQL WHERE clause specifying the condition for selection.
- params: An array of parameters to replace placeholders in the WHERE clause.
Usage
Use Cases
Example: Basic SQLite Setup
This example demonstrates how to set up a basic SQLite database and perform common operations using the SQLIte
class.
This example shows how to use the SQLIte
class to interact with a SQLite database in Node.js, including creating a table, inserting data, selecting data, deleting data, and dropping the table.