surrealdb / surrealdb.js

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

db.create does not throw an error if a string contains '\n', it just hangs #9

Closed Balrog994 closed 1 year ago

Balrog994 commented 2 years ago

I'm trying to import 17652 rows from SQL Server to SurrealDB to make some tests. I wrote a small nodejs script that connects to SQL Server, reads the contents of a table and then calls db.create for each row (maybe a bulk insert method in the future? :D)

After exactly 9360 rows, every time I tested it, the script just hangs on a db.create and never returns. I'm using the standard docker container version of SurrealDB running on Docker Desktop.

import sql from "mssql";
import Surreal from "surrealdb.js";
import _ from "lodash";

async function main() {
    const db = new Surreal("http://localhost:8000/rpc");

    const config = {
        server: "###",
        user: "###",
        password: "###",
        database: "###",
        options: {
            encrypt: false,
        },
    };

    console.log("Connecting to SQL Server...");
    await sql.connect(config);

    console.log("Connecting to SurrealDB...");
    await db.signin({
        user: "###",
        pass: "###",
    });
    await db.use("test", "test");

    console.log("Reading rows from the Table...");
    const result = await sql.query`SELECT * FROM <tableName>`;

    for (const row of result.recordset) {
        await db.create(
            "item:" + row.Id,
            row
        );
    }
    await sql.close();
    db.close();

    console.log("DONE!");
}

main();

The script is pretty simple as shown above. After it hangs, selecting from SurrealDB shows that exactly 9360 rows where created, on a total of 17652 to be created.

SELECT count() FROM task GROUP BY ALL;
[
    {
        "time": "107.4225ms",
        "status": "OK",
        "result": [
            {
                "count": 9360
            }
        ]
    }
]

Am I doing something wrong?

Thanks!

Balrog994 commented 2 years ago

Ok, looks like I have an update.

It turns out the problem was a \n in a string. Looks like I cannot use \n as an escape sequence in a string and the server returns an error, but the client library does not handle nor throws the error and just hangs there.

BTW, anyone knows how to encode a newline in a string? 😄

LeoDog896 commented 2 years ago

My temporary fix was to replace all \n and \t characters with a space " " and split queries by the ; seperator.

LeoDog896 commented 2 years ago

This issue is also in surrealdb/surrealdb https://github.com/surrealdb/surrealdb/issues/90

Balrog994 commented 2 years ago

I think the library should at least throw an error in this case instead of just hanging indefinitely, even if the error is due to a bug in the server parser. The REST API correctly returns an error (not really useful to "debug" the problem but at least you get an error).

tobiemh commented 2 years ago

Hi @LeoDog896 , @Balrog994 , there is a fix coming to the database for this, and a new beta release coming this weekend!

It will support escaped characters like in JSON strings (\b, \n, \f, \r, \t) and unicode characters \u1234.

abdoufma commented 2 years ago

@tobiemh That's exciting to hear! am I correct in assuming the new release would also fix the problem of not being able to insert objects that have been JSON.stringify'd more than twice? (ex : '{"foo":"{\\"bar\\":\\"baz\\"}"}')

tobiemh commented 2 years ago

Absolutely @abdoufma 👍

mathe42 commented 2 years ago

@Balrog994 fixed with surreal 1.0.0-beta.8. @tobiemh close this.