Skip to content

Commit

Permalink
Implement .insert() method (#150)
Browse files Browse the repository at this point in the history
  • Loading branch information
kearfy authored Jul 21, 2023
1 parent fa9cb26 commit 627a9b5
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/library/SurrealSocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ export class SurrealSocket {

const id = getIncrementalID();
this.queue[id] = callback;
console.log("message", JSON.stringify({ id, method, params }));
this.ws?.send(JSON.stringify({ id, method, params }));
}

Expand Down
17 changes: 17 additions & 0 deletions src/strategies/websocket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,23 @@ export class WebSocketStrategy implements Connection {
return this.outputHandler(res);
}

/**
* Inserts one or multiple records in the database.
* @param thing - The table name or the specific record ID to create.
* @param data - The document(s) / record(s) to insert.
*/
async insert<
T extends Record<string, unknown>,
U extends Record<string, unknown> = T,
>(thing: string, data?: U | U[]) {
await this.ready;
const res = await this.send<ActionResult<T, U>>("insert", [
thing,
data,
]);
return this.outputHandler(res);
}

/**
* Updates all records in a table, or a specific record, in the database.
*
Expand Down
12 changes: 12 additions & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ export interface Connection {
connect: (url: string, options?: ConnectionOptions) => void;
ping: () => Promise<void>;
use: (opt: { ns: string; db: string }) => MaybePromise<void>;

// Info method is not available in the HTTP REST API
info?: <T extends Record<string, unknown> = Record<string, unknown>>() =>
Promise<T | undefined>;

Expand All @@ -16,6 +18,7 @@ export interface Connection {
authenticate: (token: Token) => MaybePromise<void>;
invalidate: () => MaybePromise<void>;

// Let/unset methods are not available in the HTTP REST API
let?: (variable: string, value: unknown) => Promise<void>;
unset?: (variable: string) => Promise<void>;

Expand All @@ -36,6 +39,15 @@ export interface Connection {
data?: U,
) => Promise<ActionResult<T, U>[]>;

// Insert method is not available in the HTTP REST API
insert?: <
T extends Record<string, unknown>,
U extends Record<string, unknown> = T,
>(
thing: string,
data?: U | U[],
) => Promise<ActionResult<T, U>[]>;

update: <
T extends Record<string, unknown>,
U extends Record<string, unknown> = T,
Expand Down

0 comments on commit 627a9b5

Please sign in to comment.