0.0.2 • Published 11 months ago

sskv v0.0.2

Weekly downloads
-
License
MIT
Repository
github
Last release
11 months ago

SSKV

Super simple key value with type safety and implement your own load and save function.

Why Type safety

Because it's cool.

Why Implement Load and Save Manually

Cross-platform.

Example

In-Memory

No ability to load and save.

type Data = {
	foo: number;
	bar: string;
	lok: boolean;
};

const defaultData: Data = {
	foo: 1,
	bar: "test",
	lok: true,
};

const dummy = new SSKV<Data>(defaultData);

dummy.get("foo");
dummy.get("bar");
dummy.set("bar", "change");
dummy.get("bar");

Node.js

Persistence with JSON file.

type Data = {
	foo: number;
	bar: string;
	lok: boolean;
};

const defaultData: Data = {
	foo: 1,
	bar: "test",
	lok: true,
};

const dummy = new SSKV<Data>(defaultData, {
	save: (currentData, defaultData) => {
		const data = Object.assign({}, defaultData, currentData);
		fs.writeFileSync("data.json", JSON.stringify(data));
	},
	load: () => {
		return JSON.parse(fs.readFileSync("data.json", "utf8")) as Data;
	},
});

// Both of these is equal operation for load and get
dummy.load().get("foo");
dummy.get("foo", true);

// Both of these is equal operation for set and save
dummy.set("bar", "change").save();
dummy.set("bar", "change", true);

Deno

Persistence with JSON file.

type Data = {
	foo: number;
	bar: string;
	lok: boolean;
};

const defaultData: Data = {
	foo: 1,
	bar: "test",
	lok: true,
};

const dummy = new SSKV<Data>(defaultData, {
	save: (currentData, defaultData) => {
		const data = Object.assign({}, defaultData, currentData);
		Deno.writeTextFileSync("data.json", JSON.stringify(data));
	},
	load: () => {
		const decoder = new TextDecoder("utf-8");
		const data = Deno.readFileSync("data.json");
		return JSON.parse(decoder.decode(data)) as Data;
	},
});

// Both of these is equal operation for load and get
dummy.load().get("foo");
dummy.get("foo", true);

// Both of these is equal operation for set and save
dummy.set("bar", "change").save();
dummy.set("bar", "change", true);

Browser localStorage

Persistence with JSON in localStorage.

type Data = {
	foo: number;
	bar: string;
	lok: boolean;
};

const defaultData: Data = {
	foo: 1,
	bar: "test",
	lok: true,
};

const dummy = new SSKV<Data>(defaultData, {
	save: (currentData, defaultData) => {
		const data = Object.assign({}, defaultData, currentData);
		localStorage.setItem("data", JSON.stringify(data));
	},
	load: () => {
		return JSON.parse(localStorage.getItem("data") ?? "{}") as Data;
	},
});

// Both of these is equal operation for load and get
dummy.load().get("foo");
dummy.get("foo", true);

// Both of these is equal operation for set and save
dummy.set("bar", "change").save();
dummy.set("bar", "change", true);
0.0.2

11 months ago

0.0.1

12 months ago