0.0.43 • Published 4 years ago

@iljucha/collection-ts v0.0.43

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

Collection-TS

Usage

Import and create

import { Collection } from "@iljucha/collection-ts";

let items = [
    { _id: 1, alias: "user1" },
    { _id: 2, alias: "user2" },
    { _id: 3, alias: "user3" }
];

let DB = new Collection(items);

Query

// Simple Query
let Query = { id: "2a6074a4-5775-4221-96cf-914aa8d3e7f7" };

// Nested Object Query
Query = { "user.alias": "iljucha" };

// Nested Array Query -> If environment[1] exists
Query = { "environment.1": { $exists: true } };
 
// $or-operated 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: "2a6074a4-5775-4221-96cf-914aa8d3e7f7" },
        { "user.alias": { $regexp: /ucha/i } },
        { "user.name": { $regexp: /ucha/i } }
    ],
    $and: [
        { "user.status": { $nin: ["banned", "unauthorized"] } },
        { "user.lastDinner": { $includes: "salad" } }
    ]
}

Insert

DB.insertOne({ _id: 4, alias: "user4" });
DB.insertMany([
    { _id: 5, alias: "user5" },
    { _id: 6, alias: "user6" }
]);

Update

// update(Query, Mixin)
DB.updateOne({ _id: 4 }, { updatedAt: new Date() });
DB.updateMany({ }, { updatedAt: new Date() });

DB.updateOneSync({ _id: 4 }, { updatedAt: new Date() });
DB.updateManySync({ }, { updatedAt: new Date() });

Delete

DB.deleteOne({ _id: 4 });
DB.deleteMany({ 
    $or: [
        { _id: 1 },
        { _id: 2 }
    ]
});

DB.deleteOneSync({ _id: 4 });
DB.deleteManySync({ alias: { $eq: "user5" } });

Find

let cursor = DB.find({ alias: { $includes: "user" } }) // creates cursor
    .limit(1000) //stops looking for matches after n items
    .reverse() // searches in reversed order and returns them reversed
    .skip(100) //skips first n matches

// asynchronous methods (preferred)
async function something() {
    let results = await cursor.toArray()
    // or
    await cursor.forEach(result => console.log(result))
    // or
    await cursor.map(result => console.log(result))
    // or single result
    let first = await cursor.first()
    let last = await cursor.last()
}

// synchronous methods
function something2() {
    let results = cursor.toArraySync()
    // or
    cursor.forEachSync(result => console.log(result))
    // or
    cursor.mapSync(result => console.log(result))
    // or single result
    let first = cursor.firstSync()
    let last = cursor.lastSync()
}

Joins

let POSTS = new Collection([]);
let USERS = new Collection([]);
let USERIMAGES = new Collection([]);

const USERID = "2a6074a4-5775-4221-96cf-914aa8d3e7f7";

// Join results of USERS with the results of POSTS where key-values matches
POSTS.find({ _user: USERID })
    .join({
        cursor: USERS.find({ _id: USERID }).limit(1).join({
            cursor: USERIMAGES.find({ _user: USERID }).limit(1),
            where: ["_id", "_user"],
            as: "USERS-target-property"
        }),
        where: ["_user", "_id"],
        as: "POSTS-target-property" // optional, will be joined on "_user" if not given
    })//.toArray() or other

JSON

DB.find().toJSON()
    .then(json => console.log(json))

console.log(DB.find().toJSONSync())
0.0.43

4 years ago

0.0.42

4 years ago

0.0.41

4 years ago

0.0.40

4 years ago

0.0.39

4 years ago

0.0.38

4 years ago

0.0.37

4 years ago

0.0.36

4 years ago

0.0.34

4 years ago

0.0.35

4 years ago

0.0.31

4 years ago

0.0.32

4 years ago

0.0.33

4 years ago

0.0.30

4 years ago

0.0.29

4 years ago

0.0.25

4 years ago

0.0.26

4 years ago

0.0.27

4 years ago

0.0.28

4 years ago

0.0.23

4 years ago

0.0.24

4 years ago

0.0.22

4 years ago

0.0.21

4 years ago

0.0.20

4 years ago

0.0.14

4 years ago

0.0.15

4 years ago

0.0.16

4 years ago

0.0.17

4 years ago

0.0.18

4 years ago

0.0.19

4 years ago

0.0.13

4 years ago

0.0.12

4 years ago

0.0.10

4 years ago

0.0.11

4 years ago

0.0.9

4 years ago

0.0.8

4 years ago

0.0.7

4 years ago

0.0.5

4 years ago

0.0.4

4 years ago

0.0.6

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago