Package Exports
- @onlydann/database
- @onlydann/database/dist/index.js
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@onlydann/database) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
Save your data locally with easy database system!
Database System written with TypeScript
All data is encrypted in model files
Events are Available!!
$ Installation
npm install @onlydann/databaseOr
yarn add @onlydann/databaseSetup
import { createDatabase } from "@onlydann/database";
// setuping folder
const db = await createDatabase("../databaseFolder");
// it is more secure, use your custom passphrase
const dbWithCustomPassphrase = await createDatabase({
enc_pass: "My Passphrase",
path: "../databaseFolder",
});Creating schema
// JavaScript
import { Schema, Value } from "@onlydann/database";
const userSchema = new Schema({
username: new Value("string", {unique: true}),
password: new Value("string"),
age: new Value("number", {default: 18}),
tags: new Value("array", {default: []}),
endsIn: new Value("date"), {default: () => new Date()}
});// TypeScript
import { Schema, Value } from "@onlydann/database";
interface User {
_id: string;
_createdAt: Date;
username: string;
password: string;
age?: number;
tags?: string[];
endsIn?: Date
}
const userSchema = new Schema<User>({
username: new Value("string", {unique: true}),
password: new Value("string"),
age: new Value("number", {default: 18}),
tags: new Value("array", {default: []}),
endsIn: new Value("date"), {default: () => new Date()}
});Default (Reserved) properties
There are some default properties extended from document
You can specify them in interface with other props, Schema will Omit them
// Unique string of symbols for database
_id: string;
// Document creation date
_createdAt: Date;Filter and Update
Filter
First argument is filter object, so we can do..
// all props are the same as filter object
await users.get({ username: "Dann" });
// if any prop exists in document
await users.all({ $or: { username: "Dann", age: 20 } });
// callback filter function, first arg is document, must return boolean
await users.delete((doc) => doc.tags.length > 10);Update
Model.update methods are using Update object as second argument
await users.update(filter, { $set: { username: "Aren" } });
// also you can use -number
await users.update(filter, { $inc: { age: 1 } });
// push works only if property is an array
await users.update(filter, { $push: { tags: "developer" } });Model
Create a model
// creation with constructor not allowed ( new Model(...) )
const users = await db.createModel("users", userSchema);Methods
create
Create a documentit takes one argument
// others props are optionan and have default values
const userDocument = await users.create({
username: "Dann",
password: "1234",
});all
Get all documents
// all
const userDocuments = await users.all();See Filter
// also you can use Filter as first argument
const userDocuments = await users.all(filter);getById
Get one document with its _id
const userDocument = await users.getById("0BQae1vE%A%Ie@X1r%5su3O5YS7^45");deleteById
Delete one document with its _id
await users.deleteById("0BQae1vE%A%Ie@X1r%5su3O5YS7^45");deleteAll
Delete all documents
await users.deleteAll();See Filter
// filter
await users.deleteAll(filter);update
Update one document
const updatedUser = await users.update(filter, update);updateAll
Update all documents
const updatedUsersArray = await users.updateAll(filter, update);Document
Document class implements Reserved Properties
Methods
delete
Delete current document
await userDocument.delete();clone
Clone current document
await userDocument.clone();toJson
we are useing this method for saving document in base
So it's not usable
const userJson = userDocument.toJson();Events
Each Model now has Event System
There are 3 types of events
create, delete, update
// create
users.on("create", (doc) => {
// created document
console.log(doc._id);
});
users.on("delete", (doc) => {
// deleted document
console.log(doc._id);
});
users.on("update", (oldDoc, newDoc) => {
// newDoc is the updated version of oldDoc
console.log(oldDoc, newDoc);
});Feature Updates
- Nested object filtering and updating