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

kvdriver

v1.0.0

Published

An npm package providing a unofficial driver for Cloudflare Key-Value (KV) storage. It allows you to interact with Cloudflare's KV storage using Node.js.

Downloads

3

Readme

KV Driver

An npm package providing a unofficial driver for Cloudflare Key-Value (KV) storage. It allows you to interact with Cloudflare's KV storage using Node.js.

Getting Started

Require the KVClient Class in the your project.

    const KVClient = require('./kvclient');

Create and instance of kvClient and provide your Cloudflare Global API Key, Registered Email ID and CF AccountID.


const kvClient = new KVClient({ 
    email: '<REGISTERED EMAIL>',
    globalApiKey: '<CF-GLOBAL-API-KEY>', 
    accountID: '<CF-ACCOUNT-ID>'
    });

Now you can use the method on the kvClient instance to do the following operations.

Currently Supported Operations

1. List Namespaces

Lists all the namespaces in a Cloudflare account

Method

async function listNS() {
    try {

        const result = await kvClient.ListNameSpaces();
        return result;
    }
    catch(e) {
        return e;
    }
}

Returns an array of the KV namespaces owned by an account.

2. Create a New Namespace

Creates a namespace under the given title. An Error is returned if the account already owns a namespace with this title. A namespace must be explicitly deleted to be replaced.

async function createNS() {
    
    try {

        const result = await kvClient.CreateNameSpace('new-kv-ns');
        return result;
    }
    catch(e) {
        return e;
    }
}

3. Write Key Value Pair

Puts a key-value pair into a Cloudflare Key-Value namespace.

async function WriteKeyValuePair() {
    
    try {
        // Unique 32 Char Long Namespace Identifier
        const namespaceId = '05f2a1a0e65c4084ad7cda2f973c3b55'

        // Metadata for the key should be a string
        const testMetaData = {metadataKey: 'Metadata Value'};
        const result = await kvClient.putKeyValue(namespaceId, 'test-key','test-value',JSON.stringify(testMetaData));
        return result;
    }
    catch(e) {
        return e;
    }
}

Note: All argument should be of type string.

4. Write Multiple Key Value Pairs

Puts multiple key-value pairs into a Cloudflare Key-Value namespace in bulk.

An array of objects representing key-value pairs is passed in. Each object should have properties 'key', 'value', and 'metadata'.

async function WriteMultipleKeyValuePair() {
    
    try {
        // Unique 32 Char Long Namespace Identifier
        const namespaceId = '05f2a1a0e65c4084ad7cda2f973c3b55'

        const keyValueArr = [
            {key: 'test-key-1', value: 'test-value-1', metadata: JSON.stringify({metadataKey_1: 'Metadata-value-1'}) },
            {key: 'test-key-2', value: 'test-value-2', metadata: JSON.stringify({metadataKey_2: 'Metadata-value-2'}) },
            {key: 'test-key-3', value: 'test-value-3', metadata: JSON.stringify({metadataKey_3: 'Metadata-value-3'}) },

        ]
        const result = await kvClient.putKeyValueBulk(namespaceId,keyValueArr );
        console.log(result);
        return result;
    }
    catch(e) {
        console.log(e);
        return e;
    }
}

5. Read Value of a Key

Retrieves the value associated with a key from a Cloudflare Key-Value namespace.

async function getKeyValue() {
    
    try {
        // Unique 32 Char Long Namespace Identifier
        const namespaceId = '05f2a1a0e65c4084ad7cda2f973c3b55'
        const result = await kvClient.getValue(namespaceId, 'test-key-1' );
        console.log(result);
        return result;
    }
    catch(e) {
        console.log(e);
        return e;
    }
}

6. Delete a Key Value Pair

Deletes a key-value pair from a Cloudflare Key-Value namespace specified by its key.

async function deleteKeyValue() {
    
    try {
        // Unique 32 Char Long Namespace Identifier
        const namespaceId = '05f2a1a0e65c4084ad7cda2f973c3b55'
        const result = await kvClient.deleteKeyValue(namespaceId, 'test-key-1' )
        console.log(result);
        return result;
    }
    catch(e) {
        console.log(e);
        return e;
    }
}

7. List All The Keys in a Namespace

Retrieves a list of keys from the specified Cloudflare Key-Value namespace.

async function listNSKeys() {
    
    // Unique 32 Char Long Namespace Identifier
    const namespaceId = '05f2a1a0e65c4084ad7cda2f973c3b55'
    try {
        const result = await kvClient.listNamespaceKeys()
        return result;
    }
    catch(e) {
        return e;
    }
}