@powersync/service-core 0.6.0 → 0.7.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/CHANGELOG.md +19 -0
- package/dist/routes/configure-fastify.d.ts +0 -171
- package/dist/routes/configure-fastify.js +1 -2
- package/dist/routes/configure-fastify.js.map +1 -1
- package/dist/routes/endpoints/admin.d.ts +0 -34
- package/dist/routes/endpoints/admin.js +2 -22
- package/dist/routes/endpoints/admin.js.map +1 -1
- package/dist/routes/endpoints/route-endpoints-index.d.ts +0 -1
- package/dist/routes/endpoints/route-endpoints-index.js +0 -1
- package/dist/routes/endpoints/route-endpoints-index.js.map +1 -1
- package/package.json +4 -4
- package/src/routes/configure-fastify.ts +1 -2
- package/src/routes/endpoints/admin.ts +2 -24
- package/src/routes/endpoints/route-endpoints-index.ts +0 -1
- package/tsconfig.tsbuildinfo +1 -1
- package/dist/routes/endpoints/dev.d.ts +0 -312
- package/dist/routes/endpoints/dev.js +0 -172
- package/dist/routes/endpoints/dev.js.map +0 -1
- package/src/routes/endpoints/dev.ts +0 -199
|
@@ -1,312 +0,0 @@
|
|
|
1
|
-
import { router, schema } from '@powersync/lib-services-framework';
|
|
2
|
-
export declare const auth: router.Endpoint<{
|
|
3
|
-
password: string;
|
|
4
|
-
user: string;
|
|
5
|
-
}, {
|
|
6
|
-
token: string;
|
|
7
|
-
user_id: string;
|
|
8
|
-
endpoint: string;
|
|
9
|
-
}, import("../router.js").Context, import("../router.js").RequestEndpointHandlerPayload<{
|
|
10
|
-
password: string;
|
|
11
|
-
user: string;
|
|
12
|
-
}, import("../router.js").Context, import("../router.js").BasicRouterRequest>, router.EndpointHandler<import("../router.js").RequestEndpointHandlerPayload<{
|
|
13
|
-
password: string;
|
|
14
|
-
user: string;
|
|
15
|
-
}, import("../router.js").Context, import("../router.js").BasicRouterRequest>, {
|
|
16
|
-
token: string;
|
|
17
|
-
user_id: string;
|
|
18
|
-
endpoint: string;
|
|
19
|
-
}>> & {
|
|
20
|
-
path: string;
|
|
21
|
-
method: router.HTTPMethod.POST;
|
|
22
|
-
validator: schema.MicroValidator<{
|
|
23
|
-
password: string;
|
|
24
|
-
user: string;
|
|
25
|
-
}, string[]>;
|
|
26
|
-
handler: (payload: import("../router.js").RequestEndpointHandlerPayload<{
|
|
27
|
-
password: string;
|
|
28
|
-
user: string;
|
|
29
|
-
}, import("../router.js").Context, import("../router.js").BasicRouterRequest>) => Promise<{
|
|
30
|
-
token: string;
|
|
31
|
-
user_id: string;
|
|
32
|
-
endpoint: string;
|
|
33
|
-
}>;
|
|
34
|
-
};
|
|
35
|
-
export declare const auth2: router.Endpoint<{
|
|
36
|
-
password: string;
|
|
37
|
-
user: string;
|
|
38
|
-
}, {
|
|
39
|
-
token: string;
|
|
40
|
-
user_id: string;
|
|
41
|
-
}, import("../router.js").Context, import("../router.js").RequestEndpointHandlerPayload<{
|
|
42
|
-
password: string;
|
|
43
|
-
user: string;
|
|
44
|
-
}, import("../router.js").Context, import("../router.js").BasicRouterRequest>, router.EndpointHandler<import("../router.js").RequestEndpointHandlerPayload<{
|
|
45
|
-
password: string;
|
|
46
|
-
user: string;
|
|
47
|
-
}, import("../router.js").Context, import("../router.js").BasicRouterRequest>, {
|
|
48
|
-
token: string;
|
|
49
|
-
user_id: string;
|
|
50
|
-
}>> & {
|
|
51
|
-
path: string;
|
|
52
|
-
method: router.HTTPMethod.POST;
|
|
53
|
-
validator: schema.MicroValidator<{
|
|
54
|
-
password: string;
|
|
55
|
-
user: string;
|
|
56
|
-
}, string[]>;
|
|
57
|
-
handler: (payload: import("../router.js").RequestEndpointHandlerPayload<{
|
|
58
|
-
password: string;
|
|
59
|
-
user: string;
|
|
60
|
-
}, import("../router.js").Context, import("../router.js").BasicRouterRequest>) => Promise<{
|
|
61
|
-
token: string;
|
|
62
|
-
user_id: string;
|
|
63
|
-
}>;
|
|
64
|
-
};
|
|
65
|
-
export declare const token: router.Endpoint<{}, {
|
|
66
|
-
token: string;
|
|
67
|
-
user_id: string | undefined;
|
|
68
|
-
endpoint: string;
|
|
69
|
-
}, import("../router.js").Context, import("../router.js").RequestEndpointHandlerPayload<{}, import("../router.js").Context, import("../router.js").BasicRouterRequest>, router.EndpointHandler<import("../router.js").RequestEndpointHandlerPayload<{}, import("../router.js").Context, import("../router.js").BasicRouterRequest>, {
|
|
70
|
-
token: string;
|
|
71
|
-
user_id: string | undefined;
|
|
72
|
-
endpoint: string;
|
|
73
|
-
}>> & {
|
|
74
|
-
path: string;
|
|
75
|
-
method: router.HTTPMethod.POST;
|
|
76
|
-
validator: schema.MicroValidator<{}, string[]>;
|
|
77
|
-
authorize: (payload: import("../router.js").RequestEndpointHandlerPayload) => Promise<{
|
|
78
|
-
authorized: boolean;
|
|
79
|
-
errors: any[];
|
|
80
|
-
} | {
|
|
81
|
-
authorized: boolean;
|
|
82
|
-
errors?: undefined;
|
|
83
|
-
}>;
|
|
84
|
-
handler: (payload: import("../router.js").RequestEndpointHandlerPayload<{}, import("../router.js").Context, import("../router.js").BasicRouterRequest>) => Promise<{
|
|
85
|
-
token: string;
|
|
86
|
-
user_id: string | undefined;
|
|
87
|
-
endpoint: string;
|
|
88
|
-
}>;
|
|
89
|
-
};
|
|
90
|
-
export declare const crud: router.Endpoint<{
|
|
91
|
-
data: {
|
|
92
|
-
type: string;
|
|
93
|
-
id: string;
|
|
94
|
-
op: string;
|
|
95
|
-
data?: any;
|
|
96
|
-
op_id?: number | undefined;
|
|
97
|
-
}[];
|
|
98
|
-
write_checkpoint?: boolean | undefined;
|
|
99
|
-
}, {
|
|
100
|
-
write_checkpoint: string;
|
|
101
|
-
checkpoint?: undefined;
|
|
102
|
-
} | {
|
|
103
|
-
write_checkpoint?: undefined;
|
|
104
|
-
checkpoint?: undefined;
|
|
105
|
-
} | {
|
|
106
|
-
checkpoint: string;
|
|
107
|
-
write_checkpoint?: undefined;
|
|
108
|
-
}, import("../router.js").Context, import("../router.js").RequestEndpointHandlerPayload<{
|
|
109
|
-
data: {
|
|
110
|
-
type: string;
|
|
111
|
-
id: string;
|
|
112
|
-
op: string;
|
|
113
|
-
data?: any;
|
|
114
|
-
op_id?: number | undefined;
|
|
115
|
-
}[];
|
|
116
|
-
write_checkpoint?: boolean | undefined;
|
|
117
|
-
}, import("../router.js").Context, import("../router.js").BasicRouterRequest>, router.EndpointHandler<import("../router.js").RequestEndpointHandlerPayload<{
|
|
118
|
-
data: {
|
|
119
|
-
type: string;
|
|
120
|
-
id: string;
|
|
121
|
-
op: string;
|
|
122
|
-
data?: any;
|
|
123
|
-
op_id?: number | undefined;
|
|
124
|
-
}[];
|
|
125
|
-
write_checkpoint?: boolean | undefined;
|
|
126
|
-
}, import("../router.js").Context, import("../router.js").BasicRouterRequest>, {
|
|
127
|
-
write_checkpoint: string;
|
|
128
|
-
checkpoint?: undefined;
|
|
129
|
-
} | {
|
|
130
|
-
write_checkpoint?: undefined;
|
|
131
|
-
checkpoint?: undefined;
|
|
132
|
-
} | {
|
|
133
|
-
checkpoint: string;
|
|
134
|
-
write_checkpoint?: undefined;
|
|
135
|
-
}>> & {
|
|
136
|
-
path: string;
|
|
137
|
-
method: router.HTTPMethod.POST;
|
|
138
|
-
validator: schema.MicroValidator<{
|
|
139
|
-
data: {
|
|
140
|
-
type: string;
|
|
141
|
-
id: string;
|
|
142
|
-
op: string;
|
|
143
|
-
data?: any;
|
|
144
|
-
op_id?: number | undefined;
|
|
145
|
-
}[];
|
|
146
|
-
write_checkpoint?: boolean | undefined;
|
|
147
|
-
}, string[]>;
|
|
148
|
-
authorize: (payload: import("../router.js").RequestEndpointHandlerPayload) => Promise<{
|
|
149
|
-
authorized: boolean;
|
|
150
|
-
errors: any[];
|
|
151
|
-
} | {
|
|
152
|
-
authorized: boolean;
|
|
153
|
-
errors?: undefined;
|
|
154
|
-
}>;
|
|
155
|
-
handler: (payload: import("../router.js").RequestEndpointHandlerPayload<{
|
|
156
|
-
data: {
|
|
157
|
-
type: string;
|
|
158
|
-
id: string;
|
|
159
|
-
op: string;
|
|
160
|
-
data?: any;
|
|
161
|
-
op_id?: number | undefined;
|
|
162
|
-
}[];
|
|
163
|
-
write_checkpoint?: boolean | undefined;
|
|
164
|
-
}, import("../router.js").Context, import("../router.js").BasicRouterRequest>) => Promise<{
|
|
165
|
-
write_checkpoint: string;
|
|
166
|
-
checkpoint?: undefined;
|
|
167
|
-
} | {
|
|
168
|
-
write_checkpoint?: undefined;
|
|
169
|
-
checkpoint?: undefined;
|
|
170
|
-
} | {
|
|
171
|
-
checkpoint: string;
|
|
172
|
-
write_checkpoint?: undefined;
|
|
173
|
-
}>;
|
|
174
|
-
};
|
|
175
|
-
export declare const DEV_ROUTES: ((router.Endpoint<{
|
|
176
|
-
password: string;
|
|
177
|
-
user: string;
|
|
178
|
-
}, {
|
|
179
|
-
token: string;
|
|
180
|
-
user_id: string;
|
|
181
|
-
}, import("../router.js").Context, import("../router.js").RequestEndpointHandlerPayload<{
|
|
182
|
-
password: string;
|
|
183
|
-
user: string;
|
|
184
|
-
}, import("../router.js").Context, import("../router.js").BasicRouterRequest>, router.EndpointHandler<import("../router.js").RequestEndpointHandlerPayload<{
|
|
185
|
-
password: string;
|
|
186
|
-
user: string;
|
|
187
|
-
}, import("../router.js").Context, import("../router.js").BasicRouterRequest>, {
|
|
188
|
-
token: string;
|
|
189
|
-
user_id: string;
|
|
190
|
-
}>> & {
|
|
191
|
-
path: string;
|
|
192
|
-
method: router.HTTPMethod.POST;
|
|
193
|
-
validator: schema.MicroValidator<{
|
|
194
|
-
password: string;
|
|
195
|
-
user: string;
|
|
196
|
-
}, string[]>;
|
|
197
|
-
handler: (payload: import("../router.js").RequestEndpointHandlerPayload<{
|
|
198
|
-
password: string;
|
|
199
|
-
user: string;
|
|
200
|
-
}, import("../router.js").Context, import("../router.js").BasicRouterRequest>) => Promise<{
|
|
201
|
-
token: string;
|
|
202
|
-
user_id: string;
|
|
203
|
-
}>;
|
|
204
|
-
}) | (router.Endpoint<{}, {
|
|
205
|
-
token: string;
|
|
206
|
-
user_id: string | undefined;
|
|
207
|
-
endpoint: string;
|
|
208
|
-
}, import("../router.js").Context, import("../router.js").RequestEndpointHandlerPayload<{}, import("../router.js").Context, import("../router.js").BasicRouterRequest>, router.EndpointHandler<import("../router.js").RequestEndpointHandlerPayload<{}, import("../router.js").Context, import("../router.js").BasicRouterRequest>, {
|
|
209
|
-
token: string;
|
|
210
|
-
user_id: string | undefined;
|
|
211
|
-
endpoint: string;
|
|
212
|
-
}>> & {
|
|
213
|
-
path: string;
|
|
214
|
-
method: router.HTTPMethod.POST;
|
|
215
|
-
validator: schema.MicroValidator<{}, string[]>;
|
|
216
|
-
authorize: (payload: import("../router.js").RequestEndpointHandlerPayload) => Promise<{
|
|
217
|
-
authorized: boolean;
|
|
218
|
-
errors: any[];
|
|
219
|
-
} | {
|
|
220
|
-
authorized: boolean;
|
|
221
|
-
errors?: undefined;
|
|
222
|
-
}>;
|
|
223
|
-
handler: (payload: import("../router.js").RequestEndpointHandlerPayload<{}, import("../router.js").Context, import("../router.js").BasicRouterRequest>) => Promise<{
|
|
224
|
-
token: string;
|
|
225
|
-
user_id: string | undefined;
|
|
226
|
-
endpoint: string;
|
|
227
|
-
}>;
|
|
228
|
-
}) | (router.Endpoint<{
|
|
229
|
-
data: {
|
|
230
|
-
type: string;
|
|
231
|
-
id: string;
|
|
232
|
-
op: string;
|
|
233
|
-
data?: any;
|
|
234
|
-
op_id?: number | undefined;
|
|
235
|
-
}[];
|
|
236
|
-
write_checkpoint?: boolean | undefined;
|
|
237
|
-
}, {
|
|
238
|
-
write_checkpoint: string;
|
|
239
|
-
checkpoint?: undefined;
|
|
240
|
-
} | {
|
|
241
|
-
write_checkpoint?: undefined;
|
|
242
|
-
checkpoint?: undefined;
|
|
243
|
-
} | {
|
|
244
|
-
checkpoint: string;
|
|
245
|
-
write_checkpoint?: undefined;
|
|
246
|
-
}, import("../router.js").Context, import("../router.js").RequestEndpointHandlerPayload<{
|
|
247
|
-
data: {
|
|
248
|
-
type: string;
|
|
249
|
-
id: string;
|
|
250
|
-
op: string;
|
|
251
|
-
data?: any;
|
|
252
|
-
op_id?: number | undefined;
|
|
253
|
-
}[];
|
|
254
|
-
write_checkpoint?: boolean | undefined;
|
|
255
|
-
}, import("../router.js").Context, import("../router.js").BasicRouterRequest>, router.EndpointHandler<import("../router.js").RequestEndpointHandlerPayload<{
|
|
256
|
-
data: {
|
|
257
|
-
type: string;
|
|
258
|
-
id: string;
|
|
259
|
-
op: string;
|
|
260
|
-
data?: any;
|
|
261
|
-
op_id?: number | undefined;
|
|
262
|
-
}[];
|
|
263
|
-
write_checkpoint?: boolean | undefined;
|
|
264
|
-
}, import("../router.js").Context, import("../router.js").BasicRouterRequest>, {
|
|
265
|
-
write_checkpoint: string;
|
|
266
|
-
checkpoint?: undefined;
|
|
267
|
-
} | {
|
|
268
|
-
write_checkpoint?: undefined;
|
|
269
|
-
checkpoint?: undefined;
|
|
270
|
-
} | {
|
|
271
|
-
checkpoint: string;
|
|
272
|
-
write_checkpoint?: undefined;
|
|
273
|
-
}>> & {
|
|
274
|
-
path: string;
|
|
275
|
-
method: router.HTTPMethod.POST;
|
|
276
|
-
validator: schema.MicroValidator<{
|
|
277
|
-
data: {
|
|
278
|
-
type: string;
|
|
279
|
-
id: string;
|
|
280
|
-
op: string;
|
|
281
|
-
data?: any;
|
|
282
|
-
op_id?: number | undefined;
|
|
283
|
-
}[];
|
|
284
|
-
write_checkpoint?: boolean | undefined;
|
|
285
|
-
}, string[]>;
|
|
286
|
-
authorize: (payload: import("../router.js").RequestEndpointHandlerPayload) => Promise<{
|
|
287
|
-
authorized: boolean;
|
|
288
|
-
errors: any[];
|
|
289
|
-
} | {
|
|
290
|
-
authorized: boolean;
|
|
291
|
-
errors?: undefined;
|
|
292
|
-
}>;
|
|
293
|
-
handler: (payload: import("../router.js").RequestEndpointHandlerPayload<{
|
|
294
|
-
data: {
|
|
295
|
-
type: string;
|
|
296
|
-
id: string;
|
|
297
|
-
op: string;
|
|
298
|
-
data?: any;
|
|
299
|
-
op_id?: number | undefined;
|
|
300
|
-
}[];
|
|
301
|
-
write_checkpoint?: boolean | undefined;
|
|
302
|
-
}, import("../router.js").Context, import("../router.js").BasicRouterRequest>) => Promise<{
|
|
303
|
-
write_checkpoint: string;
|
|
304
|
-
checkpoint?: undefined;
|
|
305
|
-
} | {
|
|
306
|
-
write_checkpoint?: undefined;
|
|
307
|
-
checkpoint?: undefined;
|
|
308
|
-
} | {
|
|
309
|
-
checkpoint: string;
|
|
310
|
-
write_checkpoint?: undefined;
|
|
311
|
-
}>;
|
|
312
|
-
}))[];
|
|
@@ -1,172 +0,0 @@
|
|
|
1
|
-
import * as t from 'ts-codec';
|
|
2
|
-
import { errors, router, schema } from '@powersync/lib-services-framework';
|
|
3
|
-
import * as util from '../../util/util-index.js';
|
|
4
|
-
import { authDevUser, authUser, endpoint, issueDevToken, issueLegacyDevToken, issuePowerSyncToken } from '../auth.js';
|
|
5
|
-
import { routeDefinition } from '../router.js';
|
|
6
|
-
const AuthParams = t.object({
|
|
7
|
-
user: t.string,
|
|
8
|
-
password: t.string
|
|
9
|
-
});
|
|
10
|
-
// For legacy web client only. Remove soon.
|
|
11
|
-
export const auth = routeDefinition({
|
|
12
|
-
path: '/auth.json',
|
|
13
|
-
method: router.HTTPMethod.POST,
|
|
14
|
-
validator: schema.createTsCodecValidator(AuthParams, { allowAdditional: true }),
|
|
15
|
-
handler: async (payload) => {
|
|
16
|
-
const { user, password } = payload.params;
|
|
17
|
-
const config = payload.context.system.config;
|
|
18
|
-
if (config.dev.demo_auth == false || config.dev.demo_password == null) {
|
|
19
|
-
throw new errors.AuthorizationError(['Demo auth disabled']);
|
|
20
|
-
}
|
|
21
|
-
if (password == config.dev.demo_password) {
|
|
22
|
-
const token = await issueLegacyDevToken(payload.request, user, payload.context.system.config);
|
|
23
|
-
return { token, user_id: user, endpoint: endpoint(payload.request) };
|
|
24
|
-
}
|
|
25
|
-
else {
|
|
26
|
-
throw new errors.AuthorizationError(['Authentication failed']);
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
});
|
|
30
|
-
export const auth2 = routeDefinition({
|
|
31
|
-
path: '/dev/auth.json',
|
|
32
|
-
method: router.HTTPMethod.POST,
|
|
33
|
-
validator: schema.createTsCodecValidator(AuthParams, { allowAdditional: true }),
|
|
34
|
-
handler: async (payload) => {
|
|
35
|
-
const { user, password } = payload.params;
|
|
36
|
-
const config = payload.context.system.config;
|
|
37
|
-
if (config.dev.demo_auth == false || config.dev.demo_password == null) {
|
|
38
|
-
throw new errors.AuthorizationError(['Demo auth disabled']);
|
|
39
|
-
}
|
|
40
|
-
if (password == config.dev.demo_password) {
|
|
41
|
-
const token = await issueDevToken(payload.request, user, payload.context.system.config);
|
|
42
|
-
return { token, user_id: user };
|
|
43
|
-
}
|
|
44
|
-
else {
|
|
45
|
-
throw new errors.AuthorizationError(['Authentication failed']);
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
});
|
|
49
|
-
const TokenParams = t.object({});
|
|
50
|
-
export const token = routeDefinition({
|
|
51
|
-
path: '/dev/token.json',
|
|
52
|
-
method: router.HTTPMethod.POST,
|
|
53
|
-
validator: schema.createTsCodecValidator(TokenParams, { allowAdditional: true }),
|
|
54
|
-
authorize: authDevUser,
|
|
55
|
-
handler: async (payload) => {
|
|
56
|
-
const { user_id } = payload.context;
|
|
57
|
-
const outToken = await issuePowerSyncToken(payload.request, user_id, payload.context.system.config);
|
|
58
|
-
return { token: outToken, user_id: user_id, endpoint: endpoint(payload.request) };
|
|
59
|
-
}
|
|
60
|
-
});
|
|
61
|
-
const OpType = {
|
|
62
|
-
PUT: 'PUT',
|
|
63
|
-
PATCH: 'PATCH',
|
|
64
|
-
DELETE: 'DELETE'
|
|
65
|
-
};
|
|
66
|
-
const CrudEntry = t.object({
|
|
67
|
-
op: t.Enum(OpType),
|
|
68
|
-
type: t.string,
|
|
69
|
-
id: t.string,
|
|
70
|
-
op_id: t.number.optional(),
|
|
71
|
-
data: t.any.optional()
|
|
72
|
-
});
|
|
73
|
-
const CrudRequest = t.object({
|
|
74
|
-
data: t.array(CrudEntry),
|
|
75
|
-
write_checkpoint: t.boolean.optional()
|
|
76
|
-
});
|
|
77
|
-
export const crud = routeDefinition({
|
|
78
|
-
path: '/crud.json',
|
|
79
|
-
method: router.HTTPMethod.POST,
|
|
80
|
-
validator: schema.createTsCodecValidator(CrudRequest, { allowAdditional: true }),
|
|
81
|
-
authorize: authUser,
|
|
82
|
-
handler: async (payload) => {
|
|
83
|
-
const { user_id, system } = payload.context;
|
|
84
|
-
const pool = system.requirePgPool();
|
|
85
|
-
if (!system.config.dev.crud_api) {
|
|
86
|
-
throw new Error('CRUD api disabled');
|
|
87
|
-
}
|
|
88
|
-
const params = payload.params;
|
|
89
|
-
let statements = [];
|
|
90
|
-
// Implementation note:
|
|
91
|
-
// Postgres does automatic "assigment cast" for query literals,
|
|
92
|
-
// e.g. a string literal to uuid. However, the same doesn't apply
|
|
93
|
-
// to query parameters.
|
|
94
|
-
// To handle those automatically, we use `json_populate_record`
|
|
95
|
-
// to automatically cast to the correct types.
|
|
96
|
-
for (let op of params.data) {
|
|
97
|
-
const table = util.escapeIdentifier(op.type);
|
|
98
|
-
if (op.op == 'PUT') {
|
|
99
|
-
const data = op.data;
|
|
100
|
-
const with_id = { ...data, id: op.id };
|
|
101
|
-
const columnsEscaped = Object.keys(with_id).map(util.escapeIdentifier);
|
|
102
|
-
const columnsJoined = columnsEscaped.join(', ');
|
|
103
|
-
let updateClauses = [];
|
|
104
|
-
for (let key of Object.keys(data)) {
|
|
105
|
-
updateClauses.push(`${util.escapeIdentifier(key)} = EXCLUDED.${util.escapeIdentifier(key)}`);
|
|
106
|
-
}
|
|
107
|
-
const updateClause = updateClauses.length > 0 ? `DO UPDATE SET ${updateClauses.join(', ')}` : `DO NOTHING`;
|
|
108
|
-
const statement = `
|
|
109
|
-
WITH data_row AS (
|
|
110
|
-
SELECT (json_populate_record(null::${table}, $1::json)).*
|
|
111
|
-
)
|
|
112
|
-
INSERT INTO ${table} (${columnsJoined})
|
|
113
|
-
SELECT ${columnsJoined} FROM data_row
|
|
114
|
-
ON CONFLICT(id) ${updateClause}`;
|
|
115
|
-
statements.push({
|
|
116
|
-
statement: statement,
|
|
117
|
-
params: [{ type: 'varchar', value: JSON.stringify(with_id) }]
|
|
118
|
-
});
|
|
119
|
-
}
|
|
120
|
-
else if (op.op == 'PATCH') {
|
|
121
|
-
const data = op.data;
|
|
122
|
-
const with_id = { ...data, id: op.id };
|
|
123
|
-
let updateClauses = [];
|
|
124
|
-
for (let key of Object.keys(data)) {
|
|
125
|
-
updateClauses.push(`${util.escapeIdentifier(key)} = data_row.${util.escapeIdentifier(key)}`);
|
|
126
|
-
}
|
|
127
|
-
const statement = `
|
|
128
|
-
WITH data_row AS (
|
|
129
|
-
SELECT (json_populate_record(null::${table}, $1::json)).*
|
|
130
|
-
)
|
|
131
|
-
UPDATE ${table}
|
|
132
|
-
SET ${updateClauses.join(', ')}
|
|
133
|
-
FROM data_row
|
|
134
|
-
WHERE ${table}.id = data_row.id`;
|
|
135
|
-
statements.push({
|
|
136
|
-
statement: statement,
|
|
137
|
-
params: [{ type: 'varchar', value: JSON.stringify(with_id) }]
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
else if (op.op == 'DELETE') {
|
|
141
|
-
statements.push({
|
|
142
|
-
statement: `
|
|
143
|
-
WITH data_row AS (
|
|
144
|
-
SELECT (json_populate_record(null::${table}, $1::json)).*
|
|
145
|
-
)
|
|
146
|
-
DELETE FROM ${table}
|
|
147
|
-
USING data_row
|
|
148
|
-
WHERE ${table}.id = data_row.id`,
|
|
149
|
-
params: [{ type: 'varchar', value: JSON.stringify({ id: op.id }) }]
|
|
150
|
-
});
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
await pool.query(...statements);
|
|
154
|
-
const storage = system.storage;
|
|
155
|
-
if (payload.params.write_checkpoint === true) {
|
|
156
|
-
const write_checkpoint = await util.createWriteCheckpoint(pool, storage, payload.context.user_id);
|
|
157
|
-
return { write_checkpoint: String(write_checkpoint) };
|
|
158
|
-
}
|
|
159
|
-
else if (payload.params.write_checkpoint === false) {
|
|
160
|
-
return {};
|
|
161
|
-
}
|
|
162
|
-
else {
|
|
163
|
-
// Legacy
|
|
164
|
-
const checkpoint = await util.getClientCheckpoint(pool, storage);
|
|
165
|
-
return {
|
|
166
|
-
checkpoint
|
|
167
|
-
};
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
});
|
|
171
|
-
export const DEV_ROUTES = [auth, auth2, token, crud];
|
|
172
|
-
//# sourceMappingURL=dev.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"dev.js","sourceRoot":"","sources":["../../../src/routes/endpoints/dev.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,CAAC,MAAM,UAAU,CAAC;AAE9B,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,mCAAmC,CAAC;AAE3E,OAAO,KAAK,IAAI,MAAM,0BAA0B,CAAC;AACjD,OAAO,EAAE,WAAW,EAAE,QAAQ,EAAE,QAAQ,EAAE,aAAa,EAAE,mBAAmB,EAAE,mBAAmB,EAAE,MAAM,YAAY,CAAC;AACtH,OAAO,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAE/C,MAAM,UAAU,GAAG,CAAC,CAAC,MAAM,CAAC;IAC1B,IAAI,EAAE,CAAC,CAAC,MAAM;IACd,QAAQ,EAAE,CAAC,CAAC,MAAM;CACnB,CAAC,CAAC;AAEH,2CAA2C;AAC3C,MAAM,CAAC,MAAM,IAAI,GAAG,eAAe,CAAC;IAClC,IAAI,EAAE,YAAY;IAClB,MAAM,EAAE,MAAM,CAAC,UAAU,CAAC,IAAI;IAC9B,SAAS,EAAE,MAAM,CAAC,sBAAsB,CAAC,UAAU,EAAE,EAAE,eAAe,EAAE,IAAI,EAAE,CAAC;IAC/E,OAAO,EAAE,KAAK,EAAE,O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AAK,CAAC,GAAG,UAAU,CAAC,CAAC;QAEhC,MAAM,OAAO,GAAG,MAAM,CAAC,OAAO,CAAC;QAC/B,IAAI,OAAO,CAAC,MAAM,CAAC,gBAAgB,KAAK,IAAI,EAAE;YAC5C,MAAM,gBAAgB,GAAG,MAAM,IAAI,CAAC,qBAAqB,CAAC,IAAI,EAAE,OAAO,EAAE,OAAO,CAAC,OAAO,CAAC,OAAQ,CAAC,CAAC;YACnG,OAAO,EAAE,gBAAgB,EAAE,MAAM,CAAC,gBAAgB,CAAC,EAAE,CAAC;SACvD;aAAM,IAAI,OAAO,CAAC,MAAM,CAAC,gBAAgB,KAAK,KAAK,EAAE;YACpD,OAAO,EAAE,CAAC;SACX;aAAM;YACL,SAAS;YACT,MAAM,UAAU,GAAG,MAAM,IAAI,CAAC,mBAAmB,CAAC,IAAI,EAAE,OAAO,CAAC,CAAC;YACjE,OAAO;gBACL,UAAU;aACX,CAAC;SACH;IACH,CAAC;CACF,CAAC,CAAC;AAEH,MAAM,CAAC,MAAM,UAAU,GAAG,CAAC,IAAI,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC"}
|
|
@@ -1,199 +0,0 @@
|
|
|
1
|
-
import * as t from 'ts-codec';
|
|
2
|
-
import * as pgwire from '@powersync/service-jpgwire';
|
|
3
|
-
import { errors, router, schema } from '@powersync/lib-services-framework';
|
|
4
|
-
|
|
5
|
-
import * as util from '../../util/util-index.js';
|
|
6
|
-
import { authDevUser, authUser, endpoint, issueDevToken, issueLegacyDevToken, issuePowerSyncToken } from '../auth.js';
|
|
7
|
-
import { routeDefinition } from '../router.js';
|
|
8
|
-
|
|
9
|
-
const AuthParams = t.object({
|
|
10
|
-
user: t.string,
|
|
11
|
-
password: t.string
|
|
12
|
-
});
|
|
13
|
-
|
|
14
|
-
// For legacy web client only. Remove soon.
|
|
15
|
-
export const auth = routeDefinition({
|
|
16
|
-
path: '/auth.json',
|
|
17
|
-
method: router.HTTPMethod.POST,
|
|
18
|
-
validator: schema.createTsCodecValidator(AuthParams, { allowAdditional: true }),
|
|
19
|
-
handler: async (payload) => {
|
|
20
|
-
const { user, password } = payload.params;
|
|
21
|
-
const config = payload.context.system.config;
|
|
22
|
-
|
|
23
|
-
if (config.dev.demo_auth == false || config.dev.demo_password == null) {
|
|
24
|
-
throw new errors.AuthorizationError(['Demo auth disabled']);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
if (password == config.dev.demo_password) {
|
|
28
|
-
const token = await issueLegacyDevToken(payload.request, user, payload.context.system.config);
|
|
29
|
-
return { token, user_id: user, endpoint: endpoint(payload.request) };
|
|
30
|
-
} else {
|
|
31
|
-
throw new errors.AuthorizationError(['Authentication failed']);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
});
|
|
35
|
-
|
|
36
|
-
export const auth2 = routeDefinition({
|
|
37
|
-
path: '/dev/auth.json',
|
|
38
|
-
method: router.HTTPMethod.POST,
|
|
39
|
-
validator: schema.createTsCodecValidator(AuthParams, { allowAdditional: true }),
|
|
40
|
-
handler: async (payload) => {
|
|
41
|
-
const { user, password } = payload.params;
|
|
42
|
-
const config = payload.context.system.config;
|
|
43
|
-
|
|
44
|
-
if (config.dev.demo_auth == false || config.dev.demo_password == null) {
|
|
45
|
-
throw new errors.AuthorizationError(['Demo auth disabled']);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
if (password == config.dev.demo_password) {
|
|
49
|
-
const token = await issueDevToken(payload.request, user, payload.context.system.config);
|
|
50
|
-
return { token, user_id: user };
|
|
51
|
-
} else {
|
|
52
|
-
throw new errors.AuthorizationError(['Authentication failed']);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
});
|
|
56
|
-
|
|
57
|
-
const TokenParams = t.object({});
|
|
58
|
-
|
|
59
|
-
export const token = routeDefinition({
|
|
60
|
-
path: '/dev/token.json',
|
|
61
|
-
method: router.HTTPMethod.POST,
|
|
62
|
-
validator: schema.createTsCodecValidator(TokenParams, { allowAdditional: true }),
|
|
63
|
-
authorize: authDevUser,
|
|
64
|
-
handler: async (payload) => {
|
|
65
|
-
const { user_id } = payload.context;
|
|
66
|
-
const outToken = await issuePowerSyncToken(payload.request, user_id!, payload.context.system.config);
|
|
67
|
-
return { token: outToken, user_id: user_id, endpoint: endpoint(payload.request) };
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
|
|
71
|
-
const OpType = {
|
|
72
|
-
PUT: 'PUT',
|
|
73
|
-
PATCH: 'PATCH',
|
|
74
|
-
DELETE: 'DELETE'
|
|
75
|
-
};
|
|
76
|
-
|
|
77
|
-
const CrudEntry = t.object({
|
|
78
|
-
op: t.Enum(OpType),
|
|
79
|
-
type: t.string,
|
|
80
|
-
id: t.string,
|
|
81
|
-
op_id: t.number.optional(),
|
|
82
|
-
data: t.any.optional()
|
|
83
|
-
});
|
|
84
|
-
|
|
85
|
-
const CrudRequest = t.object({
|
|
86
|
-
data: t.array(CrudEntry),
|
|
87
|
-
write_checkpoint: t.boolean.optional()
|
|
88
|
-
});
|
|
89
|
-
|
|
90
|
-
export const crud = routeDefinition({
|
|
91
|
-
path: '/crud.json',
|
|
92
|
-
method: router.HTTPMethod.POST,
|
|
93
|
-
validator: schema.createTsCodecValidator(CrudRequest, { allowAdditional: true }),
|
|
94
|
-
authorize: authUser,
|
|
95
|
-
|
|
96
|
-
handler: async (payload) => {
|
|
97
|
-
const { user_id, system } = payload.context;
|
|
98
|
-
|
|
99
|
-
const pool = system.requirePgPool();
|
|
100
|
-
|
|
101
|
-
if (!system.config.dev.crud_api) {
|
|
102
|
-
throw new Error('CRUD api disabled');
|
|
103
|
-
}
|
|
104
|
-
|
|
105
|
-
const params = payload.params;
|
|
106
|
-
|
|
107
|
-
let statements: pgwire.Statement[] = [];
|
|
108
|
-
|
|
109
|
-
// Implementation note:
|
|
110
|
-
// Postgres does automatic "assigment cast" for query literals,
|
|
111
|
-
// e.g. a string literal to uuid. However, the same doesn't apply
|
|
112
|
-
// to query parameters.
|
|
113
|
-
// To handle those automatically, we use `json_populate_record`
|
|
114
|
-
// to automatically cast to the correct types.
|
|
115
|
-
|
|
116
|
-
for (let op of params.data) {
|
|
117
|
-
const table = util.escapeIdentifier(op.type);
|
|
118
|
-
if (op.op == 'PUT') {
|
|
119
|
-
const data = op.data as Record<string, any>;
|
|
120
|
-
const with_id = { ...data, id: op.id };
|
|
121
|
-
|
|
122
|
-
const columnsEscaped = Object.keys(with_id).map(util.escapeIdentifier);
|
|
123
|
-
const columnsJoined = columnsEscaped.join(', ');
|
|
124
|
-
|
|
125
|
-
let updateClauses: string[] = [];
|
|
126
|
-
|
|
127
|
-
for (let key of Object.keys(data)) {
|
|
128
|
-
updateClauses.push(`${util.escapeIdentifier(key)} = EXCLUDED.${util.escapeIdentifier(key)}`);
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const updateClause = updateClauses.length > 0 ? `DO UPDATE SET ${updateClauses.join(', ')}` : `DO NOTHING`;
|
|
132
|
-
|
|
133
|
-
const statement = `
|
|
134
|
-
WITH data_row AS (
|
|
135
|
-
SELECT (json_populate_record(null::${table}, $1::json)).*
|
|
136
|
-
)
|
|
137
|
-
INSERT INTO ${table} (${columnsJoined})
|
|
138
|
-
SELECT ${columnsJoined} FROM data_row
|
|
139
|
-
ON CONFLICT(id) ${updateClause}`;
|
|
140
|
-
|
|
141
|
-
statements.push({
|
|
142
|
-
statement: statement,
|
|
143
|
-
params: [{ type: 'varchar', value: JSON.stringify(with_id) }]
|
|
144
|
-
});
|
|
145
|
-
} else if (op.op == 'PATCH') {
|
|
146
|
-
const data = op.data as Record<string, any>;
|
|
147
|
-
const with_id = { ...data, id: op.id };
|
|
148
|
-
|
|
149
|
-
let updateClauses: string[] = [];
|
|
150
|
-
|
|
151
|
-
for (let key of Object.keys(data)) {
|
|
152
|
-
updateClauses.push(`${util.escapeIdentifier(key)} = data_row.${util.escapeIdentifier(key)}`);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
const statement = `
|
|
156
|
-
WITH data_row AS (
|
|
157
|
-
SELECT (json_populate_record(null::${table}, $1::json)).*
|
|
158
|
-
)
|
|
159
|
-
UPDATE ${table}
|
|
160
|
-
SET ${updateClauses.join(', ')}
|
|
161
|
-
FROM data_row
|
|
162
|
-
WHERE ${table}.id = data_row.id`;
|
|
163
|
-
|
|
164
|
-
statements.push({
|
|
165
|
-
statement: statement,
|
|
166
|
-
params: [{ type: 'varchar', value: JSON.stringify(with_id) }]
|
|
167
|
-
});
|
|
168
|
-
} else if (op.op == 'DELETE') {
|
|
169
|
-
statements.push({
|
|
170
|
-
statement: `
|
|
171
|
-
WITH data_row AS (
|
|
172
|
-
SELECT (json_populate_record(null::${table}, $1::json)).*
|
|
173
|
-
)
|
|
174
|
-
DELETE FROM ${table}
|
|
175
|
-
USING data_row
|
|
176
|
-
WHERE ${table}.id = data_row.id`,
|
|
177
|
-
params: [{ type: 'varchar', value: JSON.stringify({ id: op.id }) }]
|
|
178
|
-
});
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
await pool.query(...statements);
|
|
182
|
-
|
|
183
|
-
const storage = system.storage;
|
|
184
|
-
if (payload.params.write_checkpoint === true) {
|
|
185
|
-
const write_checkpoint = await util.createWriteCheckpoint(pool, storage, payload.context.user_id!);
|
|
186
|
-
return { write_checkpoint: String(write_checkpoint) };
|
|
187
|
-
} else if (payload.params.write_checkpoint === false) {
|
|
188
|
-
return {};
|
|
189
|
-
} else {
|
|
190
|
-
// Legacy
|
|
191
|
-
const checkpoint = await util.getClientCheckpoint(pool, storage);
|
|
192
|
-
return {
|
|
193
|
-
checkpoint
|
|
194
|
-
};
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
});
|
|
198
|
-
|
|
199
|
-
export const DEV_ROUTES = [auth, auth2, token, crud];
|