Comparing version 0.0.5 to 0.0.6
@@ -1,6 +0,6 @@ | ||
export declare function KVStorage({ runtime, databaseName, storageName, databaseBindings }: { | ||
export declare function KVStorage({ runtime, databaseName, storageName, databaseBinding }: { | ||
runtime?: string; | ||
storageName?: string; | ||
databaseName?: string; | ||
databaseBindings?: any; | ||
databaseBinding?: any; | ||
}): Promise<any>; |
@@ -36,3 +36,3 @@ "use strict"; | ||
exports.KVStorage = void 0; | ||
function KVStorage({ runtime = 'node', databaseName = 'kvstorage', storageName = 'storage', databaseBindings }) { | ||
function KVStorage({ runtime = 'node', databaseName = 'kvstorage', storageName = 'storage', databaseBinding }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -53,50 +53,58 @@ function isAlphanumeric(str) { | ||
case 'node': | ||
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({ | ||
dataDirName: databaseName, | ||
storageName | ||
}); | ||
return dbnode; | ||
if (typeof caches === "undefined" && typeof global !== "undefined" && typeof window === "undefined") { | ||
let nodepkg = './kv-storage-module'; | ||
const runnode = yield Promise.resolve(`${nodepkg}`).then(s => __importStar(require(s))); | ||
const dbnode = yield runnode.KVStorageModule.init({ | ||
dataDirName: databaseName, | ||
storageName | ||
}); | ||
return dbnode; | ||
} | ||
break; | ||
case 'deno': | ||
let denopkg = './deno-kv-storage'; | ||
const rundeno = yield Promise.resolve(`${denopkg}`).then(s => __importStar(require(s))); | ||
const dbdeno = yield rundeno.DenoKVStorage.init({ | ||
dataDirName: databaseName, | ||
storageName | ||
}); | ||
return dbdeno; | ||
if (typeof caches !== "undefined" && typeof global === "undefined" && typeof window !== "undefined") { | ||
let rundeno; | ||
try { | ||
const url = './kv-storage-module.ts'; | ||
rundeno = yield Promise.resolve(`${url}`).then(s => __importStar(require(s))); | ||
} | ||
catch (_a) { | ||
const url = './kv-storage-module.js'; | ||
rundeno = yield Promise.resolve(`${url}`).then(s => __importStar(require(s))); | ||
} | ||
const dbdeno = yield rundeno.KVStorageModule.init({ | ||
dataDirName: databaseName, | ||
storageName | ||
}); | ||
return dbdeno; | ||
} | ||
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; | ||
if (typeof caches === "undefined" && typeof global !== "undefined" && typeof window === "undefined") { | ||
let bunpkg = './kv-storage-module'; | ||
const runbun = yield Promise.resolve(`${bunpkg}`).then(s => __importStar(require(s))); | ||
const dbbun = yield runbun.KVStorageModule.init({ | ||
dataDirName: databaseName, | ||
storageName | ||
}); | ||
return dbbun; | ||
} | ||
break; | ||
case 'browser': | ||
let browserpkg = './browser-kv-storage'; | ||
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 dbbrowser = yield runbrowser.BrowserKVStorage.init({ | ||
databaseName, | ||
storageName | ||
}); | ||
return dbbrowser; | ||
if (typeof window !== "undefined" && typeof window.document !== "undefined") { | ||
const dbbrowser = KVStorageBrowser.init({ | ||
databaseName, | ||
storageName | ||
}); | ||
return dbbrowser; | ||
} | ||
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; | ||
if (typeof caches !== "undefined" && typeof global === "undefined" && typeof window === "undefined") { | ||
const dbcloudflare = yield KVStorageCloudflare.init({ | ||
databaseBinding, | ||
storageName | ||
}); | ||
return dbcloudflare; | ||
} | ||
break; | ||
@@ -109,5 +117,5 @@ default: | ||
exports.KVStorage = KVStorage; | ||
class CloudflareKVStorage { | ||
constructor({ databaseBindings, storageName }) { | ||
this._databaseBindings = databaseBindings; | ||
class KVStorageCloudflare { | ||
constructor({ databaseBinding, storageName }) { | ||
this._databaseBinding = databaseBinding; | ||
this._storageName = storageName; | ||
@@ -121,3 +129,3 @@ } | ||
} | ||
static init({ databaseBindings, storageName, }) { | ||
static init({ databaseBinding, storageName, }) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
@@ -132,5 +140,5 @@ function isAlphanumeric(str) { | ||
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 stmt = databaseBinding.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 }); | ||
return new KVStorageCloudflare({ databaseBinding, storageName }); | ||
}); | ||
@@ -142,11 +150,11 @@ } | ||
this.showError('Key must be Alphanumeric'); | ||
const stmt = this._databaseBindings.prepare('SELECT value FROM ' + this._storageName + ' WHERE key = ?1').bind(key); | ||
const stmt = this._databaseBinding.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 stmt = this._databaseBinding.prepare('INSERT INTO ' + this._storageName + ' (key,value) VALUES (?1,?2)').bind(key, value); | ||
const values = yield stmt.run(); | ||
return values.succes; | ||
return values.success; | ||
} | ||
else { | ||
const stmt = this._databaseBindings.prepare('UPDATE ' + this._storageName + ' SET value = ?2 WHERE key = ?1').bind(key, value); | ||
const stmt = this._databaseBinding.prepare('UPDATE ' + this._storageName + ' SET value = ?2 WHERE key = ?1').bind(key, value); | ||
const values = yield stmt.run(); | ||
@@ -161,3 +169,3 @@ return values.success; | ||
this.showError('Key must be Alphanumeric'); | ||
const stmt = this._databaseBindings.prepare('SELECT value FROM ' + this._storageName + ' WHERE key = ?1').bind(key); | ||
const stmt = this._databaseBinding.prepare('SELECT value FROM ' + this._storageName + ' WHERE key = ?1').bind(key); | ||
const values = yield stmt.first(); | ||
@@ -178,12 +186,5 @@ let output; | ||
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; | ||
const stmt = this._databaseBinding.prepare('DELETE FROM ' + this._storageName + ' WHERE key = ?1').bind(key); | ||
const values = yield stmt.run(); | ||
return values.success; | ||
}); | ||
@@ -195,6 +196,6 @@ } | ||
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(); | ||
const stmt = this._databaseBinding.prepare('SELECT value FROM ' + this._storageName + ' WHERE key = ?1').bind(key); | ||
const values = yield stmt.run(); | ||
let output; | ||
if (values == null) { | ||
if (values.results == 0) { | ||
output = false; | ||
@@ -210,3 +211,3 @@ } | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const stmt = this._databaseBindings.prepare('SELECT key FROM ' + this._storageName).bind(); | ||
const stmt = this._databaseBinding.prepare('SELECT key FROM ' + this._storageName).bind(); | ||
const values = yield stmt.all(); | ||
@@ -232,1 +233,156 @@ let output; | ||
} | ||
class KVStorageBrowser { | ||
constructor({ databaseName, storageName, dbVersion, iDB }) { | ||
this._databaseName = databaseName; | ||
this._storageName = storageName; | ||
this._dbVersion = 1; | ||
this._iDB = iDB; | ||
} | ||
isAlphanumeric(str) { | ||
return /^[a-zA-Z0-9]+$/.test(str); | ||
} | ||
showError(msg = 'Error') { | ||
throw new Error(msg); | ||
} | ||
static init({ databaseName = "data", 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); | ||
} | ||
function indexedDBStuff() { | ||
// Check for IndexedDB support | ||
if (!('indexedDB' in window)) { | ||
// Can't use IndexedDB | ||
showError("This browser doesn't support IndexedDB"); | ||
return; | ||
} | ||
else { | ||
// Do IndexedDB stuff here | ||
} | ||
} | ||
// Run IndexedDB code | ||
indexedDBStuff(); | ||
const dbVersion = 1; | ||
if (!isAlphanumeric(databaseName)) | ||
showError('dataDirName must be Alphanumeric'); | ||
if (!isAlphanumeric(storageName)) | ||
showError('storageName must be Alphanumeric'); | ||
const createdb = new Promise((resolve) => { | ||
let request = window.indexedDB.open(databaseName, dbVersion); | ||
request.onerror = function () { | ||
console.error("Error", request.error); | ||
}; | ||
request.onsuccess = function () { | ||
let db = request.result; | ||
resolve(db); | ||
}; | ||
request.onupgradeneeded = function (event) { | ||
let db = request.result; | ||
if (!db.objectStoreNames.contains(storageName)) { | ||
db.createObjectStore(storageName, { keyPath: 'key' }); | ||
} | ||
}; | ||
request.onblocked = function () { | ||
console.error("Error, conflict"); | ||
}; | ||
}); | ||
const iDB = yield createdb; | ||
return new KVStorageBrowser({ databaseName, storageName, dbVersion, iDB }); | ||
}); | ||
} | ||
put(key, value) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return new Promise((resolve) => { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
let transaction = this._iDB.transaction(this._storageName, "readwrite"); | ||
let request = transaction.objectStore(this._storageName).put({ key: key, value: value }); | ||
request.onsuccess = function () { | ||
resolve(true); | ||
}; | ||
request.onerror = function () { | ||
resolve(false); | ||
}; | ||
}); | ||
}); | ||
} | ||
get(key) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return new Promise((resolve) => { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
let transaction = this._iDB.transaction(this._storageName, "readonly"); | ||
let request = transaction.objectStore(this._storageName).get(key); | ||
request.onsuccess = function (event) { | ||
if (request.result != undefined) { | ||
resolve(request.result.value); | ||
} | ||
else { | ||
resolve(false); | ||
} | ||
}; | ||
request.onerror = function () { | ||
resolve(false); | ||
}; | ||
}); | ||
}); | ||
} | ||
delete(key) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return new Promise((resolve) => { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
let transaction = this._iDB.transaction(this._storageName, "readwrite"); | ||
let request = transaction.objectStore(this._storageName).delete(key); | ||
request.onsuccess = function (event) { | ||
resolve(true); | ||
}; | ||
request.onerror = function () { | ||
resolve(false); | ||
}; | ||
}); | ||
}); | ||
} | ||
has(key) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return new Promise((resolve) => { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
let transaction = this._iDB.transaction(this._storageName, "readonly"); | ||
let request = transaction.objectStore(this._storageName).get(key); | ||
request.onsuccess = function (event) { | ||
if (request.result != undefined) { | ||
resolve(true); | ||
} | ||
else { | ||
resolve(false); | ||
} | ||
}; | ||
request.onerror = function () { | ||
resolve(false); | ||
}; | ||
}); | ||
}); | ||
} | ||
list() { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
return new Promise((resolve) => { | ||
let transaction = this._iDB.transaction(this._storageName, "readonly"); | ||
let request = transaction.objectStore(this._storageName).getAllKeys(); | ||
request.onsuccess = function (event) { | ||
let result = { | ||
keys: request.result, | ||
complete: true | ||
}; | ||
resolve(result); | ||
}; | ||
request.onerror = function () { | ||
resolve(false); | ||
}; | ||
}); | ||
}); | ||
} | ||
} |
@@ -1,6 +0,6 @@ | ||
export declare function KVStorage({ runtime, databaseName, storageName, databaseBindings }: { | ||
export declare function KVStorage({ runtime, databaseName, storageName, databaseBinding }: { | ||
runtime?: string; | ||
storageName?: string; | ||
databaseName?: string; | ||
databaseBindings?: any; | ||
databaseBinding?: any; | ||
}): Promise<any>; |
@@ -1,2 +0,2 @@ | ||
export async function KVStorage({ runtime = 'node', databaseName = 'kvstorage', storageName = 'storage', databaseBindings }) { | ||
export async function KVStorage({ runtime = 'node', databaseName = 'kvstorage', storageName = 'storage', databaseBinding }) { | ||
function isAlphanumeric(str) { | ||
@@ -16,50 +16,58 @@ return /^[a-zA-Z0-9]+$/.test(str); | ||
case 'node': | ||
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({ | ||
dataDirName: databaseName, | ||
storageName | ||
}); | ||
return dbnode; | ||
if (typeof caches === "undefined" && typeof global !== "undefined" && typeof window === "undefined") { | ||
let nodepkg = './kv-storage-module'; | ||
const runnode = await import(nodepkg); | ||
const dbnode = await runnode.KVStorageModule.init({ | ||
dataDirName: databaseName, | ||
storageName | ||
}); | ||
return dbnode; | ||
} | ||
break; | ||
case 'deno': | ||
let denopkg = './deno-kv-storage'; | ||
const rundeno = await import(denopkg); | ||
const dbdeno = await rundeno.DenoKVStorage.init({ | ||
dataDirName: databaseName, | ||
storageName | ||
}); | ||
return dbdeno; | ||
if (typeof caches !== "undefined" && typeof global === "undefined" && typeof window !== "undefined") { | ||
let rundeno; | ||
try { | ||
const url = './kv-storage-module.ts'; | ||
rundeno = await import(url); | ||
} | ||
catch { | ||
const url = './kv-storage-module.js'; | ||
rundeno = await import(url); | ||
} | ||
const dbdeno = await rundeno.KVStorageModule.init({ | ||
dataDirName: databaseName, | ||
storageName | ||
}); | ||
return dbdeno; | ||
} | ||
break; | ||
case 'bun': | ||
let bunpkg = './bun-kv-storage'; | ||
const runbun = await import(bunpkg); | ||
const dbbun = await runbun.BunKVStorage.init({ | ||
dataDirName: databaseName, | ||
storageName | ||
}); | ||
return dbbun; | ||
if (typeof caches === "undefined" && typeof global !== "undefined" && typeof window === "undefined") { | ||
let bunpkg = './kv-storage-module'; | ||
const runbun = await import(bunpkg); | ||
const dbbun = await runbun.KVStorageModule.init({ | ||
dataDirName: databaseName, | ||
storageName | ||
}); | ||
return dbbun; | ||
} | ||
break; | ||
case 'browser': | ||
let browserpkg = './browser-kv-storage'; | ||
if (typeof window !== "undefined" && typeof window.document !== "undefined") | ||
browserpkg = './browser-kv-storage.js'; | ||
const runbrowser = await import(browserpkg); | ||
const dbbrowser = await runbrowser.BrowserKVStorage.init({ | ||
databaseName, | ||
storageName | ||
}); | ||
return dbbrowser; | ||
if (typeof window !== "undefined" && typeof window.document !== "undefined") { | ||
const dbbrowser = KVStorageBrowser.init({ | ||
databaseName, | ||
storageName | ||
}); | ||
return dbbrowser; | ||
} | ||
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; | ||
if (typeof caches !== "undefined" && typeof global === "undefined" && typeof window === "undefined") { | ||
const dbcloudflare = await KVStorageCloudflare.init({ | ||
databaseBinding, | ||
storageName | ||
}); | ||
return dbcloudflare; | ||
} | ||
break; | ||
@@ -70,7 +78,7 @@ default: | ||
} | ||
class CloudflareKVStorage { | ||
class KVStorageCloudflare { | ||
_storageName; | ||
_databaseBindings; | ||
constructor({ databaseBindings, storageName }) { | ||
this._databaseBindings = databaseBindings; | ||
_databaseBinding; | ||
constructor({ databaseBinding, storageName }) { | ||
this._databaseBinding = databaseBinding; | ||
this._storageName = storageName; | ||
@@ -84,3 +92,3 @@ } | ||
} | ||
static async init({ databaseBindings, storageName, }) { | ||
static async init({ databaseBinding, storageName, }) { | ||
function isAlphanumeric(str) { | ||
@@ -94,5 +102,5 @@ return /^[a-zA-Z0-9]+$/.test(str); | ||
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 stmt = databaseBinding.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 }); | ||
return new KVStorageCloudflare({ databaseBinding, storageName }); | ||
} | ||
@@ -102,11 +110,11 @@ async put(key, value) { | ||
this.showError('Key must be Alphanumeric'); | ||
const stmt = this._databaseBindings.prepare('SELECT value FROM ' + this._storageName + ' WHERE key = ?1').bind(key); | ||
const stmt = this._databaseBinding.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 stmt = this._databaseBinding.prepare('INSERT INTO ' + this._storageName + ' (key,value) VALUES (?1,?2)').bind(key, value); | ||
const values = await stmt.run(); | ||
return values.succes; | ||
return values.success; | ||
} | ||
else { | ||
const stmt = this._databaseBindings.prepare('UPDATE ' + this._storageName + ' SET value = ?2 WHERE key = ?1').bind(key, value); | ||
const stmt = this._databaseBinding.prepare('UPDATE ' + this._storageName + ' SET value = ?2 WHERE key = ?1').bind(key, value); | ||
const values = await stmt.run(); | ||
@@ -119,3 +127,3 @@ return values.success; | ||
this.showError('Key must be Alphanumeric'); | ||
const stmt = this._databaseBindings.prepare('SELECT value FROM ' + this._storageName + ' WHERE key = ?1').bind(key); | ||
const stmt = this._databaseBinding.prepare('SELECT value FROM ' + this._storageName + ' WHERE key = ?1').bind(key); | ||
const values = await stmt.first(); | ||
@@ -134,12 +142,5 @@ let output; | ||
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; | ||
const stmt = this._databaseBinding.prepare('DELETE FROM ' + this._storageName + ' WHERE key = ?1').bind(key); | ||
const values = await stmt.run(); | ||
return values.success; | ||
} | ||
@@ -149,6 +150,6 @@ async has(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(); | ||
const stmt = this._databaseBinding.prepare('SELECT value FROM ' + this._storageName + ' WHERE key = ?1').bind(key); | ||
const values = await stmt.run(); | ||
let output; | ||
if (values == null) { | ||
if (values.results == 0) { | ||
output = false; | ||
@@ -162,3 +163,3 @@ } | ||
async list() { | ||
const stmt = this._databaseBindings.prepare('SELECT key FROM ' + this._storageName).bind(); | ||
const stmt = this._databaseBinding.prepare('SELECT key FROM ' + this._storageName).bind(); | ||
const values = await stmt.all(); | ||
@@ -183,1 +184,148 @@ let output; | ||
} | ||
class KVStorageBrowser { | ||
_databaseName; | ||
_storageName; | ||
_dbVersion; | ||
_iDB; | ||
constructor({ databaseName, storageName, dbVersion, iDB }) { | ||
this._databaseName = databaseName; | ||
this._storageName = storageName; | ||
this._dbVersion = 1; | ||
this._iDB = iDB; | ||
} | ||
isAlphanumeric(str) { | ||
return /^[a-zA-Z0-9]+$/.test(str); | ||
} | ||
showError(msg = 'Error') { | ||
throw new Error(msg); | ||
} | ||
static async init({ databaseName = "data", storageName, }) { | ||
function isAlphanumeric(str) { | ||
return /^[a-zA-Z0-9]+$/.test(str); | ||
} | ||
function showError(msg = 'Error') { | ||
throw new Error(msg); | ||
} | ||
function indexedDBStuff() { | ||
// Check for IndexedDB support | ||
if (!('indexedDB' in window)) { | ||
// Can't use IndexedDB | ||
showError("This browser doesn't support IndexedDB"); | ||
return; | ||
} | ||
else { | ||
// Do IndexedDB stuff here | ||
} | ||
} | ||
// Run IndexedDB code | ||
indexedDBStuff(); | ||
const dbVersion = 1; | ||
if (!isAlphanumeric(databaseName)) | ||
showError('dataDirName must be Alphanumeric'); | ||
if (!isAlphanumeric(storageName)) | ||
showError('storageName must be Alphanumeric'); | ||
const createdb = new Promise((resolve) => { | ||
let request = window.indexedDB.open(databaseName, dbVersion); | ||
request.onerror = function () { | ||
console.error("Error", request.error); | ||
}; | ||
request.onsuccess = function () { | ||
let db = request.result; | ||
resolve(db); | ||
}; | ||
request.onupgradeneeded = function (event) { | ||
let db = request.result; | ||
if (!db.objectStoreNames.contains(storageName)) { | ||
db.createObjectStore(storageName, { keyPath: 'key' }); | ||
} | ||
}; | ||
request.onblocked = function () { | ||
console.error("Error, conflict"); | ||
}; | ||
}); | ||
const iDB = await createdb; | ||
return new KVStorageBrowser({ databaseName, storageName, dbVersion, iDB }); | ||
} | ||
async put(key, value) { | ||
return new Promise((resolve) => { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
let transaction = this._iDB.transaction(this._storageName, "readwrite"); | ||
let request = transaction.objectStore(this._storageName).put({ key: key, value: value }); | ||
request.onsuccess = function () { | ||
resolve(true); | ||
}; | ||
request.onerror = function () { | ||
resolve(false); | ||
}; | ||
}); | ||
} | ||
async get(key) { | ||
return new Promise((resolve) => { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
let transaction = this._iDB.transaction(this._storageName, "readonly"); | ||
let request = transaction.objectStore(this._storageName).get(key); | ||
request.onsuccess = function (event) { | ||
if (request.result != undefined) { | ||
resolve(request.result.value); | ||
} | ||
else { | ||
resolve(false); | ||
} | ||
}; | ||
request.onerror = function () { | ||
resolve(false); | ||
}; | ||
}); | ||
} | ||
async delete(key) { | ||
return new Promise((resolve) => { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
let transaction = this._iDB.transaction(this._storageName, "readwrite"); | ||
let request = transaction.objectStore(this._storageName).delete(key); | ||
request.onsuccess = function (event) { | ||
resolve(true); | ||
}; | ||
request.onerror = function () { | ||
resolve(false); | ||
}; | ||
}); | ||
} | ||
async has(key) { | ||
return new Promise((resolve) => { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
let transaction = this._iDB.transaction(this._storageName, "readonly"); | ||
let request = transaction.objectStore(this._storageName).get(key); | ||
request.onsuccess = function (event) { | ||
if (request.result != undefined) { | ||
resolve(true); | ||
} | ||
else { | ||
resolve(false); | ||
} | ||
}; | ||
request.onerror = function () { | ||
resolve(false); | ||
}; | ||
}); | ||
} | ||
async list() { | ||
return new Promise((resolve) => { | ||
let transaction = this._iDB.transaction(this._storageName, "readonly"); | ||
let request = transaction.objectStore(this._storageName).getAllKeys(); | ||
request.onsuccess = function (event) { | ||
let result = { | ||
keys: request.result, | ||
complete: true | ||
}; | ||
resolve(result); | ||
}; | ||
request.onerror = function () { | ||
resolve(false); | ||
}; | ||
}); | ||
} | ||
} |
@@ -7,3 +7,3 @@ (function (global, factory) { | ||
async function KVStorage({ runtime = 'node', databaseName = 'kvstorage', storageName = 'storage', databaseBindings }) { | ||
async function KVStorage({ runtime = 'node', databaseName = 'kvstorage', storageName = 'storage', databaseBinding }) { | ||
function isAlphanumeric(str) { | ||
@@ -23,46 +23,59 @@ return /^[a-zA-Z0-9]+$/.test(str); | ||
case 'node': | ||
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({ | ||
dataDirName: databaseName, | ||
storageName | ||
}); | ||
return dbnode; | ||
if (typeof caches === "undefined" && typeof global !== "undefined" && typeof window === "undefined") { | ||
let nodepkg = './kv-storage-module'; | ||
const runnode = await import(nodepkg); | ||
const dbnode = await runnode.KVStorageModule.init({ | ||
dataDirName: databaseName, | ||
storageName | ||
}); | ||
return dbnode; | ||
} | ||
break; | ||
case 'deno': | ||
let denopkg = './deno-kv-storage'; | ||
const rundeno = await import(denopkg); | ||
const dbdeno = await rundeno.DenoKVStorage.init({ | ||
dataDirName: databaseName, | ||
storageName | ||
}); | ||
return dbdeno; | ||
if (typeof caches !== "undefined" && typeof global === "undefined" && typeof window !== "undefined") { | ||
let rundeno; | ||
try { | ||
const url = './kv-storage-module.ts'; | ||
rundeno = await import(url); | ||
} | ||
catch { | ||
const url = './kv-storage-module.js'; | ||
rundeno = await import(url); | ||
} | ||
const dbdeno = await rundeno.KVStorageModule.init({ | ||
dataDirName: databaseName, | ||
storageName | ||
}); | ||
return dbdeno; | ||
} | ||
break; | ||
case 'bun': | ||
let bunpkg = './bun-kv-storage'; | ||
const runbun = await import(bunpkg); | ||
const dbbun = await runbun.BunKVStorage.init({ | ||
dataDirName: databaseName, | ||
storageName | ||
}); | ||
return dbbun; | ||
if (typeof caches === "undefined" && typeof global !== "undefined" && typeof window === "undefined") { | ||
let bunpkg = './kv-storage-module'; | ||
const runbun = await import(bunpkg); | ||
const dbbun = await runbun.KVStorageModule.init({ | ||
dataDirName: databaseName, | ||
storageName | ||
}); | ||
return dbbun; | ||
} | ||
break; | ||
case 'browser': | ||
let browserpkg = './browser-kv-storage'; | ||
if (typeof window !== "undefined" && typeof window.document !== "undefined") | ||
browserpkg = './browser-kv-storage.js'; | ||
const runbrowser = await import(browserpkg); | ||
const dbbrowser = await runbrowser.BrowserKVStorage.init({ | ||
databaseName, | ||
storageName | ||
}); | ||
return dbbrowser; | ||
if (typeof window !== "undefined" && typeof window.document !== "undefined") { | ||
const dbbrowser = KVStorageBrowser.init({ | ||
databaseName, | ||
storageName | ||
}); | ||
return dbbrowser; | ||
} | ||
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; | ||
if (typeof caches !== "undefined" && typeof global === "undefined" && typeof window === "undefined") { | ||
const dbcloudflare = await KVStorageCloudflare.init({ | ||
databaseBinding, | ||
storageName | ||
}); | ||
return dbcloudflare; | ||
} | ||
break; | ||
default: | ||
@@ -72,5 +85,5 @@ showError('Runtime unknown'); | ||
} | ||
class CloudflareKVStorage { | ||
constructor({ databaseBindings, storageName }) { | ||
this._databaseBindings = databaseBindings; | ||
class KVStorageCloudflare { | ||
constructor({ databaseBinding, storageName }) { | ||
this._databaseBinding = databaseBinding; | ||
this._storageName = storageName; | ||
@@ -84,3 +97,3 @@ } | ||
} | ||
static async init({ databaseBindings, storageName, }) { | ||
static async init({ databaseBinding, storageName, }) { | ||
function isAlphanumeric(str) { | ||
@@ -94,5 +107,5 @@ return /^[a-zA-Z0-9]+$/.test(str); | ||
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 stmt = databaseBinding.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 }); | ||
return new KVStorageCloudflare({ databaseBinding, storageName }); | ||
} | ||
@@ -102,11 +115,11 @@ async put(key, value) { | ||
this.showError('Key must be Alphanumeric'); | ||
const stmt = this._databaseBindings.prepare('SELECT value FROM ' + this._storageName + ' WHERE key = ?1').bind(key); | ||
const stmt = this._databaseBinding.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 stmt = this._databaseBinding.prepare('INSERT INTO ' + this._storageName + ' (key,value) VALUES (?1,?2)').bind(key, value); | ||
const values = await stmt.run(); | ||
return values.succes; | ||
return values.success; | ||
} | ||
else { | ||
const stmt = this._databaseBindings.prepare('UPDATE ' + this._storageName + ' SET value = ?2 WHERE key = ?1').bind(key, value); | ||
const stmt = this._databaseBinding.prepare('UPDATE ' + this._storageName + ' SET value = ?2 WHERE key = ?1').bind(key, value); | ||
const values = await stmt.run(); | ||
@@ -119,3 +132,3 @@ return values.success; | ||
this.showError('Key must be Alphanumeric'); | ||
const stmt = this._databaseBindings.prepare('SELECT value FROM ' + this._storageName + ' WHERE key = ?1').bind(key); | ||
const stmt = this._databaseBinding.prepare('SELECT value FROM ' + this._storageName + ' WHERE key = ?1').bind(key); | ||
const values = await stmt.first(); | ||
@@ -134,12 +147,5 @@ let output; | ||
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; | ||
const stmt = this._databaseBinding.prepare('DELETE FROM ' + this._storageName + ' WHERE key = ?1').bind(key); | ||
const values = await stmt.run(); | ||
return values.success; | ||
} | ||
@@ -149,6 +155,6 @@ async has(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(); | ||
const stmt = this._databaseBinding.prepare('SELECT value FROM ' + this._storageName + ' WHERE key = ?1').bind(key); | ||
const values = await stmt.run(); | ||
let output; | ||
if (values == null) { | ||
if (values.results == 0) { | ||
output = false; | ||
@@ -162,3 +168,3 @@ } | ||
async list() { | ||
const stmt = this._databaseBindings.prepare('SELECT key FROM ' + this._storageName).bind(); | ||
const stmt = this._databaseBinding.prepare('SELECT key FROM ' + this._storageName).bind(); | ||
const values = await stmt.all(); | ||
@@ -183,2 +189,142 @@ let output; | ||
} | ||
class KVStorageBrowser { | ||
constructor({ databaseName, storageName, dbVersion, iDB }) { | ||
this._databaseName = databaseName; | ||
this._storageName = storageName; | ||
this._dbVersion = 1; | ||
this._iDB = iDB; | ||
} | ||
isAlphanumeric(str) { | ||
return /^[a-zA-Z0-9]+$/.test(str); | ||
} | ||
showError(msg = 'Error') { | ||
throw new Error(msg); | ||
} | ||
static async init({ databaseName = "data", storageName, }) { | ||
function isAlphanumeric(str) { | ||
return /^[a-zA-Z0-9]+$/.test(str); | ||
} | ||
function showError(msg = 'Error') { | ||
throw new Error(msg); | ||
} | ||
function indexedDBStuff() { | ||
// Check for IndexedDB support | ||
if (!('indexedDB' in window)) { | ||
// Can't use IndexedDB | ||
showError("This browser doesn't support IndexedDB"); | ||
return; | ||
} | ||
} | ||
// Run IndexedDB code | ||
indexedDBStuff(); | ||
const dbVersion = 1; | ||
if (!isAlphanumeric(databaseName)) | ||
showError('dataDirName must be Alphanumeric'); | ||
if (!isAlphanumeric(storageName)) | ||
showError('storageName must be Alphanumeric'); | ||
const createdb = new Promise((resolve) => { | ||
let request = window.indexedDB.open(databaseName, dbVersion); | ||
request.onerror = function () { | ||
console.error("Error", request.error); | ||
}; | ||
request.onsuccess = function () { | ||
let db = request.result; | ||
resolve(db); | ||
}; | ||
request.onupgradeneeded = function (event) { | ||
let db = request.result; | ||
if (!db.objectStoreNames.contains(storageName)) { | ||
db.createObjectStore(storageName, { keyPath: 'key' }); | ||
} | ||
}; | ||
request.onblocked = function () { | ||
console.error("Error, conflict"); | ||
}; | ||
}); | ||
const iDB = await createdb; | ||
return new KVStorageBrowser({ databaseName, storageName, dbVersion, iDB }); | ||
} | ||
async put(key, value) { | ||
return new Promise((resolve) => { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
let transaction = this._iDB.transaction(this._storageName, "readwrite"); | ||
let request = transaction.objectStore(this._storageName).put({ key: key, value: value }); | ||
request.onsuccess = function () { | ||
resolve(true); | ||
}; | ||
request.onerror = function () { | ||
resolve(false); | ||
}; | ||
}); | ||
} | ||
async get(key) { | ||
return new Promise((resolve) => { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
let transaction = this._iDB.transaction(this._storageName, "readonly"); | ||
let request = transaction.objectStore(this._storageName).get(key); | ||
request.onsuccess = function (event) { | ||
if (request.result != undefined) { | ||
resolve(request.result.value); | ||
} | ||
else { | ||
resolve(false); | ||
} | ||
}; | ||
request.onerror = function () { | ||
resolve(false); | ||
}; | ||
}); | ||
} | ||
async delete(key) { | ||
return new Promise((resolve) => { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
let transaction = this._iDB.transaction(this._storageName, "readwrite"); | ||
let request = transaction.objectStore(this._storageName).delete(key); | ||
request.onsuccess = function (event) { | ||
resolve(true); | ||
}; | ||
request.onerror = function () { | ||
resolve(false); | ||
}; | ||
}); | ||
} | ||
async has(key) { | ||
return new Promise((resolve) => { | ||
if (!this.isAlphanumeric(key)) | ||
this.showError('Key must be Alphanumeric'); | ||
let transaction = this._iDB.transaction(this._storageName, "readonly"); | ||
let request = transaction.objectStore(this._storageName).get(key); | ||
request.onsuccess = function (event) { | ||
if (request.result != undefined) { | ||
resolve(true); | ||
} | ||
else { | ||
resolve(false); | ||
} | ||
}; | ||
request.onerror = function () { | ||
resolve(false); | ||
}; | ||
}); | ||
} | ||
async list() { | ||
return new Promise((resolve) => { | ||
let transaction = this._iDB.transaction(this._storageName, "readonly"); | ||
let request = transaction.objectStore(this._storageName).getAllKeys(); | ||
request.onsuccess = function (event) { | ||
let result = { | ||
keys: request.result, | ||
complete: true | ||
}; | ||
resolve(result); | ||
}; | ||
request.onerror = function () { | ||
resolve(false); | ||
}; | ||
}); | ||
} | ||
} | ||
@@ -185,0 +331,0 @@ exports.KVStorage = KVStorage; |
{ | ||
"name": "kv-storage", | ||
"version": "0.0.5", | ||
"version": "0.0.6", | ||
"description": "Create data storage that uses a simple key-value method for Node, Browser, Deno, Bun, Cloudflare Workers", | ||
@@ -14,22 +14,26 @@ "main": "dist/cjs/kv-storage.js", | ||
"scripts": { | ||
"test": "echo \"Error: no test specified\" && exit 1", | ||
"build": "tsc -p tsconfig-dev.json", | ||
"start": "node public/test/server.js", | ||
"start-mjs": "npm run build && node public/test/server-mjs.js", | ||
"dev-ts": "nodemon -e js,ts --watch src --watch test --exec \"ts-node test/server\"", | ||
"dev-browser": "nodemon -e js,ts,html --watch src --watch test --exec \"npm run prepare-public && tsc -p tsconfig-browser.json && tsc -p tsconfig-umd.json && rollup -c public/config/rollup.config.js && ts-node test/server-browser.ts\"", | ||
"dev-umd": "nodemon -e js,ts,html --watch src --watch test --exec \"npm run prepare-public && tsc -p tsconfig-umd.json && rollup -c public/config/rollup.config.js && ts-node test/server-browser.ts\"", | ||
"dev-deno": "nodemon -e js,ts --watch src --watch test --exec \"deno run --allow-read --allow-write --unstable-sloppy-imports test/server-deno.ts\"", | ||
"dev": "nodemon -e js,ts --watch src --watch test --exec \"npm run build && npm start\"", | ||
"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", | ||
"prepare-build": "if exist .\\dist (echo ok) && mkdir dist && del /S /Q .\\dist\\*", | ||
"prepare-build-win": "if not exist .\\dist (mkdir dist) else (rmdir /S /Q .\\dist\\)", | ||
"prepare-typedoc": "if not exist .\\docs (mkdir docs) else (rmdir /S /Q .\\docs\\)", | ||
"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 src/bun-kv-storage.ts", | ||
"gh-deploy": "git push origin :gh-pages && git subtree push --prefix docs origin gh-pages", | ||
"gh-deploy-init": "git push origin && git subtree push --prefix docs origin gh-pages", | ||
"gh-deploydoc": "npm run typedoc && git add docs -f && git commit -m \"docs\" && npm run gh-deploy && git reset --soft HEAD~ && git restore --staged ." | ||
"dev-node": "nodemon -e js,ts --watch src --watch test --exec \"npm run build && npm start\"", | ||
"dev-bun": "nodemon -e js,ts --watch src --watch test --exec \"bun run test/server.ts\"", | ||
"dev-deno": "nodemon -e js,ts --watch src --watch test --exec \"deno run --allow-read --allow-write test/test-deno/server-deno.ts\"", | ||
"dev-cf": "nodemon -e js,ts,html --watch src --watch test --exec \"npm run prepare-public && tsc -p tsconfig-browser.json && wrangler dev\"", | ||
"dev-browser": "nodemon -e js,ts,html --watch src --watch test --exec \"npm run prepare-public && tsc -p tsconfig-browser.json && tsc -p tsconfig-umd.json && rollup -c public/config/rollup.config.js && ts-node test/server-browser.ts\"", | ||
"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", | ||
"gh-deploydoc": "npm run typedoc && git add docs -f && git commit -m \"docs\" && npm run gh-deploy && git reset --soft HEAD~ && git restore --staged .", | ||
"build": "tsc -p tsconfig-dev.json", | ||
"start": "node public/test/server.js", | ||
"start-mjs": "npm run build && node public/test/server-mjs.js", | ||
"dev-umd": "nodemon -e js,ts,html --watch src --watch test --exec \"npm run prepare-public && tsc -p tsconfig-umd.json && rollup -c public/config/rollup.config.js && ts-node test/server-browser.ts\"", | ||
"dev-mjs": "nodemon -e js,ts --watch src --watch test --exec \"npm run start-mjs\"", | ||
"prepare-build": "if exist .\\dist (echo ok) && mkdir dist && del /S /Q .\\dist\\*", | ||
"prepare-build-win": "if not exist .\\dist (mkdir dist) else (rmdir /S /Q .\\dist\\)", | ||
"prepare-typedoc": "if not exist .\\docs (mkdir docs) else (rmdir /S /Q .\\docs\\)", | ||
"prepare-public": "if not exist .\\public (mkdir public) else (rmdir /S /Q .\\public\\)", | ||
"typedoc": "npm run prepare-typedoc && typedoc src/kv-storage.ts src/kv-storage-module.ts", | ||
"gh-deploy": "git push origin :gh-pages && git subtree push --prefix docs origin gh-pages", | ||
"gh-deploy-init": "git push origin && git subtree push --prefix docs origin gh-pages", | ||
"dev-ts": "nodemon -e js,ts --watch src --watch test --exec \"ts-node test/server\"", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
@@ -36,0 +40,0 @@ "files": [ |
@@ -24,3 +24,3 @@ # kv-storage | ||
```javascript | ||
<script src="https://cdn.jsdelivr.net/npm/kv-storage@0.0.4/dist/umd/kv-storage.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/kv-storage@0.0.6/dist/umd/kv-storage.js"></script> | ||
``` | ||
@@ -42,2 +42,3 @@ | ||
//Node, Browser, Deno & Bun Initialization | ||
const db = await KVStorage({ | ||
@@ -48,6 +49,7 @@ runtime:'node', //node | browser | deno | bun | ||
//Cloudflare Initialization | ||
const db = await KVStorage({ | ||
runtime:'cloudflare', | ||
storageName:'storage', | ||
databaseBindings:env.D1 //Cloudflare D1 database bindings env | ||
storageName:'storage', //Cloudflare D1 database name | ||
databaseBinding:env.D1 //Cloudflare D1 database binding env | ||
}) | ||
@@ -103,3 +105,3 @@ ``` | ||
```javascript | ||
<script src="https://cdn.jsdelivr.net/npm/kv-storage@0.0.4/dist/umd/kv-storage.js"></script> | ||
<script src="https://cdn.jsdelivr.net/npm/kv-storage@0.0.6/dist/umd/kv-storage.js"></script> | ||
<script> | ||
@@ -126,3 +128,3 @@ //Browser using CDN example | ||
//Browser ES Modules example | ||
import {KVStorage} from 'https://cdn.jsdelivr.net/npm/kv-storage@0.0.4/dist/mjs/kv-storage.js' | ||
import {KVStorage} from 'https://cdn.jsdelivr.net/npm/kv-storage@0.0.6/dist/mjs/kv-storage.js' | ||
@@ -166,23 +168,17 @@ void async function main() { | ||
export default { | ||
async fetch(request: Request, env: Env): Promise<Response> { | ||
async fetch(request, env) { | ||
const db = await KVStorage({ | ||
runtime:'cloudflare', | ||
storageName:'storage', | ||
databaseBindings:env.D1 | ||
storageName:'storage', //Cloudflare D1 database name | ||
databaseBinding:env.D1 //Cloudflare D1 database binding env | ||
}) | ||
let data = [] | ||
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)) | ||
return new Response(JSON.stringify(data, null, 2)) | ||
} | ||
@@ -204,3 +200,3 @@ } | ||
storageName?:string, | ||
databaseBindings?:any //Cloudflare only | ||
databaseBinding?:any | ||
}) | ||
@@ -211,3 +207,3 @@ ``` | ||
storageName = Alphanumeric storage name | ||
databaseBindings = Cloudflare D1 database bindings env | ||
databaseBinding = Cloudflare only D1 database binding env | ||
``` | ||
@@ -219,3 +215,3 @@ Supported runtime : | ||
- [x] `bun` | ||
- [x] `cloudflare-workers` use D1 Database [docs](https://developers.cloudflare.com/d1/get-started/#4-bind-your-worker-to-your-d1-database) | ||
- [x] `cloudflare` workers use D1 Database [docs](https://developers.cloudflare.com/d1/get-started/#4-bind-your-worker-to-your-d1-database) example [wrangler.toml](https://github.com/nuzulul/kv-storage/blob/main/wrangler.toml) | ||
@@ -222,0 +218,0 @@ ### Write key-value pairs |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Filesystem access
Supply chain riskAccesses the file system, and could potentially read sensitive data.
Found 1 instance in 1 package
4
63107
14
1336
249
1