Minimal, easy to use, fast Object Document Mapping(ODM) library for NeDB. Supports type-checking and schema validation.
npm install sagitarius
yarn add sagitarius
pnpm add sagitarius
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();
- save() Saves the new or updated instance to the database.
- delete() Saves the new or updated instance to the database.
- toJSON() Saves the new or updated instance to the database.
- validate() Saves the new or updated instance to the database.
async interface.save()
Creates a new instance of the model with the provided document.
async interface.delete()
Deletes the document from the database.
async interface.toJSON()
Converts the document from the database to a JavaScript Object.
async interface.validate()
Checks the datatypes and properties of the document. Returns true if valid, false if not.
- 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.
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.
Checks the datatypes and properties of the document with the model's schema. Returns true if valid, false if not.
Creates a document after validating it if schema exists, without constructing a class.
Bulk creates multiple documents after validating each without constructing a class.
Finds all documents that are matching the query. If none found, returns an empty array.
Finds the first element that is matching the given query. If none found, returns null
Updates all documents that match the query, with the given document.
Updates the first document that matches the query, with the given document.
Deletes all documents that match the query.
Deletes the first document that matches the query.
Counts all documents that match the query. Returns true if the number is bigger than 0, false if not.
Counts all documents that match the given query.