Skip to content

Instantly share code, notes, and snippets.

@kimerran
Created November 13, 2023 12:32
Show Gist options
  • Save kimerran/50131e2b760f448157d9896492b21932 to your computer and use it in GitHub Desktop.
Save kimerran/50131e2b760f448157d9896492b21932 to your computer and use it in GitHub Desktop.
bun-surrealdb-elysia-ethers.ts
import { Elysia } from "elysia";
import { Surreal } from "surrealdb.node"
// const ethers = require('ethers');
import { ethers } from 'ethers'
const db = new Surreal();
await db.connect('ws://localhost:8000/');
// await db.signin({ username: 'root', password: 'root' })
await db.use({ ns: 'test', db: 'test' })
const app = new Elysia()
app.state('dbStore', db);
app.get("/", async () => {
// Create a new person with a random id
let created = await db.create('person', {
title: 'Founder & CEO',
name: {
first: 'Tobie',
last: 'Morgan Hitchcock',
},
marketing: true,
identifier: Math.random().toString(36).slice(2, 12),
});
return created;
})
app.get("/context", async (context) => {
const { dbStore } = context.store as any;
const persons = await dbStore.query('select * from person')
console.log("returning persons...")
return persons
}, {
beforeHandle: ({ request: { headers } }) => {
console.log('headers', headers)
}
})
app.get('/ethers', async (context) => {
const provider = ethers.getDefaultProvider('matic')
const balance = await provider.getBalance('0x83FE64Bc14b124f65Eb5249b9BA45b66e3eFFe4C')
return balance.toString();
})
app.listen(3000);
console.log(
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`
);
@kimerran
Copy link
Author

SurrealDB started with

docker run --rm --pull always --name surrealdb -p 8000:8000 surrealdb/surrealdb:latest start

@kimerran
Copy link
Author

Server run with

bun run src/index.ts

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment