fastkv 0.0.0 → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md ADDED
@@ -0,0 +1,46 @@
1
+ <div align="center">
2
+ <img src="https://jvk6hm88bttdy90i.public.blob.vercel-storage.com/fastkv.png" width="400" style="border-radius: 25px;" />
3
+ </div>
4
+
5
+ # FastKV
6
+
7
+ FastKV is a key-value store which offers the following features:
8
+
9
+ - Persistent storage in JSON files 📁
10
+ - Caching with in-memory storage 🕒
11
+
12
+ ## Installation
13
+
14
+ Get FastKV via npm using your favourite package manager:
15
+
16
+ ```sh
17
+ npm install fastkv
18
+ # or
19
+ yarn add fastkv
20
+ # or
21
+ pnpm add fastkv
22
+ ```
23
+
24
+ ## Example
25
+
26
+ ```js
27
+ import { KV } from 'fastkv';
28
+
29
+ // For TypeScript users, the KV supports generics:
30
+ // const example = new KV<string>();
31
+
32
+ const kv = new KV('./db.json'); // Save the data. Path resolves with process.cwd()
33
+ const cache = new KV('::memory::'); // Keep the data in the system's memory.
34
+
35
+ // Set data
36
+ await kv.set('userSettings', { theme: 'dark' }); // => Promise<KV>
37
+
38
+ // Retreive data by a key
39
+ await kv.get('userSettings'); // => Promise resolving to { theme: 'dark' }
40
+
41
+ // Retreive all data
42
+ await kv.all(); // => Promise resolving an array: [{ key: 'userSettings', value: { theme: 'dark' } }]
43
+
44
+ // Clear the store
45
+ await kv.clear(); // => Promise<KV>
46
+ ```
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Represents a key-value store.
3
+ * Initializing with `::memory::` uses in-memory storage.
4
+ */
5
+ declare class KV<T = any> {
6
+ #private;
7
+ /**
8
+ * Creates a new instance of the class.
9
+ * @param location - Where the data will be stored.
10
+ * Can be `::memory::` or a file location.
11
+ */
12
+ constructor(location?: string);
13
+ /**
14
+ * Sets a value to a key.
15
+ * @param key - The name of the key.
16
+ * @param value - The value to store.
17
+ * @returns A promise returning the KV instance.
18
+ */
19
+ set(key: string, value: T): Promise<this>;
20
+ /**
21
+ * Gets data by a key.
22
+ * @param key - The existing key's name.
23
+ * @returns A promise returning the found key, or null.
24
+ */
25
+ get(key: string): Promise<T | null>;
26
+ /**
27
+ * Gets all data in the store.
28
+ * @returns A promise returning an array of data.
29
+ */
30
+ all(): Promise<T[]>;
31
+ /**
32
+ * Clears all data in the store.
33
+ * @returns A promise returning the KV instance.
34
+ */
35
+ clear(): Promise<this>;
36
+ }
37
+ declare const _default: KV<any>;
38
+
39
+ export { KV, _default as default };
@@ -0,0 +1,39 @@
1
+ /**
2
+ * Represents a key-value store.
3
+ * Initializing with `::memory::` uses in-memory storage.
4
+ */
5
+ declare class KV<T = any> {
6
+ #private;
7
+ /**
8
+ * Creates a new instance of the class.
9
+ * @param location - Where the data will be stored.
10
+ * Can be `::memory::` or a file location.
11
+ */
12
+ constructor(location?: string);
13
+ /**
14
+ * Sets a value to a key.
15
+ * @param key - The name of the key.
16
+ * @param value - The value to store.
17
+ * @returns A promise returning the KV instance.
18
+ */
19
+ set(key: string, value: T): Promise<this>;
20
+ /**
21
+ * Gets data by a key.
22
+ * @param key - The existing key's name.
23
+ * @returns A promise returning the found key, or null.
24
+ */
25
+ get(key: string): Promise<T | null>;
26
+ /**
27
+ * Gets all data in the store.
28
+ * @returns A promise returning an array of data.
29
+ */
30
+ all(): Promise<T[]>;
31
+ /**
32
+ * Clears all data in the store.
33
+ * @returns A promise returning the KV instance.
34
+ */
35
+ clear(): Promise<this>;
36
+ }
37
+ declare const _default: KV<any>;
38
+
39
+ export { KV, _default as default };
package/dist/index.js ADDED
@@ -0,0 +1 @@
1
+ "use strict";var u=Object.create;var a=Object.defineProperty;var m=Object.getOwnPropertyDescriptor;var y=Object.getOwnPropertyNames;var p=Object.getPrototypeOf,d=Object.prototype.hasOwnProperty;var f=(r,t)=>{for(var s in t)a(r,s,{get:t[s],enumerable:!0})},h=(r,t,s,i)=>{if(t&&typeof t=="object"||typeof t=="function")for(let e of y(t))!d.call(r,e)&&e!==s&&a(r,e,{get:()=>t[e],enumerable:!(i=m(t,e))||i.enumerable});return r};var n=(r,t,s)=>(s=r!=null?u(p(r)):{},h(t||!r||!r.__esModule?a(s,"default",{value:r,enumerable:!0}):s,r)),g=r=>h(a({},"__esModule",{value:!0}),r);var T={};f(T,{KV:()=>c,default:()=>w});module.exports=g(T);var o=n(require("fs/promises")),l=n(require("path")),c=class{#t;#r;constructor(t="::memory::"){if(this.#t=t,this.#r={},t!="::memory::"){this.#t=l.default.resolve(process.cwd(),this.#t);let s=i=>{this.#r=i};this.#i().then(s).catch(()=>({}))}}async#i(){if(this.#t==="::memory::")return{};try{let t=await o.default.readFile(this.#t,"utf8");return JSON.parse(t)||{}}catch{return{}}}async#s(){this.#t!=="::memory::"&&await o.default.writeFile(this.#t,JSON.stringify(this.#r),"utf8")}async set(t,s){try{return this.#r[t]=s,await this.#s(),this}catch(i){throw i}}async get(t){return this.#r[t]??null}async all(){return Object.values(this.#r)}async clear(){try{return this.#r={},await this.#s(),this}catch(t){throw t}}},w=new c;0&&(module.exports={KV});
package/dist/index.mjs ADDED
@@ -0,0 +1 @@
1
+ import e from"node:fs/promises";import a from"node:path";var i=class{#t;#r;constructor(t="::memory::"){if(this.#t=t,this.#r={},t!="::memory::"){this.#t=a.resolve(process.cwd(),this.#t);let r=s=>{this.#r=s};this.#i().then(r).catch(()=>({}))}}async#i(){if(this.#t==="::memory::")return{};try{let t=await e.readFile(this.#t,"utf8");return JSON.parse(t)||{}}catch{return{}}}async#s(){this.#t!=="::memory::"&&await e.writeFile(this.#t,JSON.stringify(this.#r),"utf8")}async set(t,r){try{return this.#r[t]=r,await this.#s(),this}catch(s){throw s}}async get(t){return this.#r[t]??null}async all(){return Object.values(this.#r)}async clear(){try{return this.#r={},await this.#s(),this}catch(t){throw t}}},u=new i;export{i as KV,u as default};
package/package.json CHANGED
@@ -1,4 +1,25 @@
1
1
  {
2
2
  "name": "fastkv",
3
- "version": "0.0.0"
4
- }
3
+ "version": "0.1.0",
4
+ "publishConfig": {
5
+ "access": "public"
6
+ },
7
+ "description": "A key-value store, helpful for caches in apps.",
8
+ "main": "dist/index.js",
9
+ "keywords": [
10
+ "kv",
11
+ "database",
12
+ "json"
13
+ ],
14
+ "license": "MIT",
15
+ "devDependencies": {
16
+ "@types/node": "^20.10.5",
17
+ "@underctrl/tsconfig": "^0.0.1",
18
+ "tsup": "^8.0.1",
19
+ "typescript": "^5.3.3"
20
+ },
21
+ "scripts": {
22
+ "build": "tsup",
23
+ "lint": "tsc --noEmit true"
24
+ }
25
+ }