Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

objdb

1.5.1 • Public • Published

Overview

Objdb is an object which is static towards the program. That means the values inside the object are automatically saved.

Requires Node 10 and later

New: Partially supports user defined prototypes

  • check out how to use the prototype restoring system in this section

Project's advancement

  • Client / Server
  • Reshaped using TypeScript
  • Use of XML or own data language to serialize objects
  • Easy prototype restoring
  • Supports static classes extending

Supported data types

It supports the most of :

  • the instances of the in-built classes Object Array Map Set Date Buffer
  • the primitive types string number boolean object bigint undefined null

Limits

You can not use these types inside a database

Circular objects are not yet supported.

Examples

Understanding

const Database = require('objdb');
 
const drift = new Database.LocalFile({
    name: 'drift',
    defaults: {
        records: []
    }
});
 
/* Adds some random records */
for (let i = 0; i < 10; i++) {
 
    drift.records.push({
        user: `User${Math.floor(Math.random() * 2000)}`,
        score: Math.floor(Math.random() * 50000)
    });
 
}
 
/* Sorts the records by score */
drift.records.sort((a, b) => b.score - a.score);
 
/* Shows a top-10 of the records */
console.log(drift.records.slice(0, 10));

Class extending

const { LocalFile } = require('objdb');
 
class Drift extends LocalFile {
 
    constructor() {
        /* The file name will be "Drift", and will be stored in the current folder */
        super({
            path: '.'
            defaults: {
                records: []
            }
        });
    }
 
}
 
const drift = new Drift();
 
drift.records.push({ user: 'username', score: 51930 });
 
console.log(drift);

Prototype restoring system

const { LocalFile } = require('objdb');
 
class Item {
 
    constructor(name = 'Unknown', durability = Infinity, price = 0) {
        this.name = name;
        this.durability = durability;
        this.price = price;
    }
 
    use() {
        if (this.durability <= 0) return console.log(`This ${this.name} is out of durability`);
        
        this.durability--;
 
        console.log(`You used ${this.name}`);
        /* More actions ... */
    }
 
}
 
/* Initialize the database */
const db = new LocalFile({
    name: 'data',
    defaults: {
        items: []
    },
    constructors: [ Item ]
});
 
/* A magic book that we can use only twice */
const item = new Item('Magic Book', 2);
 
/* Save the magic book */
db.items.push(item);
 
/* Try to use all the magic books */
db.items.forEach(i => i.use());
 
/* Output after 5 iterations:
 *   This Magic Book is out of durability
 *   This Magic Book is out of durability
 *   This Magic Book is out of durability
 *   You used Magic Book
 *   You used Magic Book
 */

Install

npm i objdb

DownloadsWeekly Downloads

4

Version

1.5.1

License

ISC

Unpacked Size

87.9 kB

Total Files

23

Last publish

Collaborators

  • avatar