npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@onlydann/database

v1.2.0

Published

A local encrypted database for easy tests

Downloads

9

Readme

Save your data locally with easy database system!

Database System written with TypeScript

All data is encrypted in model files

Events are Available!!

See Events

New Filter IN Methods

$in, $nin

See Filter IN Methods

$ Installation

npm install @onlydann/database

Or

yarn add @onlydann/database

Setup

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 and model

// JavaScript
import { Schema, Value, Model } 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()}
});

const UserModel = new Model("users", userSchema);

export default UserModel;
// TypeScript
import { Schema, Value, Model } 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()}
});


const UserModel = new Model("users", userSchema);

export default UserModel;

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);
// if any doc's username in array
await users.get({ username: { $in: ["Dann", "Meri"] } });
// if any doc's username NOT in array
await users.get({ username: { $nin: ["Dann", "Meri"] } });

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

Register a models

await db.registerModels(UserModel);
// await db.registerModels(UserModel, MessageModel, AnyModel, ...);

Methods

it takes one argument

// others props are optional and have default values
const userDocument = await users.create({
  username: "Dann",
  password: "1234",
});

it takes one argument - array

const userDocument = await users.createAll([
  {
    username: "Dann",
    password: "1234",
  },
  { username: "Meri", password: "cuteOne" },
]);

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);

Get one document

See Filter

// filter
const userDocument = await users.get(filter);

Get one document with its _id

const userDocument = await users.getById("0BQae1vE%A%Ie@X1r%5su3O5YS7^45");

Delete one document

See Filter

await users.delete(filter);

Delete one document with its _id

await users.deleteById("0BQae1vE%A%Ie@X1r%5su3O5YS7^45");

Delete all documents

await users.deleteAll();

See Filter

// filter
await users.deleteAll(filter);

Update one document

See Filter and Update

const updatedUser = await users.update(filter, update);

Update all documents

See Filter and Update

const updatedUsersArray = await users.updateAll(filter, update);

Document

Document class implements Reserved Properties

Methods

Delete current document

await userDocument.delete();

Clone current document

await userDocument.clone();

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