Created
November 13, 2023 12:32
-
-
Save kimerran/50131e2b760f448157d9896492b21932 to your computer and use it in GitHub Desktop.
bun-surrealdb-elysia-ethers.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}` | |
); |
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
SurrealDB started with