@sidekickdb/platform 0.0.0 → 2.0.1
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/.tshy/build.json +8 -0
- package/.tshy/commonjs.json +16 -0
- package/.tshy/esm.json +15 -0
- package/.turbo/turbo-build.log +50 -0
- package/LICENSE.md +202 -0
- package/NOTICE +27 -0
- package/README.md +444 -2
- package/__tests__/src/apiURI.test.ts +58 -0
- package/__tests__/src/backupDownload.test.ts +427 -0
- package/__tests__/src/backups.test.ts +168 -0
- package/__tests__/src/clerk.test.ts +13 -0
- package/__tests__/src/consts.test.ts +11 -0
- package/__tests__/src/migrations.test.ts +885 -0
- package/__tests__/src/perms.test.ts +102 -0
- package/__tests__/src/rename.test.ts +126 -0
- package/__tests__/src/schema.push.e2e.test.ts +186 -0
- package/__tests__/src/schema.test.ts +644 -0
- package/__tests__/src/typescript-schema.test.ts +117 -0
- package/dist/commonjs/ProgressPromise.d.ts +18 -0
- package/dist/commonjs/ProgressPromise.d.ts.map +1 -0
- package/dist/commonjs/ProgressPromise.js +77 -0
- package/dist/commonjs/ProgressPromise.js.map +1 -0
- package/dist/commonjs/api.d.ts +480 -0
- package/dist/commonjs/api.d.ts.map +1 -0
- package/dist/commonjs/api.js +1132 -0
- package/dist/commonjs/api.js.map +1 -0
- package/dist/commonjs/backupDownload.d.ts +89 -0
- package/dist/commonjs/backupDownload.d.ts.map +1 -0
- package/dist/commonjs/backupDownload.js +357 -0
- package/dist/commonjs/backupDownload.js.map +1 -0
- package/dist/commonjs/backups.d.ts +146 -0
- package/dist/commonjs/backups.d.ts.map +1 -0
- package/dist/commonjs/backups.js +248 -0
- package/dist/commonjs/backups.js.map +1 -0
- package/dist/commonjs/clerk.d.ts +2 -0
- package/dist/commonjs/clerk.d.ts.map +1 -0
- package/dist/commonjs/clerk.js +29 -0
- package/dist/commonjs/clerk.js.map +1 -0
- package/dist/commonjs/consts.d.ts +11 -0
- package/dist/commonjs/consts.d.ts.map +1 -0
- package/dist/commonjs/consts.js +17 -0
- package/dist/commonjs/consts.js.map +1 -0
- package/dist/commonjs/crypto.d.ts +4 -0
- package/dist/commonjs/crypto.d.ts.map +1 -0
- package/dist/commonjs/crypto.js +26 -0
- package/dist/commonjs/crypto.js.map +1 -0
- package/dist/commonjs/index.d.ts +18 -0
- package/dist/commonjs/index.d.ts.map +1 -0
- package/dist/commonjs/index.js +61 -0
- package/dist/commonjs/index.js.map +1 -0
- package/dist/commonjs/migrationUtils.d.ts +4 -0
- package/dist/commonjs/migrationUtils.d.ts.map +1 -0
- package/dist/commonjs/migrationUtils.js +52 -0
- package/dist/commonjs/migrationUtils.js.map +1 -0
- package/dist/commonjs/migrations.d.ts +99 -0
- package/dist/commonjs/migrations.d.ts.map +1 -0
- package/dist/commonjs/migrations.js +597 -0
- package/dist/commonjs/migrations.js.map +1 -0
- package/dist/commonjs/oauth.d.ts +98 -0
- package/dist/commonjs/oauth.d.ts.map +1 -0
- package/dist/commonjs/oauth.js +277 -0
- package/dist/commonjs/oauth.js.map +1 -0
- package/dist/commonjs/oauthCommon.d.ts +12 -0
- package/dist/commonjs/oauthCommon.d.ts.map +1 -0
- package/dist/commonjs/oauthCommon.js +26 -0
- package/dist/commonjs/oauthCommon.js.map +1 -0
- package/dist/commonjs/package.json +3 -0
- package/dist/commonjs/perms.d.ts +3 -0
- package/dist/commonjs/perms.d.ts.map +1 -0
- package/dist/commonjs/perms.js +66 -0
- package/dist/commonjs/perms.js.map +1 -0
- package/dist/commonjs/relationships.d.ts +7 -0
- package/dist/commonjs/relationships.d.ts.map +1 -0
- package/dist/commonjs/relationships.js +46 -0
- package/dist/commonjs/relationships.js.map +1 -0
- package/dist/commonjs/schema.d.ts +161 -0
- package/dist/commonjs/schema.d.ts.map +1 -0
- package/dist/commonjs/schema.js +174 -0
- package/dist/commonjs/schema.js.map +1 -0
- package/dist/commonjs/serverOAuth.d.ts +25 -0
- package/dist/commonjs/serverOAuth.d.ts.map +1 -0
- package/dist/commonjs/serverOAuth.js +72 -0
- package/dist/commonjs/serverOAuth.js.map +1 -0
- package/dist/commonjs/typescript-schema.d.ts +17 -0
- package/dist/commonjs/typescript-schema.d.ts.map +1 -0
- package/dist/commonjs/typescript-schema.js +314 -0
- package/dist/commonjs/typescript-schema.js.map +1 -0
- package/dist/commonjs/util.d.ts +13 -0
- package/dist/commonjs/util.d.ts.map +1 -0
- package/dist/commonjs/util.js +55 -0
- package/dist/commonjs/util.js.map +1 -0
- package/dist/commonjs/version.d.ts +3 -0
- package/dist/commonjs/version.d.ts.map +1 -0
- package/dist/commonjs/version.js +5 -0
- package/dist/commonjs/version.js.map +1 -0
- package/dist/esm/ProgressPromise.d.ts +18 -0
- package/dist/esm/ProgressPromise.d.ts.map +1 -0
- package/dist/esm/ProgressPromise.js +73 -0
- package/dist/esm/ProgressPromise.js.map +1 -0
- package/dist/esm/api.d.ts +480 -0
- package/dist/esm/api.d.ts.map +1 -0
- package/dist/esm/api.js +1121 -0
- package/dist/esm/api.js.map +1 -0
- package/dist/esm/backupDownload.d.ts +89 -0
- package/dist/esm/backupDownload.d.ts.map +1 -0
- package/dist/esm/backupDownload.js +354 -0
- package/dist/esm/backupDownload.js.map +1 -0
- package/dist/esm/backups.d.ts +146 -0
- package/dist/esm/backups.d.ts.map +1 -0
- package/dist/esm/backups.js +237 -0
- package/dist/esm/backups.js.map +1 -0
- package/dist/esm/clerk.d.ts +2 -0
- package/dist/esm/clerk.d.ts.map +1 -0
- package/dist/esm/clerk.js +26 -0
- package/dist/esm/clerk.js.map +1 -0
- package/dist/esm/consts.d.ts +11 -0
- package/dist/esm/consts.d.ts.map +1 -0
- package/dist/esm/consts.js +13 -0
- package/dist/esm/consts.js.map +1 -0
- package/dist/esm/crypto.d.ts +4 -0
- package/dist/esm/crypto.d.ts.map +1 -0
- package/dist/esm/crypto.js +21 -0
- package/dist/esm/crypto.js.map +1 -0
- package/dist/esm/index.d.ts +18 -0
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +17 -0
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/migrationUtils.d.ts +4 -0
- package/dist/esm/migrationUtils.d.ts.map +1 -0
- package/dist/esm/migrationUtils.js +47 -0
- package/dist/esm/migrationUtils.js.map +1 -0
- package/dist/esm/migrations.d.ts +99 -0
- package/dist/esm/migrations.d.ts.map +1 -0
- package/dist/esm/migrations.js +588 -0
- package/dist/esm/migrations.js.map +1 -0
- package/dist/esm/oauth.d.ts +98 -0
- package/dist/esm/oauth.d.ts.map +1 -0
- package/dist/esm/oauth.js +268 -0
- package/dist/esm/oauth.js.map +1 -0
- package/dist/esm/oauthCommon.d.ts +12 -0
- package/dist/esm/oauthCommon.d.ts.map +1 -0
- package/dist/esm/oauthCommon.js +22 -0
- package/dist/esm/oauthCommon.js.map +1 -0
- package/dist/esm/package.json +3 -0
- package/dist/esm/perms.d.ts +3 -0
- package/dist/esm/perms.d.ts.map +1 -0
- package/dist/esm/perms.js +62 -0
- package/dist/esm/perms.js.map +1 -0
- package/dist/esm/relationships.d.ts +7 -0
- package/dist/esm/relationships.d.ts.map +1 -0
- package/dist/esm/relationships.js +43 -0
- package/dist/esm/relationships.js.map +1 -0
- package/dist/esm/schema.d.ts +161 -0
- package/dist/esm/schema.d.ts.map +1 -0
- package/dist/esm/schema.js +158 -0
- package/dist/esm/schema.js.map +1 -0
- package/dist/esm/serverOAuth.d.ts +25 -0
- package/dist/esm/serverOAuth.d.ts.map +1 -0
- package/dist/esm/serverOAuth.js +68 -0
- package/dist/esm/serverOAuth.js.map +1 -0
- package/dist/esm/typescript-schema.d.ts +17 -0
- package/dist/esm/typescript-schema.d.ts.map +1 -0
- package/dist/esm/typescript-schema.js +310 -0
- package/dist/esm/typescript-schema.js.map +1 -0
- package/dist/esm/util.d.ts +13 -0
- package/dist/esm/util.d.ts.map +1 -0
- package/dist/esm/util.js +46 -0
- package/dist/esm/util.js.map +1 -0
- package/dist/esm/version.d.ts +3 -0
- package/dist/esm/version.d.ts.map +1 -0
- package/dist/esm/version.js +3 -0
- package/dist/esm/version.js.map +1 -0
- package/dist/standalone/index.js +13747 -0
- package/dist/standalone/index.umd.cjs +72 -0
- package/package.json +69 -3
- package/src/ProgressPromise.ts +97 -0
- package/src/api.ts +1903 -0
- package/src/backupDownload.ts +519 -0
- package/src/backups.ts +342 -0
- package/src/clerk.ts +24 -0
- package/src/consts.ts +22 -0
- package/src/crypto.ts +28 -0
- package/src/index.ts +147 -0
- package/src/migrationUtils.ts +56 -0
- package/src/migrations.ts +920 -0
- package/src/oauth.ts +390 -0
- package/src/oauthCommon.ts +38 -0
- package/src/perms.ts +81 -0
- package/src/relationships.ts +57 -0
- package/src/schema.ts +497 -0
- package/src/serverOAuth.ts +119 -0
- package/src/typescript-schema.ts +439 -0
- package/src/util.ts +77 -0
- package/src/version.ts +3 -0
- package/tsconfig.dev.json +14 -0
- package/tsconfig.json +14 -0
- package/vite.config.ts +19 -0
package/README.md
CHANGED
|
@@ -1,4 +1,446 @@
|
|
|
1
|
+
<!-- SidekickDB: plain-text header in place of upstream's logo, badges and links. -->
|
|
2
|
+
|
|
1
3
|
# @sidekickdb/platform
|
|
2
4
|
|
|
3
|
-
|
|
4
|
-
|
|
5
|
+
Part of [SidekickDB](https://github.com/SidekickDB/sidekick), an independent continuation of the Apache-2.0 InstantDB code. It is not affiliated with or endorsed by Instant.
|
|
6
|
+
|
|
7
|
+
Welcome to SidekickDB's platform SDK.
|
|
8
|
+
|
|
9
|
+
The Platform SDK allows you to manage apps on behalf of a SidekickDB user.
|
|
10
|
+
|
|
11
|
+
# Usage
|
|
12
|
+
|
|
13
|
+
## OAuth flow
|
|
14
|
+
|
|
15
|
+
You'll need an OAuth access token to make request on the SidekickDB user's behalf. The platform SDK includes helpers for performing the OAuth flow in the `OAuthHandler` class.
|
|
16
|
+
|
|
17
|
+
Go to the OAuth Apps page of your dashboard to create a new OAuth client id.
|
|
18
|
+
|
|
19
|
+
First you'll need to create a page that you can redirect to complete the OAuth flow. It should live on the same domain as the page where you're originating the OAuth flow:
|
|
20
|
+
|
|
21
|
+
```tsx
|
|
22
|
+
// oauth/instant/redirect.tsx
|
|
23
|
+
import { OAuthHandler } from '@sidekickdb/platform';
|
|
24
|
+
import { useEffect } from 'react';
|
|
25
|
+
|
|
26
|
+
const oauthHandler = new OAuthHandler({
|
|
27
|
+
// from the OAuth Apps page of your dashboard
|
|
28
|
+
clientId: 'YOUR_CLIENT_ID',
|
|
29
|
+
// Be sure the redirectUri matches one of the authorized redirect uris
|
|
30
|
+
// in your OAuth App client config on the SidekickDB dashboard.
|
|
31
|
+
redirectUri: 'https://example.com/oauth/instant/redirect',
|
|
32
|
+
// URL of your SidekickDB server. Required: there is no default.
|
|
33
|
+
apiURI: 'https://sidekick.example.com',
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
export default function Page() {
|
|
37
|
+
useEffect(() => {
|
|
38
|
+
// This will send the oauth code to the originating window,
|
|
39
|
+
// then close the window.
|
|
40
|
+
return oauthHandler.handleClientRedirect();
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
return <div>Loading...</div>;
|
|
44
|
+
}
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
On the page where you want the user to connect:
|
|
48
|
+
|
|
49
|
+
```tsx
|
|
50
|
+
// ConnectPage.tsx
|
|
51
|
+
import { OAuthHandler } from '@sidekickdb/platform';
|
|
52
|
+
|
|
53
|
+
// Available scopes are: apps-read, apps-write,
|
|
54
|
+
// data-read, data-write,
|
|
55
|
+
// storage-read, storage-write
|
|
56
|
+
const scopes = ['apps-write'];
|
|
57
|
+
|
|
58
|
+
const handler = new OAuthHandler({
|
|
59
|
+
// from the OAuth Apps page of your dashboard
|
|
60
|
+
clientId: 'YOUR_CLIENT_ID',
|
|
61
|
+
// Be sure the redirectUri matches one of the authorized redirect uris
|
|
62
|
+
// in your OAuth App client config on the SidekickDB dashboard.
|
|
63
|
+
redirectUri: 'https://example.com/oauth/instant/redirect',
|
|
64
|
+
// URL of your SidekickDB server. Required: there is no default.
|
|
65
|
+
apiURI: 'https://sidekick.example.com',
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
export default function Page() {
|
|
69
|
+
const handleConnect = async () => {
|
|
70
|
+
try {
|
|
71
|
+
// This will open a new window where the user will complete the OAuth flow
|
|
72
|
+
const token = await oauthHandler.startClientOnlyFlow(scopes);
|
|
73
|
+
// doSomethingWith(token)
|
|
74
|
+
} catch (e) {
|
|
75
|
+
alert(e.message);
|
|
76
|
+
}
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
return (
|
|
80
|
+
<button onClick={handleConnect}>Connect to your SidekickDB Account</button>
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
The client-only flow described here uses PKCE and does not support refresh tokens. For a server-based flow that supports refresh tokens, see the [Platform OAuth docs](https://sidekickdb.co/docs/auth/platform-oauth).
|
|
86
|
+
|
|
87
|
+
## Making API requests
|
|
88
|
+
|
|
89
|
+
Once you have your token, you can perform actions on the user's behalf with the `PlatformApi` class.
|
|
90
|
+
|
|
91
|
+
Setup:
|
|
92
|
+
|
|
93
|
+
```js
|
|
94
|
+
import { PlatformApi } from '@sidekickdb/platform';
|
|
95
|
+
|
|
96
|
+
const api = new PlatformApi({
|
|
97
|
+
apiURI: 'https://sidekick.example.com',
|
|
98
|
+
auth: { token: 'OAUTH_ACCESS_TOKEN' },
|
|
99
|
+
});
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
### getApps
|
|
103
|
+
|
|
104
|
+
Requires the `apps-read` or `apps-write` scope.
|
|
105
|
+
|
|
106
|
+
Get all of the user's apps, optionally fetching the schema and permissions:
|
|
107
|
+
|
|
108
|
+
```js
|
|
109
|
+
const { apps } = await api.getApps({ includeSchema: true, includePerms: true });
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### getApp
|
|
113
|
+
|
|
114
|
+
Requires the `apps-read` or `apps-write` scope.
|
|
115
|
+
|
|
116
|
+
Get an app by its id, optionally fetching the schema and permissions:
|
|
117
|
+
|
|
118
|
+
```js
|
|
119
|
+
const { app } = await api.getApp(YOUR_APP_ID, {
|
|
120
|
+
includeSchema: true,
|
|
121
|
+
includePerms: true,
|
|
122
|
+
});
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
### getSchema
|
|
126
|
+
|
|
127
|
+
Requires the `apps-read` or `apps-write` scope.
|
|
128
|
+
|
|
129
|
+
Get the schema for an app by its id:
|
|
130
|
+
|
|
131
|
+
```ts
|
|
132
|
+
const { schema } = await api.getSchema(YOUR_APP_ID);
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
The Platform SDK includes a helper for generating the `instant.schema.ts` file from the schema returned by the API.
|
|
136
|
+
|
|
137
|
+
```ts
|
|
138
|
+
import { generateSchemaTypescriptFile } from '@sidekickdb/platform';
|
|
139
|
+
|
|
140
|
+
// Generate the instant.schema.ts file with:
|
|
141
|
+
const schemaTs = generateSchemaTypescriptFile(
|
|
142
|
+
null,
|
|
143
|
+
schema,
|
|
144
|
+
'@sidekickdb/react',
|
|
145
|
+
);
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
### getPerms
|
|
149
|
+
|
|
150
|
+
Requires the `apps-read` or `apps-write` scope.
|
|
151
|
+
|
|
152
|
+
Get the permissions for an app by its id:
|
|
153
|
+
|
|
154
|
+
```ts
|
|
155
|
+
const { perms } = await api.getPerms(YOUR_APP_ID);
|
|
156
|
+
```
|
|
157
|
+
|
|
158
|
+
The Platform SDK includes a helper for generating the `instant.perms.ts` file from the perms returned by the API.
|
|
159
|
+
|
|
160
|
+
```ts
|
|
161
|
+
import { generatePermsTypescriptFile } from '@sidekickdb/platform';
|
|
162
|
+
|
|
163
|
+
// Generate the instant.schema.ts file with:
|
|
164
|
+
const permsTs = generatePermsTypescriptFile(perms, '@sidekickdb/react');
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### createApp
|
|
168
|
+
|
|
169
|
+
Requires the `apps-write` scope.
|
|
170
|
+
|
|
171
|
+
Create a new app in the authenticated user's account:
|
|
172
|
+
|
|
173
|
+
```ts
|
|
174
|
+
const { app } = await api.createApp({ title: 'Great new app' });
|
|
175
|
+
```
|
|
176
|
+
|
|
177
|
+
`createApp` takes optional `schema` and `perms` arguments.
|
|
178
|
+
|
|
179
|
+
Create an app with permissions:
|
|
180
|
+
|
|
181
|
+
```ts
|
|
182
|
+
import type { InstantRules } from '@sidekickdb/platform';
|
|
183
|
+
|
|
184
|
+
const perms: InstantRules = {
|
|
185
|
+
$default: {
|
|
186
|
+
allow: {
|
|
187
|
+
$default: false,
|
|
188
|
+
},
|
|
189
|
+
},
|
|
190
|
+
todos: {
|
|
191
|
+
allow: {
|
|
192
|
+
view: 'true',
|
|
193
|
+
create: 'isOwner',
|
|
194
|
+
update: 'isOwner',
|
|
195
|
+
delete: 'isOwner',
|
|
196
|
+
},
|
|
197
|
+
bind: { isOwner: 'auth.id != null && auth.id == data.creatorId' },
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
|
|
201
|
+
const { app } = await api.createApp({
|
|
202
|
+
title: 'My app with permissions',
|
|
203
|
+
perms: perms,
|
|
204
|
+
});
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
Create an app with a schema:
|
|
208
|
+
|
|
209
|
+
```ts
|
|
210
|
+
import { i } from '@sidekickdb/platform';
|
|
211
|
+
|
|
212
|
+
const schema = i.schema({
|
|
213
|
+
entities: {
|
|
214
|
+
todos: i.entity({
|
|
215
|
+
title: i.string(),
|
|
216
|
+
done: i.boolean(),
|
|
217
|
+
}),
|
|
218
|
+
},
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
const { app } = await api.createApp({
|
|
222
|
+
title: 'Todo app',
|
|
223
|
+
schema: schema,
|
|
224
|
+
});
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
### updateApp
|
|
228
|
+
|
|
229
|
+
Requires the `apps-write` scope.
|
|
230
|
+
|
|
231
|
+
Update an app's title by its id:
|
|
232
|
+
|
|
233
|
+
```ts
|
|
234
|
+
const { app } = await api.updateApp(YOUR_APP_ID, { title: 'New app title' });
|
|
235
|
+
```
|
|
236
|
+
|
|
237
|
+
### deleteApp
|
|
238
|
+
|
|
239
|
+
Requires the `apps-write` scope.
|
|
240
|
+
|
|
241
|
+
Delete an app by its id:
|
|
242
|
+
|
|
243
|
+
```ts
|
|
244
|
+
const { app } = await api.deleteApp(YOUR_APP_ID);
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### pushPerms
|
|
248
|
+
|
|
249
|
+
Requires the `apps-write` scope.
|
|
250
|
+
|
|
251
|
+
Update the permissions for an app by its id. This will completely replace the current set of permissions.
|
|
252
|
+
|
|
253
|
+
```ts
|
|
254
|
+
import type { InstantRules } from '@sidekickdb/platform';
|
|
255
|
+
|
|
256
|
+
const perms: InstantRules = {
|
|
257
|
+
$default: {
|
|
258
|
+
allow: {
|
|
259
|
+
$default: false,
|
|
260
|
+
},
|
|
261
|
+
},
|
|
262
|
+
todos: {
|
|
263
|
+
allow: {
|
|
264
|
+
view: 'true',
|
|
265
|
+
create: 'isOwner',
|
|
266
|
+
update: 'isOwner',
|
|
267
|
+
delete: 'isOwner',
|
|
268
|
+
},
|
|
269
|
+
bind: { isOwner: 'auth.id != null && auth.id == data.creatorId' },
|
|
270
|
+
},
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
const { app } = await api.pushPerms(YOUR_APP_ID, {
|
|
274
|
+
perms: perms,
|
|
275
|
+
});
|
|
276
|
+
```
|
|
277
|
+
|
|
278
|
+
### planSchemaPush
|
|
279
|
+
|
|
280
|
+
Requires the `apps-read` or `apps-write` scope.
|
|
281
|
+
|
|
282
|
+
Performs a dry-run of `schemaPush`, returning the steps that would be taken.
|
|
283
|
+
|
|
284
|
+
```ts
|
|
285
|
+
import { i } from '@sidekickdb/platform';
|
|
286
|
+
|
|
287
|
+
const schema = i.schema({
|
|
288
|
+
entities: {
|
|
289
|
+
todos: i.entity({
|
|
290
|
+
title: i.string().indexed(),
|
|
291
|
+
done: i.boolean(),
|
|
292
|
+
completedAt: i.date().optional(),
|
|
293
|
+
}),
|
|
294
|
+
},
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
const { steps } = await api.planSchemaPush(YOUR_APP_ID, { schema: schema });
|
|
298
|
+
|
|
299
|
+
// Or, to allow deletions and renames:
|
|
300
|
+
const { steps } = await api.planSchemaPush(YOUR_APP_ID, {
|
|
301
|
+
schema: schema,
|
|
302
|
+
overwrite: true,
|
|
303
|
+
renames: ['todos.name:todos.title'],
|
|
304
|
+
});
|
|
305
|
+
|
|
306
|
+
// steps
|
|
307
|
+
// [
|
|
308
|
+
// { type: 'index', friendlyDescription: 'Add index to todos.title.' },
|
|
309
|
+
// {
|
|
310
|
+
// type: 'add-attr',
|
|
311
|
+
// friendlyDescription: 'Add attribute todos.completedAt.',
|
|
312
|
+
// attr: {...},
|
|
313
|
+
// },
|
|
314
|
+
// ];
|
|
315
|
+
```
|
|
316
|
+
|
|
317
|
+
### schemaPush
|
|
318
|
+
|
|
319
|
+
Requires the `apps-write` scope.
|
|
320
|
+
|
|
321
|
+
Push a new schema to an app.
|
|
322
|
+
|
|
323
|
+
Some schema updates, like adding an index to an attribute with existing data, kick off a process that completes in the background. The `schemaPush` method will wait for all of the background processes to finish before it returns.
|
|
324
|
+
|
|
325
|
+
You can subscribe to progress updates to display feedback to the user.
|
|
326
|
+
|
|
327
|
+
```ts
|
|
328
|
+
import { i } from '@sidekickdb/platform';
|
|
329
|
+
|
|
330
|
+
const schema = i.schema({
|
|
331
|
+
entities: {
|
|
332
|
+
todos: i.entity({
|
|
333
|
+
title: i.string().indexed(),
|
|
334
|
+
done: i.boolean(),
|
|
335
|
+
completedAt: i.date().optional(),
|
|
336
|
+
}),
|
|
337
|
+
},
|
|
338
|
+
});
|
|
339
|
+
|
|
340
|
+
const pushPromise = api.schemaPush(YOUR_APP_ID, { schema: schema });
|
|
341
|
+
|
|
342
|
+
// Or, to allow deletions and renames:
|
|
343
|
+
const { steps } = await api.planSchemaPush(YOUR_APP_ID, {
|
|
344
|
+
schema: schema,
|
|
345
|
+
overwrite: true,
|
|
346
|
+
renames: ['todos.name:todos.title'],
|
|
347
|
+
});
|
|
348
|
+
|
|
349
|
+
pushPromise.subscribe({
|
|
350
|
+
next(status) {
|
|
351
|
+
console.log(status.friendlyDescription);
|
|
352
|
+
},
|
|
353
|
+
});
|
|
354
|
+
|
|
355
|
+
const { steps } = await pushPromise;
|
|
356
|
+
```
|
|
357
|
+
|
|
358
|
+
Note that the promise will return the steps without throwing even if some of the background steps failed. For example, if an attribute was marked `unique`, but there were multiple entities with the same value. In the case of failure, the step will have a `backgroundJob` field where `error` is set to a string.
|
|
359
|
+
|
|
360
|
+
Here is an example of a failed step to make the title attribute unique because mulitple entities had the same `Write docs` value.
|
|
361
|
+
|
|
362
|
+
```ts
|
|
363
|
+
// steps
|
|
364
|
+
[
|
|
365
|
+
{
|
|
366
|
+
type: 'unique',
|
|
367
|
+
friendlyDescription: 'Ensure that todos.title is unique.',
|
|
368
|
+
attrId: '07145112-59b6-4617-9abc-544a3c3e146c',
|
|
369
|
+
forwardIdentity: ['24c6c019-97ef-4915-aef5-7f4f09b372e1', 'todos', 'title'],
|
|
370
|
+
backgroundJob: {
|
|
371
|
+
id: '1a5941ad-542b-4d0a-bce8-81ced3eaed1a',
|
|
372
|
+
createdAt: '2025-05-30T23:26:54.000Z',
|
|
373
|
+
updatedAt: '2025-05-30T23:26:54.000Z',
|
|
374
|
+
status: 'errored',
|
|
375
|
+
workEstimate: 2,
|
|
376
|
+
workCompleted: null,
|
|
377
|
+
error: 'triple-not-unique-error',
|
|
378
|
+
invalidTriplesSample: [
|
|
379
|
+
{
|
|
380
|
+
entityId: 'b59b3be0-9d76-419f-8f95-e28830d12a40',
|
|
381
|
+
value: 'Write docs',
|
|
382
|
+
jsonType: 'string',
|
|
383
|
+
},
|
|
384
|
+
{
|
|
385
|
+
entityId: 'eb57dbc8-5e09-4978-b255-1b81ff46b63e',
|
|
386
|
+
value: 'Write docs',
|
|
387
|
+
jsonType: 'string',
|
|
388
|
+
},
|
|
389
|
+
],
|
|
390
|
+
type: 'unique',
|
|
391
|
+
invalidUniqueValue: 'Write docs',
|
|
392
|
+
},
|
|
393
|
+
},
|
|
394
|
+
];
|
|
395
|
+
```
|
|
396
|
+
|
|
397
|
+
Some failed steps will include an `invalidTriplesSample` in the background job, which show some of the triples that caused the error.
|
|
398
|
+
|
|
399
|
+
## Managing schemas
|
|
400
|
+
|
|
401
|
+
The platform package includes helpers for generating the `instant.schema.ts` and `instant.perms.ts` config files from the schema and perms data returned from the API.
|
|
402
|
+
|
|
403
|
+
```ts
|
|
404
|
+
import {
|
|
405
|
+
generateSchemaTypescriptFile,
|
|
406
|
+
generatePermsTypescriptFile,
|
|
407
|
+
} from '@sidekickdb/platform';
|
|
408
|
+
|
|
409
|
+
console.log(
|
|
410
|
+
generateSchemaTypescriptFile(null, shemaFromApi, '@sidekickdb/core'),
|
|
411
|
+
);
|
|
412
|
+
|
|
413
|
+
console.log(generatePermsTypescriptFile(permsFromApi, '@sidekickdb/core'));
|
|
414
|
+
```
|
|
415
|
+
|
|
416
|
+
Use `schemaTypescriptFileToInstantSchema` to recover an `InstantSchemaDef` from the typescript file:
|
|
417
|
+
|
|
418
|
+
```ts
|
|
419
|
+
import {
|
|
420
|
+
i,
|
|
421
|
+
generateSchemaTypescriptFile,
|
|
422
|
+
schemaTypescriptFileToInstantSchema,
|
|
423
|
+
} from '@sidekickdb/platform';
|
|
424
|
+
|
|
425
|
+
// Example schema
|
|
426
|
+
const schema = i.schema({
|
|
427
|
+
entities: {
|
|
428
|
+
books: i.entity({
|
|
429
|
+
title: i.string().unique(),
|
|
430
|
+
}),
|
|
431
|
+
},
|
|
432
|
+
});
|
|
433
|
+
|
|
434
|
+
// Example of the typescript file that was generated for a schema
|
|
435
|
+
const code = generateTypescriptFile(null, schema, '@sidekickdb/core');
|
|
436
|
+
|
|
437
|
+
const recoveredSchema = schemaTypescriptFileToInstantSchema(code);
|
|
438
|
+
|
|
439
|
+
schema == recoveredSchema;
|
|
440
|
+
```
|
|
441
|
+
|
|
442
|
+
This is useful when parsing a schema that the user manually edited or pulled with `npx @sidekickdb/cli pull`.
|
|
443
|
+
|
|
444
|
+
# Questions?
|
|
445
|
+
|
|
446
|
+
If you have any questions, feel free to open an issue on [GitHub](https://github.com/SidekickDB/sidekick/issues).
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { describe, expect, test } from 'vitest';
|
|
2
|
+
import { MissingApiURIError } from '@sidekickdb/core';
|
|
3
|
+
import {
|
|
4
|
+
OAuthHandler,
|
|
5
|
+
PlatformApi,
|
|
6
|
+
exchangeCodeForToken,
|
|
7
|
+
exchangeRefreshToken,
|
|
8
|
+
} from '../../src/index';
|
|
9
|
+
|
|
10
|
+
function missingMessage(target: string) {
|
|
11
|
+
return `No SidekickDB server URL configured. Pass \`apiURI\` to ${target}, for example apiURI: 'https://sidekick.example.com'. SidekickDB has no default server.`;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
describe('platform server URL configuration', () => {
|
|
15
|
+
test('PlatformApi throws without apiURI', () => {
|
|
16
|
+
expect(() => new PlatformApi()).toThrow(MissingApiURIError);
|
|
17
|
+
expect(() => new PlatformApi({ auth: { token: 'token' } })).toThrow(
|
|
18
|
+
missingMessage('new PlatformApi()'),
|
|
19
|
+
);
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
test('OAuthHandler throws without apiURI', () => {
|
|
23
|
+
expect(
|
|
24
|
+
() =>
|
|
25
|
+
new OAuthHandler({
|
|
26
|
+
clientId: 'client-id',
|
|
27
|
+
redirectUri: 'https://app.example.com/callback',
|
|
28
|
+
}),
|
|
29
|
+
).toThrow(missingMessage('new OAuthHandler()'));
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
test('OAuthHandler keeps an explicit apiURI', () => {
|
|
33
|
+
const handler = new OAuthHandler({
|
|
34
|
+
clientId: 'client-id',
|
|
35
|
+
redirectUri: 'https://app.example.com/callback',
|
|
36
|
+
apiURI: 'http://localhost:8888',
|
|
37
|
+
});
|
|
38
|
+
expect(handler.apiURI).toBe('http://localhost:8888');
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
test('token exchange helpers reject without apiURI', async () => {
|
|
42
|
+
await expect(
|
|
43
|
+
exchangeCodeForToken({
|
|
44
|
+
code: 'code',
|
|
45
|
+
clientId: 'client-id',
|
|
46
|
+
clientSecret: 'secret',
|
|
47
|
+
redirectUri: 'https://app.example.com/callback',
|
|
48
|
+
}),
|
|
49
|
+
).rejects.toThrow(missingMessage('exchangeCodeForToken()'));
|
|
50
|
+
await expect(
|
|
51
|
+
exchangeRefreshToken({
|
|
52
|
+
clientId: 'client-id',
|
|
53
|
+
clientSecret: 'secret',
|
|
54
|
+
refreshToken: 'refresh',
|
|
55
|
+
}),
|
|
56
|
+
).rejects.toThrow(missingMessage('exchangeRefreshToken()'));
|
|
57
|
+
});
|
|
58
|
+
});
|