1.1.30 • Published 3 years ago

simpledatabases v1.1.30

Weekly downloads
952
License
ISC
Repository
github
Last release
3 years ago

SimpleDatabases

This package is a system that will allow you to storage your objects in a database without having to deal with anything regarding the database itsself. We manage everything with tables and adding/updating/removing data for you.

How to use

To get started with SimpleDatabases you need to create a StorageHolder, which will keep track of all your storages. A storage is a collection of all of your objects of ONE specific type. Then to store the data in the database you will need to create a database object. The currently implemented ones are MySQLDatabase, MongoDBDatabase and FlatFileDatabase.

const holder: StorageHolder = new StorageHolder();
const mysqldatabase: MySQLDatabase = new MySQLDatabase({database: "simpledb", user: "simpledb", password: "simpledb"});
const mongo: MongoDBDatabase = new MongoDBDatabase("simpledb", "mongodb://localhost:27017/", { auth: { user: "simpledb", password: "simpledb" } });
const flatfile: FlatFileDatabase = new FlatFileDatabase();

At this point you need to create an object you want to store. This must extend an AbstractBody. The currently implemented ones are MySQLBody, MongoDBBody and FlatFileBody.

class ExampleObject extends MySQLBody {

    id: string;
    prefix: string;
    level: number;

    constructor(storage: MySQLStorage<MySQLBody>, database: MySQLDatabase) {
        super(storage, database);

        // Store default values here in constructor
        this.prefix = "!";
        this.level = 0;
    }

    getCollection = () => "tablename";
    getIdentifier = () => "id";
    getIdentifierValues = () => this.id;

    getColumns = () => new Map()
        .set("id", ColumnType.VARCHAR50)
        .set("prefix", ColumnType.TINYTEXT)
        .set("level", ColumnType.INT);

    serialize(data: SerializedData) {
        data.write("id", this.id);
        data.write("prefix", this.prefix);
        data.write("level", this.level)
    }

    deserialize(data: SerializedData) {
        this.id = data.get("id");
        this.prefix = data.get("prefix");
        this.level = data.get("level", "number"); // Because we pass "number" as the second parameter, we call parseInt on the data to make sure its a number
    }
}

Now we need a holder which will hold our ExampleObject's. This needs to extend AbstractStorage. The currently implemented ones are MySQLStorage, MongoDBStorage and FlatFileStorage. This is mainly used to implement your own way of caching data (if you want to cache data, otherwise just have empty functions.)

class DataHolder extends MySQLStorage<DataObject> {

    cached: Map<string, DataObject>;

    constructor(holder: StorageHolder, database: MySQLDatabase) {
        super(holder, database, DataObject);
        this.cached = new Map();
    }

    onRemove(object: DataObject): Promise<void> {
        this.cached.delete(object.getIdentifierValues()); // Remove from cache
        return;
    }

    onAdd(object: DataObject): Promise<void> {
        this.cached.set(object.getIdentifierValues(), object); // Add to cache
        return;
    }

    getValues(): DataObject[] {
        return Array.from(this.cached.values());
    }
}

Now that we have our dataholder we need to create one. By creating an instance of the class we automatically register it in our storage holder.

const holder: StorageHolder = new StorageHolder();
const database: MySQLDatabase = new MySQLDatabase({database: "simpledb", user: "simpledb", password: "simpledb"});

const dataHolder: DataHolder = new DataHolder(holder,database);

Now everything is set up for us to start creating and storing objects.

const holder: StorageHolder = new StorageHolder();
const database: MySQLDatabase = new MySQLDatabase({database: "simpledb", user: "simpledb", password: "simpledb"});

const dataHolder: DataHolder = new DataHolder(holder,database);

const dataHolderReference: DataHolder = holder.get("tablename"); // This is another way of getting a reference to the data holder

const obj = dataHolder.getOrCreate({id: 1});
obj.prefix = ".";
obj.level = 5;
await obj.save(); // Save this object

const obj2 = dataHolder.getOrCreate({id: 2});
obj2.prefix = "/";

const obj3 = dataHolder.getOrCreate({id: 3});
obj3.level = 6;

await dataHolder.save(); // Save all objects in cache
1.1.30

3 years ago

1.1.29

3 years ago

1.1.28

3 years ago

1.1.12

3 years ago

1.1.11

3 years ago

1.1.16

3 years ago

1.1.15

3 years ago

1.1.14

3 years ago

1.1.13

3 years ago

1.1.18

3 years ago

1.1.17

3 years ago

1.1.23

3 years ago

1.1.22

3 years ago

1.1.27

3 years ago

1.1.26

3 years ago

1.1.25

3 years ago

1.1.24

3 years ago

1.1.10

3 years ago

1.1.8

3 years ago

1.1.7

3 years ago

1.1.6

3 years ago

1.1.1

3 years ago

1.1.4

3 years ago

1.1.3

3 years ago

1.1.2

3 years ago

1.0.28

3 years ago

1.0.27

3 years ago

1.0.26

3 years ago

1.0.25

3 years ago

1.0.24

3 years ago

1.0.22

3 years ago

1.0.21

3 years ago

1.0.23

3 years ago

1.0.20

3 years ago

1.0.19

3 years ago

1.0.18

3 years ago

1.0.17

3 years ago

1.0.16

3 years ago

1.0.15

4 years ago

1.0.14

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.7

4 years ago

1.0.6

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago