@lionrockjs/adapter-database-bun-postgres
v0.0.1
Published
LionRockJS database adapter, using Bun and PostgreSQL
Maintainers
Readme
@lionrockjs/adapter-database-bun-postgresql
LionRockJS Database Adapter for Bun with PostgreSQL
Requirements
- Bun >= 1.0.0
- PostgreSQL database server
- @lionrockjs/central >= 1.0.0
Installation
bun add @lionrockjs/adapter-database-bun-postgresqlUsage
Quick Setup
// In your application bootstrap
import '@lionrockjs/adapter-database-bun-postgresql/init.mjs';This will set ORMAdapterPostgreSQL as the default ORM adapter and DatabaseAdapterBunPostgres as the default database adapter.
Manual Setup
import { ControllerMixinDatabase, Model } from "@lionrockjs/central";
import { DatabaseAdapterBunPostgres, ORMAdapterPostgreSQL } from "@lionrockjs/adapter-database-bun-postgresql";
Model.defaultAdapter = ORMAdapterPostgreSQL;
ControllerMixinDatabase.defaultAdapter = DatabaseAdapterBunPostgres;Database Connection
The adapter accepts a PostgreSQL connection string:
import { DatabaseAdapterBunPostgres } from "@lionrockjs/adapter-database-bun-postgresql";
const db = DatabaseAdapterBunPostgres.create("postgres://user:password@localhost:5432/database");Environment Variables
You can use environment variables for configuration:
DATABASE_URL=postgres://user:password@localhost:5432/databaseconst db = DatabaseAdapterBunPostgres.create(process.env.DATABASE_URL);Key Differences from SQLite Adapter
| Feature | SQLite | PostgreSQL |
|---------|--------|------------|
| Placeholders | ? | $1, $2, $3... |
| Boolean values | 1 / 0 | true / false |
| Case-insensitive | COLLATE NOCASE | LOWER() / ILIKE |
| JSON extract | json_extract(col, '$.key') | col->>'key' |
| Upsert | INSERT OR IGNORE | ON CONFLICT DO NOTHING |
API
DatabaseAdapterBunPostgres
constructor(connectionString)- Create a new database connectionquery(sql, values)- Execute a query with parametersexec(sql)- Execute raw SQLclose()- Close the database connectiontransactionStart()- Begin a transactiontransactionCommit()- Commit a transactiontransactionRollback()- Rollback a transaction
ORMAdapterPostgreSQL
Extends ORMAdapter from @lionrockjs/central with PostgreSQL-specific implementations for:
read(),insert(),update(),delete()readAll(),readBy(),readWith()countAll(),countBy(),countWith()deleteAll(),deleteBy(),deleteWith()updateAll(),updateBy(),updateWith()insertAll()hasMany(),belongsToMany(),add(),remove(),removeAll()
Running Tests
Tests require a running PostgreSQL server. Set the TEST_DATABASE_URL environment variable:
# Create test database
createdb lionrock_test
# Run tests with connection string
TEST_DATABASE_URL="postgres://username:password@localhost:5432/lionrock_test" bun testDefault connection: If TEST_DATABASE_URL is not set, tests will try postgres://postgres:postgres@localhost:5432/lionrock_test.
License
MIT License - see LICENSE file for details.
