igalklebanov/kysely-surrealdb

Can't connect to SurrealDB

Closed this issue · 7 comments

Hello, its me again :)
For some reason Typescript couldn't call create because for some reason that doesn't exist(?).
Minimal reproduction project here: https://github.com/sonicjhon1/kysely-surreal-error
The code is from the Readme.md example:

await db
	.create('person:100')
	.set({
		first_name: 'Jennifer',
		age: 15,
	})
	.return('none')
	.execute()
> pnpm ts-node index.ts

D:\SJ\Devs\Github\kysely-surreal-error\node_modules\.pnpm\ts-node@10.9.1_@types+node@20.2.1_typescript@5.0.4\node_modules\ts-node\src\index.ts:859
    return new TSError(diagnosticText, diagnosticCodes, diagnostics);
           ^
TSError:  Unable to compile TypeScript:
index.ts:29:5 - error TS2339: Property 'create' does not exist on type 'Kysely<SurrealDatabase<Database>>'.

29    .create('person:100')
       ~~~~~~

    at createTSError (D:\SJ\Devs\Github\kysely-surreal-error\node_modules\.pnpm\ts-node@10.9.1_@types+node@20.2.1_typescript@5.0.4\node_modules\ts-node\src\index.ts:859:12)
    at reportTSError (D:\SJ\Devs\Github\kysely-surreal-error\node_modules\.pnpm\ts-node@10.9.1_@types+node@20.2.1_typescript@5.0.4\node_modules\ts-node\src\index.ts:863:19)
    at getOutput (D:\SJ\Devs\Github\kysely-surreal-error\node_modules\.pnpm\ts-node@10.9.1_@types+node@20.2.1_typescript@5.0.4\node_modules\ts-node\src\index.ts:1077:36)
\node_modules\ts-node\src\index.ts:1433:41)
    at Module.m._compile (D:\SJ\Devs\Github\kysely-surreal-error\node_modules\.pnpm\ts-node@10.9.1_@types+node@20.2.1_typescript@5.0.4\node_modules\ts-node\src\index.ts:1617:30)
    at Module._extensions..js (node:internal/modules/cjs/loader:1329:10)
    at Object.require.extensions.<computed> [as .ts] (D:\SJ\Devs\Github\kysely-surreal-error\node_modules\.pnpm\ts-node@10.9.1_@types+node@20.2.1_typescript@5.0.4\node_modules\ts-node\src\index.ts:1621:12)
    at Module.load (node:internal/modules/cjs/loader:1133:32)
    at Function.Module._load (node:internal/modules/cjs/loader:972:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:83:12) {
  diagnosticCodes: [ 2339 ]
}

Also, the project above doesn't seem to do any connection to the DB even if I modified the code to not use create:

await db.selectFrom("person").selectAll().execute()
$ pnpm ts-node index.ts
> surreal start --log trace --user root --pass root --bind 0.0.0.0:8080 file://D:/SJ/Devs/DB/SurrealDB

2023-05-19T13:42:32.235373Z  INFO surrealdb::env: Running 1.0.0-beta.9+20230402.5eafebd for windows on x86_64
2023-05-19T13:42:32.235497Z  INFO surrealdb::iam: Root authentication is enabled
2023-05-19T13:42:32.235577Z  INFO surrealdb::iam: Root username is 'root'
2023-05-19T13:42:32.235655Z  INFO surrealdb::dbs: Database strict mode is disabled
2023-05-19T13:42:32.235734Z  INFO surrealdb::kvs: Starting kvs store at file://D:/SJ/Devs/DB/SurrealDB
2023-05-19T13:42:32.308941Z  INFO surrealdb::kvs: Started kvs store at file://D:/SJ/Devs/DB/SurrealDB
2023-05-19T13:42:32.309469Z  INFO surrealdb::net: Starting web server on 0.0.0.0:8080
2023-05-19T13:42:32.310541Z  INFO surrealdb::net: Started web server on 0.0.0.0:8080

Hey 👋

In order to use surrealdb specific statements, you need to instantiate SurrealKysely instead of Kysely.
https://github.com/igalklebanov/kysely-surrealdb#surrealkysely-query-builder

Ah, yes that fixed the issue of not being able to call create 😁

But now it seems like it has frozen up 🤔
It doesn't crash nor does it connects to the database.
The SurrealDB log stays the same, and no data in the database got updated.

I've updated the Minimal reproduction project again,
https://github.com/sonicjhon1/kysely-surreal-error

Following up, after updating to "kysely-surrealdb": "^0.7.2", it doesn't freeze anymore but rather show the following error:

Error: connect ECONNREFUSED ::1:8000
    at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1532:16) {
  errno: -4078,
  code: 'ECONNREFUSED',
  syscall: 'connect',
  address: '::1',
  port: 8000
}

Updated minimal reproduction project here
This happens with SurrealDbHttpDialect or SurrealDbWebSocketsDialect.

I've made sure that:

  • I ran pnpm i --force
  • SurrealDB is running at localhost:8000 (both http and ws work in the Surrealist app)
  • Could connect via Surrealist

I've cloned your repo, it's working against my local surrealdb docker container (I'm using this project's docker-compose file), and persists person:100 (can be retrieved later).

When using SurrealDbWebSocketsDialect, you need to .destroy() to close the connection when completing, otherwise, it hangs.

Do you know the address Surrealist uses to connect?

That ::1 (IPv6) is suspicious, might wanna explore that situation. Maybe surrealdb's image only supports IPv4. 🤷

Do you know the address Surrealist uses to connect?

I don't really know if it changes the address inputed inside of the app before connecting to the DB or not, but http://localhost:8080 or ws://localhost:8080 works in Surrealist.

That ::1 (IPv6) is suspicious, might wanna explore that situation. Maybe surrealdb's image only supports IPv4. 🤷

Right, it seems that instead of using localhost:8080, using 127.0.0.1:8080 did fixed that problem (which I guess made it use IPv4 instead?). But even after using 127.0.0.1:8080 it still errors out with:

Error: write EPROTO FC630000:error:0A00010B:SSL routines:ssl3_get_record:wrong version number:c:\ws\deps\openssl\openssl\ssl\record\ssl3_record.c:355:

    at WriteWrap.onWriteComplete [as oncomplete] (node:internal/stream_base_commons:94:16) {
  errno: -4046,
  code: 'EPROTO',
  syscall: 'write'
}

Which then I found out why that's happening: https://github.com/igalklebanov/kysely-surrealdb/blob/e488dc2e08f8cddb6b07ab44842a0ccc6b63b884/src/dialect/shared.ts#LL4C7-L4C7
This line made everything that isn't localhost use the https protocol, which means that 127.0.0.1 would also use the https protocol.
A simple fix is:

const protocol = (hostname.startsWith("localhost") || hostname.startsWith("127.0.0.1")) ? "http" : "https";

Released in v0.7.4.