1.6.0 • Published 4 years ago

@iljucha/mango-db v1.6.0

Weekly downloads
1
License
ISC
Repository
github
Last release
4 years ago

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

4 years ago

1.5.30

4 years ago

1.5.31

4 years ago

1.5.28

4 years ago

1.5.27

4 years ago

1.5.26

4 years ago

1.5.25

4 years ago

1.5.24

4 years ago

1.5.23

4 years ago

1.5.22

4 years ago

1.5.21

4 years ago

1.5.18

4 years ago

1.5.17

4 years ago

1.5.19

4 years ago

1.5.20

4 years ago

1.5.16

4 years ago

1.5.15

4 years ago

1.5.14

4 years ago

1.5.13

4 years ago

1.5.12

4 years ago

1.5.9

4 years ago

1.5.10

4 years ago

1.5.11

4 years ago

1.5.8

4 years ago

1.5.7

4 years ago

1.5.6

4 years ago

1.5.5

4 years ago

1.5.4

4 years ago

1.5.3

4 years ago

1.5.2

4 years ago

1.5.1

4 years ago

1.5.0

4 years ago

1.3.2

4 years ago

1.3.1

4 years ago

1.3.0

4 years ago

1.2.13

4 years ago

1.2.16

4 years ago

1.2.14

4 years ago

1.2.15

4 years ago

1.2.12

4 years ago

1.2.10

4 years ago

1.2.11

4 years ago

1.2.8

4 years ago

1.2.7

4 years ago

1.2.9

4 years ago

1.2.6

4 years ago

1.2.5

4 years ago

1.2.4

4 years ago

1.2.3

4 years ago

1.2.2

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.29

4 years ago

1.1.28

4 years ago

1.1.27

4 years ago

1.1.26

4 years ago

1.1.25

4 years ago

1.1.23

4 years ago

1.1.24

4 years ago

1.1.22

4 years ago

1.1.21

4 years ago

1.1.20

4 years ago

1.1.19

4 years ago

1.1.18

4 years ago

1.1.17

4 years ago

1.1.16

4 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.18

4 years ago

1.1.9

4 years ago

1.1.8

4 years ago

1.1.7

4 years ago

1.1.6

4 years ago

1.1.5

4 years ago

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.12

4 years ago

1.1.11

4 years ago

1.1.10

4 years ago

1.1.15

4 years ago

1.1.14

4 years ago

1.1.13

4 years ago

1.0.17

4 years ago

1.0.16

4 years ago

1.0.15

4 years ago

1.0.13

4 years ago

1.0.12

4 years ago

1.0.11

4 years ago

1.0.10

4 years ago

1.0.9

4 years ago

1.0.8

4 years ago

1.0.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago