Skip to content

Commit

Permalink
Allow partial use (#310)
Browse files Browse the repository at this point in the history
  • Loading branch information
kearfy authored Jul 17, 2024
1 parent 6c8bfdf commit b073440
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 23 deletions.
10 changes: 7 additions & 3 deletions src/engines/http.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,13 @@ export class HttpEngine extends AbstractEngine {
}

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

if (ns) this.connection.namespace = ns;
if (db) this.connection.database = db;
return {
result: true as Result,
};
Expand Down
10 changes: 7 additions & 3 deletions src/engines/ws.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,9 +145,13 @@ export class WebsocketEngine extends AbstractEngine {
if ("result" in res) {
switch (request.method) {
case "use": {
const [ns, db] = request.params as [string, string];
this.connection.namespace = ns;
this.connection.database = db;
const [ns, db] = request.params as [
string | undefined,
string | undefined,
];

if (ns) this.connection.namespace = ns;
if (db) this.connection.database = db;
break;
}

Expand Down
18 changes: 1 addition & 17 deletions src/surreal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,6 @@ export class Surreal {

// Process options
const { prepare, auth, namespace, database } = opts;
if (opts.namespace || opts.database) {
if (!opts.namespace) throw new NoNamespaceSpecified();
if (!opts.database) throw new NoDatabaseSpecified();
}

// Close any existing connections
await this.close();
Expand Down Expand Up @@ -216,19 +212,7 @@ export class Surreal {
database?: string;
}): Promise<true> {
if (!this.connection) throw new NoActiveSocket();

if (!namespace && !this.connection.connection.namespace) {
throw new NoNamespaceSpecified();
}
if (!database && !this.connection.connection.database) {
throw new NoDatabaseSpecified();
}

const { error } = await this.rpc("use", [
namespace ?? this.connection.connection.namespace,
database ?? this.connection.connection.database,
]);

const { error } = await this.rpc("use", [namespace, database]);
if (error) throw new ResponseError(error.message);
return true;
}
Expand Down

0 comments on commit b073440

Please sign in to comment.