surrealdb / surrealdb.node Goto Github PK
View Code? Open in Web Editor NEWA Node.js engine for the SurrealDB JavaScript SDK
Home Page: https://surrealdb.com
License: Apache License 2.0
A Node.js engine for the SurrealDB JavaScript SDK
Home Page: https://surrealdb.com
License: Apache License 2.0
I tried running the sample code given in the instruction:
const { Surreal } = require('surrealdb.node');
const db = new Surreal();
async function main() {
try {
await db.connect('memory');
// connect to database server
await db.connect('http://localhost:8000');
// use rocksdb file
await db.connect(`rocksdb://${process.cwd()}/test.db`);
// 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).substr(2, 10),
});
// 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($table) GROUP BY marketing",
{
table: "person",
},
);
// Delete all people upto but not including Jaime
let deleted = await db.delete("person:..jaime");
} catch (e) {
console.error('An error occurred:', e);
}
}
main();
This is the error I am getting. I wonder, what am I doing wrong here:
Last release here in GitHub Releases is 0.4.0 but on the NPM repo we have 0.3.0.
So we cannot install the latest package because it's missing.
I am getting this error when connecting to database
[Error: There was a problem with the underlying datastore: Cannot connect to the
HTTP remote engine as it is not enabled in this build of SurrealDB] { code: 'GenericFailure' }
Code snippet:
const {Surreal } = require('surrealdb.node');
const client = new Surreal();
async function connect() {
try {
await client.connect("http://0.0.0.0:8000/rpc");
} catch (error) {
console.log(error);
}
}
connect()
Why is rocksdb limited to certain build targets?
I compiled it for aarch64-apple-darwin
and dabbled around with it while using rocksdb as store data and everything worked perfectly fine.
Is there something I'm not aware of?
yarn add surrealdb.node
results in the following error
error Couldn't find package "[email protected]" required by "surrealdb.node" on the "npm" registry.
I'm on macOS Big Sur (11.7.10) and unfortunately can't upgrade due to 3rd party ssd 🥲
Anything I can do? I wan't to embed ideally via file (I guess RocksDB) and potentially memory
Update: just in case this happens using yarn 1.22.19
First: 👏 great job - cool product and lib's!
The surrealdb.js package already provides typings for results like this:
query: <T extends RawQueryResult[]>(
query: string,
vars?: Record<string, unknown>,
) => Promise<MapQueryResult<T>>;
Would be awesome if this would also become available in surrealdb.node
.
Also, there is a difference in the response shapes of surrealdb.node
, surrealdb.wasm
and surrealdb.js
The surrealdb.js
package provides a result like this:
[
{
status: string
result?: <result type>
}
]
while surrealdb.node
and surrealdb.wasm
is returning a less deep, structured response like this:
[
<result type>
]
It would be great if this becomes aligned across all the different cool packages.
Hello,
I am writing health checks for my services and I need to check DB status. There is method health(): Promise<void>
in the class Surreal, but returns void.
Is there way how to use it or will it be implemented in the future?
For now I am using [GET /health]
via HTTP.
Thank you.
I'm using surrealdb redis server on my dev env (ubuntu), and connecting to it via WS with surrealdb.node
. Always after a while (10 sec ~ 10 min) I got this error:
ERROR [unhandledRejection] connect ECONNREFUSED 127.0.0.1:42111
at TCPConnectWrap.afterConnect [as oncomplete] (node:net:1494:16)
and client won't work until restart. server works fine.
What is this repository for in comparison to the JS and Deno repos?
Suggestion: Let's bring together the TypeScript fun:
surrealdb/surrealdb.deno#5
Hello, folks, I'm a Rust user so that I'd like to try this surrealDb, as it seems awesome.
The thing is, in this test project I'm not using Rust but node (specifically nestJs).
I want to have my db to run locally, but I'm seeming to get an error that I don't know where it comes from, nor what should I do.
Here's my function for connecting to it:
import { Surreal } from 'surrealdb.node';
async onModuleInit() {
this._db = new Surreal();
await this._connectAndSignIn();
}
private async _connectAndSignIn() {
const username = this.config.get<string>('SURREAL_DB_USER');
const password = this.config.get<string>('SURREAL_DB_PASSWORD');
const surrealDbFile = this.config.get<string>('SURREAL_DB_FILE');
try {
await this._db.connect(`rocksdb://${process.cwd()}/${surrealDbFile}.db`);
await this._db.signin({
username,
password,
});
const namespace = this.config.get<string>('SURREAL_DB_NS');
const databaseName = this.config.get<string>('SURREAL_DB_DATABASE');
} catch (error) {
this.logger.error(
'_connectAndSignIn error: ',
inspect({ error }, { depth: null }),
);
}
}
So, when I tried to run a query I got this error error: [Error: Specify a namespace to use] { code: 'GenericFailure' }
, now in the code above, if I add this await this._db.use({ namespace: 'questions', database: });
I get another error: error: [Error: Select either namespace or database to use] { code: 'GenericFailure' }
Thing is, I've already run DEFINE statements using the commands
surreal start --log trace --user root --pass root file://questions.db
surreal sql --conn http://localhost:8000 \ --user root --pass root
DEFINE NS questions
USE NS questions
DEFINE DATABASE dev
, but they didn't seem to work. What am I doing wrong?
const { default: Surreal } = require('surrealdb.node');
const db = new Surreal();
async function connect_surreal_db(){
try {
await db.connect('http://127.0.0.1:8080/rpc');
await db.signin({
username: process.env.DB_USERNAME,
password: process.env.DB_PASSWORD
});
await db.use({namespace:'EVENT_CONTRACT', database:'event_db1'});
} catch (error) {
return new Error("Database connection failed");
}
}
To connect to the database
1.0.0
It seems that there is a diff between the online documentation (https://docs.surrealdb.com/docs/integration/sdks/nodejs) and the actual implemented methods.
Website:
connect
close
use
info
signup
signin
invalidate
authenticate
let
unset
query
select
create
insert
update
merge
patch
delete
Source (from src/lib.rs
and index.d.ts
):
connect
use
set
signup
signin
invalidate
authenticate
unset
query
select
create
update
merge
patch
delete
version
health
Diff (red for missing methods, green for extra ones):
< close
< info
< let
< insert
---
> set
> version
> health
Which version is right?
Not sure how useful this will be but I found myself needing to use my database over HTTP in serverless cloud functions. Using Websockets there would have several issues do to their short-lived nature (performance overheads, resource usage, connection disruptions, etc...). I noticed that the python sdk contains this class, which is great, but I think this also needs it. Bare in mind I have never used rust.
class SurrealHTTP {
constructor(url, ns, db, auth) {
this.url = url;
this.ns = ns;
this.db = db;
this.auth = auth;
}
async query(sql, bindings) {
let config = {
headers: {
'NS': this.ns,
'DB': this.db,
'Accept': 'application/json'
},
params: bindings // bind variables in the request params
};
// auth configuration based on whether JWT token or user and password are used
if (this.auth.token) {
config.headers['Token'] = `${this.auth.token}`;
} else {
// Basic authentication
config.auth = {
username: this.auth.username,
password: this.auth.password
};
}
try {
const response = await axios.post(`${this.url}/sql`, sql, config);
if (response.data[0].status !== 'OK') {
throw new Error('Server responded with a status other than OK.');
}
return response.data;
}
catch (error) {
console.error(error);
return null;
}
}
}
EDIT: Added JWT auth
There doesn't seem a way to explicitly disconnect from the database, which means if using local file system (rocksdb://), the database will stay locked and be unusable for as long as the program runs.
I'm working on an electron application, using the database from a preload script to have it be isolated, and each refresh also reloads the preload script, which makes it try to connect again. But since the process itself isn't destroyed, the value in rust is never dropped, or at least not in a reasonable timeframe.
I don't know much about napi, but maybe it's a simple case of adding a disconnect method that consumes self, i.e.
surrealdb.node/src/lib.rs
#[napi]
pub fn disconnect(self) -> Result<()> {}
When introducing a second element in a record id create statement, the library does not recognize the record id as one.
this.surrealDb.create(`${collatorAuthoredBlocksTable}:${collator}:${round}`
this.surrealDb.create(`${collatorAuthoredBlocksTable}:⟨${collator}⟩:⟨${round}⟩`
Both statements above result to
"id": "⟨mb_sd_collator_authored_blocks:0xAb4B115b2D23EF8DA7b0aa54FC074fB4C6886441:1669⟩:5a2sviuj8vxyrak9amna",
Anyone have any idea how this works under the hood 🧐
Because I want to integrate it in orm
When I attempt to make a record link:
let customerOrganization = null;
let customerUser1 = null;
customerOrganization = await db.create('organization', {
name: 'Customer organization',
type: ['customer']
});
console.log('created customer', customerOrganization)
// created customer [
// {
// id: 'organization:id1az0rct82w9gw0vd7t',
// name: 'Customer organization',
// type: [ 'customer' ],
// updatedAt: '2023-09-23T16:24:32.941479Z'
// }
// ]
customerUser1 = await db.query(`
INSERT INTO userAccount {
firstName: $firstName,
lastName: $lastName,
email: $email,
organization: $organization
};
`, {
firstName: 'Customer',
lastName: 'User1',
email: '[email protected]',
organization: customerOrganization[0].id
})
// Found 'organization:rxzhzkxhx7c0bmx6j2x8' for field `organization`, with record `userAccount:ki86v3aqoe8h9yvdq6o3`, but expected a record<organization>
console.log('created user', customerUser1)
It doesn't work because it treats the ID as a string and not an actual record. This might be resolved by using string::split
combined with type::thing
but we shouldn't have to do that.
Because this is using the rust driver under the hood, its probably not handling the type in a way that works well for JS based platforms.
Installing with yarn 4 does not work.
yarn add surrealdb.node
➤ YN0000: · Yarn 4.0.0
➤ YN0000: ┌ Resolution step
➤ YN0035: │ surrealdb.node-linux-arm-gnueabihf@npm:0.3.0: Package not found
➤ YN0035: │ Response Code: 404 (Not Found)
➤ YN0035: │ Request Method: GET
➤ YN0035: │ Request URL: https://registry.yarnpkg.com/surrealdb.node-linux-arm-gnueabihf
➤ YN0000: └ Completed in 0s 916ms
➤ YN0000: · Failed with errors in 0s 946ms
Hello Surreal team,
I am trying out your database but having a hard time making it work with my express server.
is there any tutorial on using your surreal with node except for the documentation?
I started SurrealDB by the command below and ran a SQL command but it threw an error but when I use the Node.js SDK it worked perfectly fine.
Start command
surreal start --log debug --user root memory --pass root
SQL query
C:\> CREATE DATABASE tewrh;
{"code":400,"details":"Request problems detected","description":"There is a problem with your request. Refer to the documentation for further information.","information":"There was a problem with the database: Parse error on line 1 at character 16 when parsing 'tewrh;'"}
Hi team,
So I am executing this tx through your js llibrary:
BEGIN TRANSACTION;
LET $row1 = SELECT block FROM mb_relation_latest:['balances','totalissuance',[],'u128'] WHERE block >= 1009157 OR dataHash='AkV6NharkwyHoHJsrwDmysK-UmY70JO5bBiDngNw8Us';
IF count($row1) = 1 THEN 'old_or_stale'
ELSE {
INSERT IGNORE INTO mb_relation $relation1;
UPDATE mb_relation_latest:['balances','totalissuance',[],'u128'] CONTENT $relationSuper1;
}
END;
COMMIT TRANSACTION;
...and occasionally the tx will fail with the returned error: "The query was not executed due to a failed transaction"
However, when I execute the above in Surrealist, I get both the above error and also "Cannot perform addition with some big number and some big number"
It looks like the nodejs library does not return the low level error which is also the most useful one. This is especially problematic for us since we cannot deal with these errors in the code, and also not in the database (this is a different issue I have brought up on discord).
One of the things that is making me hesitate to adopt Surreal DB into my stack is a lack of meaningful TypeScript support. There are a variety of ways to go about making this happen, some more complicated then others. For example Prisma is able to introspect the db to generate a prisma schema file (or you can write the schema file yourself without introspection) and it will (among potentially other things) generate types based on your schema and incorporate them into the Prisma client. This type generation process only becomes helpful when used with the Prisma ORM and it seems that SurrealDB is not very interested in making an ORM right now (understandably!)
That's why I think we can look to what Knex.js is doing. It's a query builder/runner for SQL databases. However, Kinex.js is not suitable for SurrealDB because SurrealQL is very different from standard SQL (if anything it's a superset). Yet in theory, you could create a library that applies its own approach to query building. As you build queries using the proposed library, it will also ensure that the outputs produced when executing queries are properly typed. In other words, if you build and execute a query with this lib, what would be returned by it will have a proper type signature describing the shape and types of the object(s) you should expect to have returned.
Anyways, this lib should be separate from the JS, Node, Deno, and WASM connector drivers. Instead when instantiating the query builder/runner object for this lib, would require the instantiated driver object for it to be able to execute any queries you build with it.
I think that if a good enough library like the one proposed is created, the community could potentially build any ORM's that they might want on top of it. For example, Mikro ORM uses Knex.js underneath. I believe that if such a lib were created, that it would help to greatly accelerate adoption of SurrealDB from the JS/TS community.
Related Content:
See https://github.com/igalklebanov/kysely-surrealdb
See knex/knex#5955
See https://github.com/StarlaneStudios/cirql
Ideas:
The builder could use Zod (very popular) or Typebox (which has the best Perf) to help manage types.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.