Skip to content

v4.0.0

Compare
Choose a tag to compare
@twlite twlite released this 01 Sep 12:55
· 35 commits to main since this release
47375cb

Updates

QuickMongo v4 is completely rewritten in TypeScript. v4 is completely different from what older versions looked like.

Breaking Changes

  • QuickMongo no longer exports Database, instead it is a collection
  • Removed methods like add, subtract, math, divide, multiply, keyArray, valueArray, import etc.
  • v4 only has 10 methods: has, get, set, delete, push, pull, drop, all, latency and export.
  • Implements the Fields logic, which is now required.
  • QuickMongo now works as a utility layer, adding key-value interface to existing mongodb collection
  • Dot notations are no longer parsed from the key, instead it needs to be supplied separately as a parameter.
  • Database states are no longer handled by QuickMongo.
  • v4 is strongly typed, providing better experience to TypeScript users.
  • It supports exporting the collection as raw json, in better form.
  • v4 does implement the old data structure of quick.db, i.e. { ID: string; data: any; } however, users must use Fields.

Basic Example

const { Collection: MongoCollection, MongoClient } = require("mongodb");
const { Collection, Fields } = require("quickmongo");

const mongo = new MongoClient("mongodb://localhost/quickmongo");
const schema = new Fields.ObjectField({
    difficulty: new Fields.StringField(),
    items: new Fields.ArrayField(new Fields.StringField()),
    balance: new Fields.NumberField()
});

mongo.connect()
    .then(() => {
        console.log("Connected to the database!");
        doStuff();
    });

function doStuff() {
    const mongoCollection = mongo.db().collection("JSON");

    const db = new Collection(mongoCollection, schema);
    
    db.set("userInfo", { difficulty: "Easy", items: [], balance: 0 }).then(console.log);
    // -> { difficulty: 'Easy', items: [], balance: 0 }

    db.push("userInfo", "Sword", "items").then(console.log);
    // -> { difficulty: 'Easy', items: ['Sword'], balance: 0 }

    db.set("userInfo", 500, "balance").then(console.log);
    // -> { difficulty: 'Easy', items: ['Sword'], balance: 500 }

    // Repeating previous examples:
    db.push("userInfo", "Watch", "items").then(console.log);
    // -> { difficulty: 'Easy', items: ['Sword', 'Watch'], balance: 500 }

    const previousBalance = await db.get("userInfo", "balance");
    db.set("userInfo", previousBalance + 500, "balance").then(console.log);
    // -> { difficulty: 'Easy', items: ['Sword', 'Watch'], balance: 1000 }

    // Fetching individual properties
    db.get("userInfo", "balance").then(console.log);
    // -> 1000
    db.get("userInfo", "items").then(console.log);
    // -> ['Sword', 'Watch']

    // remove item
    db.pull("userInfo", "Sword", "items").then(console.log);
    // -> { difficulty: 'Easy', items: ['Watch'], balance: 1000 }
}