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.

Revisions

  1. kimerran created this gist Nov 13, 2023.
    53 changes: 53 additions & 0 deletions bun-surrealdb-elysia-ethers.ts
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,53 @@
    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}`
    );