-
Notifications
You must be signed in to change notification settings - Fork 234
Expand file tree
/
Copy pathsdk.ts
More file actions
590 lines (560 loc) · 22.2 KB
/
sdk.ts
File metadata and controls
590 lines (560 loc) · 22.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
import { sdk } from '$lib/stores/sdk';
import type { Page } from '@sveltejs/kit';
import type { TerminologyResult } from './types';
import {
type DatabaseType,
type Entity,
type EntityList,
type Index,
type Record,
type RecordList,
toSupportiveEntity,
toSupportiveRecord,
toSupportiveIndex
} from './terminology';
import type {
Models,
OrderBy,
TablesDBIndexType,
DocumentsDBIndexType,
VectorsDBIndexType
} from '@appwrite.io/console';
export type DatabaseSdkResult = {
create: (
type: DatabaseType,
params: {
databaseId: string;
name: string;
enabled?: boolean;
}
) => Promise<Models.Database>;
list: (params: { queries?: string[]; search?: string }) => Promise<Models.DatabaseList>;
createEntity: (params: {
databaseId: string;
entityId: string;
name: string;
databaseType?: DatabaseType;
dimension?: number /* vectorsDB specific */;
}) => Promise<Entity>;
getEntity: (params: {
databaseId: string;
entityId: string;
databaseType?: DatabaseType;
}) => Promise<Entity>;
listEntities: (params: {
databaseId: string;
queries?: string[];
search?: string;
databaseType?: DatabaseType;
}) => Promise<EntityList>;
delete: (params: { databaseId: string; databaseType?: DatabaseType }) => Promise<{}>;
deleteEntity: (params: {
databaseId: string;
entityId: string;
databaseType?: DatabaseType;
}) => Promise<{}>;
updateEntity: (params: {
databaseId: string;
entityId: string;
name?: string;
permissions?: string[];
documentSecurity?: boolean;
enabled?: boolean;
databaseType?: DatabaseType;
}) => Promise<Entity>;
createRecord: (params: {
databaseId: string;
entityId: string;
recordId: string;
data?: object;
permissions?: string[];
databaseType?: DatabaseType;
}) => Promise<Record>;
updateRecord: (params: {
databaseId: string;
entityId: string;
recordId: string;
data?: object;
permissions?: string[];
databaseType?: DatabaseType;
}) => Promise<Record>;
updateRecordPermissions: (params: {
databaseId: string;
entityId: string;
recordId: string;
permissions: string[];
databaseType?: DatabaseType;
}) => Promise<Record>;
deleteRecord: (params: {
databaseId: string;
entityId: string;
recordId: string;
databaseType?: DatabaseType;
}) => Promise<Record>;
deleteRecords: (params: {
databaseId: string;
entityId: string;
queries?: string[];
databaseType?: DatabaseType;
}) => Promise<RecordList>;
createIndex: (params: {
databaseId: string;
entityId: string;
key: string;
type: string;
attributes: string[];
lengths?: number[];
orders?: OrderBy[];
databaseType?: DatabaseType;
}) => Promise<Index>;
deleteIndex: (params: {
databaseId: string;
entityId: string;
key: string;
databaseType?: DatabaseType;
}) => Promise<{}>;
};
/**
* Returns the raw DocumentsDB or VectorsDB SDK service for a given database type.
* Use in load functions (.ts) where Svelte runes aren't available.
*/
export function getCollectionService(region: string, project: string, type: DatabaseType) {
const projectSdk = sdk.forProject(region, project);
switch (type) {
case 'documentsdb':
return projectSdk.documentsDB;
case 'vectorsdb':
return projectSdk.vectorsDB;
default:
throw new Error(`Unsupported collection database type: ${type}`);
}
}
export function useDatabaseSdk(
regionOrPage: string | Page,
projectOrTerminology: string | TerminologyResult,
databaseType?: DatabaseType /* nullable for use at top `databases` level */
): DatabaseSdkResult {
let region: string;
let project: string;
let type: DatabaseType;
if (typeof regionOrPage === 'object' && typeof projectOrTerminology === 'object') {
type = projectOrTerminology.type;
region = regionOrPage?.params?.region || '';
project = regionOrPage?.params?.project || '';
} else {
type = databaseType!;
region = regionOrPage as string;
project = projectOrTerminology as string;
}
const baseSdk = sdk.forProject(region, project);
return {
async create(type, params): Promise<Models.Database> {
switch (type) {
case 'legacy': /* databases api */
case 'tablesdb': {
return await baseSdk.tablesDB.create(params);
}
case 'documentsdb': {
return await baseSdk.documentsDB.create(params);
}
case 'vectorsdb': {
return await baseSdk.vectorsDB.create(params);
}
default:
throw new Error('Unknown database type');
}
},
async list(params): Promise<Models.DatabaseList> {
const results = await Promise.all([
baseSdk.tablesDB.list(params),
baseSdk.documentsDB.list(params),
baseSdk.vectorsDB.list(params)
]);
return results.reduce(
(acc, curr) => ({
total: acc.total + curr.total,
databases: [...acc.databases, ...curr.databases]
}),
{ total: 0, databases: [] as Models.Database[] }
);
},
async createEntity(params) {
switch (type ?? params.databaseType) {
case 'legacy': /* databases api */
case 'tablesdb': {
const table = await baseSdk.tablesDB.createTable({
...params,
tableId: params.entityId
});
return toSupportiveEntity(table);
}
case 'documentsdb': {
const table = await baseSdk.documentsDB.createCollection({
...params,
collectionId: params.entityId
});
return toSupportiveEntity(table);
}
case 'vectorsdb': {
const collection = await baseSdk.vectorsDB.createCollection({
...params,
dimension: params.dimension,
collectionId: params.entityId
});
return toSupportiveEntity(collection);
}
default:
throw new Error('Unknown database type');
}
},
async listEntities(params) {
switch (type ?? params.databaseType) {
case 'legacy': /* databases api */
case 'tablesdb': {
const { total, tables } = await baseSdk.tablesDB.listTables(params);
return { total, entities: tables.map(toSupportiveEntity) };
}
case 'documentsdb': {
const { total, collections } =
await baseSdk.documentsDB.listCollections(params);
return { total, entities: collections.map(toSupportiveEntity) };
}
case 'vectorsdb': {
const { total, collections } = await baseSdk.vectorsDB.listCollections(params);
return { total, entities: collections.map(toSupportiveEntity) };
}
default:
throw new Error(`Unknown database type`);
}
},
async getEntity(params) {
switch (type ?? params.databaseType) {
case 'legacy': /* databases api */
case 'tablesdb': {
const table = await baseSdk.tablesDB.getTable({
databaseId: params.databaseId,
tableId: params.entityId
});
return toSupportiveEntity(table);
}
case 'documentsdb': {
const collection = await baseSdk.documentsDB.getCollection({
databaseId: params.databaseId,
collectionId: params.entityId
});
return toSupportiveEntity(collection);
}
case 'vectorsdb': {
const collection = await baseSdk.vectorsDB.getCollection({
databaseId: params.databaseId,
collectionId: params.entityId
});
return toSupportiveEntity(collection);
}
default:
throw new Error(`Unknown database type`);
}
},
async delete(params) {
switch (type ?? params.databaseType) {
case 'legacy': /* databases api */
case 'tablesdb':
return await baseSdk.tablesDB.delete(params);
case 'documentsdb':
return await baseSdk.documentsDB.delete(params);
case 'vectorsdb':
return await baseSdk.vectorsDB.delete(params);
default:
throw new Error(`Unknown database type`);
}
},
async deleteEntity(params) {
switch (type ?? params.databaseType) {
case 'legacy': /* databases api */
case 'tablesdb':
return await baseSdk.tablesDB.deleteTable({
databaseId: params.databaseId,
tableId: params.entityId
});
case 'documentsdb':
return await baseSdk.documentsDB.deleteCollection({
databaseId: params.databaseId,
collectionId: params.entityId
});
case 'vectorsdb':
return await baseSdk.vectorsDB.deleteCollection({
databaseId: params.databaseId,
collectionId: params.entityId
});
default:
throw new Error(`Unknown database type`);
}
},
async updateEntity(params) {
switch (type ?? params.databaseType) {
case 'legacy': /* databases api */
case 'tablesdb':
return toSupportiveEntity(
await baseSdk.tablesDB.updateTable({
databaseId: params.databaseId,
tableId: params.entityId,
name: params.name,
permissions: params.permissions,
rowSecurity: params.documentSecurity,
enabled: params.enabled
})
);
case 'documentsdb':
return toSupportiveEntity(
await baseSdk.documentsDB.updateCollection({
databaseId: params.databaseId,
collectionId: params.entityId,
name: params.name,
permissions: params.permissions,
documentSecurity: params.documentSecurity,
enabled: params.enabled
})
);
case 'vectorsdb':
return toSupportiveEntity(
await baseSdk.vectorsDB.updateCollection({
databaseId: params.databaseId,
collectionId: params.entityId,
name: params.name,
permissions: params.permissions,
documentSecurity: params.documentSecurity,
enabled: params.enabled
})
);
default:
throw new Error(`Unknown database type`);
}
},
async createRecord(params) {
switch (type ?? params.databaseType) {
case 'legacy': /* databases api */
case 'tablesdb':
return await baseSdk.tablesDB.createRow({
databaseId: params.databaseId,
tableId: params.entityId,
rowId: params.recordId,
data: params.data,
permissions: params.permissions
});
case 'documentsdb':
return await baseSdk.documentsDB.createDocument({
databaseId: params.databaseId,
collectionId: params.entityId,
documentId: params.recordId,
data: params.data,
permissions: params.permissions
});
case 'vectorsdb': {
return await baseSdk.vectorsDB.createDocument({
databaseId: params.databaseId,
collectionId: params.entityId,
documentId: params.recordId,
data: params.data,
permissions: params.permissions
});
}
default:
throw new Error(`Unknown database type`);
}
},
async updateRecord(params) {
switch (type ?? params.databaseType) {
case 'legacy': /* databases api */
case 'tablesdb':
return await baseSdk.tablesDB.updateRow({
databaseId: params.databaseId,
tableId: params.entityId,
rowId: params.recordId,
data: params.data,
permissions: params.permissions
});
case 'documentsdb':
return await baseSdk.documentsDB.upsertDocument({
databaseId: params.databaseId,
collectionId: params.entityId,
documentId: params.recordId,
data: params.data,
permissions: params.permissions
});
case 'vectorsdb': {
return await baseSdk.vectorsDB.upsertDocument({
databaseId: params.databaseId,
collectionId: params.entityId,
documentId: params.recordId,
data: params.data,
permissions: params.permissions
});
}
default:
throw new Error(`Unknown database type`);
}
},
async updateRecordPermissions(params) {
switch (type ?? params.databaseType) {
case 'legacy': /* databases api */
case 'tablesdb':
return await baseSdk.tablesDB.updateRow({
databaseId: params.databaseId,
tableId: params.entityId,
rowId: params.recordId,
permissions: params.permissions
});
case 'documentsdb':
return await baseSdk.documentsDB.upsertDocument({
databaseId: params.databaseId,
collectionId: params.entityId,
documentId: params.recordId,
permissions: params.permissions
});
case 'vectorsdb': {
return await baseSdk.vectorsDB.upsertDocument({
databaseId: params.databaseId,
collectionId: params.entityId,
documentId: params.recordId,
permissions: params.permissions
});
}
default:
throw new Error(`Unknown database type`);
}
},
async deleteRecord(params) {
switch (type ?? params.databaseType) {
case 'legacy': /* databases api */
case 'tablesdb': {
const row = await baseSdk.tablesDB.deleteRow({
databaseId: params.databaseId,
tableId: params.entityId,
rowId: params.recordId
});
return toSupportiveRecord(row);
}
case 'documentsdb': {
const document = await baseSdk.documentsDB.deleteDocument({
databaseId: params.databaseId,
collectionId: params.entityId,
documentId: params.recordId
});
return toSupportiveRecord(document);
}
case 'vectorsdb': {
if (!params.recordId) {
throw new Error('Record ID is required to delete a VectorsDB document');
}
const document = await baseSdk.vectorsDB.deleteDocument({
databaseId: params.databaseId,
collectionId: params.entityId,
documentId: params.recordId
});
return toSupportiveRecord(document);
}
default:
throw new Error(`Unknown database type`);
}
},
async deleteRecords(params) {
switch (type ?? params.databaseType) {
case 'legacy': /* databases api */
case 'tablesdb': {
const { total, rows } = await baseSdk.tablesDB.deleteRows({
databaseId: params.databaseId,
tableId: params.entityId,
queries: params.queries
});
return { total, records: rows.map(toSupportiveRecord) };
}
case 'documentsdb': {
const { total, documents } = await baseSdk.documentsDB.deleteDocuments({
databaseId: params.databaseId,
collectionId: params.entityId,
queries: params.queries
});
return { total, records: documents.map(toSupportiveRecord) };
}
case 'vectorsdb': {
const { total, documents } = await baseSdk.vectorsDB.deleteDocuments({
databaseId: params.databaseId,
collectionId: params.entityId,
queries: params.queries
});
return { total, records: documents.map(toSupportiveRecord) };
}
default:
throw new Error(`Unknown database type`);
}
},
async createIndex(params) {
switch (type ?? params.databaseType) {
case 'legacy': /* databases api */
case 'tablesdb': {
const index = await baseSdk.tablesDB.createIndex({
databaseId: params.databaseId,
tableId: params.entityId,
key: params.key,
type: params.type as TablesDBIndexType,
columns: params.attributes,
lengths: params.lengths,
orders: params.orders
});
return toSupportiveIndex(index);
}
case 'documentsdb': {
const index = await baseSdk.documentsDB.createIndex({
databaseId: params.databaseId,
collectionId: params.entityId,
key: params.key,
type: params.type as DocumentsDBIndexType,
attributes: params.attributes,
lengths: params.lengths,
orders: params.orders
});
return toSupportiveIndex(index);
}
case 'vectorsdb': {
const index = await baseSdk.vectorsDB.createIndex({
databaseId: params.databaseId,
collectionId: params.entityId,
key: params.key,
type: params.type as VectorsDBIndexType,
attributes: params.attributes,
lengths: params.lengths,
orders: params.orders
});
return toSupportiveIndex(index);
}
default:
throw new Error(`Unknown database type`);
}
},
async deleteIndex(params) {
switch (type ?? params.databaseType) {
case 'legacy': /* databases api */
case 'tablesdb':
return await baseSdk.tablesDB.deleteIndex({
databaseId: params.databaseId,
tableId: params.entityId,
key: params.key
});
case 'documentsdb':
return await baseSdk.documentsDB.deleteIndex({
databaseId: params.databaseId,
collectionId: params.entityId,
key: params.key
});
case 'vectorsdb':
return await baseSdk.vectorsDB.deleteIndex({
databaseId: params.databaseId,
collectionId: params.entityId,
key: params.key
});
default:
throw new Error(`Unknown database type`);
}
}
};
}