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

@iljucha/mango-db

v1.6.0

Published

simple in memory db

Downloads

2

Readme

MangoDB

Simple in-memory (with option to serialize) MongoDB parody.

Usage

Creation

import MangoDB from "@iljucha/mango-db"
import VUID from "@iljucha/vuid" // ID generator

const postsConfig = {
    name: "posts",
    path: "./",
    schema: {
        _id: {
            type: "string",
            minimum: 32,
            maximum: 32,
            default: VUID
        },
        datetime: {
            type: "Date",
            default: () =>  new Date()
        },
        title: {
            type: "string",
            minimum: 4,
            maximum: 64
        },
        text: {
            type: "string",
            minimum: 4,
            maximum: 256
        }
    }
}
DB.configure(postsConfig)

// also possible with setters:
let DB = new MangoDB()
DB.name = "posts"
DB.path = "./"
DB.schema = {
    _id: {
        type: "string",
        minimum: 32,
        maximum: 32,
        default: VUID
    },
    datetime: {
        type: "Date",
        default: () =>  new Date()
    },
    title: {
        type: "string",
        minimum: 4,
        maximum: 64
    },
    text: {
        type: "string",
        minimum: 4,
        maximum: 256
    }
}

Actions

Query

This is the object you use to find Items.
Of course you don't have to use all the Query-Filters at the same time, lol.

// Very simple Query
let Query = {
    _id: "wA357Fr3bv2bYfWKLxQmwIM8GogEOCOy"
}

// Very weird Query, matches all item in $or
Query = {
    $or: [
        { _id: { $regexp: /findme/i } }, // regexp.test(...)
        { _id: { $includes: "substring"} }, // str.includes(...)
        { _id: { $eq: "onlyme" } }, // value === input
        { _id: { $ne: "notme" } }, // value !== input
        { _id: { $lt: 5 } }, // value > input
        { _id: { $lte: 5 } }, // value >= input
        { _id: { $gt: 5 } }, // value < input
        { _id: { $gte: 5 } }, // value <= input
        { _id: { $in: ["findme", "orme"] } }, // arr.indexOf(input) >= 0
        { _id: { $nin: ["findmenot"] } }, // arr.indexOf(input) === -1
        { _id: { $exists: true } }, // property exists
        { _id: { $type: "string" } }, // value has type "string"
    ]
}

// Complex Query
Query = {
    $or: [
        { _id: "wA357Fr3bv2bYfWKLxQmwIM8GogEOCOy" },
        { alias: { $regexp: /iljucha/i } },
        { name: { $includes: /ilj/i } }
    ],
    $and: [
        { status: { $nin: ["banned", "kicked"] } },
        { auth: { $eq: "admin" } }
    ]
}

Join

You can join Items from other MangoDBs.

let DB1 = new MangoDB()
let DB2 = new MangoDB()
let DB3 = new MangoDB()
DB1.find({ _user: 1 })
    .join({
        cursor: DB2.find({ _id: 1 }).join({
            cursor: DB3.find({ _user: 1 }).reverse(),
            where: ["fk_db2", "_id"],
            as: "db2_join"
        }),
        where: ["fk_db1", "_id"],
        as: "db1_join"
    })
    .toArray()

Project

You can explicitly show or hide item fields.

let DB1 = new MangoDB()
let DB2 = new MangoDB()
DB1.find({ _user: 1 })
    .project({ 
        _id: {
            $alias: "post" // rename "_id" to "post"
        },
        "db1_join._id": false // hide joined items "_id"
    }) 
    .join({
        cursor: DB2.find(),
        where: ["fk_db1", "_id"],
        as: "db1_join"
    })
    .toArray()

Insert

let items = [
    {
        title: "hewwo",
        text: "how are ya? :3"
    },
    {
        title: "im sick",
        text: "im a sick rapper, yo!"
    }
]

// takes items as arguments, no need to use Arrays (or if so, then ...array like here)
DB.insert(...items)
    .then(res => console.log(res.items))
    .catch(res => console.log(res.error))

Delete

// delete single item
DB.deleteOne(Query)
    .then(res => console.log(res.item, "deleted"))
    .catch(res => console.log(res.error))

// delete all, or type in query
DB.deleteMany({ })
    .then(res => console.log(res.items, "deleted"))
    .catch(res => console.log(res.error))

Update

// update single item
DB.updateOne(Query, { title: "yay, i got updated!" })
    .then(res => console.log(res.item, "updated"))
    .catch(res => console.log(res.error))

// update all, or type in query
DB.updateMany({ }, { title: "we all have now the same title :(" })
    .then(res => console.log(res.items, "updated"))
    .catch(res => console.log(res.error))

Find

DB.find({ text: { $regexp: /hello/i }})
    .limit(1000)
    .skip(10)
    .reverse(true)
    // to Promise<Item[]>
    .toArray()
    // to Promise<Item>
    .single(0 /** index */)
    // Promise<callbackfn<forEach>>
    .each(item => console.log(item))