surrealdb / surrealdb.java

SurrealDB SDK for Java
https://surrealdb.com
Apache License 2.0
67 stars 21 forks source link

Errors when creating database records with specified ID. #43

Closed Zyrenth closed 1 week ago

Zyrenth commented 1 year ago

In the nodejs module I always used the create function like this:

database.create("user:id", ...)

I always specified an ID, but now as I do the same in Java this doesn't appear to be working, I'm getting errors and messed up database (though I'm not sure if this is really the database or the database browser I use). image

Here is what errors I get:

[org.java_websocket.drafts.Draft_6455] Runtime exception during onWebsocketMessage
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at path $
    at com.google.gson.Gson.fromJson(Gson.java:1226) ~[gson-2.10.jar:?]
    at com.google.gson.Gson.fromJson(Gson.java:1319) ~[gson-2.10.jar:?]
    at com.google.gson.Gson.fromJson(Gson.java:1290) ~[gson-2.10.jar:?]
    at com.surrealdb.connection.SurrealWebSocketConnection.onMessage(SurrealWebSocketConnection.java:110) ~[LobbyService.jar:?]
    at org.java_websocket.client.WebSocketClient.onWebsocketMessage(WebSocketClient.java:636) ~[LobbyService.jar:?]
    at org.java_websocket.drafts.Draft_6455.processFrameText(Draft_6455.java:986) ~[LobbyService.jar:?]
    at org.java_websocket.drafts.Draft_6455.processFrame(Draft_6455.java:910) ~[LobbyService.jar:?]
    at org.java_websocket.WebSocketImpl.decodeFrames(WebSocketImpl.java:401) ~[LobbyService.jar:?]
    at org.java_websocket.WebSocketImpl.decode(WebSocketImpl.java:233) ~[LobbyService.jar:?]
    at org.java_websocket.client.WebSocketClient.run(WebSocketClient.java:516) ~[LobbyService.jar:?]
    at java.lang.Thread.run(Thread.java:833) ~[?:?]
Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at path $
    at com.google.gson.internal.bind.JsonTreeReader.expect(JsonTreeReader.java:165) ~[gson-2.10.jar:?]
    at com.google.gson.internal.bind.JsonTreeReader.beginArray(JsonTreeReader.java:73) ~[gson-2.10.jar:?]
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:80) ~[gson-2.10.jar:?]
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61) ~[gson-2.10.jar:?]
    at com.google.gson.Gson.fromJson(Gson.java:1214) ~[gson-2.10.jar:?]
    ... 10 more
[com.surrealdb.connection.SurrealWebSocketConnection] onError
com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at path $
    at com.google.gson.Gson.fromJson(Gson.java:1226) ~[gson-2.10.jar:?]
    at com.google.gson.Gson.fromJson(Gson.java:1319) ~[gson-2.10.jar:?]
    at com.google.gson.Gson.fromJson(Gson.java:1290) ~[gson-2.10.jar:?]
    at com.surrealdb.connection.SurrealWebSocketConnection.onMessage(SurrealWebSocketConnection.java:110) ~[LobbyService.jar:?]
    at org.java_websocket.client.WebSocketClient.onWebsocketMessage(WebSocketClient.java:636) ~[LobbyService.jar:?]
    at org.java_websocket.drafts.Draft_6455.processFrameText(Draft_6455.java:986) ~[LobbyService.jar:?]
    at org.java_websocket.drafts.Draft_6455.processFrame(Draft_6455.java:910) ~[LobbyService.jar:?]
    at org.java_websocket.WebSocketImpl.decodeFrames(WebSocketImpl.java:401) ~[LobbyService.jar:?]
    at org.java_websocket.WebSocketImpl.decode(WebSocketImpl.java:233) ~[LobbyService.jar:?]
    at org.java_websocket.client.WebSocketClient.run(WebSocketClient.java:516) ~[LobbyService.jar:?]
    at java.lang.Thread.run(Thread.java:833) ~[?:?]
Caused by: java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJECT at path $
    at com.google.gson.internal.bind.JsonTreeReader.expect(JsonTreeReader.java:165) ~[gson-2.10.jar:?]
    at com.google.gson.internal.bind.JsonTreeReader.beginArray(JsonTreeReader.java:73) ~[gson-2.10.jar:?]
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:80) ~[gson-2.10.jar:?]
    at com.google.gson.internal.bind.CollectionTypeAdapterFactory$Adapter.read(CollectionTypeAdapterFactory.java:61) ~[gson-2.10.jar:?]
    at com.google.gson.Gson.fromJson(Gson.java:1214) ~[gson-2.10.jar:?]
    ... 10 more

Here is my code:

public class Database {
    private final SyncSurrealDriver driver;

    public Database(Main main){
        SurrealWebSocketConnection conn = new SurrealWebSocketConnection("localhost", 8000, false);
        conn.connect(5);
        SyncSurrealDriver driver = new SyncSurrealDriver(conn);

        driver.signIn("root", "root");
        driver.use("database", "main");

        this.driver = driver;
    }

    public void createPlayer(String uuid){
        String id = "player:" + uuid;
       PlayerData p = this.driver.create(id, new PlayerData(0, 0, "player"));

        List<PlayerData> people = driver.select(id, PlayerData.class);

        System.out.println(people);

    }
}

class PlayerData {

    public final int age;
    public final int rank;
    public final String name;

    public PlayerData(int age, int rank, String name) {
        this.age = age;
        this.rank = rank;
        this.name = name;
    }
}

That's all for now. If there is confusion just ask me, thanks!

phughk commented 1 year ago

Thanks for the detailed explanation and sample! The protocol returns a single json object if there is only one result, so that is the issue. The driver needs to handle that.

Zyrenth commented 1 year ago

Thanks for the detailed explanation and sample! The protocol returns a single json object if there is only one result, so that is the issue. The driver needs to handle that.

Thanks for the explanation, is there a solution for it right now or do I have to wait for a new release?

Zyrenth commented 1 year ago

Thanks for the detailed explanation and sample! The protocol returns a single json object if there is only one result, so that is the issue. The driver needs to handle that.

Thanks for the explanation, is there a solution for it right now or do I have to wait for a new release?

I would like to know it because I'm doing a personal project and I don't know if I should keep trying to implement a patch by myself or wait for a new release.

phughk commented 1 year ago

I am very focused on live queries at the moment, but if you would like help solving that then I can help out. It should be an easy fix, but it requires tests. It looks like we may have approached it already? But potentially might not be valid https://github.com/surrealdb/surrealdb.java/blame/main/src/main/java/com/surrealdb/connection/SurrealWebSocketConnection.java#L116 If you request multiple values then you can circumvent that, but that is a massive pain and we should solve it properly. A test for it can be written here https://github.com/surrealdb/surrealdb.java/blob/main/src/test/java/com/surrealdb/connection/SurrealWebSocketConnectionTest.java

Zyrenth commented 1 year ago

I am very focused on live queries at the moment, but if you would like help solving that then I can help out. It should be an easy fix, but it requires tests. It looks like we may have approached it already? But potentially might not be valid https://github.com/surrealdb/surrealdb.java/blame/main/src/main/java/com/surrealdb/connection/SurrealWebSocketConnection.java#L116 If you request multiple values then you can circumvent that, but that is a massive pain and we should solve it properly. A test for it can be written here https://github.com/surrealdb/surrealdb.java/blob/main/src/test/java/com/surrealdb/connection/SurrealWebSocketConnectionTest.java

Okay thanks, I will try to patch it when I will have time for it. Thanks for the help and explanations, I really appreciate it.

emmanuel-keller commented 1 week ago

The SurrealDB driver v0.2.0 comes with a new set of methods. There are different variants of the create method, as described here: https://surrealdb.github.io/surrealdb.java/javadoc/com/surrealdb/Surreal.html#create(com.surrealdb.RecordId,T)