surrealdb / surrealdb.js

SurrealDB SDK for JavaScript
https://surrealdb.com
Apache License 2.0
301 stars 48 forks source link

Bug: Not working with Bun? #159

Closed gluax closed 1 month ago

gluax commented 1 year ago

Describe the bug

I'm running the following code with bun(version 0.7.0)

import { Surreal } from 'surrealdb.js';

console.log("foo");
const db = new Surreal('http://127.0.0.1:8000/rpc', {
  // Set the namespace and database for the connection
  ns: 'test',
  db: 'test',

  // Set the authentication details for the connection
  auth: {
      user: 'root',
      pass: 'root',
  },
});

await db.info();
console.log("bar"); //this is never hit

The surrealdb logs are as follows: image

Steps to reproduce

Install bun. Install surrealdb bun add surrealdb.js surreal start --log trace --user root --pass root memory copy the minimal example to file bun run index.ts

Expected behaviour

To be able to connect to the DB from typescript within Bun.

SurrealDB version

1.0.0-beta.9+20230402.5eafebd for linux on x86_6

SurrealDB.js version

^0.8.3

Contact Details

No response

Is there an existing issue for this?

Code of Conduct

gluax commented 1 year ago

I did attempt this with a try catch block as well, but no ever was ever caught and it would still hang at the info or any other execution method.

gluax commented 1 year ago

This issue does not affect the ExperimentalSurrealHTTP this functions as normal :)

kearfy commented 1 year ago

Hi @gluax, we actually run tests against bun. The only known issue there is that the awaiting the closing of the websocket will run forever, looking into that soon. Will also have a look at this, it should work just fine...

MurkyTheMurloc commented 1 year ago

I have a similar issue when using Bun, I get 2023-08-22 22:30:08 2023-08-22T20:30:08.902561Z TRACE surrealdb::net: RPC Received: { id: '1259', method: 'ping', params: [] } ](surrealdb::net: WebSocket error: Io(Os { code: 32, kind: BrokenPipe, message: "Broken pipe" })) and my promise from, the operation waits infinity. However, the same code using node works peferkt.

Micnubinub commented 1 year ago

Hi @gluax, we actually run tests against bun. The only known issue there is that the awaiting the closing of the websocket will run forever, looking into that soon. Will also have a look at this, it should work just fine...

@kearfy Hey, I'm on Bun 0.7.3, surreal 1.0.0-beta.9+20230820.b350f05 and surrealdb.js 0.8.4 and mine just hangs when connecting. You manage to find a fix?

*still the same on Bun 0.8.0

jpg-gamepad commented 1 year ago

Bun 1.0 just released and still the same issue.

jpg-gamepad commented 1 year ago

I looked at the source code, and it seems the experimental http version works with Bun run! There's no documentation on it, but something like this should work.

const { ExperimentalSurrealHTTP } = require('surrealdb.js');
const db = new ExperimentalSurrealHTTP();

async function main() {
    try {
        await db.connect('http://127.0.0.1:8000/rpc', {});
        // Select a specific namespace / database
        await db.use({ns: 'test', db: 'test'});

        // 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),
        });

        // Update a person record with a specific id
        let updated = await db.merge("person:jaime", {
            marketing: true,
        });

        // Select all people records
        let people = await db.select("person");

        // Perform a custom advanced query
        let groups = await db.query('SELECT marketing, count() FROM type::table($tb) GROUP BY marketing', {
            tb: 'person',
        });

        console.log(people);
        console.log(groups);

    } catch (e) {
        console.error('ERROR', e);
    }
}

main();
baadc0de commented 1 year ago

For me, the one-shot connect method worked and the above separated signing/use did not.

import {ExperimentalSurrealHTTP} from 'surrealdb.js'

const surreal = new ExperimentalSurrealHTTP()

await surreal.connect('http://localhost:8000/rpc', {auth: {user: 'root', pass: 'root'}, ns: 'test', db: 'test'})
kearfy commented 1 year ago

Hey everyone, as per the bun docs, their websocket implementation is still experimental and it does not properly pass all test suites. The broken pipe error is something that the JS side of things does not influence and really rather is an issue in Bun's websocket implementation. I'm afraid I can't make it any better for the moment! Will leave the issue open however

paperdave commented 1 year ago

This is a bug in Bun, not surrealdb. Our WebSocket client has some issues. We should open a tracking issue in Bun's repo so we don't forget about it though.

Autumnlight02 commented 1 year ago

https://github.com/oven-sh/bun/issues/5434

LucyEgan commented 11 months ago

This is now working for us in bun 1.0.15 as they have updated their websocket client

burakakca commented 5 months ago

I tried yesterday and surreal.js it's working but surreal.node gave me error

AlbertMarashi commented 3 months ago

@burakakca I recommend using surreal.js surrealdb package with websockets for Bun, I haven't run into any issues w/ it

LucyEgan commented 3 months ago

@kearfy This is good to close isn't it unless there's more issues with surrealdb.js on bun?

If its a issue with surreal.node then that should be in the https://github.com/surrealdb/surrealdb.node repo?