prostgles-client 4.0.410 → 4.0.412

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/README.md CHANGED
@@ -21,7 +21,7 @@ Isomorphic TypeScript client for PostgreSQL
21
21
  npm/yarn
22
22
 
23
23
  ```bash
24
- $ npm install prostgles-client socket.io-client
24
+ npm install prostgles-client socket.io-client
25
25
  ```
26
26
 
27
27
  CDN
@@ -29,11 +29,11 @@ CDN
29
29
  ```html
30
30
  <head>
31
31
  <script
32
- src="https://unpkg.com/socket.io-client@3.0.3/dist/socket.io.min.js"
32
+ src="https://unpkg.com/socket.io-client@4.8.3/dist/socket.io.min.js"
33
33
  type="text/javascript"
34
34
  ></script>
35
35
  <script
36
- src="https://unpkg.com/prostgles-client@latest/dist/index.js"
36
+ src="https://unpkg.com/prostgles-client@latest/dist/index.umd.js"
37
37
  type="text/javascript"
38
38
  ></script>
39
39
  </head>
@@ -46,7 +46,10 @@ CDN
46
46
  ```ts
47
47
  import prostgles from "prostgles-client";
48
48
 
49
- const prostglesClient = await prostgles({ endpoint, token });
49
+ export const connect = async (endpoint: string, token: string) => {
50
+ const prostglesClient = await prostgles({ endpoint, token });
51
+ return prostglesClient;
52
+ };
50
53
  ```
51
54
 
52
55
  ### React hooks
@@ -54,36 +57,39 @@ const prostglesClient = await prostgles({ endpoint, token });
54
57
  Subscribe to data changes with automatic re-renders:
55
58
 
56
59
  ```tsx
57
- import { useProstglesClient } from "prostgles-client/dist/prostgles";
58
- import type { DBGeneratedSchema } from "@common/DBGeneratedSchema";
60
+ import { defineJoin, useProstglesClient, type TableHandlerClient } from "prostgles-client";
61
+ import type { DBGeneratedSchema } from "./DBGeneratedSchema";
59
62
 
60
- const App = () => {
63
+ export const App = () => {
61
64
  const client = useProstglesClient<DBGeneratedSchema>({ socketOptions: { path: "/ws-api" } });
62
65
 
63
- if(client.isLoading) return "Loading...";
64
- if(client.hasError) return <>Error: {client.error}</>;
66
+ if (client.isLoading) return "Loading...";
67
+ if (client.hasError) return <>Error: {String(client.error)}</>;
68
+ if (!client.db.users) return "Users unavailable";
65
69
 
66
- return <UserPosts dbo={client.dbo} />
67
- }
70
+ return <UserPosts users={client.db.users} />;
71
+ };
68
72
 
69
- const UserPosts = ({ dbo }: { dbo: }) => {
70
-
71
- const { data: user, isLoading } = dbo.users.useSubscribeOne(
73
+ export const UserPosts = ({ users }: { users: TableHandlerClient<DBGeneratedSchema, "users"> }) => {
74
+ const { data: user, isLoading, error } = users.useSubscribeOne(
72
75
  { id: 1 },
73
76
  {
74
77
  select: {
75
78
  id: 1,
76
79
  first_name: 1,
77
80
  email: 1,
78
- latest_posts: {
81
+ latest_posts: defineJoin({
79
82
  $leftJoin: ["posts"],
83
+ select: "*",
80
84
  orderBy: { created: -1 }
81
- }
85
+ })
82
86
  }
83
87
  }
84
88
  );
85
89
 
86
- if(isLoading) return "Loading ..."
90
+ if (isLoading) return "Loading...";
91
+ if (error) return <>Error: {String(error)}</>;
92
+ if (!user) return "User not found";
87
93
 
88
94
  return (
89
95
  <div>
@@ -97,25 +103,7 @@ const UserPosts = ({ dbo }: { dbo: }) => {
97
103
  </ul>
98
104
  </div>
99
105
  );
100
- }
101
- ```
102
-
103
- ### Vanilla JavaScript
104
-
105
- ```js
106
- prostgles({
107
- socket: io(),
108
- onReady: async (db) => {
109
- const latest_posts = await db.posts.find({}, { orderBy: { created: -1 } });
110
-
111
- // Insert data
112
- await dbo.posts.insert({
113
- title: "Hello World",
114
- content: "My first post",
115
- published: true,
116
- });
117
- },
118
- });
106
+ };
119
107
  ```
120
108
 
121
109
  ## Type Safety
@@ -158,32 +146,38 @@ The key difference between **sync** and **subscribe** methods:
158
146
  - **Sync** (sync, syncOne) - Two-way data flow. Allows local optimistic updates that are automatically synced back to the server, while also receiving server updates.
159
147
 
160
148
  ```tsx
161
- // Sync: locally modified data is propagated instantly to the client
162
- const { data: draftPost, isLoading } = dbo.posts.useSyncOne(
163
- { published: false },
164
- { handlesOnData: true },
165
- );
166
- if (isLoading) return "Loading...";
167
- if (!draftPost) {
168
- return <button onClick={() => dbo.posts.insert({ published: false })}>Create new post</button>;
169
- }
170
- return (
171
- <form>
172
- <input
173
- type="text"
174
- placeholder="Title"
175
- value={draftPost.title}
176
- onChange={(e) => draftPost.$update({ title: e.target.value })}
177
- />
178
- <input
179
- type="text"
180
- placeholder="Content"
181
- value={draftPost.content}
182
- onChange={(e) => draftPost.$update({ content: e.target.value })}
183
- />
184
- <button onClick={() => draftPost.$update({ published: true })}>Publish</button>
185
- </form>
186
- );
149
+ import type { TableHandlerClient } from "prostgles-client";
150
+ import type { DBGeneratedSchema } from "./DBGeneratedSchema";
151
+
152
+ export const DraftPost = ({ posts }: { posts: TableHandlerClient<DBGeneratedSchema, "posts"> }) => {
153
+ // Requires sync to be enabled for posts on the server
154
+ const { data: draftPost, isLoading, error } = posts.useSyncOne!(
155
+ { published: false },
156
+ { handlesOnData: true },
157
+ );
158
+ if (isLoading) return "Loading...";
159
+ if (error) return <>Error: {String(error)}</>;
160
+ if (!draftPost) {
161
+ return <button onClick={() => posts.insert({ published: false })}>Create new post</button>;
162
+ }
163
+ return (
164
+ <form>
165
+ <input
166
+ type="text"
167
+ placeholder="Title"
168
+ value={draftPost.title}
169
+ onChange={(e) => draftPost.$update({ title: e.target.value })}
170
+ />
171
+ <input
172
+ type="text"
173
+ placeholder="Content"
174
+ value={draftPost.content}
175
+ onChange={(e) => draftPost.$update({ content: e.target.value })}
176
+ />
177
+ <button type="button" onClick={() => draftPost.$update({ published: true })}>Publish</button>
178
+ </form>
179
+ );
180
+ };
187
181
  ```
188
182
 
189
183
  ## Documentation
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { defineJoin, ROW_ACTIONS_COLUMN, type UserLike } from "prostgles-types";
1
+ import { defineJoin, getActionFunctions, ROW_ACTIONS_COLUMN, type UserLike } from "prostgles-types";
2
2
  import { prostgles as pgls, type InitOptions, type ProstglesInitOptions } from "./prostgles";
3
3
  import type { ClientFunctionHandler } from "./getMethods";
4
4
  declare function prostgles<DBSchema = void, FuncSchema extends ClientFunctionHandler = ClientFunctionHandler, U extends UserLike = UserLike>(params: ProstglesInitOptions<DBSchema, FuncSchema, U>): ReturnType<typeof pgls<DBSchema, FuncSchema, U>>;
@@ -19,5 +19,5 @@ export type { SQLHandler } from "prostgles-types";
19
19
  export { defineJoin };
20
20
  export type { ClientFunctionHandler, FunctionHandle } from "./getMethods";
21
21
  export default prostgles;
22
- export { ROW_ACTIONS_COLUMN };
22
+ export { getActionFunctions, ROW_ACTIONS_COLUMN };
23
23
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../lib/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,kBAAkB,EAAE,KAAK,QAAQ,EAAE,MAAM,iBAAiB,CAAC;AAChF,OAAO,EAAE,SAAS,IAAI,IAAI,EAAE,KAAK,WAAW,EAAE,KAAK,oBAAoB,EAAE,MAAM,aAAa,CAAC;AAC7F,OAAO,KAAK,EAAE,qBAAqB,EAAE,MAAM,cAAc,CAAC;AAE1D,iBAAS,SAAS,CAChB,QAAQ,GAAG,IAAI,EACf,UAAU,SAAS,qBAAqB,GAAG,qBAAqB,EAChE,CAAC,SAAS,QAAQ,GAAG,QAAQ,EAE7B,MAAM,EAAE,oBAAoB,CAAC,QAAQ,EAAE,UAAU,EAAE,CAAC,CAAC,GACpD,UAAU,CAAC,OAAO,IAAI,CAAC,QAAQ,EAAE,UAAU,EAAE,CAAC,CAAC,CAAC,CAAC;AACpD,iBAAS,SAAS,CAChB,QAAQ,GAAG,IAAI,EACf,UAAU,SAAS,qBAAqB,GAAG,qBAAqB,EAChE,CAAC,SAAS,QAAQ,GAAG,QAAQ,EAC7B,MAAM,EAAE,WAAW,CAAC,QAAQ,EAAE,UAAU,EAAE,CAAC,CAAC,GAAG,UAAU,CAAC,OAAO,IAAI,CAAC,QAA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(var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.ROW_ACTIONS_COLUMN = exports.defineJoin = exports.ERR_CODE_MESSAGES = exports.useAuthState = exports.asName = void 0;
17
+ exports.ROW_ACTIONS_COLUMN = exports.getActionFunctions = exports.defineJoin = exports.ERR_CODE_MESSAGES = exports.useAuthState = exports.asName = void 0;
18
18
  const prostgles_types_1 = require("prostgles-types");
19
19
  Object.defineProperty(exports, "defineJoin", { enumerable: true, get: function () { return prostgles_types_1.defineJoin; } });
20
+ Object.defineProperty(exports, "getActionFunctions", { enumerable: true, get: function () { return prostgles_types_1.getActionFunctions; } });
20
21
  Object.defineProperty(exports, "ROW_ACTIONS_COLUMN", { enumerable: true, get: function () { return prostgles_types_1.ROW_ACTIONS_COLUMN; } });
21
22
  const prostgles_1 = require("./prostgles");
22
23
  function prostgles(params) {