-
A lightweight, type-safe SQLite database wrapper for Bun with an elegant API and zero dependencies.
-
install
spacefirst.# install space i @je-es/sdb// import import { DB, table, integer, text, primaryKey, notNull, unique } from '@je-es/sdb';
-
-
// In-memory database const db = new DB(); // Or file-based database const db = new DB('./my-database.db');
-
import { table, integer, text, real, primaryKey, notNull, unique, defaultValue } from '@je-es/sdb'; // Define table schema const usersSchema = table('users', [ primaryKey(integer('id'), true), // auto-increment primary key notNull(text('name')), unique(text('email')), integer('age'), defaultValue(text('status'), 'active') ]); // Create the table db.defineSchema(usersSchema);
-
// Insert const user = db.insert('users', { name: 'Alice', email: 'alice@example.com', age: 25 }); // Find by ID const foundUser = db.findById('users', 1); // Find one const user = db.findOne('users', { email: 'alice@example.com' }); // Find many const youngUsers = db.find('users', { status: 'active' }); // Get all const allUsers = db.all('users'); // Update db.update('users', 1, { age: 26 }); // Delete db.delete('users', 1);
-
// Simple query const results = db.query() .select(['name', 'email']) .from('users') .where({ column: 'age', operator: '>', value: 21 }) .orderBy('name', 'ASC') .limit(10) .execute(); // Complex query with multiple conditions const activeUsers = db.query() .select() .from('users') .where({ column: 'status', operator: '=', value: 'active' }) .and({ column: 'age', operator: '>=', value: 18 }) .orderBy('name', 'ASC') .execute(); // OR conditions const users = db.query() .select() .from('users') .where({ column: 'status', operator: '=', value: 'active' }) .or({ column: 'status', operator: '=', value: 'pending' }) .execute(); // IN operator const specificUsers = db.query() .select() .from('users') .where({ column: 'id', operator: 'IN', value: [1, 2, 3] }) .execute(); // LIKE operator const searchResults = db.query() .select() .from('users') .where({ column: 'name', operator: 'LIKE', value: 'A%' }) .execute(); // Pagination const page2 = db.query() .select() .from('users') .orderBy('id', 'ASC') .limit(10) .offset(10) .execute(); // Get single result const user = db.query() .select() .from('users') .where({ column: 'email', operator: '=', value: 'test@example.com' }) .executeOne();
-
db.transaction((txDb) => { txDb.insert('users', { name: 'Alice', email: 'alice@example.com' }); txDb.insert('users', { name: 'Bob', email: 'bob@example.com' }); // Automatically commits if no error // Automatically rolls back on error });
-
// Execute without return db.exec('DELETE FROM users WHERE status = "inactive"'); // Execute with parameters and get results const results = db.raw( 'SELECT * FROM users WHERE age > ? AND status = ?', [21, 'active'] ); // Get single result const user = db.rawOne( 'SELECT * FROM users WHERE email = ?', ['test@example.com'] );
-
import { references, index } from '@je-es/sdb'; // Table with foreign key const postsSchema = table('posts', [ primaryKey(integer('id'), true), notNull(text('title')), text('content'), references(integer('user_id'), 'users', 'id') ]); // Foreign key with cascade delete const ordersSchema = table('orders', [ primaryKey(integer('id'), true), notNull(references(integer('user_id'), 'users', 'id', { onDelete: 'CASCADE' })), text('description') ]); // Foreign key with set null on delete const projectsSchema = table('projects', [ primaryKey(integer('id'), true), text('name'), references(integer('org_id'), 'organizations', 'id', { onDelete: 'SET NULL' }) ]); // Table with composite unique constraints const providersSchema = table('oauth_providers', [ primaryKey(integer('id'), true), notNull(integer('user_id')), notNull(text('provider')), text('provider_id'), unique(['user_id', 'provider']), // One provider per account unique(['provider', 'provider_id']), // Provider ID must be globally unique index('idx_user_id', 'user_id'), index('idx_provider', 'provider') ]); // Table with indexes const productsSchema = table('products', [ primaryKey(integer('id'), true), text('name'), defaultValue(real('discount_percent'), 0), index('idx_name', 'name'), index('idx_discount', 'discount_percent', true) // unique index ]); db.defineSchema(postsSchema); db.defineSchema(ordersSchema); db.defineSchema(projectsSchema); db.defineSchema(providersSchema); db.defineSchema(productsSchema);
-
// Get table schema const schema = db.getSchema('users'); // List all tables const tables = db.listTables(); // Drop table db.dropTable('old_table'); // Close database connection db.close();
-
-
integer(name)- INTEGER columntext(name)- TEXT columnreal(name)- REAL column (floating point)blob(name)- BLOB column (binary data)numeric(name)- NUMERIC column
-
primaryKey(col, autoIncrement?)- Mark as primary keynotNull(col)- Add NOT NULL constraintunique(col)- Add UNIQUE constraint on single columnunique(columns[])- Add UNIQUE constraint on multiple columns (composite)defaultValue(col, value)- Set default valuereferences(col, table, column, options?)- Add foreign key constraint with optionalonDeleteandonUpdatebehaviorindex(name, columns, unique?)- Create index on one or more columns
-
-
-
-
Notifications
You must be signed in to change notification settings - Fork 0
A lightweight, type-safe SQLite database wrapper for Bun with an elegant API and zero dependencies.
License
je-es/sdb
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
 |  | |||
 |  | |||
 |  | |||
 |  | |||
 |  | |||
 |  | |||
 |  | |||
 |  | |||
 |  | |||
 |  | |||
 |  | |||
 |  | |||
 |  | |||
 |  | |||
 |  | |||
 |  | |||
 |  | |||
Repository files navigation
About
A lightweight, type-safe SQLite database wrapper for Bun with an elegant API and zero dependencies.

