Skip to content

Commit

Permalink
Stabilize 1.0.0 (#338)
Browse files Browse the repository at this point in the history
  • Loading branch information
kearfy authored Sep 18, 2024
1 parent c1502a7 commit e69c7ee
Show file tree
Hide file tree
Showing 5 changed files with 124 additions and 38 deletions.
11 changes: 3 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "surrealdb",
"version": "1.0.0-beta.21",
"version": "1.0.0",
"type": "module",
"license": "Apache-2.0",
"repository": {
Expand Down Expand Up @@ -46,10 +46,5 @@
"browser": "./dist/index.bundled.mjs"
}
},
"files": [
"dist",
"README.md",
"LICENCE",
"SECURITY.md"
]
}
"files": ["dist", "README.md", "LICENCE", "SECURITY.md"]
}
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export * from "./types.ts";
export * from "./util/jsonify.ts";
export * from "./util/versionCheck.ts";
export * from "./util/getIncrementalID.ts";
export * from "./util/string-prefixes.ts";
export {
ConnectionStatus,
AbstractEngine,
Expand Down
92 changes: 62 additions & 30 deletions src/surreal.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
type StringRecordId,
type Table,
Table,
type Uuid,
type RecordId as _RecordId,
decodeCbor,
Expand Down Expand Up @@ -442,8 +442,8 @@ export class Surreal {
* Selects all records in a table, or a specific record, from the database.
* @param thing - The table name or a record ID to select.
*/
async select<T extends R>(thing: Table | string): Promise<ActionResult<T>[]>;
async select<T extends R>(thing: RecordId): Promise<ActionResult<T>>;
async select<T extends R>(thing: Table | string): Promise<ActionResult<T>[]>;
async select<T extends R>(thing: RecordId | Table | string) {
await this.ready;
const res = await this.rpc<ActionResult<T>>("select", [thing]);
Expand All @@ -456,14 +456,14 @@ export class Surreal {
* @param thing - The table name or the specific record ID to create.
* @param data - The document / record data to insert.
*/
async create<T extends R, U extends R = T>(
thing: Table | string,
data?: U,
): Promise<ActionResult<T>[]>;
async create<T extends R, U extends R = T>(
thing: RecordId,
data?: U,
): Promise<ActionResult<T>>;
async create<T extends R, U extends R = T>(
thing: Table | string,
data?: U,
): Promise<ActionResult<T>[]>;
async create<T extends R, U extends R = T>(
thing: RecordId | Table | string,
data?: U,
Expand All @@ -480,19 +480,51 @@ export class Surreal {
* @param data - The document(s) / record(s) to insert.
*/
async insert<T extends R, U extends R = T>(
thing: Table | string,
data?: U | U[],
): Promise<ActionResult<T>[]>;
async insert<T extends R, U extends R = T>(
thing: RecordId,
data?: U,
): Promise<ActionResult<T>>;
table: Table | string,
data?: U | U[],
): Promise<ActionResult<T>[]>;
async insert<T extends R, U extends R = T>(
thing: RecordId | Table | string,
arg1: Table | string | U | U[],
arg2?: U | U[],
) {
await this.ready;
const [table, data] =
typeof arg1 === "string" || arg1 instanceof Table
? [arg1, arg2]
: [undefined, arg1];
const res = await this.rpc<ActionResult<T>>("insert", [table, data]);
if (res.error) throw new ResponseError(res.error.message);
return res.result;
}

/**
* 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_relation<T extends R, U extends R = T>(
data?: U | U[],
): Promise<ActionResult<T>[]>;
async insert_relation<T extends R, U extends R = T>(
table: Table | string,
data?: U | U[],
): Promise<ActionResult<T>[]>;
async insert_relation<T extends R, U extends R = T>(
arg1: Table | string | U | U[],
arg2?: U | U[],
) {
await this.ready;
const res = await this.rpc<ActionResult<T>>("insert", [thing, data]);
const [table, data] =
typeof arg1 === "string" || arg1 instanceof Table
? [arg1, arg2]
: [undefined, arg1];
const res = await this.rpc<ActionResult<T>>("insert_relation", [
table,
data,
]);
if (res.error) throw new ResponseError(res.error.message);
return res.result;
}
Expand All @@ -504,14 +536,14 @@ export class Surreal {
* @param thing - The table name or the specific record ID to update.
* @param data - The document / record data to insert.
*/
async update<T extends R, U extends R = T>(
thing: Table | string,
data?: U,
): Promise<ActionResult<T>[]>;
async update<T extends R, U extends R = T>(
thing: RecordId,
data?: U,
): Promise<ActionResult<T>>;
async update<T extends R, U extends R = T>(
thing: Table | string,
data?: U,
): Promise<ActionResult<T>[]>;
async update<T extends R, U extends R = T>(
thing: RecordId | Table | string,
data?: U,
Expand All @@ -529,14 +561,14 @@ export class Surreal {
* @param thing - The table name or the specific record ID to upsert.
* @param data - The document / record data to insert.
*/
async upsert<T extends R, U extends R = T>(
thing: Table | string,
data?: U,
): Promise<ActionResult<T>[]>;
async upsert<T extends R, U extends R = T>(
thing: RecordId,
data?: U,
): Promise<ActionResult<T>>;
async upsert<T extends R, U extends R = T>(
thing: Table | string,
data?: U,
): Promise<ActionResult<T>[]>;
async upsert<T extends R, U extends R = T>(
thing: RecordId | Table | string,
data?: U,
Expand All @@ -554,14 +586,14 @@ export class Surreal {
* @param thing - The table name or the specific record ID to change.
* @param data - The document / record data to insert.
*/
async merge<T extends R, U extends R = Partial<T>>(
thing: Table | string,
data?: U,
): Promise<ActionResult<T>[]>;
async merge<T extends R, U extends R = Partial<T>>(
thing: RecordId,
data?: U,
): Promise<ActionResult<T>>;
async merge<T extends R, U extends R = Partial<T>>(
thing: Table | string,
data?: U,
): Promise<ActionResult<T>[]>;
async merge<T extends R, U extends R = Partial<T>>(
thing: RecordId | Table | string,
data?: U,
Expand Down Expand Up @@ -617,8 +649,8 @@ export class Surreal {
* Deletes all records in a table, or a specific record, from the database.
* @param thing - The table name or a record ID to select.
*/
async delete<T extends R>(thing: Table | string): Promise<ActionResult<T>[]>;
async delete<T extends R>(thing: RecordId): Promise<ActionResult<T>>;
async delete<T extends R>(thing: Table | string): Promise<ActionResult<T>[]>;
async delete<T extends R>(thing: RecordId | Table | string) {
await this.ready;
const res = await this.rpc<ActionResult<T>>("delete", [thing]);
Expand Down Expand Up @@ -669,16 +701,16 @@ export class Surreal {
*/
async relate<T extends R, U extends R = T>(
from: string | RecordId | RecordId[],
thing: string,
thing: RecordId,
to: string | RecordId | RecordId[],
data?: U,
): Promise<T[]>;
): Promise<T>;
async relate<T extends R, U extends R = T>(
from: string | RecordId | RecordId[],
thing: RecordId,
thing: string,
to: string | RecordId | RecordId[],
data?: U,
): Promise<T>;
): Promise<T[]>;
async relate<T extends R, U extends R = T>(
from: string | RecordId | RecordId[],
thing: string | RecordId,
Expand All @@ -696,7 +728,7 @@ export class Surreal {
* @param method - Type of message to send.
* @param params - Parameters for the message.
*/
protected rpc<Result>(
public rpc<Result>(
method: string,
params?: unknown[],
): Promise<RpcResponse<Result>> {
Expand Down
32 changes: 32 additions & 0 deletions src/util/string-prefixes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { StringRecordId, Uuid } from "../data";

export function s(
string: string[] | TemplateStringsArray,
...values: unknown[]
): string {
return string.reduce(
(prev, curr, i) => `${prev}${curr}${values[i] ?? ""}`,
"",
);
}

export function d(
string: string[] | TemplateStringsArray,
...values: unknown[]
): Date {
return new Date(s(string, values));
}

export function r(
string: string[] | TemplateStringsArray,
...values: unknown[]
): StringRecordId {
return new StringRecordId(s(string, values));
}

export function u(
string: string[] | TemplateStringsArray,
...values: unknown[]
): Uuid {
return new Uuid(s(string, values));
}
26 changes: 26 additions & 0 deletions tests/unit/string-prefixes.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { describe, expect, test } from "bun:test";
import { StringRecordId, Uuid, d, r, s, u } from "../../src";

describe("string prefixes", () => {
test("s", () => {
expect(s`Hello World!`).toBe("Hello World!");
expect(s`Hello ${"World"}!`).toBe("Hello World!");
expect(s`Hello ${"World"}! ${123}`).toBe("Hello World! 123");
});

test("d", () => {
expect(d`2024-09-18T13:27:42.050Z`).toMatchObject(
new Date("2024-09-18T13:27:42.050Z"),
);
});

test("r", () => {
expect(r`person:123`).toMatchObject(new StringRecordId("person:123"));
});

test("u", () => {
expect(u`3c467084-4ac4-4938-b26a-13cadf3ab7e9`).toMatchObject(
new Uuid("3c467084-4ac4-4938-b26a-13cadf3ab7e9"),
);
});
});

0 comments on commit e69c7ee

Please sign in to comment.