Skip to content

Instantly share code, notes, and snippets.

@tombl
Last active May 12, 2021 13:09
Show Gist options
  • Save tombl/49935a4d27f02de036c2f66302db002b to your computer and use it in GitHub Desktop.
Save tombl/49935a4d27f02de036c2f66302db002b to your computer and use it in GitHub Desktop.
raccoDB

raccoDB

Very sime plen nodeejs database.

Install

Copy either racco.js or racco.ts straight into your project folder, depending on if you use javascript or typescript. You must then set the DATA_DIR variable at the top to the path to the folder which will contain the data, relative to the racco.js file.

How?

The Datastore class takes a string of the name, and a function which returns the initial data. Then, it returns an instance with a property called data. This is your data, and you can change it just like a regular javascript value. When you change it, it is automatically persisted to disk and loaded the next time you start your server.

Example?

// data.js ->
const { Datastore } = require("./racco");

exports.people = new Datastore("people", () => new Map());

// yourcode.js
const { people } = require("./data");

exports.signup = (username, password) => {
    people.data.set(username, { password });
}

exports.checkPassword = (username, password) {
    return people.data.get(username).password === password;
}

Credits

The name is elijah's fault.

"use strict";
const DATA_DIR = "./data";
const fs = require("fs");
const path = require("path");
const v8 = require("v8");
exports.Datastore = class Datastore {
constructor(name, getInitialData) {
this.name = name;
this.file = path.join(__dirname, DATA_DIR, name);
if (fs.existsSync(this.file)) {
this.data = v8.deserialize(fs.readFileSync(this.file));
console.log(`Loaded ${name} with existing data from disk`);
} else {
this.data = getInitialData();
console.log(`Initialized ${name} with fresh data`);
}
setInterval(() => {
this.save();
}, 1000 * 60 * 5);
process.on("exit", () => {
this.save();
});
process.on("SIGINT", () => {
process.exit(1);
});
process.on("SIGUSR2", () => {
process.exit(1);
});
process.on("uncaughtException", (error) => {
console.error(error);
process.exit(1);
});
}
save() {
console.log(`Saving datastore ${this.name}`);
fs.writeFileSync(this.file, v8.serialize(this.data));
}
};
const DATA_DIR = "./data";
import { existsSync, readFileSync, writeFileSync } from "fs";
import { join } from "path";
import { deserialize, serialize } from "v8";
export class Datastore<Data> {
private file: string;
public data: Data;
constructor(private name: string, getInitialData: () => Data) {
this.file = join(__dirname, DATA_DIR, name);
if (existsSync(this.file)) {
this.data = deserialize(readFileSync(this.file)) as Data;
console.log(`Loaded ${name} with existing data from disk`);
} else {
this.data = getInitialData();
console.log(`Initialized ${name} with fresh data`);
}
setInterval(() => {
this.save();
}, 1000 * 60 * 5);
process.on("exit", () => {
this.save();
});
process.on("SIGINT", () => {
process.exit(1);
});
process.on("SIGUSR2", () => {
process.exit(1);
});
process.on("uncaughtException", (error) => {
console.error(error);
process.exit(1);
});
}
public save(): void {
console.log(`Saving datastore ${this.name}`);
writeFileSync(this.file, serialize(this.data));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment