Comparing version 0.0.4 to 0.0.5
@@ -1,5 +0,6 @@ | ||
export declare function KVStorage({ runtime, databaseName, storageName }: { | ||
export declare function KVStorage({ runtime, databaseName, storageName, databaseBindings }: { | ||
runtime?: string; | ||
storageName?: string; | ||
databaseName?: string; | ||
databaseBindings?: any; | ||
}): Promise<any>; |
@@ -36,3 +36,3 @@ "use strict"; | ||
exports.KVStorage = void 0; | ||
function KVStorage({ runtime = 'node', databaseName = 'kvstorage', storageName = 'storage' }) { | ||
function KVStorage({ runtime = 'node', databaseName = 'kvstorage', storageName = 'storage', databaseBindings }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -47,2 +47,4 @@ function isAlphanumeric(str) { | ||
showError('Runtime must be Alphanumeric'); | ||
if (!isAlphanumeric(databaseName)) | ||
showError('storageName must be Alphanumeric'); | ||
if (!isAlphanumeric(storageName)) | ||
@@ -52,3 +54,6 @@ showError('storageName must be Alphanumeric'); | ||
case 'node': | ||
const runnode = yield Promise.resolve().then(() => __importStar(require('./node-kv-storage'))); | ||
let nodepkg = './node-kv-storage'; | ||
if (typeof caches !== "undefined" && typeof global === "undefined" && typeof window === "undefined") | ||
nodepkg = ''; | ||
const runnode = yield Promise.resolve(`${nodepkg}`).then(s => __importStar(require(s))); | ||
const dbnode = yield runnode.NodeKVStorage.init({ | ||
@@ -61,3 +66,4 @@ dataDirName: databaseName, | ||
case 'deno': | ||
const rundeno = yield Promise.resolve().then(() => __importStar(require('./deno-kv-storage'))); | ||
let denopkg = './deno-kv-storage'; | ||
const rundeno = yield Promise.resolve(`${denopkg}`).then(s => __importStar(require(s))); | ||
const dbdeno = yield rundeno.DenoKVStorage.init({ | ||
@@ -69,8 +75,16 @@ dataDirName: databaseName, | ||
break; | ||
case 'bun': | ||
let bunpkg = './bun-kv-storage'; | ||
const runbun = yield Promise.resolve(`${bunpkg}`).then(s => __importStar(require(s))); | ||
const dbbun = yield runbun.BunKVStorage.init({ | ||
dataDirName: databaseName, | ||
storageName | ||
}); | ||
return dbbun; | ||
break; | ||
case 'browser': | ||
let browserpkg = './browser-kv-storage'; | ||
if (window) | ||
if (typeof window !== "undefined" && typeof window.document !== "undefined") | ||
browserpkg = './browser-kv-storage.js'; | ||
const runbrowser = yield Promise.resolve(`${browserpkg}`).then(s => __importStar(require(s))); | ||
//const runbrowser = await import('./browser-kv-storage') | ||
const dbbrowser = yield runbrowser.BrowserKVStorage.init({ | ||
@@ -82,2 +96,12 @@ databaseName, | ||
break; | ||
case 'cloudflare': | ||
//let cloudflarepkg = 'server.js' | ||
//if(typeof caches !== "undefined" && typeof global === "undefined" && typeof window === "undefined")cloudflarepkg = './cloudflare-kv-storage' | ||
//const runcloudflare = await import(cloudflarepkg) | ||
const dbcloudflare = yield CloudflareKVStorage.init({ | ||
databaseBindings, | ||
storageName | ||
}); | ||
return dbcloudflare; | ||
break; | ||
default: | ||
@@ -89,1 +113,116 @@ showError('Runtime unknown'); | ||
exports.KVStorage = KVStorage; | ||
class CloudflareKVStorage { | ||
constructor({ databaseBindings, storageName }) { | ||
this._databaseBindings = databaseBindings; | ||
this._storageName = storageName; | ||
} | ||
isAlphanumeric(str) { | ||
return /^[a-zA-Z0-9]+$/.test(str); | ||
} | ||
showError(msg = 'Error') { | ||
throw new Error(msg); | ||
} | ||
static init({ databaseBindings, storageName, }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
function isAlphanumeric(str) { | ||
return /^[a-zA-Z0-9]+$/.test(str); | ||
} | ||
function showError(msg = 'Error') { | ||
throw new Error(msg); | ||
} | ||
if (!isAlphanumeric(storageName)) | ||
showError('storageName must be Alphanumeric'); | ||
const stmt = databaseBindings.prepare('CREATE TABLE IF NOT EXISTS ' + storageName + ' (key text NOT NULL PRIMARY KEY,value text NOT NULL)'); | ||
const values = yield stmt.run(); | ||
return new CloudflareKVStorage({ databaseBindings, storageName }); | ||
}); | ||
} | ||
put(key, value) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
const stmt = this._databaseBindings.prepare('SELECT value FROM ' + this._storageName + ' WHERE key = ?1').bind(key); | ||
const values = yield stmt.first(); | ||
if (values == null) { | ||
const stmt = this._databaseBindings.prepare('INSERT INTO ' + this._storageName + ' (key,value) VALUES (?1,?2)').bind(key, value); | ||
const values = yield stmt.run(); | ||
return values.succes; | ||
} | ||
else { | ||
const stmt = this._databaseBindings.prepare('UPDATE ' + this._storageName + ' SET value = ?2 WHERE key = ?1').bind(key, value); | ||
const values = yield stmt.run(); | ||
return values.success; | ||
} | ||
}); | ||
} | ||
get(key) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
const stmt = this._databaseBindings.prepare('SELECT value FROM ' + this._storageName + ' WHERE key = ?1').bind(key); | ||
const values = yield stmt.first(); | ||
let output; | ||
if (values == null) { | ||
output = false; | ||
} | ||
else { | ||
output = values.value; | ||
} | ||
return output; | ||
}); | ||
} | ||
delete(key) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
const stmt = this._databaseBindings.prepare('DELETE FROM ' + this._storageName + ' WHERE key = ?1').bind(key); | ||
const values = yield stmt.first(); | ||
let output; | ||
if (values == null) { | ||
output = false; | ||
} | ||
else { | ||
output = true; | ||
} | ||
return output; | ||
}); | ||
} | ||
has(key) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
const stmt = this._databaseBindings.prepare('SELECT value FROM ' + this._storageName + ' WHERE key = ?1').bind(key); | ||
const values = yield stmt.first(); | ||
let output; | ||
if (values == null) { | ||
output = false; | ||
} | ||
else { | ||
output = true; | ||
} | ||
return output; | ||
}); | ||
} | ||
list() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const stmt = this._databaseBindings.prepare('SELECT key FROM ' + this._storageName).bind(); | ||
const values = yield stmt.all(); | ||
let output; | ||
if (values.success) { | ||
let keys = []; | ||
values.results.forEach((obj) => { | ||
keys.push(obj.key); | ||
}); | ||
let result = { | ||
keys: keys, | ||
complete: true | ||
}; | ||
output = result; | ||
} | ||
else { | ||
output = false; | ||
} | ||
return output; | ||
}); | ||
} | ||
} |
@@ -1,5 +0,6 @@ | ||
export declare function KVStorage({ runtime, databaseName, storageName }: { | ||
export declare function KVStorage({ runtime, databaseName, storageName, databaseBindings }: { | ||
runtime?: string; | ||
storageName?: string; | ||
databaseName?: string; | ||
databaseBindings?: any; | ||
}): Promise<any>; |
@@ -1,2 +0,2 @@ | ||
export async function KVStorage({ runtime = 'node', databaseName = 'kvstorage', storageName = 'storage' }) { | ||
export async function KVStorage({ runtime = 'node', databaseName = 'kvstorage', storageName = 'storage', databaseBindings }) { | ||
function isAlphanumeric(str) { | ||
@@ -10,2 +10,4 @@ return /^[a-zA-Z0-9]+$/.test(str); | ||
showError('Runtime must be Alphanumeric'); | ||
if (!isAlphanumeric(databaseName)) | ||
showError('storageName must be Alphanumeric'); | ||
if (!isAlphanumeric(storageName)) | ||
@@ -15,3 +17,6 @@ showError('storageName must be Alphanumeric'); | ||
case 'node': | ||
const runnode = await import('./node-kv-storage'); | ||
let nodepkg = './node-kv-storage'; | ||
if (typeof caches !== "undefined" && typeof global === "undefined" && typeof window === "undefined") | ||
nodepkg = ''; | ||
const runnode = await import(nodepkg); | ||
const dbnode = await runnode.NodeKVStorage.init({ | ||
@@ -24,3 +29,4 @@ dataDirName: databaseName, | ||
case 'deno': | ||
const rundeno = await import('./deno-kv-storage'); | ||
let denopkg = './deno-kv-storage'; | ||
const rundeno = await import(denopkg); | ||
const dbdeno = await rundeno.DenoKVStorage.init({ | ||
@@ -32,8 +38,16 @@ dataDirName: databaseName, | ||
break; | ||
case 'bun': | ||
let bunpkg = './bun-kv-storage'; | ||
const runbun = await import(bunpkg); | ||
const dbbun = await runbun.BunKVStorage.init({ | ||
dataDirName: databaseName, | ||
storageName | ||
}); | ||
return dbbun; | ||
break; | ||
case 'browser': | ||
let browserpkg = './browser-kv-storage'; | ||
if (window) | ||
if (typeof window !== "undefined" && typeof window.document !== "undefined") | ||
browserpkg = './browser-kv-storage.js'; | ||
const runbrowser = await import(browserpkg); | ||
//const runbrowser = await import('./browser-kv-storage') | ||
const dbbrowser = await runbrowser.BrowserKVStorage.init({ | ||
@@ -45,2 +59,12 @@ databaseName, | ||
break; | ||
case 'cloudflare': | ||
//let cloudflarepkg = 'server.js' | ||
//if(typeof caches !== "undefined" && typeof global === "undefined" && typeof window === "undefined")cloudflarepkg = './cloudflare-kv-storage' | ||
//const runcloudflare = await import(cloudflarepkg) | ||
const dbcloudflare = await CloudflareKVStorage.init({ | ||
databaseBindings, | ||
storageName | ||
}); | ||
return dbcloudflare; | ||
break; | ||
default: | ||
@@ -50,1 +74,106 @@ showError('Runtime unknown'); | ||
} | ||
class CloudflareKVStorage { | ||
_storageName; | ||
_databaseBindings; | ||
constructor({ databaseBindings, storageName }) { | ||
this._databaseBindings = databaseBindings; | ||
this._storageName = storageName; | ||
} | ||
isAlphanumeric(str) { | ||
return /^[a-zA-Z0-9]+$/.test(str); | ||
} | ||
showError(msg = 'Error') { | ||
throw new Error(msg); | ||
} | ||
static async init({ databaseBindings, storageName, }) { | ||
function isAlphanumeric(str) { | ||
return /^[a-zA-Z0-9]+$/.test(str); | ||
} | ||
function showError(msg = 'Error') { | ||
throw new Error(msg); | ||
} | ||
if (!isAlphanumeric(storageName)) | ||
showError('storageName must be Alphanumeric'); | ||
const stmt = databaseBindings.prepare('CREATE TABLE IF NOT EXISTS ' + storageName + ' (key text NOT NULL PRIMARY KEY,value text NOT NULL)'); | ||
const values = await stmt.run(); | ||
return new CloudflareKVStorage({ databaseBindings, storageName }); | ||
} | ||
async put(key, value) { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
const stmt = this._databaseBindings.prepare('SELECT value FROM ' + this._storageName + ' WHERE key = ?1').bind(key); | ||
const values = await stmt.first(); | ||
if (values == null) { | ||
const stmt = this._databaseBindings.prepare('INSERT INTO ' + this._storageName + ' (key,value) VALUES (?1,?2)').bind(key, value); | ||
const values = await stmt.run(); | ||
return values.succes; | ||
} | ||
else { | ||
const stmt = this._databaseBindings.prepare('UPDATE ' + this._storageName + ' SET value = ?2 WHERE key = ?1').bind(key, value); | ||
const values = await stmt.run(); | ||
return values.success; | ||
} | ||
} | ||
async get(key) { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
const stmt = this._databaseBindings.prepare('SELECT value FROM ' + this._storageName + ' WHERE key = ?1').bind(key); | ||
const values = await stmt.first(); | ||
let output; | ||
if (values == null) { | ||
output = false; | ||
} | ||
else { | ||
output = values.value; | ||
} | ||
return output; | ||
} | ||
async delete(key) { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
const stmt = this._databaseBindings.prepare('DELETE FROM ' + this._storageName + ' WHERE key = ?1').bind(key); | ||
const values = await stmt.first(); | ||
let output; | ||
if (values == null) { | ||
output = false; | ||
} | ||
else { | ||
output = true; | ||
} | ||
return output; | ||
} | ||
async has(key) { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
const stmt = this._databaseBindings.prepare('SELECT value FROM ' + this._storageName + ' WHERE key = ?1').bind(key); | ||
const values = await stmt.first(); | ||
let output; | ||
if (values == null) { | ||
output = false; | ||
} | ||
else { | ||
output = true; | ||
} | ||
return output; | ||
} | ||
async list() { | ||
const stmt = this._databaseBindings.prepare('SELECT key FROM ' + this._storageName).bind(); | ||
const values = await stmt.all(); | ||
let output; | ||
if (values.success) { | ||
let keys = []; | ||
values.results.forEach((obj) => { | ||
keys.push(obj.key); | ||
}); | ||
let result = { | ||
keys: keys, | ||
complete: true | ||
}; | ||
output = result; | ||
} | ||
else { | ||
output = false; | ||
} | ||
return output; | ||
} | ||
} |
(function (global, factory) { | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports, require('fs'), require('node:fs')) : | ||
typeof define === 'function' && define.amd ? define(['exports', 'fs', 'node:fs'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.kvstorage = {}, global.fs, global.fs$1)); | ||
})(this, (function (exports, fs, fs$1) { 'use strict'; | ||
typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) : | ||
typeof define === 'function' && define.amd ? define(['exports'], factory) : | ||
(global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.kvstorage = {})); | ||
})(this, (function (exports) { 'use strict'; | ||
async function KVStorage({ runtime = 'node', databaseName = 'kvstorage', storageName = 'storage' }) { | ||
async function KVStorage({ runtime = 'node', databaseName = 'kvstorage', storageName = 'storage', databaseBindings }) { | ||
function isAlphanumeric(str) { | ||
@@ -16,2 +16,4 @@ return /^[a-zA-Z0-9]+$/.test(str); | ||
showError('Runtime must be Alphanumeric'); | ||
if (!isAlphanumeric(databaseName)) | ||
showError('storageName must be Alphanumeric'); | ||
if (!isAlphanumeric(storageName)) | ||
@@ -21,3 +23,6 @@ showError('storageName must be Alphanumeric'); | ||
case 'node': | ||
const runnode = await Promise.resolve().then(function () { return nodeKvStorage; }); | ||
let nodepkg = './node-kv-storage'; | ||
if (typeof caches !== "undefined" && typeof global === "undefined" && typeof window === "undefined") | ||
nodepkg = ''; | ||
const runnode = await import(nodepkg); | ||
const dbnode = await runnode.NodeKVStorage.init({ | ||
@@ -29,3 +34,4 @@ dataDirName: databaseName, | ||
case 'deno': | ||
const rundeno = await Promise.resolve().then(function () { return denoKvStorage; }); | ||
let denopkg = './deno-kv-storage'; | ||
const rundeno = await import(denopkg); | ||
const dbdeno = await rundeno.DenoKVStorage.init({ | ||
@@ -36,8 +42,15 @@ dataDirName: databaseName, | ||
return dbdeno; | ||
case 'bun': | ||
let bunpkg = './bun-kv-storage'; | ||
const runbun = await import(bunpkg); | ||
const dbbun = await runbun.BunKVStorage.init({ | ||
dataDirName: databaseName, | ||
storageName | ||
}); | ||
return dbbun; | ||
case 'browser': | ||
let browserpkg = './browser-kv-storage'; | ||
if (window) | ||
if (typeof window !== "undefined" && typeof window.document !== "undefined") | ||
browserpkg = './browser-kv-storage.js'; | ||
const runbrowser = await import(browserpkg); | ||
//const runbrowser = await import('./browser-kv-storage') | ||
const dbbrowser = await runbrowser.BrowserKVStorage.init({ | ||
@@ -48,2 +61,11 @@ databaseName, | ||
return dbbrowser; | ||
case 'cloudflare': | ||
//let cloudflarepkg = 'server.js' | ||
//if(typeof caches !== "undefined" && typeof global === "undefined" && typeof window === "undefined")cloudflarepkg = './cloudflare-kv-storage' | ||
//const runcloudflare = await import(cloudflarepkg) | ||
const dbcloudflare = await CloudflareKVStorage.init({ | ||
databaseBindings, | ||
storageName | ||
}); | ||
return dbcloudflare; | ||
default: | ||
@@ -53,6 +75,6 @@ showError('Runtime unknown'); | ||
} | ||
class NodeKVStorage { | ||
constructor({ storageDir }) { | ||
this._storageDir = storageDir; | ||
class CloudflareKVStorage { | ||
constructor({ databaseBindings, storageName }) { | ||
this._databaseBindings = databaseBindings; | ||
this._storageName = storageName; | ||
} | ||
@@ -65,3 +87,3 @@ isAlphanumeric(str) { | ||
} | ||
static async init({ dataDirName = "data", storageName }) { | ||
static async init({ databaseBindings, storageName, }) { | ||
function isAlphanumeric(str) { | ||
@@ -73,243 +95,90 @@ return /^[a-zA-Z0-9]+$/.test(str); | ||
} | ||
async function makeDir(dir) { | ||
return new Promise((resolve) => { | ||
fs.stat(dir, (err) => { | ||
if (err) { | ||
fs.mkdir(dir, { recursive: true }, (err, path) => { | ||
if (err) { | ||
throw err; | ||
} | ||
else { | ||
resolve(true); | ||
} | ||
}); | ||
} | ||
else { | ||
resolve(true); | ||
} | ||
}); | ||
}); | ||
} | ||
if (!isAlphanumeric(dataDirName)) | ||
showError('dataDirName must be Alphanumeric'); | ||
if (!isAlphanumeric(storageName)) | ||
showError('storageName must be Alphanumeric'); | ||
let _dataDirName = "./" + dataDirName; | ||
let storageDir = _dataDirName + '/' + storageName; | ||
await makeDir(storageDir); | ||
return new NodeKVStorage({ storageDir }); | ||
const stmt = databaseBindings.prepare('CREATE TABLE IF NOT EXISTS ' + storageName + ' (key text NOT NULL PRIMARY KEY,value text NOT NULL)'); | ||
await stmt.run(); | ||
return new CloudflareKVStorage({ databaseBindings, storageName }); | ||
} | ||
async put(key, value) { | ||
return new Promise((resolve) => { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
const keyFilePath = this._storageDir + '/' + key; | ||
fs.writeFile(keyFilePath, value, (err) => { | ||
if (err) { | ||
resolve(false); | ||
} | ||
else { | ||
resolve(true); | ||
} | ||
}); | ||
}); | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
const stmt = this._databaseBindings.prepare('SELECT value FROM ' + this._storageName + ' WHERE key = ?1').bind(key); | ||
const values = await stmt.first(); | ||
if (values == null) { | ||
const stmt = this._databaseBindings.prepare('INSERT INTO ' + this._storageName + ' (key,value) VALUES (?1,?2)').bind(key, value); | ||
const values = await stmt.run(); | ||
return values.succes; | ||
} | ||
else { | ||
const stmt = this._databaseBindings.prepare('UPDATE ' + this._storageName + ' SET value = ?2 WHERE key = ?1').bind(key, value); | ||
const values = await stmt.run(); | ||
return values.success; | ||
} | ||
} | ||
async get(key) { | ||
return new Promise((resolve) => { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
const keyFilePath = this._storageDir + '/' + key; | ||
fs.readFile(keyFilePath, (err, data) => { | ||
if (err) { | ||
resolve(false); | ||
} | ||
else { | ||
resolve(data.toString('utf8', 0, data.length)); | ||
} | ||
}); | ||
}); | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
const stmt = this._databaseBindings.prepare('SELECT value FROM ' + this._storageName + ' WHERE key = ?1').bind(key); | ||
const values = await stmt.first(); | ||
let output; | ||
if (values == null) { | ||
output = false; | ||
} | ||
else { | ||
output = values.value; | ||
} | ||
return output; | ||
} | ||
async delete(key) { | ||
return new Promise((resolve) => { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
const keyFilePath = this._storageDir + '/' + key; | ||
fs.unlink(keyFilePath, (err) => { | ||
if (err) { | ||
resolve(false); | ||
} | ||
else { | ||
resolve(true); | ||
} | ||
}); | ||
}); | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
const stmt = this._databaseBindings.prepare('DELETE FROM ' + this._storageName + ' WHERE key = ?1').bind(key); | ||
const values = await stmt.first(); | ||
let output; | ||
if (values == null) { | ||
output = false; | ||
} | ||
else { | ||
output = true; | ||
} | ||
return output; | ||
} | ||
async has(key) { | ||
return new Promise((resolve) => { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
const keyFilePath = this._storageDir + '/' + key; | ||
fs.stat(keyFilePath, (err) => { | ||
if (err) { | ||
resolve(false); | ||
} | ||
else { | ||
resolve(true); | ||
} | ||
}); | ||
}); | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
const stmt = this._databaseBindings.prepare('SELECT value FROM ' + this._storageName + ' WHERE key = ?1').bind(key); | ||
const values = await stmt.first(); | ||
let output; | ||
if (values == null) { | ||
output = false; | ||
} | ||
else { | ||
output = true; | ||
} | ||
return output; | ||
} | ||
async list() { | ||
return new Promise((resolve) => { | ||
fs.readdir(this._storageDir, (err, files) => { | ||
if (err) { | ||
resolve(false); | ||
} | ||
else { | ||
let result = { | ||
keys: files, | ||
complete: true | ||
}; | ||
resolve(result); | ||
} | ||
const stmt = this._databaseBindings.prepare('SELECT key FROM ' + this._storageName).bind(); | ||
const values = await stmt.all(); | ||
let output; | ||
if (values.success) { | ||
let keys = []; | ||
values.results.forEach((obj) => { | ||
keys.push(obj.key); | ||
}); | ||
}); | ||
} | ||
} | ||
var nodeKvStorage = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
NodeKVStorage: NodeKVStorage | ||
}); | ||
class DenoKVStorage { | ||
constructor({ storageDir }) { | ||
this._storageDir = storageDir; | ||
} | ||
isAlphanumeric(str) { | ||
return /^[a-zA-Z0-9]+$/.test(str); | ||
} | ||
showError(msg = 'Error') { | ||
throw new Error(msg); | ||
} | ||
static async init({ dataDirName = "data", storageName }) { | ||
function isAlphanumeric(str) { | ||
return /^[a-zA-Z0-9]+$/.test(str); | ||
let result = { | ||
keys: keys, | ||
complete: true | ||
}; | ||
output = result; | ||
} | ||
function showError(msg = 'Error') { | ||
throw new Error(msg); | ||
else { | ||
output = false; | ||
} | ||
async function makeDir(dir) { | ||
return new Promise((resolve) => { | ||
fs$1.stat(dir, (err) => { | ||
if (err) { | ||
fs$1.mkdir(dir, { recursive: true }, (err, path) => { | ||
if (err) { | ||
throw err; | ||
} | ||
else { | ||
resolve(true); | ||
} | ||
}); | ||
} | ||
else { | ||
resolve(true); | ||
} | ||
}); | ||
}); | ||
} | ||
if (!isAlphanumeric(dataDirName)) | ||
showError('dataDirName must be Alphanumeric'); | ||
if (!isAlphanumeric(storageName)) | ||
showError('storageName must be Alphanumeric'); | ||
let _dataDirName = "./" + dataDirName; | ||
let storageDir = _dataDirName + '/' + storageName; | ||
await makeDir(storageDir); | ||
return new DenoKVStorage({ storageDir }); | ||
return output; | ||
} | ||
async put(key, value) { | ||
return new Promise((resolve) => { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
const keyFilePath = this._storageDir + '/' + key; | ||
fs$1.writeFile(keyFilePath, value, (err) => { | ||
if (err) { | ||
resolve(false); | ||
} | ||
else { | ||
resolve(true); | ||
} | ||
}); | ||
}); | ||
} | ||
async get(key) { | ||
return new Promise((resolve) => { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
const keyFilePath = this._storageDir + '/' + key; | ||
fs$1.readFile(keyFilePath, (err, data) => { | ||
if (err) { | ||
resolve(false); | ||
} | ||
else { | ||
resolve(data.toString('utf8', 0, data.length)); | ||
} | ||
}); | ||
}); | ||
} | ||
async delete(key) { | ||
return new Promise((resolve) => { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
const keyFilePath = this._storageDir + '/' + key; | ||
fs$1.unlink(keyFilePath, (err) => { | ||
if (err) { | ||
resolve(false); | ||
} | ||
else { | ||
resolve(true); | ||
} | ||
}); | ||
}); | ||
} | ||
async has(key) { | ||
return new Promise((resolve) => { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
const keyFilePath = this._storageDir + '/' + key; | ||
fs$1.stat(keyFilePath, (err) => { | ||
if (err) { | ||
resolve(false); | ||
} | ||
else { | ||
resolve(true); | ||
} | ||
}); | ||
}); | ||
} | ||
async list() { | ||
return new Promise((resolve) => { | ||
fs$1.readdir(this._storageDir, (err, files) => { | ||
if (err) { | ||
resolve(false); | ||
} | ||
else { | ||
let result = { | ||
keys: files, | ||
complete: true | ||
}; | ||
resolve(result); | ||
} | ||
}); | ||
}); | ||
} | ||
} | ||
var denoKvStorage = /*#__PURE__*/Object.freeze({ | ||
__proto__: null, | ||
DenoKVStorage: DenoKVStorage | ||
}); | ||
exports.KVStorage = KVStorage; | ||
})); |
{ | ||
"name": "kv-storage", | ||
"version": "0.0.4", | ||
"version": "0.0.5", | ||
"description": "Create data storage that uses a simple key-value method for Node, Browser, Deno, Bun, Cloudflare Workers", | ||
@@ -15,3 +15,3 @@ "main": "dist/cjs/kv-storage.js", | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"build": "tsc", | ||
"build": "tsc -p tsconfig-dev.json", | ||
"start": "node public/test/server.js", | ||
@@ -25,2 +25,3 @@ "start-mjs": "npm run build && node public/test/server-mjs.js", | ||
"dev-mjs": "nodemon -e js,ts --watch src --watch test --exec \"npm run start-mjs\"", | ||
"dev-cf": "nodemon -e js,ts,html --watch src --watch test --exec \"npm run prepare-public && tsc -p tsconfig-browser.json && wrangler dev --env dev\"", | ||
"build-win": "npm run prepare-build-win && tsc -p tsconfig-mjs.json && tsc -p tsconfig-cjs.json && tsc -p tsconfig-umd.json && rollup -c public/config/rollup.config.umd.js && echo {\"type\": \"commonjs\"}>dist\\cjs\\package.json && echo {\"type\": \"module\"}>dist\\mjs\\package.json", | ||
@@ -31,3 +32,3 @@ "prepare-build": "if exist .\\dist (echo ok) && mkdir dist && del /S /Q .\\dist\\*", | ||
"prepare-public": "if not exist .\\public (mkdir public) else (rmdir /S /Q .\\public\\)", | ||
"typedoc": "npm run prepare-typedoc && typedoc src/kv-storage.ts src/node-kv-storage.ts src/deno-kv-storage.ts src/browser-kv-storage.ts", | ||
"typedoc": "npm run prepare-typedoc && typedoc src/kv-storage.ts src/node-kv-storage.ts src/deno-kv-storage.ts src/browser-kv-storage.ts src/bun-kv-storage.ts", | ||
"gh-deploy": "git push origin :gh-pages && git subtree push --prefix docs origin gh-pages", | ||
@@ -75,4 +76,5 @@ "gh-deploy-init": "git push origin && git subtree push --prefix docs origin gh-pages", | ||
"typedoc": "^0.25.7", | ||
"typescript": "^5.3.3" | ||
"typescript": "^5.3.3", | ||
"wrangler": "^3.26.0" | ||
} | ||
} |
@@ -18,3 +18,3 @@ # kv-storage | ||
NPM (node, browser, deno) | ||
NPM (node, browser, deno, bun, cloudflare) | ||
```javascript | ||
@@ -30,7 +30,9 @@ npm install kv-storage | ||
NPM | ||
```javascript | ||
//Node CommonJS import style | ||
//Node & Bun CommonJS import style | ||
const {KVStorage} = require('kv-storage') | ||
//Node & Browser ES Modules import style | ||
//Node, Browser, Bun & Cloudflare ES Modules import style | ||
import {KVStorage} from 'kv-storage' | ||
@@ -42,6 +44,13 @@ | ||
const db = await KVStorage({ | ||
runtime:'node', //node | browser| deno | ||
runtime:'node', //node | browser | deno | bun | ||
storageName:'storage' | ||
}) | ||
const db = await KVStorage({ | ||
runtime:'cloudflare', | ||
storageName:'storage', | ||
databaseBindings:env.D1 //Cloudflare D1 database bindings env | ||
}) | ||
``` | ||
CDN | ||
```javascript | ||
@@ -59,3 +68,3 @@ //Browser using CDN | ||
```javascript | ||
//Node CommonJS example | ||
//Node & Bun CommonJS example | ||
const {KVStorage} = require('kv-storage') | ||
@@ -65,3 +74,3 @@ | ||
const db = await KVStorage({ | ||
runtime:'node', | ||
runtime:'node',//node | bun | ||
storageName:'storage' | ||
@@ -79,3 +88,3 @@ }) | ||
```javascript | ||
//Node ES Modules example | ||
//Node & Bun ES Modules example | ||
import {KVStorage} from 'kv-storage' | ||
@@ -85,3 +94,3 @@ | ||
const db = await KVStorage({ | ||
runtime:'node', | ||
runtime:'node',//node | bun | ||
storageName:'storage' | ||
@@ -154,2 +163,32 @@ }) | ||
```javascript | ||
//Cloudflare workers example | ||
import {KVStorage} from 'kv-storage' | ||
export default { | ||
async fetch(request: Request, env: Env): Promise<Response> { | ||
const db = await KVStorage({ | ||
runtime:'cloudflare', | ||
storageName:'storage', | ||
databaseBindings:env.D1 | ||
}) | ||
let data = [] | ||
data.push(await db.put('key','value')) | ||
data.push(await db.get('key')) | ||
data.push(await db.has('key')) | ||
data.push(await db.list()) | ||
data.push(await db.delete('key')) | ||
return new Response(JSON.stringify(data)) | ||
} | ||
} | ||
``` | ||
## API Reference | ||
@@ -166,3 +205,4 @@ | ||
runtime?:string, | ||
storageName?:string | ||
storageName?:string, | ||
databaseBindings?:any //Cloudflare only | ||
}) | ||
@@ -173,2 +213,3 @@ ``` | ||
storageName = Alphanumeric storage name | ||
databaseBindings = Cloudflare D1 database bindings env | ||
``` | ||
@@ -179,5 +220,5 @@ Supported runtime : | ||
- [x] `browser` use IndexedDB | ||
- [ ] `bun` | ||
- [ ] `cloudflare-workers` | ||
- [ ] `memory` | ||
- [x] `bun` | ||
- [x] `cloudflare-workers` use D1 Database [docs](https://developers.cloudflare.com/d1/get-started/#4-bind-your-worker-to-your-d1-database) | ||
### Write key-value pairs | ||
@@ -184,0 +225,0 @@ |
85487
1992
253
8
8