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

surrealism

v0.1.2

Published

A SurrealDB Driver

Downloads

3

Readme

surrealism

A SurrealDB Driver.

Getting Started

WARN: As of SurrealDB 1.0.0-beta.9, WebSockets in Bun currently do not work as intended. As a workaround, use HTTP-only connections. See: surrealdb/surrealdb.js#159.

Deno

Import the module from deno.land:

import { Surreal } from "https://deno.land/x/surrealism/mod.ts";

Node.js / Bun / Browser (bundled)

Add Surrealism as a dependency:

$ npm i surrealism

Then, import it in your code:

import { Surreal } from 'surrealism'

Alternatively, a bundle in CommonJS format is included:

const { Surreal } = require('surrealism')

Connecting to SurrealDB

// connect using an http url, with ws url inferred
const db = await Surreal
  .new('http://localhost:8000', {
    user: 'root',
    pass: 'root',
  })
  .use({ ns: 'test', db: 'test' })

// connect using a ws url, with http url inferred
const db = await Surreal
  .new('ws://localhost:8000/rpc', {
    user: 'root',
    pass: 'root',
  })
  .use({ ns: 'test', db: 'test' })

// connect using explicit ws and http urls,
// omitting the url removes the driver for that protocol
const db = await Surreal
  .new({
    http: 'http://localhost:8000',
    ws: 'ws://localhost:8000/rpc',
  }, {
    user: 'root',
    pass: 'root',
  })
  .use({ ns: 'test', db: 'test' })

// connect as a namespaced user
const db = await Surreal
  .new('http://localhost:5423', {
    ns: 'test',
    user: 'john.doe',
    pass: '123456',
  })
  .use({ ns: 'test', db: 'test' })

// connect as a scoped user
const db = await Surreal
  .new('http://localhost:5423', {
    ns: 'test',
    db: 'test',
    sc: 'user_scope',
    user: 'john.doe',
    pass: '123456',
  })

// for modern environments with explicit resource management,
// async disposal is available for the ws driver
await using db = await Surreal
  .new('ws://localhost:5423/rpc', {
    user: 'root',
    pass: 'root',
  })
  .use({ ns: 'test', db: 'test' })

Using Surrealism

Switching NS and DB

await db.use({ ns: 'foo', db: 'bar' })

Getting records

// get all records on table "foo"
const result = await db.get('foo')

// get record with id "bar" on table "foo"
const result = await db.get('foo', 'bar')

Works with: HTTP, WS driver

Creating records

// create a new record on table "foo" with random id
const result = await db.create('foo', { value: 0 })

// create a new record with id "bar" on table "foo"
const result = await db.create('foo', 'bar', { value: 0 })

Works with: HTTP, WS driver

Deleting records

// delete all records in table "foo"
const result = await db.delete('foo')

// delete record with id "bar" in table "foo"
const result = await db.delete('foo', 'bar')

Works with: HTTP, WS driver

Updating records

// update record with id "bar" on table "foo" with data "{ value: 1 }"
const result = await db.update('foo', 'bar', { value: 1 })

Works with: HTTP, WS driver

Mutating records

// set property of "value" to "1" on record with id "bar" on table "foo"
const result = await db.mutate('foo', 'bar', { value: 1 })

Works with: HTTP driver

Patching records using JSON Patch

// apply patches to all records on table "foo"
const result = await db.patch('foo', [
  { op: 'add', path: '/value', value: 0 },
])

// apply patches to record with id "bar" on table "foo"
const result = await db.patch('foo', 'bar', [
  { op: 'replace', path: '/value', value: 1 },
])

Works with: WS driver

Execute raw SurrealQL

// execute raw queries
const result = await db.sql`SELECT * FROM foo`

// execute raw queries with variables
// variables are automatically escaped as LET parameters
const result = await db.sql`SELECT * FROM foo WHERE id > ${0}`

Works with: HTTP, WS driver

Execute live queries

// execute a live query using async iterators
for await (const result of db.live`SELECT * FROM foo WHERE value > ${0}`) {
  break // stop live query
}

// execute a live query using a listener
const kill = await db.live`SELECT * FROM foo WHERE value > ${0}`(data => {
})
await kill() // stop live query

// queries can be saved and reused,
// each starting a new live query
const liveQuery = db.live`SELECT * FROM foo WHERE value > ${0}`

const kill1 = await liveQuery(data => {
})

const kill2 = await liveQuery(data => {
})

for await (const result of liveQuery) {
}

Works with: WS driver

Get database health

const result = await db.health()

Works with: HTTP driver

Get version

// get connected database's version
const result = await db.version()

Works with: HTTP driver

Close WebSocket connection

// close current connection
await db.close()

Works with: WS driver