From e69c7eebf73bf3984d422337386f2d6639b84fea Mon Sep 17 00:00:00 2001 From: Micha de Vries Date: Wed, 18 Sep 2024 14:41:44 +0100 Subject: [PATCH] Stabilize 1.0.0 (#338) --- package.json | 11 +--- src/index.ts | 1 + src/surreal.ts | 92 ++++++++++++++++++++---------- src/util/string-prefixes.ts | 32 +++++++++++ tests/unit/string-prefixes.test.ts | 26 +++++++++ 5 files changed, 124 insertions(+), 38 deletions(-) create mode 100644 src/util/string-prefixes.ts create mode 100644 tests/unit/string-prefixes.test.ts diff --git a/package.json b/package.json index 71593805..9fe2ce81 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "surrealdb", - "version": "1.0.0-beta.21", + "version": "1.0.0", "type": "module", "license": "Apache-2.0", "repository": { @@ -46,10 +46,5 @@ "browser": "./dist/index.bundled.mjs" } }, - "files": [ - "dist", - "README.md", - "LICENCE", - "SECURITY.md" - ] -} \ No newline at end of file + "files": ["dist", "README.md", "LICENCE", "SECURITY.md"] +} diff --git a/src/index.ts b/src/index.ts index 818283c1..d386aaab 100644 --- a/src/index.ts +++ b/src/index.ts @@ -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, diff --git a/src/surreal.ts b/src/surreal.ts index e2c987be..f40a2a89 100644 --- a/src/surreal.ts +++ b/src/surreal.ts @@ -1,6 +1,6 @@ import { type StringRecordId, - type Table, + Table, type Uuid, type RecordId as _RecordId, decodeCbor, @@ -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(thing: Table | string): Promise[]>; async select(thing: RecordId): Promise>; + async select(thing: Table | string): Promise[]>; async select(thing: RecordId | Table | string) { await this.ready; const res = await this.rpc>("select", [thing]); @@ -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( - thing: Table | string, - data?: U, - ): Promise[]>; async create( thing: RecordId, data?: U, ): Promise>; + async create( + thing: Table | string, + data?: U, + ): Promise[]>; async create( thing: RecordId | Table | string, data?: U, @@ -480,19 +480,51 @@ export class Surreal { * @param data - The document(s) / record(s) to insert. */ async insert( - thing: Table | string, data?: U | U[], ): Promise[]>; async insert( - thing: RecordId, - data?: U, - ): Promise>; + table: Table | string, + data?: U | U[], + ): Promise[]>; async insert( - 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>("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( + data?: U | U[], + ): Promise[]>; + async insert_relation( + table: Table | string, data?: U | U[], + ): Promise[]>; + async insert_relation( + arg1: Table | string | U | U[], + arg2?: U | U[], ) { await this.ready; - const res = await this.rpc>("insert", [thing, data]); + const [table, data] = + typeof arg1 === "string" || arg1 instanceof Table + ? [arg1, arg2] + : [undefined, arg1]; + const res = await this.rpc>("insert_relation", [ + table, + data, + ]); if (res.error) throw new ResponseError(res.error.message); return res.result; } @@ -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( - thing: Table | string, - data?: U, - ): Promise[]>; async update( thing: RecordId, data?: U, ): Promise>; + async update( + thing: Table | string, + data?: U, + ): Promise[]>; async update( thing: RecordId | Table | string, data?: U, @@ -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( - thing: Table | string, - data?: U, - ): Promise[]>; async upsert( thing: RecordId, data?: U, ): Promise>; + async upsert( + thing: Table | string, + data?: U, + ): Promise[]>; async upsert( thing: RecordId | Table | string, data?: U, @@ -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>( - thing: Table | string, - data?: U, - ): Promise[]>; async merge>( thing: RecordId, data?: U, ): Promise>; + async merge>( + thing: Table | string, + data?: U, + ): Promise[]>; async merge>( thing: RecordId | Table | string, data?: U, @@ -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(thing: Table | string): Promise[]>; async delete(thing: RecordId): Promise>; + async delete(thing: Table | string): Promise[]>; async delete(thing: RecordId | Table | string) { await this.ready; const res = await this.rpc>("delete", [thing]); @@ -669,16 +701,16 @@ export class Surreal { */ async relate( from: string | RecordId | RecordId[], - thing: string, + thing: RecordId, to: string | RecordId | RecordId[], data?: U, - ): Promise; + ): Promise; async relate( from: string | RecordId | RecordId[], - thing: RecordId, + thing: string, to: string | RecordId | RecordId[], data?: U, - ): Promise; + ): Promise; async relate( from: string | RecordId | RecordId[], thing: string | RecordId, @@ -696,7 +728,7 @@ export class Surreal { * @param method - Type of message to send. * @param params - Parameters for the message. */ - protected rpc( + public rpc( method: string, params?: unknown[], ): Promise> { diff --git a/src/util/string-prefixes.ts b/src/util/string-prefixes.ts new file mode 100644 index 00000000..21bd4b60 --- /dev/null +++ b/src/util/string-prefixes.ts @@ -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)); +} diff --git a/tests/unit/string-prefixes.test.ts b/tests/unit/string-prefixes.test.ts new file mode 100644 index 00000000..5f3b812d --- /dev/null +++ b/tests/unit/string-prefixes.test.ts @@ -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"), + ); + }); +});