Giter Club home page Giter Club logo

surrealdb.node's Introduction

surrealdb.node

The official SurrealDB library for Node.js.

  What is SurrealDB?

SurrealDB is an end-to-end cloud-native database designed for modern applications, including web, mobile, serverless, Jamstack, backend, and traditional applications. With SurrealDB, you can simplify your database and API infrastructure, reduce development time, and build secure, performant apps quickly and cost-effectively.

Key features of SurrealDB include:

  • Reduces development time: SurrealDB simplifies your database and API stack by removing the need for most server-side components, allowing you to build secure, performant apps faster and cheaper.
  • Real-time collaborative API backend service: SurrealDB functions as both a database and an API backend service, enabling real-time collaboration.
  • Support for multiple querying languages: SurrealDB supports SQL querying from client devices, GraphQL, ACID transactions, WebSocket connections, structured and unstructured data, graph querying, full-text indexing, and geospatial querying.
  • Granular access control: SurrealDB provides row-level permissions-based access control, giving you the ability to manage data access with precision.

View the features, the latest releases, the product roadmap, and documentation.

  Getting started

// import as ES module or common JS
const { default: Surreal } = require('surrealdb.node');

const db = new Surreal();

async function main() {
	try {

		// Use any of these 3 connect methods to connect to the database
		// 1.Connect to the database
		await db.connect('http://127.0.0.1:8000/rpc');
		// 2. Connect to database server
		await db.connect('ws://127.0.0.1:8000');
		// 3. Connect via rocksdb file
		await db.connect(`rocksdb://${process.cwd()}/test.db`);

		// Signin as a namespace, database, or root user
		await db.signin({
			username: 'root',
			password: 'root',
		});

		// Select a specific namespace / database
		await db.use({ namespace: 'test', database: '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',
			}
		);
	} catch (e) {
		console.error('ERROR', e);
	}
}

main();

Supported targets

Tripple supported rocksdb support reason
aarch64-apple-darwin x
aarch64-linux-android
aarch64-unknown-linux-gnu
aarch64-unknown-linux-musl x
aarch64-pc-windows-msvc x x ring build fails
armv7-unknown-linux-gnueabihf x x psm build fails
x86_64-unknown-linux-musl x
x86_64-unknown-freebsd x
i686-pc-windows-msvc
armv7-linux-androideabi
universal-apple-darwi x

surrealdb.node's People

Contributors

ekwuno avatar kearfy avatar naisofly avatar raphaeldarley avatar tobiemh avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

surrealdb.node's Issues

Surreal.node sample code not working

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:
Screenshot 2023-09-25 at 7 24 24 PM

Does not work with Yarn@4

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

Disconnect method

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<()> {}

Unable to install (error Couldn't find package "[email protected]")

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

Getting generic error while trying to connect to database

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()

create statement cannot parses redord ids that have more than one elements

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",

ECONNREFUSED Error

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.

Select either namespace or database to use

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?

New Project Proposal: Create a Type Safe SurrealQL Query Builder

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.

rocksdb support

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?

Bug: Getting a Surreal is not a constructor although i've gotte

Describe the bug

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");
    }
}

Steps to reproduce

  1. I installed the nodejs sdk
  2. I copied your docs word for word bar for bar
  3. It crashes

Expected behaviour

To connect to the database

SurrealDB version

1.0.0

Contact Details

[email protected]

Is there an existing issue for this?

  • I have searched the existing issues

Code of Conduct

  • I agree to follow this project's Code of Conduct

Use of health(): Promise<void>

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.

No typescript types and different response shapes between packages

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.

missing methods

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?

SurrealDB works with Node.js but not shell.

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

image

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;'"}

image

SurrealHTTP() class

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

Unable to make record links

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.