From a93f72ca313951e73328b778f4198a01822ef3e5 Mon Sep 17 00:00:00 2001 From: "soya.miruku" Date: Thu, 23 Nov 2023 10:14:36 +0000 Subject: [PATCH] added type on websocket param return type --- compile.ts | 1 + project.json | 2 +- src/client.ts | 10 ++++----- src/index.ts | 61 ++++++++++++++++++++++++++++++++++++++++----------- src/query.ts | 2 +- src/types.ts | 2 +- 6 files changed, 57 insertions(+), 21 deletions(-) diff --git a/compile.ts b/compile.ts index 2a80f77..2580270 100644 --- a/compile.ts +++ b/compile.ts @@ -34,6 +34,7 @@ await build({ "ws": "^8.13.0", }, devDependencies: { + "esbuild": "latest", "@types/node": "^18.7.18", "@types/ws": "8.5.3", "type-fest": "latest" diff --git a/project.json b/project.json index c067609..90644ff 100644 --- a/project.json +++ b/project.json @@ -1,4 +1,4 @@ { "name": "@soyamiruku/typed-surql", - "version": "0.0.2" + "version": "1.0.15" } \ No newline at end of file diff --git a/src/client.ts b/src/client.ts index add24a9..0e1767d 100644 --- a/src/client.ts +++ b/src/client.ts @@ -1,9 +1,9 @@ import "npm:reflect-metadata@latest"; -import { Class, Constructor } from "npm:type-fest@latest"; +import { Class, Constructor } from "npm:type-fest"; import { IFieldParams, ITable, Idx } from "./decerators.ts"; import { AsBasicModel, CreateInput, IModel, LengthGreaterThanOne, ModelKeysDot, OnlyFields, TransformSelected, UnionToArray } from "./types.ts"; -import { STRATEGY, TypedSurQL } from "./index.ts"; +import { TypedSurQL } from "./index.ts"; import { Surreal } from "https://deno.land/x/surrealdb/mod.ts"; import { field, val, ql, SQLType, Instance, FnBody, queryModel } from "./query.ts"; import { ActionResult, LiveQueryResponse, Patch } from "./surreal-types.ts"; @@ -85,7 +85,7 @@ export class Model implements IModel { public static async live(this: { new(): SubModel }, callback?: (data: LiveQueryResponse>) => unknown, diff?: boolean): Promise { const instance = new this(); - if (STRATEGY === "HTTP") throw new Error("Live queries are not supported in HTTP mode"); + if (TypedSurQL.STRATEGY === "HTTP") throw new Error("Live queries are not supported in HTTP mode"); return (TypedSurQL.SurrealDB as Surreal).live>>(instance.tableName, callback as any, diff); } @@ -180,7 +180,7 @@ export class Model implements IModel { if (!transformedData) { throw new Error("transformedData is undefined"); } - if (STRATEGY === "HTTP") { + if (TypedSurQL.STRATEGY === "HTTP") { if (Array.isArray(transformedData)) { if (!transformedData.length) return []; return await TypedSurQL.SurrealDB.query(`INSERT INTO ${instance.tableName} ${JSON.stringify(transformedData)}`) @@ -204,7 +204,7 @@ export class Model implements IModel { public static async patch(this: { new(): SubModel }, data?: Patch[] | undefined): Promise { const instance = new this(); - if (STRATEGY === "HTTP") throw new Error("Patch queries are not supported in HTTP mode") + if (TypedSurQL.STRATEGY === "HTTP") throw new Error("Patch queries are not supported in HTTP mode") return await (TypedSurQL.SurrealDB as Surreal).patch(instance.tableName, data); } diff --git a/src/index.ts b/src/index.ts index ba75adc..d32966e 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,25 +1,60 @@ import "npm:reflect-metadata@latest"; import "npm:core-js@latest"; import { ExperimentalSurrealHTTP, Surreal } from "https://deno.land/x/surrealdb/mod.ts"; -import { AsyncReturnType } from "npm:type-fest@latest"; +import { AsyncReturnType } from "npm:type-fest"; import { ConnectionOptions } from "./surreal-types.ts"; -export let STRATEGY: "HTTP" | "WS" = "HTTP"; +export type StrategyType = "HTTP" | "WS"; +export type SurrealClient = AsyncReturnType; +export type SurrealStratClient = Strategy extends "WS" ? Surreal : ExperimentalSurrealHTTP; -export class TypedSurQL { +export class TypedSurQL { public static SurrealDB: Surreal; + public static STRATEGY: StrategyType = "WS"; - public static Init(url: string, opts?: ConnectionOptions & { websocket?: boolean }) { - if (!url) throw new Error("URL is required"); - if (opts?.websocket === undefined || opts?.websocket === true) - STRATEGY = "WS"; + constructor(public readonly SurrealDB: SurrealStratClient) { } - return new Promise((resolve, reject) => { - const db = STRATEGY === "WS" ? new Surreal() : new ExperimentalSurrealHTTP(); + public static Init(url: string, opts?: ConnectionOptions & { websocket?: Strategy }) { + if (!url) throw new Error("URL is required"); + this.SetStrategy(opts?.websocket); + return new Promise>((resolve, reject) => { + const db = TypedSurQL.STRATEGY === "WS" ? new Surreal() : new ExperimentalSurrealHTTP(); try { void db.connect(url, opts as any).then(() => { TypedSurQL.SurrealDB = db as Surreal; - resolve(db) + resolve(db as SurrealStratClient) + }); + } catch (e) { + reject(e) + } + }) + } + + public promisify() { + return new Promise>((resolve, reject) => { + try { + resolve(this.SurrealDB) + } catch (e) { + reject(e) + } + }) + } + + public static SetStrategy(strategy?: Strategy) { + if (strategy === undefined || strategy === true) + this.STRATEGY = "WS"; + else this.STRATEGY = "HTTP"; + } + + public static Create(url: string, opts?: ConnectionOptions & { websocket?: Strategy }) { + if (!url) throw new Error("URL is required"); + this.SetStrategy(opts?.websocket); + + return new Promise>((resolve, reject) => { + const db = this.STRATEGY === "WS" ? new Surreal() : new ExperimentalSurrealHTTP(); + try { + void db.connect(url, opts as any).then(() => { + resolve(new TypedSurQL(db as SurrealStratClient)) }); } catch (e) { reject(e) @@ -30,14 +65,14 @@ export class TypedSurQL { public static async Wait(iterations = 5): Promise { if (iterations === 0) return false; if (!TypedSurQL.SurrealDB) { - await new Promise((resolve) => setTimeout(resolve, 100)); + await new Promise((resolve) => setTimeout(resolve, 150)); return TypedSurQL.Wait(iterations - 1); } - return await TypedSurQL.SurrealDB.wait().then(() => true); + return true; + // return await TypedSurQL.SurrealDB.wait().then(() => true); } } -export type SurrealClient = AsyncReturnType; export * from "./decerators.ts"; export type * from "./types.ts"; diff --git a/src/query.ts b/src/query.ts index 9dfe7e7..af8baf8 100644 --- a/src/query.ts +++ b/src/query.ts @@ -1,4 +1,4 @@ -import { Constructor, Simplify } from "npm:type-fest@latest"; +import { Constructor, Simplify } from "npm:type-fest"; import { qlFn } from "./functions/index.ts"; import { DotNestedKeys, IModel, OnlyFields } from "./types.ts"; import { alias, arrays, count, cryptos, durations, http, math, meta, operations, parse, rands, search, session, strings, time } from "./functions/mod.ts"; diff --git a/src/types.ts b/src/types.ts index 3936176..db64d9b 100644 --- a/src/types.ts +++ b/src/types.ts @@ -1,4 +1,4 @@ -import { ConditionalExcept, ConditionalPick, ConditionalPickDeepOptions, EmptyObject, Except, IsEqual, Merge, ReadonlyKeysOf, Simplify, UnknownRecord } from "npm:type-fest@latest"; +import { ConditionalExcept, ConditionalPick, ConditionalPickDeepOptions, EmptyObject, Except, IsEqual, Merge, ReadonlyKeysOf, Simplify, UnknownRecord } from "npm:type-fest"; /** Simplifies a type while including and/or excluding certain types from being simplified. Useful to improve type hints shown in editors. And also to transform an interface into a type to aide with assignability.