Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Select either namespace or database to use #26

Open
vitordhers opened this issue Nov 27, 2023 · 4 comments
Open

Select either namespace or database to use #26

vitordhers opened this issue Nov 27, 2023 · 4 comments

Comments

@vitordhers
Copy link

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?

@MountainAndMorning
Copy link

Encounter the same error.

@giveerr
Copy link

giveerr commented Dec 24, 2023

I also encountered the same error and solved it this way

first create database with surreal cli

to connect via rocksdb file you should create database with surreal cli once (for creating the root user)

surreal start --user $USERNAME --pass $PASSWORD file:database.db --auth

then sign in with the namespace and database

await db.connect(options.url)
await db.signin({
  namespace: 'root',
  database: 'root',
  username: options.username,
  password: options.password,
})

@CanRau
Copy link

CanRau commented Jan 4, 2024

Thanks for sharing this possible workaround, haven't had the time to try it out yet, it's also a little inconvenient on deploys, hmm could be able to throw the CLI call into the Dockerfile and then have NodeJS connect to that I suppose 🤔

@travishorn
Copy link

await db.signin({
  namespace: 'root',
  database: 'root',
  username: options.username,
  password: options.password,
})

Thank you. The key to the solution for me was the property names namespace and database. In various parts of the documentation, they are referenced as NS, DB, ns and db. But changing them to the fully spelled out versions like you described worked for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants