SurrealDB on the edge!

Overview

surrealdb-cloudflare

SurrealDB on the edge!

Cloudflare workers

This module has been build specifically to be compatible with cloudflare workers. Tested with wrangler2, though should probably work fine with wrangler1 aswell.

Basic interpetation

It's just a basic interpetation of the SurrealDB HTTP/REST API, You can check them here: https://surrealdb.com/docs/integration/http

Get started

You can check out the demo.ts file or check the examples down below:


Connection variables from environment

import Surreal from 'surrealdb-cloudflare';

// Type safety, typescript example :D
type Env = {
    HOST: string;
    USER: string;
    PASS: string;
    NAMESPACE: string;
    DATABASE: string;
}

// We can update the connection variables later on, as we don't have them available here just yet...
const db = new Surreal();

export default {
	async fetch(
		request: Request,
        env: Env
	): Promise<Response> {
        // If database not yet connected, let's update the connection details.
        if (!db.connected()) db.connect({
            host: env.HOST ?? '',
            username: env.USER ?? '',
            password: env.PASS ?? '',
            namespace: env.NAMESPACE ?? '',
            database: env.DATABASE ?? ''
        });

        // Example query: Retrieves all records from table 'table'.
        let res = await db.getRecords('table');
		console.log(res[0].result);

		return new Response("Hello World!");
	},
};

Connection variables defined in code

import Surreal from 'surrealdb-cloudflare';

// We can update the connection variables later on, as we don't have them available here just yet...
const db = new Surreal({
    host: 'http://surreal.domain.com',
    username: 'root',
    password: 'password',
    namespace: 'awesome',
    database: 'example'
});

export default {
	async fetch(
		request: Request
	): Promise<Response> {
        // Example query: Retrieves all records from table 'table'.
        let res = await db.getRecords('table');
		console.log(res[0].result);

		return new Response("Hello World!");
	},
};

Strong type result

const res = await db.getRecords<{
    id: string;
    username: string;
    status: "verified" | "unverified";
}>('user');

res.forEach(record => {
    // Everything in "result" is now strong typed with the defined type.
    const { result } = record;
    console.log(`${result.id} - User ${result.username} is ${result.status}`);
});

Exported types and classes

  • type SurrealConfig
  • type SurrealResponse
  • (default) class Surreal

Available functions in Surreal class

  • Surreal.connect(config: SurrealConfig): Update connection details to Surreal database.
  • Surreal.connected(): boolean: Check if connection details are successfully specified.

All functions in the following section return the following:

    [
        {
            time: string;
            status: string;
            result: TResponse;
        },
        ....
    ]
  • async Surreal.query(query: string): Run a query on the specified database.
  • async Surreal.getRecords(table: string): Get all records from table.
  • async Surreal.createRecord(table: string, data: object): Create a record with random ID.
  • async Surreal.deleteRecords(table: string): Delete all records from table.
  • async Surreal.getRecordWithId(table: string, id: string): Get record from table with ID.
  • async Surreal.createRecordWithId(table: string, id: string, data: object): Create record in table with ID.
  • async Surreal.setRecordWithId(table: string, id: string, data: object): Overwrite record in table with ID.
  • async Surreal.updateRecordWithId(table: string, id: string, data: object): Update record in table with ID.
  • async Surreal.deleteRecordWithId(table: string, id: string): Delete record from table with ID.
You might also like...

Deploy Serverless Functions at the Edge. Current status: Dev

Deploy Serverless Functions at the Edge. Current status: Dev

Deploy Serverless Functions at the Edge lagon.app ✉️ Get email updates Open Source • TypeScript • Web APIs Cron triggers • Instant deployments Interac

Dec 30, 2022

A Cutting-Edge, Modular Score Tracker

Tachi This is the main monorepo for Tachi. What is Tachi? Tachi is a modern, powerful, modular Rhythm Game Score Tracker. In short, it does the things

Dec 19, 2022

A brand new bleeding edge non bloated Discord library (based on Discordeno!)

biscuit A brand new bleeding edge non bloated Discord library Install (for node18) npm install @oasisjs/biscuit pnpm add @oasisjs/biscuit yarn add @oa

Nov 15, 2022

Deploy Serverless Functions at the Edge. Current status: Dev

Deploy Serverless Functions at the Edge. Current status: Dev

Deploy Serverless Functions at the Edge 💻 Join the Discord ✉️ Get email updates Open Source • TypeScript • Web APIs Cron triggers • Instant deploymen

Jul 26, 2022

This is a demo of updating a map to show air quality data for the user’s current location using Next.js Advanced Middleware, powered by Netlify Edge Functions.

Show Local Air Quality Based on the User's Location Use AQI data to show the air quality near the current user. This is built using Next.js Advanced M

Nov 4, 2022

High performance personalization & a/b testing example using Next.js, Edge Middleware, and Builder.io

High performance personalization & a/b testing example using Next.js, Edge Middleware, and Builder.io

Next.js + Builder.io Personalization & A/B Testing with Edge Middleware This is a fork of Next.js Commerce with Builder.io integrated and using Edge M

Sep 6, 2022

Display attribution text sideways along the right edge of Revealjs slides.

Display attribution text sideways along the right edge of Revealjs slides.

attribution A Revealjs plugin extension for displaying attribution text sideways along the right edge of the viewport. Based on the attribution plugin

Dec 17, 2022

Cutting-edge Development Pushes for new Koders' Website

This is a Next.js project bootstrapped with create-next-app. Getting Started First, run the development server: npm run dev # or yarn dev Open http://

Dec 27, 2022
Owner
The Open Source Company
You guessed right! Open source products :D
The Open Source Company
a simple wrapper nestjs dynamic module on top of surrealdb.js driver, with a consumer app to show case library in action, nothing fancy

README README Project Components Dynamic Module Consumer App Install SurrealDb Starts SurrealDb Init surrealDb Database Run App from Source Code Launc

Mário Monteiro 0 Oct 3, 2022
🚀 A really straight-forward SurrealDB Rest API wrapper.

SurrealDB Node.js npm i surrealdb Quick Start surreal start --root user --pass user import SurrealDB from 'surrealdb' const Surreal = new SurrealDB('

Jareer Abdullah 5 Dec 10, 2022
Extensão de temas para o site Gatry.com, compatível com Firefox, Chrome e Edge.

Gatry Shades Extensão que dá a possibilidade de aplicar temas ao site gatry.com, bem como demais funcionalidade para reduzir a luminosidade de imagens

João Ribeiro 2 Dec 3, 2022
🚀 A web extension starter built with React, Typescript, and Tailwind CSS. Build once, and run on multiple browsers: Google Chrome, Mozilla Firefox, Microsoft Edge, Brave, and Opera..

Web Extension Starter A web extension starter, built with React, Typescript, and Tailwind CSS. Build once, and run on multiple browsers: Google Chrome

BCIT Design & Development Club (BCIT-DDC) 28 Dec 28, 2022
ACME client microservice for the Cumulocity IoT Edge to automatically issue and renew valid certificates via e.g. Let's Encrypt.

Cumulocity IoT Edge - ACME This repository contains the sources for an ACME microservice that can be used to periodically issue/renew certificates for

Software AG 3 May 3, 2022
An unreliable and overall unusable sorting library for numbers with a global cache on the edge.

unsort An unreliable and overall unusable sorting library for numbers with a global cache on the edge. the algorithm This library implements a number

Jonas Wanner 6 May 19, 2022
A Tauri update server, hosted as a Cloudflare edge function

Tauri Update Server: Cloudflare One-Click Deploy Click the button above, let Cloudflare walk you through: it's easy! Go to your forked repository, edi

KilleenCode 33 Dec 14, 2022
Get started with AI vision at the edge with no coding experience at all!

No-Code Edge AI Vision with Node-RED Now you can get started with AI vision at the edge in just THREE STEPS with no coding experience at all! Prerequi

Seeed Studio 47 Dec 5, 2022
Sample code for resizing Images with Lambda@Edge using the Custom Origin. You can deploy using AWS CDK.

Resizing Images with Lambda@Edge using the Custom Origin You can resize the images and convert the image format by query parameters. This Lambda@Edge

AWS Samples 16 Dec 11, 2022
Abusing Cloudflare Workers to establish persistence and exfiltrate sensitive data at the edge.

Abusing Cloudflare Workers This repository contains companion code for the blog post MITM at the Edge: Abusing Cloudflare Workers. malicious-worker/ c

Christophe Tafani-Dereeper 10 Sep 16, 2022