A helpful SQLite3 wrapper that enhances your database operations with a fluent interface, transactions, and modern async/await support.
- 🎯 Simple and intuitive API
- đź”— Chainable query building
- đź’« Promise-based operations
- 🔄 Transaction support
- 📦 Batch processing helper
- đź“„ Built-in pagination
- 🎮 Easy debugging
- 🚀 Performance optimized
npm install @salarizadi/sql3OR
yarn add @salarizadi/sql3const SQL3 = require('@salarizadi/sql3');
// In-memory database (temporary, for testing)
const db = new SQL3(':memory:');
// File-based database (persistent storage)
// const db = new SQL3('database.sqlite');
async function main() {
// Create a table
await db.createTable('users', {
id: 'INTEGER PRIMARY KEY AUTOINCREMENT',
name: 'TEXT NOT NULL',
email: 'TEXT UNIQUE'
});
// Insert data
const result = await db.insert('users', {
name: 'John Doe',
email: '[email protected]'
});
console.log('Inserted user ID:', result.lastID);
// Query with conditions
const users = await db
.where('name', 'LIKE', '%John%')
.get('users');
console.log('Found users:', users);
}
main().catch(console.error);// Get all records
const all = await db.get('users');
// Get one record
const user = await db.getOne('users');
// Count records
const count = await db.count('users');const users = await db
.where('age', 18, 'AND', '>')
.where('status', 'active')
.where('role', 'admin', 'OR')
.get('users');try {
await db.beginTransaction();
await db.insert('users', { name: 'User 1' });
await db.insert('logs', { action: 'User created' });
await db.commit();
} catch (error) {
await db.rollback();
console.error('Error:', error);
}await db.batchSize('users', 50, async (batch) => {
for (const user of batch) {
await processUser(user);
}
});const { data, pagination } = await db.paginate('users', 1, 20);
console.log('Users:', data);
console.log('Page info:', pagination);createTable(table, columns): Create a new tabledropTable(table): Drop an existing tabletableExists(table): Check if a table exists
get(table, columns = '*'): Get multiple recordsgetOne(table, columns = '*'): Get a single recordinsert(table, data): Insert new recordupdate(table, data): Update recordsdelete(table): Delete recordscount(table, column = '*'): Count records
where(column, value, operator = 'AND', comparison = '='): Add a where condition
beginTransaction(): Start a transactioncommit(): Commit transactionrollback(): Rollback transaction
paginate(table, page = 1, perPage = 10): Get paginated resultsbatchSize(table, size, callback): Process records in batchesvacuum(into?): Optimize databaseclose(): Close database connectiongetLastQuery(): Get last executed query details
// Create users table
await db.createTable('users', {
id: 'INTEGER PRIMARY KEY AUTOINCREMENT',
username: 'TEXT UNIQUE NOT NULL',
email: 'TEXT UNIQUE NOT NULL',
created_at: 'DATETIME DEFAULT CURRENT_TIMESTAMP'
});
// Add new user
const { lastID } = await db.insert('users', {
username: 'johndoe',
email: '[email protected]'
});
// Find user by email
const user = await db
.where('email', '[email protected]')
.getOne('users');// Log user activity with transaction
async function logActivity(userId, action) {
try {
await db.beginTransaction();
await db.insert('logs', {
user_id: userId,
action: action,
timestamp: new Date().toISOString()
});
await db.update('users', {
last_activity: new Date().toISOString()
}).where('id', userId);
await db.commit();
} catch (error) {
await db.rollback();
throw error;
}
}MIT License - see the LICENSE file for details.
Created and maintained by Salar Izadi
If you encounter any issues or have questions, please open an issue on GitHub.