Repository navigation
1. Getting Started
This package is part of the kirbkis-database-handler monorepo. To get started with this package, follow these steps:
Decide which database adapter you want to use (e.g., BetterSQLite3, PostgreSQL). Each adapter has its own package within the monorepo.
You need to install both the core package and the adapter package.
npm install @kirbkis/database-handler-core @kirbkis/database-handler-better-sqlite3npm install @kirbkis/database-handler-core @kirbkis/database-handler-postgresimport { BetterSqlite3Adapter } from '@kirbkis/database-handler-better-sqlite3';
const adapter = new BetterSqlite3Adapter();
adapter.connect('./my-database.db');import { PostgresAdapter } from '@kirbkis/database-handler-postgres';
const yourPoolConfig: PoolConfig = {
host: string,
port: number,
database: string,
user: string,
password: string,
max?: number, // Pool size (default: 10)
idleTimeoutMillis?: number, // Idle timeout (default: 30000)
connectionTimeoutMillis?: number, // Connection timeout (default: 2000)
ssl?: boolean | object // SSL config
};
const adapter = new PostgresAdapter();
adapter.connect(yourPoolConfig);After creating your adapter, register it as the default adapter using the Container. This is required for the package to know which database connection to use. Otherwise, you'll need to specify the adapter manually for each operation.
import { Container } from '@kirbkis/database-handler-core';
Container.getInstance().registerAdapter('default', adapter, true);Parameters:
| Parameter | Type | Description |
|---|---|---|
name |
string |
The identifier for the adapter (e.g., 'default', 'primary', 'secondary') |
adapter |
DatabaseAdapter |
The adapter instance to register |
setAsDefault |
boolean |
Whether to set this adapter as the default for all operations (optional, default: false) |
You can now start using the core classes like Query, Table, and Record to interact with your database.
import { Table } from '@kirbkis/database-handler-core';
const usersTable = new Table({ name: 'users' });
// Fetch all users
const users = await usersTable.FetchRecords({ base: { from: 'users' } });
console.log(users);To use a specific adapter for an operation, pass the adapter name in the constructor:
import { Table } from '@kirbkis/database-handler-core';
const usersTable = new Table({ name: 'users', adapter: 'default' }); // Specify the adapter nameParameters:
| Parameter | Type | Description |
|---|---|---|
name |
string |
The name of the database table |
adapter |
string |
The name of the adapter to use for this table (optional, default: uses the default adapter) |