FerryDB
FerryDB is a simple, flexible, and powerful SQLite-based ORM for Node.js. It allows you to define schemas, validate data, and perform CRUD operations with ease.
Table of Contents
Installation
To install FerryDB, run:
npm install ferrydb
Usage
Here's a quick example of how to use FerryDB:
import { Schema, model, connect } from 'ferrydb';
connect('path/to/your/database.sqlite');
const userSchema = new Schema({
name: { type: 'string', required: true },
age: { type: 'number', required: true }
});
const User = model('User', userSchema);
const newUser = User.create({ name: 'John Doe', age: 30 });
const user = User.findOne({ name: 'John Doe' });
User.update({ name: 'John Doe' }, { age: 31 });
User.delete({ name: 'John Doe' });
API Reference
connect(pathToDb: string)
Connect to the SQLite database.
pathToDb
: The path to the SQLite database file. Must end with .sqlite
.
Schema
Create a new schema for your data model.
const userSchema = new Schema({
name: { type: 'string', required: true },
age: { type: 'number', required: true }
});
validate(data: Partial<InferSchema<T>>, isCreate: boolean = false)
Validate the data against the schema.
data
: The data to validate.isCreate
: Whether this is a create operation. Default is false
.
model
Create a new data model.
const User = model('User', userSchema);
create(data: Partial<InferSchema<T>>): ModelInstance<InferSchema<T>>
Create a new record in the database.
data
: The data to create.
findOne(conditions: QueryConditions<InferSchema<T>>): ModelInstance<InferSchema<T>> | null
Find a single record matching the conditions.
conditions
: The conditions to match.
findAll(conditions: QueryConditions<InferSchema<T>>): ModelInstance<InferSchema<T>>[]
Find all records matching the conditions.
conditions
: The conditions to match.
find(): ModelInstance<InferSchema<T>>[]
Find all records in the database.
update(conditions: QueryConditions<InferSchema<T>>, data: Partial<InferSchema<T>>): number
Update records matching the conditions.
conditions
: The conditions to match.data
: The data to update.
delete(conditions: QueryConditions<InferSchema<T>>): number
Delete records matching the conditions.
conditions
: The conditions to match.
Examples
Creating a New Record
const newUser = User.create({ name: 'John Doe', age: 30 });
Finding a Record
const user = User.findOne({ name: 'John Doe' });
Updating a Record
User.update({ name: 'John Doe' }, { age: 31 });
Deleting a Record
User.delete({ name: 'John Doe' });
Finding a Record with Condition Function
const user = User.findOne({ name: x => x.startsWith("John ") });
Updating Records with Condition Function
User.update({ name: x => x.startsWith("John ") }, { age: 31 });
Deleting a Record with Condition Function
User.delete({ name: x => x.startsWith("John ") });
Support
If you need help or have questions, feel free to join our Discord community.
Developer
FerryDB is developed and maintained by ferrymehdi.