Introduction
Sagitarius is a fast, minimal, modern Object Document Mapping(ODM) library for NeDB. Supports type-checking and schema validation.
Installation
npm install sagitarius
yarn add sagitarius
pnpm add sagitarius
Quick Start
Create and initialize the model:
import { Model } from "sagitarius";
class User extends Model{
constructor(doc){
super(doc);
Object.assign(this, doc);
}
}
Define a schema to be able to perform validation:
User.define("User", {
username: String,
age: Number,
});
Create a new user:
const user = new User({ username: "Rednexie", age: 18 });
await user.save();
Update user properties:
user.age = 19;
await user.save();
API Reference
Instance Methods
- save() - Saves the new or updated instance to the database.
- delete() - Removes the instance from the database.
- toJSON() - Converts the instance to a JavaScript Object.
- validate() - If the schema exists, checks if the instance is valid.
async instance.save()
Creates a new instance of the model with the provided document.
async instance.delete()
Deletes the document from the database.
instance.toJSON()
Converts the document from the database to a JavaScript Object.
instance.validate()
Checks the datatypes and properties of the document. Returns true if valid, false if not.
Model Methods
- Model.define(name, schema, path?) - Creates a new database model.
- Model.validate(d) - If the schema exists, checks if the document is valid.
- Model.create(d) - Creates a document and saves it to the database.
- Model.createMany(d[]) - Creates multiple documents and saves them to the database.
- Model.find(query) - Finds matching document instances in the database.
- Model.findOne(query) - Finds a matching document from the database or returns null.
- Model.update(query, d) - Finds matching documents from the database and updates them with the given document.
- Model.updateOne(query, d) - Finds a matching document from the database or returns null.
- Model.delete(query) - Finds matching documents and removes them from the database.
- Model.deleteOne(query) - Finds a matching document and removes it from the database.
- Model.exists(query) - Checks if any document matching the query exist in the database.
- Model.count(query) - Counts how many matching documents exists in the database.
async Model.define(name, schema, path?)
Initializes the model and creates a corresponding database. You can customize database name and path. If you want to do schema validation and type-checking, you have to define a schema.
async Model.validate(d)
Checks the datatypes and properties of the document with the model's schema. Returns true if valid, false if not.
async Model.create(d)
Creates a document after validating it if schema exists, without constructing a class.
async Model.createMany(d[])
Bulk creates multiple documents after validating each without constructing a class.
async Model.find(query)
Finds all documents that are matching the query. If none found, returns an empty array.
async Model.findOne(query)
Finds the first element that is matching the given query. If none found, returns null
async Model.update(query, d)
Updates all documents that match the query, with the given document.
async Model.updateOne(query, d)
Updates the first document that matches the query, with the given document.
async Model.delete(query)
Deletes all documents that match the query.
async Model.deleteOne(query)
Deletes the first document that matches the query.
async Model.exists(query)
Counts all documents that match the query. Returns true if the number is bigger than 0, false if not.
async Model.count(query)
Counts all documents that match the given query.