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.
To install FerryDB, run:
npm install ferrydb
Here's a quick example of how to use FerryDB:
import { Schema, model, connect } from 'ferrydb';
// Connect to the database
connect('path/to/your/database.sqlite');
// Define a schema
const userSchema = new Schema({
name: { type: 'string', required: true },
age: { type: 'number', required: true }
});
// Create a model
const User = model('User', userSchema);
// Create a new user
const newUser = User.create({ name: 'John Doe', age: 30 });
// Find a user
const user = User.findOne({ name: 'John Doe' });
// Update a user
User.update({ name: 'John Doe' }, { age: 31 });
// Delete a user
User.delete({ name: 'John Doe' });
Connect to the SQLite database.
-
pathToDb
: The path to the SQLite database file. Must end with.sqlite
.
Create a new schema for your data model.
const userSchema = new Schema({
name: { type: 'string', required: true },
age: { type: 'number', required: true }
});
Validate the data against the schema.
-
data
: The data to validate. -
isCreate
: Whether this is a create operation. Default isfalse
.
Create a new data model.
const User = model('User', userSchema);
Create a new record in the database.
-
data
: The data to create.
Find a single record matching the conditions.
-
conditions
: The conditions to match.
Find all records matching the conditions.
-
conditions
: The conditions to match.
Find all records in the database.
Update records matching the conditions.
-
conditions
: The conditions to match. -
data
: The data to update.
Delete records matching the conditions.
-
conditions
: The conditions to match.
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' });
const user = User.findOne({ name: x => x.startsWith("John ") });
User.update({ name: x => x.startsWith("John ") }, { age: 31 });
User.delete({ name: x => x.startsWith("John ") });
If you need help or have questions, feel free to join our Discord community.
FerryDB is developed and maintained by ferrymehdi.