npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

kv-storage

v0.0.9

Published

💾 Create data storage that uses a simple key-value method for Node, Browser, Deno, Bun, Cloudflare Workers

Downloads

30

Readme

kv-storage

💾 Create data storage that uses a simple key-value method for Node, Browser, Deno, Bun, Cloudflare Workers

NPM npm version

Demo

Features

  • ✅ 0 Dependencies
  • ✅ NoSQL Database
  • ✅ Lightwight

Installation

NPM (node, browser, deno, bun, cloudflare)

npm install kv-storage

CDN (browser)

<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/kv-storage.js"></script>

Initialization

NPM

//Node & Bun CommonJS import style
const {KVStorage} = require('kv-storage')

//Node, Browser, Deno, Bun & Cloudflare ES Modules import style
import {KVStorage} from 'kv-storage'

//Deno import style
import {KVStorage} from 'npm:[email protected]'

//Node, Browser, Deno & Bun Initialization
const db = await KVStorage({
	runtime:'node', //node | browser | deno | bun
	storageName:'storage'
})

//Cloudflare Initialization
const db = await KVStorage({
	runtime:'cloudflare',
	storageName:'storage', //Cloudflare D1 database name
	databaseBinding:env.D1 //Cloudflare D1 database binding env
})

CDN

//Browser initialization if using CDN

const db = await KVStorage({
	runtime:'browser',
	storageName:'storage'
})

Example Usage

//Node & Bun CommonJS example
const {KVStorage} = require('kv-storage')

void async function main() {
	const db = await KVStorage({
		runtime:'node',//node | bun
		storageName:'storage'
	})
	
	console.log(await db.put('key','value'))
	console.log(await db.get('key'))
	console.log(await db.list())
	console.log(await db.delete('key'))
	console.log(await db.has('key'))
	console.log(await db.clear())
}()
//Node, Deno & Bun ES Modules example
import {KVStorage} from 'kv-storage'

void async function main() {
	const db = await KVStorage({
		runtime:'node',//node | deno | bun
		storageName:'storage'
	})
	
	console.log(await db.put('key','value'))
	console.log(await db.get('key'))
	console.log(await db.list())
	console.log(await db.delete('key'))
	console.log(await db.has('key'))
	console.log(await db.clear())
}()
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/kv-storage.js"></script>
<script>
//Browser using CDN example

void async function main() {
	const db = await KVStorage({
		runtime:'browser',
		storageName:'storage'
	})
	
	console.log(await db.put('key','value'))
	console.log(await db.get('key'))
	console.log(await db.list())
	console.log(await db.delete('key'))
	console.log(await db.has('key'))
	console.log(await db.clear())
}()
</script>
<script type="module">
//Browser ES Modules example
import {KVStorage} from 'https://cdn.jsdelivr.net/npm/[email protected]/dist/mjs/kv-storage.js'

void async function main() {
	const db = await KVStorage({
		runtime:'browser',
		storageName:'storage'
	})
	
	console.log(await db.put('key','value'))
	console.log(await db.get('key'))
	console.log(await db.list())
	console.log(await db.delete('key'))
	console.log(await db.has('key'))
	console.log(await db.clear())
}()
</script>
//Deno example
import {KVStorage} from 'npm:[email protected]'

void async function main() {
	const db = await KVStorage({
		runtime:'deno',
		storageName:'storage'
	})
	
	console.log(await db.put('key','value'))
	console.log(await db.get('key'))
	console.log(await db.list())
	console.log(await db.delete('key'))
	console.log(await db.has('key'))
	console.log(await db.clear())
}()
//Cloudflare workers example
import {KVStorage} from 'kv-storage'

export default { 
	async fetch(request, env) { 

		const db = await KVStorage({
			runtime:'cloudflare',
			storageName:'storage', //Cloudflare D1 database name
			databaseBinding:env.D1 //Cloudflare D1 database binding env
		})
		
		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'))
		data.push(await db.clear())
		return new Response(JSON.stringify(data, null, 2)) 
	} 
}

API Reference

Documentation

https://nuzulul.github.io/kv-storage/

Initialization parameters

await init({
	runtime,
	storageName,
	databaseBinding
})
runtime =  Javascript runtime 
storageName = Alphanumeric storage name
databaseBinding = Cloudflare only D1 database binding env

Supported runtime :

  • [x] node use File System
  • [x] deno use File System need --allow-read --allow-write
  • [x] browser use IndexedDB
  • [x] bun use File System
  • [x] cloudflare workers use D1 Database docs example wrangler.toml

Write key-value pairs

await put(key,value)

The put() method returns a Promise that you should await on to verify a successful update, resolve to true or false

Read key-value pairs

await get(key)

The get() method returns a promise you can await on to get the value, resolve to the value or false

List keys

await list()

Use a list operation to view all the keys that live in a given storage, return a promise which resolves with an object consist of:

  • keys = Array of keys
  • complete = true if operation complete

Delete key-value pairs

await delete(key)

To delete a key-value pair, call the delete() method, resolve to true or false

Has key-value pairs

await has(key)

To check for the existence of a key, resolve to true or false

Clear storage

await clear()

To delete all key value pairs, resolve to true or false

License

MIT