Socket
Socket
Sign inDemoInstall

@replit/database

Package Overview
Dependencies
0
Maintainers
33
Versions
10
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @replit/database

Client for Repl.it Database


Version published
Weekly downloads
1.5K
increased by7.7%
Maintainers
33
Install size
22.7 kB
Created
Weekly downloads
 

Readme

Source

Replit Database Client

Run on Repl.it npm: @replit/database

The Replit Database client is a simple way to use Replit Database in your Node.js repls. The client expects to run within a Replit managed server context. Use this library in servers or other applications that execute on a Replit server, rather than in your user's browser.

Installation

Install the TypeScript Library with

npm install @replit/database

This library supports Bun, Deno, and Node.js (Node versions 18+ or any Node version polyfilled with the fetch API).

Quickstart

import Client from "@replit/database";

const client = new Client();
await client.set("key", "value");
let value = await client.get("key");

console.log(value); // { ok: true, value: "value" }

Docs

Initiate a new database client:

import Client from "@replit/database";

/**
 * Initiates Class.
 * @param {String} dbUrl Custom database URL
 */
new Client()

Retrieve a value for a key from the database:

/**
 * Gets a key
 * @param {String} key Key
 * @param {boolean} [options.raw=false] Makes it so that we return the raw string value. Default is false.
 * @returns Promise<OkResult<any> | ErrResult<RequestError>>
 */
const value = await client.get(key, /* options?: {raw: boolean} */)
console.log(value)
// { ok: true, value: "value" } | { ok: false, error: RequestError }

Sets a value for a key in the database:

/**
 * Sets a key
 * @param {String} key Key
 * @param {any} value Value
 */
await client.set(key, value)

Deletes a key from the database:

/**
 * Deletes a key
 * @param {String} key Key
 */
const result = await client.delete(key)
console.log(result.ok) // boolean

Lists all keys starting with the provided prefix:

/**
 * List key starting with a prefix if provided. Otherwise list all keys.
 * @param {String} prefix The prefix to filter by.
 */
const keys = await client.list("prefix-")
console.log(keys) // { ok: true, value: [...] } | { ok: false, error: RequestError }

Clears the database:

/**
 * Clears the database.
 * @returns a Promise containing this
 */
await client.empty()

Gets all records in the database:

/**
 * Get all key/value pairs and return as an object
 * @param {boolean} [options.raw=false] Makes it so that we return the raw
 * string value for each key. Default is false.
 */
const records = await client.getAll(/* options?: {raw: boolean} */)

Sets multiple key value pairs:

/**
 * Sets multiple keys from an object.
 * @param {Object} obj The object.
 */
await client.setMultiple({keyOne: "valueOne", keyTwo: "valueTwo"})

Deletes multiple keys from the database:

/**
 * Delete multiple entries by key.
 * @param {Array<string>} args Keys
 */
await client.deleteMultiple(['keyOne', 'keyTwo'])

Tests

npm i
npm run test

FAQs

Last updated on 15 Apr 2024

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc