prostgles-client 4.0.76 → 4.0.78

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/prostgles.ts CHANGED
@@ -5,26 +5,42 @@
5
5
  *--------------------------------------------------------------------------------------------*/
6
6
 
7
7
  import {
8
- TableHandler, DbJoinMaker,
9
- CHANNELS, DBNotifConfig,
10
- DBNoticeConfig, AnyObject, SubscriptionHandler,
11
- SQLHandler, DBEventHandles, AuthGuardLocation, DBSchemaTable,
12
- AuthGuardLocationResponse, MethodHandler, ClientSyncHandles, UpdateParams, DeleteParams, ClientSchema, SQLResult, DBSchema, ViewHandler,
13
- asName,
14
- SubscriptionChannels,
8
+ AnyObject,
9
+ AuthGuardLocation,
10
+ AuthGuardLocationResponse,
11
+ CHANNELS,
12
+ ClientSchema,
13
+ ClientSyncHandles,
14
+ DBEventHandles,
15
+ DBNoticeConfig,
16
+ DBNotifConfig,
17
+ DBSchema,
18
+ DBSchemaTable,
19
+ DbJoinMaker,
20
+ DeleteParams,
21
+ EqualityFilter,
15
22
  FullFilter,
16
- SubscribeParams,
17
- OnError,
18
23
  GetSelectReturnType,
19
- getKeys,
20
- getJoinHandlers,
24
+ MethodHandler,
25
+ OnError,
26
+ SQLHandler,
27
+ SQLResult,
28
+ SelectParams,
21
29
  SocketSQLStreamClient,
22
30
  SocketSQLStreamServer,
23
- SelectParams
31
+ SubscribeParams,
32
+ SubscriptionChannels,
33
+ SubscriptionHandler,
34
+ TableHandler,
35
+ UpdateParams,
36
+ ViewHandler,
37
+ asName,
38
+ getJoinHandlers,
39
+ getKeys
24
40
  } from "prostgles-types";
25
41
 
26
- import { SyncedTable, type DbTableSync, type Sync, type SyncOne } from "./SyncedTable";
27
- import { getReact, useAsyncEffectQueue, useIsMounted, usePromise, useSubscribe } from "./react-hooks";
42
+ import { SyncDataItem, SyncOptions, SyncedTable, type DbTableSync, type Sync, type SyncOne, SyncOneOptions } from "./SyncedTable";
43
+ import { getReact, useAsyncEffectQueue, useIsMounted, usePromise, useSubscribe, useSubscribeV2, useSync } from "./react-hooks";
28
44
 
29
45
  const DEBUG_KEY = "DEBUG_SYNCEDTABLE";
30
46
  const hasWnd = typeof window !== "undefined";
@@ -36,7 +52,7 @@ export const debug: any = function (...args: any[]) {
36
52
 
37
53
  export { MethodHandler, SQLResult, asName };
38
54
 
39
- export * from "./react-hooks";
55
+ export * from "./react-hooks";
40
56
 
41
57
  type OnReadyParams<DBSchema> = {
42
58
  dbo: DBHandlerClient<DBSchema>;
@@ -45,7 +61,8 @@ type OnReadyParams<DBSchema> = {
45
61
  auth: Auth | undefined;
46
62
  isReconnect: boolean;
47
63
  }
48
- export const useProstglesClient = <DBSchema>(initOpts: InitOptions<DBSchema>): undefined | OnReadyParams<DBSchema> => {
64
+ type HookInitOpts = Omit<InitOptions<DBSchema>, "onReady"> & Pick<Partial<InitOptions<DBSchema>>, "onReady">;
65
+ export const useProstglesClient = <DBSchema>(initOpts: HookInitOpts): undefined | OnReadyParams<DBSchema> => {
49
66
  const React = getReact(true);
50
67
  const [onReadyArgs, setOnReadyArgs] = React.useState<undefined | OnReadyParams<DBSchema>>();
51
68
  const getIsMounted = useIsMounted();
@@ -72,7 +89,23 @@ export type ViewHandlerClient<T extends AnyObject = AnyObject, S extends DBSchem
72
89
  _syncInfo?: any;
73
90
  getSync?: any;
74
91
  sync?: Sync<T>;
92
+ useSync?: (
93
+ basicFilter: EqualityFilter<T>,
94
+ syncOptions: SyncOptions,
95
+ ) => {
96
+ data: undefined | SyncDataItem<Required<T>>[];
97
+ isLoading: boolean;
98
+ error?: any;
99
+ };
75
100
  syncOne?: SyncOne<T>;
101
+ useSyncOne?: (
102
+ basicFilter: EqualityFilter<T>,
103
+ syncOptions: SyncOneOptions,
104
+ ) => {
105
+ data: undefined | SyncDataItem<Required<T>>;
106
+ isLoading: boolean;
107
+ error?: any;
108
+ };
76
109
  _sync?: any;
77
110
  /**
78
111
  * Will return undefined while loading
@@ -90,6 +123,28 @@ export type ViewHandlerClient<T extends AnyObject = AnyObject, S extends DBSchem
90
123
  options?: SubParams,
91
124
  onError?: OnError
92
125
  ) => GetSelectReturnType<S, SubParams, T, false> | undefined;
126
+ /**
127
+ * Will return undefined while loading
128
+ */
129
+ useSubscribeV2: <SubParams extends SubscribeParams<T, S>>(
130
+ filter?: FullFilter<T, S>,
131
+ options?: SubParams,
132
+ ) => {
133
+ data: GetSelectReturnType<S, SubParams, T, false>[] | undefined;
134
+ error?: any;
135
+ isLoading: boolean;
136
+ }
137
+ /**
138
+ * Will return undefined while loading
139
+ */
140
+ useSubscribeOneV2: <SubParams extends SubscribeParams<T, S>>(
141
+ filter?: FullFilter<T, S>,
142
+ options?: SubParams,
143
+ ) => {
144
+ data: GetSelectReturnType<S, SubParams, T, false> | undefined;
145
+ error?: any;
146
+ isLoading: boolean;
147
+ };
93
148
  useFind: <P extends SelectParams<T, S>>(filter?: FullFilter<T, S>, selectParams?: P) => undefined | GetSelectReturnType<S, P, T, true>;
94
149
  useFindOne: <P extends SelectParams<T, S>>(filter?: FullFilter<T, S>, selectParams?: P) => undefined | GetSelectReturnType<S, P, T, false>;
95
150
  useCount: <P extends SelectParams<T, S>>(filter?: FullFilter<T, S>, selectParams?: P) => number | undefined;
@@ -108,7 +163,6 @@ export type TableHandlerClient<T extends AnyObject = AnyObject, S extends DBSche
108
163
  _sync?: any;
109
164
  }
110
165
 
111
-
112
166
  export type DBHandlerClient<Schema = void> = (Schema extends DBSchema ? {
113
167
  [tov_name in keyof Schema]: Schema[tov_name]["is_view"] extends true ?
114
168
  ViewHandlerClient<Schema[tov_name]["columns"], Schema> :
@@ -872,18 +926,21 @@ export function prostgles<DBSchema>(initOpts: InitOptions<DBSchema>, syncedTable
872
926
  }
873
927
  return syncedTables[syncName]
874
928
  }
875
- dboTable.sync = async (basicFilter, options = { handlesOnData: true, select: "*" }, onChange, onError) => {
929
+ const sync: Sync<AnyObject> = async (basicFilter, options = { handlesOnData: true, select: "*" }, onChange, onError) => {
876
930
  await onDebug?.({ type: "table", command: "sync", tableName, data: { basicFilter, options } });
877
931
  checkSubscriptionArgs(basicFilter, options, onChange, onError);
878
932
  const s = await upsertSTable(basicFilter, options, onError);
879
933
  return await s.sync(onChange, options.handlesOnData);
880
934
  }
881
- dboTable.syncOne = async (basicFilter, options = { handlesOnData: true }, onChange, onError) => {
935
+ const syncOne: SyncOne<AnyObject> = async (basicFilter, options = { handlesOnData: true }, onChange, onError) => {
882
936
  await onDebug?.({ type: "table", command: "syncOne", tableName, data: { basicFilter, options } });
883
937
  checkSubscriptionArgs(basicFilter, options, onChange, onError);
884
938
  const s = await upsertSTable(basicFilter, options, onError);
885
939
  return await s.syncOne(basicFilter, onChange, options.handlesOnData);
886
940
  }
941
+ dboTable.sync = sync;
942
+ dboTable.syncOne = syncOne;
943
+ dboTable.useSync = (basicFilter, options) => useSync(dboTable.sync!, basicFilter, options) as any;
887
944
  }
888
945
 
889
946
  dboTable._sync = async function (param1, param2, syncHandles) {
@@ -914,6 +971,7 @@ export function prostgles<DBSchema>(initOpts: InitOptions<DBSchema>, syncedTable
914
971
  const handlerName = command === "subscribe" ? "useSubscribe" : command === "subscribeOne"? "useSubscribeOne" : undefined;
915
972
  if(handlerName){
916
973
  dboTable[handlerName] = (...args) => useSubscribe(startHook(...args) as any)
974
+ dboTable[handlerName + "v2"] = (filter, options) => useSubscribeV2(subFunc, filter, options)
917
975
  }
918
976
 
919
977
  if (command === SUBONE || !sub_commands.includes(SUBONE)) {
@@ -1,4 +1,5 @@
1
- import { SubscriptionHandler, getKeys, isObject } from "prostgles-types";
1
+ import { AnyObject, SubscriptionHandler, getKeys, isObject } from "prostgles-types";
2
+ import { TableHandlerClient } from "./prostgles";
2
3
  let React: typeof import("react") | undefined;
3
4
 
4
5
 
@@ -14,12 +15,12 @@ export const getReact = (throwError?: boolean): typeof import("react") => {
14
15
  return React as any;
15
16
  };
16
17
  getReact();
17
- const { useEffect = alertNoReact, useCallback = alertNoReact, useRef = alertNoReactT, useState = alertNoReactT } = React ?? {};
18
+ const { useEffect = alertNoReact, useCallback = alertNoReact, useRef, useState = alertNoReactT } = React! ?? {};
18
19
 
19
20
  export const isEqual = function (x, y) {
20
21
  if (x === y) {
21
22
  return true;
22
-
23
+
23
24
  } else if ((typeof x == "object" && x != null) && (typeof y == "object" && y != null)) {
24
25
  if (Object.keys(x).length != Object.keys(y).length){
25
26
  return false;
@@ -106,7 +107,7 @@ export const useAsyncEffectQueue = (effect: () => Promise<void | (() => void)>,
106
107
  }
107
108
  }
108
109
  const cleanupActiveEffect = async () => {
109
- await queue.current.activeEffect.resolvedCleanup.run?.();
110
+ await queue.current.activeEffect?.resolvedCleanup?.run?.();
110
111
  queue.current.activeEffect = undefined;
111
112
  runAsyncEffect(queue)
112
113
  }
@@ -233,6 +234,63 @@ export const useSubscribe = <SubHook extends ReturnType<SubHooks>>(
233
234
  return data;
234
235
  }
235
236
 
237
+ type HookResult =
238
+ | { data: any; error?: undefined; isLoading: false; }
239
+ | { data?: undefined; error: any; isLoading: false; }
240
+ | { data?: undefined; error?: undefined; isLoading: true; };
241
+
242
+ export const useSubscribeV2 = (
243
+ subFunc: (filter: any, options: any, onData: any, onError: any) => Promise<SubscriptionHandler>,
244
+ filter: any,
245
+ options: any
246
+ ) => {
247
+ const [{ data, error, isLoading }, setResult] = useState<HookResult>({ data: undefined, error: undefined, isLoading: true });
248
+
249
+ const getIsMounted = useIsMounted();
250
+ useAsyncEffectQueue(async () => {
251
+ const sub = await subFunc(
252
+ filter,
253
+ options,
254
+ newData => {
255
+ if (!getIsMounted()) return;
256
+ setResult({ data: newData, error: undefined });
257
+ },
258
+ newError => {
259
+ if (!getIsMounted()) return;
260
+ setResult({ data: undefined, error: newError });
261
+ }
262
+ );
263
+
264
+ return sub.unsubscribe;
265
+ }, [subFunc, filter, options]);
266
+
267
+ return { data, error, isLoading };
268
+ }
269
+
270
+ export const useSync = (
271
+ sync: Required<TableHandlerClient>["sync"] | Required<TableHandlerClient>["syncOne"],
272
+ basicFilter: Parameters<Required<TableHandlerClient>["sync"]>[0],
273
+ syncOptions: Parameters<Required<TableHandlerClient>["sync"]>[1],
274
+ ) => {
275
+ const [{ data, error, isLoading }, setResult] = useState<HookResult>({ data: undefined, error: undefined, isLoading: true });
276
+ const getIsMounted = useIsMounted();
277
+ useAsyncEffectQueue(async () => {
278
+ const syncHandlers = await sync(
279
+ basicFilter,
280
+ syncOptions,
281
+ newData => {
282
+ if (!getIsMounted()) return;
283
+ setResult({ data: newData, error: undefined, isLoading: false });
284
+ },
285
+ newError => {
286
+ if (!getIsMounted()) return;
287
+ setResult({ data: undefined, error: newError, isLoading: false });
288
+ }
289
+ );
290
+ return syncHandlers.$unsync();
291
+ }, [sync, basicFilter, syncOptions]);
292
+ return { data, error, isLoading };
293
+ }
236
294
 
237
295
  export const __prglReactInstalled = () => Boolean(React && useRef);
238
296
 
package/lib/typeTests.ts CHANGED
@@ -1,5 +1,5 @@
1
- import { AnyObject, DBSchema, FullFilter, Select, TableHandler } from "prostgles-types";
2
- import { DBHandlerClient, TableHandlerClient } from "./prostgles";
1
+ import { AnyObject, FullFilter, TableHandler } from "prostgles-types";
2
+ import { DBHandlerClient, useProstglesClient } from "./prostgles";
3
3
 
4
4
  (async () => {
5
5
  // const schema: DBSchema = {
@@ -16,6 +16,8 @@ import { DBHandlerClient, TableHandlerClient } from "./prostgles";
16
16
  }
17
17
  };
18
18
 
19
+ const client = useProstglesClient<GeneratedSchema>({ socket: 1 as any })
20
+ const t1 = client?.dbo.table1.useFind({ }, { orderBy: { col1: 1 } });
19
21
  const dbo: DBHandlerClient<GeneratedSchema> = 1 as any;
20
22
 
21
23
  const filter: FullFilter<GeneratedSchema["table1"]["columns"], GeneratedSchema> = { };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "prostgles-client",
3
- "version": "4.0.76",
3
+ "version": "4.0.78",
4
4
  "description": "Reactive client for Postgres",
5
5
  "main": "dist/prostgles-full.js",
6
6
  "types": "dist/prostgles-full.d.ts",
@@ -28,7 +28,7 @@
28
28
  "pushpublish": "npm version patch --git-tag-version false && git push && npm publish"
29
29
  },
30
30
  "dependencies": {
31
- "prostgles-types": "^4.0.71"
31
+ "prostgles-types": "^4.0.72"
32
32
  },
33
33
  "devDependencies": {
34
34
  "@types/node": "^14.14.14",
@@ -17,10 +17,10 @@
17
17
  },
18
18
  "..": {
19
19
  "name": "prostgles-client",
20
- "version": "4.0.75",
20
+ "version": "4.0.77",
21
21
  "license": "MIT",
22
22
  "dependencies": {
23
- "prostgles-types": "^4.0.71"
23
+ "prostgles-types": "^4.0.72"
24
24
  },
25
25
  "devDependencies": {
26
26
  "@types/node": "^14.14.14",