Sagitarius

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

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

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.