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

@contiguity/base

v1.1.2

Published

Contiguity's Base JavaScript SDK

Downloads

113

Readme

Installation 🏗 & Setup 🛠

You can install the SDK using NPM.

$ npm install @contiguity/base

Then, import & initialize it like this:

Using CommonJS (require):

const contiguity = require('@contiguity/base')
const db = contiguity.db("your-api-key", "your-project-id")

Using ES6 modules (import):

import contiguity from '@contiguity/base'
const db = contiguity.db("your-api-key", "your-project-id")

You can get an API key by fetching it in the dashboard, and a project ID is given to you when creating a project.

For those moving from Deta Space

Contiguity Base is a one-to-one replacement for the old Deta Base API, Deta Base JavaScript SDK, Deta Base Python SDK, and Deta Base Go SDK. The only thing that has changed is initialization.

Instead of const deta = Deta(projectKey), you'll use const db = contiguity.db(apiKey, projectId)

The rest stays the same, because at Contiguity, we think it's crazy for a cloud provider to give you 45 days to move dozens of apps from their proprietary database.

If you're transitioning from Deta Space to Contiguity, welcome!

Creating your first "base" 📊

To start working with a base, you can create a Base instance:

const myBase = db.Base("my-awesome-base")

Now you're ready to perform some cool database operations!

Putting data into your base 📥

To add an item to your base, use the put method:

const item = {
    name: "Contiguity",
    is_awesome: true,
    coolness_level: 9000
}

await myBase.put(item)

You can also specify a key for your item:

await myBase.put(item, "unique-key-1")

You can set an expiration time for the item:

// Expire in 3600 seconds (1 hour)
await myBase.put(item, "unique-key-1", { expireIn: 3600 })

// Expire at a specific date/time
await myBase.put(item, "unique-key-1", { expireAt: "2023-12-31T23:59:59Z" })

Batch putting 📦

Need to add multiple items at once? No problem! Just pass an array of items:

const items = [
    { name: "Item 1", value: 100 },
    { name: "Item 2", value: 200 },
    { name: "Item 3", value: 300, key: "some-unique-key" }
]

await myBase.putMany(items)

Getting data from your base 🔍

To retrieve an item, use the get method:

const myItem = await myBase.get("unique-key-1")
console.log(myItem.name) // Outputs: Contiguity

Updating data in your base 🔄

Need to update an item? Use the update method:

await myBase.update({ coolness_level: 9001 }, "unique-key-1")

You can also use utility operations for updating:

// Increment a value
await myBase.update({ views: myBase.util.increment(1) }, "blog-post-1")

// Append to an array
await myBase.update({ tags: myBase.util.append("awesome") }, "product-1")

// Prepend to an array
await myBase.update({ recent_visitors: myBase.util.prepend("Alice") }, "website-stats")

// Trim a string
await myBase.update({ description: myBase.util.trim() }, "user-bio")

Deleting data from your base 🗑️

To remove an item, use the delete method:

await myBase.delete("unique-key-1")

Querying (fetching) your base 🕵️‍♀️

You can perform complex queries using the fetch method:

const results = await myBase.fetch({ 
  "is_awesome": true, 
  "profile.name?contains": "John" 
});

console.log(results.items); // Array of matching items
console.log(results.count); // Total count of matching items
console.log(results.last);  // Last evaluated key for pagination

Query Operators

Contiguity Base supports various query operators. Here are some examples:

  • Equal: { "age": 22 }
  • Not Equal: { "age?ne": 22 }
  • Less Than: { "age?lt": 22 }
  • Greater Than: { "age?gt": 22 }
  • Less Than or Equal: { "age?lte": 22 }
  • Greater Than or Equal: { "age?gte": 22 }
  • Prefix: { "name?pfx": "Jo" }
  • Range: { "age?r": [18, 30] }
  • Contains: { "tags?contains": "javascript" }
  • Not Contains: { "tags?not_contains": "python" }

For more detailed information on query operators, please refer to our documentation.

Pagination

When fetching large datasets, you can use pagination:

let lastKey = undefined;
do {
  const results = await myBase.fetch(query, { limit: 1000, last: lastKey });
  // Process results.items
  lastKey = results.last;
} while (lastKey);

Debug mode 🐛

If you enable debug mode during initialization, the SDK will log detailed information about your requests. This can be super helpful for troubleshooting!

const db = contiguity.db("your-api-key", "your-project-id", true)

Error handling 🚨

The SDK won't throw errors when things don't go as planned. Instead, it will return undefined in most cases, like if you attempt to GET a non-existent key. However, it is always recommended to put database calls in a try/catch block:

try {
    const item = await myBase.get("non-existent-key")
    if (item === undefined) {
        console.log("Item not found")
    } else {
        console.log("Item:", item)
    }
} catch (error) {
    console.error("Oops!", error.message)
}

Roadmap 🚦

  • Support for more complex query operations
  • Batch operations for deleting multiple items
  • "Deta Drive" support (file storage)
  • And many more exciting features!

For more detailed information and advanced usage, please refer to our full documentation.