Skip to content

Commit

Permalink
Allow unsetting ns/db (#313)
Browse files Browse the repository at this point in the history
  • Loading branch information
kearfy authored Jul 17, 2024
1 parent 423a150 commit 2caaab1
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/engines/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ export class HttpEngine extends AbstractEngine {

if (request.method === "use") {
const [ns, db] = request.params as [
string | undefined,
string | undefined,
string | false | undefined,
string | false | undefined,
];

if (ns === false) this.connection.namespace = undefined;
if (db === false) this.connection.database = undefined;
if (ns) this.connection.namespace = ns;
if (db) this.connection.database = db;
return {
Expand Down
6 changes: 4 additions & 2 deletions src/engines/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,12 @@ export class WebsocketEngine extends AbstractEngine {
switch (request.method) {
case "use": {
const [ns, db] = request.params as [
string | undefined,
string | undefined,
string | false | undefined,
string | false | undefined,
];

if (ns === false) this.connection.namespace = undefined;
if (db === false) this.connection.database = undefined;
if (ns) this.connection.namespace = ns;
if (db) this.connection.database = db;
break;
Expand Down
10 changes: 8 additions & 2 deletions src/surreal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ import {
NoNamespaceSpecified,
NoTokenReturned,
ResponseError,
SurrealDbError,
UnsupportedEngine,
} from "./errors.ts";

Expand Down Expand Up @@ -208,10 +209,15 @@ export class Surreal {
namespace,
database,
}: {
namespace?: string;
database?: string;
namespace?: string | false;
database?: string | false;
}): Promise<true> {
if (!this.connection) throw new NoActiveSocket();

if (namespace === false && database !== false)
throw new SurrealDbError(
"Cannot unset namespace without unsetting database",
);
const { error } = await this.rpc("use", [namespace, database]);
if (error) throw new ResponseError(error.message);
return true;
Expand Down

0 comments on commit 2caaab1

Please sign in to comment.