### Getting Started with the package This package is part of the `kirbkis-database-handler` monorepo. To get started with this package, follow these steps: ### 1. **Choose an adapter** Decide which database adapter you want to use (e.g., BetterSQLite3, PostgreSQL). Each adapter has its own package within the monorepo.
### 2. **Install the packages** You need to install both the core package and the adapter package. ## For Better-sqlite3 ```bash npm install @kirbkis/database-handler-core @kirbkis/database-handler-better-sqlite3 ``` ## For PostgreSQL ```bash npm install @kirbkis/database-handler-core @kirbkis/database-handler-postgres ```
### 3. **Set up the database connection** ## Better-sqlite3 Example ```typescript import { BetterSqlite3Adapter } from '@kirbkis/database-handler-better-sqlite3'; const adapter = new BetterSqlite3Adapter(); adapter.connect('./my-database.db'); ``` ## PostgreSQL Example ```typescript 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); ``` ### 4. **Set a default adapter** 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. ```typescript 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`) | ### 5. **Start using the package** You can now start using the core classes like `Query`, `Table`, and `Record` to interact with your database. ```typescript 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: ```typescript import { Table } from '@kirbkis/database-handler-core'; const usersTable = new Table({ name: 'users', adapter: 'default' }); // Specify the adapter name ``` **Parameters:** | 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) |