@xata.io/client 0.0.0-beta.3b2daff → 0.0.0-beta.3d1e440
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/.eslintrc.cjs +13 -0
- package/CHANGELOG.md +12 -0
- package/dist/api/client.d.ts +1 -1
- package/dist/api/client.js +4 -3
- package/dist/api/components.d.ts +27 -31
- package/dist/api/components.js +27 -31
- package/dist/api/fetcher.js +11 -16
- package/dist/api/providers.js +3 -2
- package/dist/api/responses.d.ts +6 -0
- package/dist/schema/filters.js +2 -1
- package/dist/schema/repository.d.ts +13 -13
- package/dist/schema/repository.js +58 -36
- package/dist/util/lang.d.ts +3 -0
- package/dist/util/lang.js +13 -1
- package/package.json +2 -2
package/.eslintrc.cjs
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
module.exports = {
|
2
|
+
ignorePatterns: ["dist"],
|
3
|
+
parserOptions: {
|
4
|
+
ecmaVersion: 2020,
|
5
|
+
sourceType: 'module',
|
6
|
+
project: 'client/tsconfig.json'
|
7
|
+
},
|
8
|
+
rules: {
|
9
|
+
'@typescript-eslint/no-explicit-any': 'off',
|
10
|
+
'@typescript-eslint/ban-types': 'off',
|
11
|
+
'@typescript-eslint/no-floating-promises': 'error',
|
12
|
+
}
|
13
|
+
};
|
package/CHANGELOG.md
CHANGED
package/dist/api/client.d.ts
CHANGED
package/dist/api/client.js
CHANGED
@@ -17,14 +17,15 @@ const components_1 = require("./components");
|
|
17
17
|
const providers_1 = require("./providers");
|
18
18
|
class XataApiClient {
|
19
19
|
constructor(options) {
|
20
|
-
var _a;
|
20
|
+
var _a, _b;
|
21
21
|
_XataApiClient_extraProps.set(this, void 0);
|
22
|
-
const
|
22
|
+
const globalFetch = typeof fetch !== 'undefined' ? fetch : undefined;
|
23
|
+
const fetchImpl = (_a = options.fetch) !== null && _a !== void 0 ? _a : globalFetch;
|
23
24
|
if (!fetchImpl) {
|
24
25
|
/** @todo add a link after docs exist */
|
25
26
|
throw new Error(`The \`fetch\` option passed to the Xata client is resolving to a falsy value and may not be correctly imported.`);
|
26
27
|
}
|
27
|
-
const provider = (
|
28
|
+
const provider = (_b = options.host) !== null && _b !== void 0 ? _b : 'production';
|
28
29
|
__classPrivateFieldSet(this, _XataApiClient_extraProps, {
|
29
30
|
apiUrl: (0, providers_1.getHostUrl)(provider, 'main'),
|
30
31
|
workspacesApiUrl: (0, providers_1.getHostUrl)(provider, 'workspaces'),
|
package/dist/api/components.d.ts
CHANGED
@@ -875,7 +875,7 @@ export declare type QueryTableVariables = {
|
|
875
875
|
* `$none`, etc.
|
876
876
|
*
|
877
877
|
* All operators start with an `$` to differentiate them from column names
|
878
|
-
* (which are not allowed to start with an
|
878
|
+
* (which are not allowed to start with an dollar sign).
|
879
879
|
*
|
880
880
|
* #### Exact matching and control operators
|
881
881
|
*
|
@@ -943,27 +943,17 @@ export declare type QueryTableVariables = {
|
|
943
943
|
* }
|
944
944
|
* ```
|
945
945
|
*
|
946
|
-
* If you want to OR together multiple values, you can use an array of values:
|
946
|
+
* If you want to OR together multiple values, you can use the `$any` operator with an array of values:
|
947
947
|
*
|
948
948
|
* ```json
|
949
949
|
* {
|
950
950
|
* "filter": {
|
951
|
-
* "settings.plan": ["free", "paid"]
|
951
|
+
* "settings.plan": {"$any": ["free", "paid"]}
|
952
952
|
* },
|
953
953
|
* }
|
954
954
|
* ```
|
955
955
|
*
|
956
|
-
*
|
957
|
-
*
|
958
|
-
* ```json
|
959
|
-
* {
|
960
|
-
* "filter": {
|
961
|
-
* "settings.plan": { "$is": ["free", "paid"]}
|
962
|
-
* },
|
963
|
-
* }
|
964
|
-
* ```
|
965
|
-
*
|
966
|
-
* Specifying multiple columns, ANDs them together:
|
956
|
+
* If you specify multiple columns in the same filter, they are logically AND'ed together:
|
967
957
|
*
|
968
958
|
* ```json
|
969
959
|
* {
|
@@ -974,6 +964,8 @@ export declare type QueryTableVariables = {
|
|
974
964
|
* }
|
975
965
|
* ```
|
976
966
|
*
|
967
|
+
* The above matches if both conditions are met.
|
968
|
+
*
|
977
969
|
* To be more explicit about it, you can use `$all` or `$any`:
|
978
970
|
*
|
979
971
|
* ```json
|
@@ -981,13 +973,13 @@ export declare type QueryTableVariables = {
|
|
981
973
|
* "filter": {
|
982
974
|
* "$any": {
|
983
975
|
* "settings.dark": true,
|
984
|
-
* "settings.plan": "free"
|
976
|
+
* "settings.plan": "free"
|
985
977
|
* }
|
986
978
|
* },
|
987
979
|
* }
|
988
980
|
* ```
|
989
981
|
*
|
990
|
-
* `$all` and `$any` can also receive an array of objects, which allows for repeating
|
982
|
+
* The `$all` and `$any` operators can also receive an array of objects, which allows for repeating column names:
|
991
983
|
*
|
992
984
|
* ```json
|
993
985
|
* {
|
@@ -1030,7 +1022,7 @@ export declare type QueryTableVariables = {
|
|
1030
1022
|
* }
|
1031
1023
|
* ```
|
1032
1024
|
*
|
1033
|
-
*
|
1025
|
+
* Or you can use the inverse operator `$notExists`:
|
1034
1026
|
*
|
1035
1027
|
* ```json
|
1036
1028
|
* {
|
@@ -1083,7 +1075,7 @@ export declare type QueryTableVariables = {
|
|
1083
1075
|
* }
|
1084
1076
|
* ```
|
1085
1077
|
*
|
1086
|
-
* #### Numeric
|
1078
|
+
* #### Numeric ranges
|
1087
1079
|
*
|
1088
1080
|
* ```json
|
1089
1081
|
* {
|
@@ -1098,18 +1090,6 @@ export declare type QueryTableVariables = {
|
|
1098
1090
|
*
|
1099
1091
|
* The supported operators are `$gt`, `$lt`, `$ge`, `$le`.
|
1100
1092
|
*
|
1101
|
-
* Date ranges would support the same operators, with the date as string in RFC 3339:
|
1102
|
-
*
|
1103
|
-
* ```json
|
1104
|
-
* {
|
1105
|
-
* "filter": {
|
1106
|
-
* "<column_name>": {
|
1107
|
-
* "$gt": "2019-10-12T07:20:50.52Z",
|
1108
|
-
* "$lt": "2021-10-12T07:20:50.52Z"
|
1109
|
-
* }
|
1110
|
-
* }
|
1111
|
-
* }
|
1112
|
-
* ```
|
1113
1093
|
*
|
1114
1094
|
* #### Negations
|
1115
1095
|
*
|
@@ -1162,7 +1142,7 @@ export declare type QueryTableVariables = {
|
|
1162
1142
|
* }
|
1163
1143
|
* ```
|
1164
1144
|
*
|
1165
|
-
* In addition,
|
1145
|
+
* In addition, you can use operators like `$isNot` or `$notExists` to simplify expressions:
|
1166
1146
|
*
|
1167
1147
|
* ```json
|
1168
1148
|
* {
|
@@ -1213,6 +1193,22 @@ export declare type QueryTableVariables = {
|
|
1213
1193
|
* predicate. The `$includes` operator is a synonym for the `$includesAny`
|
1214
1194
|
* operator.
|
1215
1195
|
*
|
1196
|
+
* Here is an example of using the `$includesAll` operator:
|
1197
|
+
*
|
1198
|
+
* ```json
|
1199
|
+
* {
|
1200
|
+
* "filter": {
|
1201
|
+
* "settings.labels": {
|
1202
|
+
* "$includesAll": [
|
1203
|
+
* {"$contains": "label"},
|
1204
|
+
* ]
|
1205
|
+
* }
|
1206
|
+
* }
|
1207
|
+
* }
|
1208
|
+
* ```
|
1209
|
+
*
|
1210
|
+
* The above matches if all label values contain the string "labels".
|
1211
|
+
*
|
1216
1212
|
* ### Sorting
|
1217
1213
|
*
|
1218
1214
|
* Sorting by one element:
|
package/dist/api/components.js
CHANGED
@@ -458,7 +458,7 @@ exports.bulkInsertTableRecords = bulkInsertTableRecords;
|
|
458
458
|
* `$none`, etc.
|
459
459
|
*
|
460
460
|
* All operators start with an `$` to differentiate them from column names
|
461
|
-
* (which are not allowed to start with an
|
461
|
+
* (which are not allowed to start with an dollar sign).
|
462
462
|
*
|
463
463
|
* #### Exact matching and control operators
|
464
464
|
*
|
@@ -526,27 +526,17 @@ exports.bulkInsertTableRecords = bulkInsertTableRecords;
|
|
526
526
|
* }
|
527
527
|
* ```
|
528
528
|
*
|
529
|
-
* If you want to OR together multiple values, you can use an array of values:
|
529
|
+
* If you want to OR together multiple values, you can use the `$any` operator with an array of values:
|
530
530
|
*
|
531
531
|
* ```json
|
532
532
|
* {
|
533
533
|
* "filter": {
|
534
|
-
* "settings.plan": ["free", "paid"]
|
534
|
+
* "settings.plan": {"$any": ["free", "paid"]}
|
535
535
|
* },
|
536
536
|
* }
|
537
537
|
* ```
|
538
538
|
*
|
539
|
-
*
|
540
|
-
*
|
541
|
-
* ```json
|
542
|
-
* {
|
543
|
-
* "filter": {
|
544
|
-
* "settings.plan": { "$is": ["free", "paid"]}
|
545
|
-
* },
|
546
|
-
* }
|
547
|
-
* ```
|
548
|
-
*
|
549
|
-
* Specifying multiple columns, ANDs them together:
|
539
|
+
* If you specify multiple columns in the same filter, they are logically AND'ed together:
|
550
540
|
*
|
551
541
|
* ```json
|
552
542
|
* {
|
@@ -557,6 +547,8 @@ exports.bulkInsertTableRecords = bulkInsertTableRecords;
|
|
557
547
|
* }
|
558
548
|
* ```
|
559
549
|
*
|
550
|
+
* The above matches if both conditions are met.
|
551
|
+
*
|
560
552
|
* To be more explicit about it, you can use `$all` or `$any`:
|
561
553
|
*
|
562
554
|
* ```json
|
@@ -564,13 +556,13 @@ exports.bulkInsertTableRecords = bulkInsertTableRecords;
|
|
564
556
|
* "filter": {
|
565
557
|
* "$any": {
|
566
558
|
* "settings.dark": true,
|
567
|
-
* "settings.plan": "free"
|
559
|
+
* "settings.plan": "free"
|
568
560
|
* }
|
569
561
|
* },
|
570
562
|
* }
|
571
563
|
* ```
|
572
564
|
*
|
573
|
-
* `$all` and `$any` can also receive an array of objects, which allows for repeating
|
565
|
+
* The `$all` and `$any` operators can also receive an array of objects, which allows for repeating column names:
|
574
566
|
*
|
575
567
|
* ```json
|
576
568
|
* {
|
@@ -613,7 +605,7 @@ exports.bulkInsertTableRecords = bulkInsertTableRecords;
|
|
613
605
|
* }
|
614
606
|
* ```
|
615
607
|
*
|
616
|
-
*
|
608
|
+
* Or you can use the inverse operator `$notExists`:
|
617
609
|
*
|
618
610
|
* ```json
|
619
611
|
* {
|
@@ -666,7 +658,7 @@ exports.bulkInsertTableRecords = bulkInsertTableRecords;
|
|
666
658
|
* }
|
667
659
|
* ```
|
668
660
|
*
|
669
|
-
* #### Numeric
|
661
|
+
* #### Numeric ranges
|
670
662
|
*
|
671
663
|
* ```json
|
672
664
|
* {
|
@@ -681,18 +673,6 @@ exports.bulkInsertTableRecords = bulkInsertTableRecords;
|
|
681
673
|
*
|
682
674
|
* The supported operators are `$gt`, `$lt`, `$ge`, `$le`.
|
683
675
|
*
|
684
|
-
* Date ranges would support the same operators, with the date as string in RFC 3339:
|
685
|
-
*
|
686
|
-
* ```json
|
687
|
-
* {
|
688
|
-
* "filter": {
|
689
|
-
* "<column_name>": {
|
690
|
-
* "$gt": "2019-10-12T07:20:50.52Z",
|
691
|
-
* "$lt": "2021-10-12T07:20:50.52Z"
|
692
|
-
* }
|
693
|
-
* }
|
694
|
-
* }
|
695
|
-
* ```
|
696
676
|
*
|
697
677
|
* #### Negations
|
698
678
|
*
|
@@ -745,7 +725,7 @@ exports.bulkInsertTableRecords = bulkInsertTableRecords;
|
|
745
725
|
* }
|
746
726
|
* ```
|
747
727
|
*
|
748
|
-
* In addition,
|
728
|
+
* In addition, you can use operators like `$isNot` or `$notExists` to simplify expressions:
|
749
729
|
*
|
750
730
|
* ```json
|
751
731
|
* {
|
@@ -796,6 +776,22 @@ exports.bulkInsertTableRecords = bulkInsertTableRecords;
|
|
796
776
|
* predicate. The `$includes` operator is a synonym for the `$includesAny`
|
797
777
|
* operator.
|
798
778
|
*
|
779
|
+
* Here is an example of using the `$includesAll` operator:
|
780
|
+
*
|
781
|
+
* ```json
|
782
|
+
* {
|
783
|
+
* "filter": {
|
784
|
+
* "settings.labels": {
|
785
|
+
* "$includesAll": [
|
786
|
+
* {"$contains": "label"},
|
787
|
+
* ]
|
788
|
+
* }
|
789
|
+
* }
|
790
|
+
* }
|
791
|
+
* ```
|
792
|
+
*
|
793
|
+
* The above matches if all label values contain the string "labels".
|
794
|
+
*
|
799
795
|
* ### Sorting
|
800
796
|
*
|
801
797
|
* Sorting by one element:
|
package/dist/api/fetcher.js
CHANGED
@@ -10,12 +10,14 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
10
10
|
};
|
11
11
|
Object.defineProperty(exports, "__esModule", { value: true });
|
12
12
|
exports.fetch = void 0;
|
13
|
+
/* eslint-disable @typescript-eslint/no-throw-literal */
|
14
|
+
/* eslint-disable @typescript-eslint/ban-types */
|
15
|
+
const lang_1 = require("../util/lang");
|
13
16
|
const resolveUrl = (url, queryParams = {}, pathParams = {}) => {
|
14
17
|
const query = new URLSearchParams(queryParams).toString();
|
15
18
|
const queryString = query.length > 0 ? `?${query}` : '';
|
16
19
|
return url.replace(/\{\w*\}/g, (key) => pathParams[key.slice(1, -1)]) + queryString;
|
17
20
|
};
|
18
|
-
const fallbackError = { message: 'Network response was not ok' };
|
19
21
|
function buildBaseUrl({ path, workspacesApiUrl, apiUrl, pathParams }) {
|
20
22
|
if (!(pathParams === null || pathParams === void 0 ? void 0 : pathParams.workspace))
|
21
23
|
return `${apiUrl}${path}`;
|
@@ -51,28 +53,21 @@ function fetch({ url: path, method, body, headers, pathParams, queryParams, fetc
|
|
51
53
|
if (response.ok) {
|
52
54
|
return jsonResponse;
|
53
55
|
}
|
54
|
-
|
55
|
-
|
56
|
-
}
|
57
|
-
else {
|
58
|
-
throw withStatus(fallbackError, response.status);
|
59
|
-
}
|
56
|
+
const { message = 'Unknown error', errors } = jsonResponse;
|
57
|
+
throw withStatus({ message, errors }, response.status);
|
60
58
|
}
|
61
59
|
catch (e) {
|
62
|
-
if (e
|
63
|
-
const error = {
|
64
|
-
message: e.message
|
65
|
-
};
|
66
|
-
throw withStatus(error, response.status);
|
67
|
-
}
|
68
|
-
else if (typeof e === 'object' && typeof e.message === 'string') {
|
60
|
+
if (isError(e)) {
|
69
61
|
throw withStatus(e, response.status);
|
70
62
|
}
|
71
63
|
else {
|
72
|
-
throw withStatus(
|
64
|
+
throw withStatus({ message: 'Network response was not ok' }, response.status);
|
73
65
|
}
|
74
66
|
}
|
75
67
|
});
|
76
68
|
}
|
77
69
|
exports.fetch = fetch;
|
78
|
-
const
|
70
|
+
const isError = (error) => {
|
71
|
+
return (0, lang_1.isObject)(error) && (0, lang_1.isString)(error.message);
|
72
|
+
};
|
73
|
+
const withStatus = (error, status) => (0, lang_1.compactObject)(Object.assign(Object.assign({}, error), { status }));
|
package/dist/api/providers.js
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.getHostUrl = void 0;
|
4
|
+
const lang_1 = require("../util/lang");
|
4
5
|
function getHostUrl(provider, type) {
|
5
6
|
if (isValidAlias(provider)) {
|
6
7
|
return providers[provider][type];
|
@@ -22,8 +23,8 @@ const providers = {
|
|
22
23
|
}
|
23
24
|
};
|
24
25
|
function isValidAlias(alias) {
|
25
|
-
return
|
26
|
+
return (0, lang_1.isString)(alias) && Object.keys(providers).includes(alias);
|
26
27
|
}
|
27
28
|
function isValidBuilder(builder) {
|
28
|
-
return
|
29
|
+
return (0, lang_1.isObject)(builder) && (0, lang_1.isString)(builder.main) && (0, lang_1.isString)(builder.workspaces);
|
29
30
|
}
|
package/dist/api/responses.d.ts
CHANGED
@@ -19,6 +19,12 @@ export declare type AuthError = {
|
|
19
19
|
id?: string;
|
20
20
|
message: string;
|
21
21
|
};
|
22
|
+
export declare type BulkError = {
|
23
|
+
errors: {
|
24
|
+
message?: string;
|
25
|
+
status?: number;
|
26
|
+
}[];
|
27
|
+
};
|
22
28
|
export declare type BranchMigrationPlan = {
|
23
29
|
version: number;
|
24
30
|
migration: Schemas.BranchMigration;
|
package/dist/schema/filters.js
CHANGED
@@ -1,8 +1,9 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
3
|
exports.buildSortFilter = exports.isSortFilterObject = void 0;
|
4
|
+
const lang_1 = require("../util/lang");
|
4
5
|
function isSortFilterObject(filter) {
|
5
|
-
return
|
6
|
+
return (0, lang_1.isObject)(filter) && filter.column !== undefined;
|
6
7
|
}
|
7
8
|
exports.isSortFilterObject = isSortFilterObject;
|
8
9
|
function buildSortFilter(filter) {
|
@@ -1,14 +1,21 @@
|
|
1
1
|
import { FetchImpl } from '../api/fetcher';
|
2
2
|
import { Page } from './pagination';
|
3
3
|
import { Query, QueryOptions } from './query';
|
4
|
-
import { BaseData, XataRecord } from './record';
|
4
|
+
import { BaseData, Identifiable, XataRecord } from './record';
|
5
5
|
import { Select, SelectableColumn } from './selection';
|
6
6
|
export declare type Links = Record<string, Array<string[]>>;
|
7
7
|
/**
|
8
8
|
* Common interface for performing operations on a table.
|
9
9
|
*/
|
10
10
|
export declare abstract class Repository<Data extends BaseData, Record extends XataRecord = Data & XataRecord> extends Query<Record> {
|
11
|
-
abstract create(object: Data): Promise<Record>;
|
11
|
+
abstract create(object: Data & Partial<Identifiable>): Promise<Record>;
|
12
|
+
/**
|
13
|
+
* Creates a single record in the table with a unique id.
|
14
|
+
* @param id The unique id.
|
15
|
+
* @param object Object containing the column names with their values to be stored in the table.
|
16
|
+
* @returns The full persisted record.
|
17
|
+
*/
|
18
|
+
abstract create(id: string, object: Data): Promise<Record>;
|
12
19
|
/**
|
13
20
|
* Creates multiple records in the table.
|
14
21
|
* @param objects Array of objects with the column names and the values to be stored in the table.
|
@@ -21,13 +28,6 @@ export declare abstract class Repository<Data extends BaseData, Record extends X
|
|
21
28
|
* @returns The persisted record for the given id or null if the record could not be found.
|
22
29
|
*/
|
23
30
|
abstract read(id: string): Promise<Record | null>;
|
24
|
-
/**
|
25
|
-
* Insert a single record with a unique id.
|
26
|
-
* @param id The unique id.
|
27
|
-
* @param object Object containing the column names with their values to be stored in the table.
|
28
|
-
* @returns The full persisted record.
|
29
|
-
*/
|
30
|
-
abstract insert(id: string, object: Data): Promise<Record>;
|
31
31
|
/**
|
32
32
|
* Partially update a single record given its unique id.
|
33
33
|
* @param id The unique id.
|
@@ -36,13 +36,13 @@ export declare abstract class Repository<Data extends BaseData, Record extends X
|
|
36
36
|
*/
|
37
37
|
abstract update(id: string, object: Partial<Data>): Promise<Record>;
|
38
38
|
/**
|
39
|
-
*
|
39
|
+
* Creates or updates a single record. If a record exists with the given id,
|
40
40
|
* it will be update, otherwise a new record will be created.
|
41
41
|
* @param id A unique id.
|
42
42
|
* @param object The column names and the values to be persisted.
|
43
43
|
* @returns The full persisted record.
|
44
44
|
*/
|
45
|
-
abstract
|
45
|
+
abstract createOrUpdate(id: string, object: Data): Promise<Record>;
|
46
46
|
/**
|
47
47
|
* Deletes a record given its unique id.
|
48
48
|
* @param id The unique id.
|
@@ -57,11 +57,11 @@ export declare class RestRepository<Data extends BaseData, Record extends XataRe
|
|
57
57
|
#private;
|
58
58
|
constructor(client: BaseClient<any>, table: string);
|
59
59
|
create(object: Data): Promise<Record>;
|
60
|
+
create(recordId: string, object: Data): Promise<Record>;
|
60
61
|
createMany(objects: Data[]): Promise<Record[]>;
|
61
62
|
read(recordId: string): Promise<Record | null>;
|
62
63
|
update(recordId: string, object: Partial<Data>): Promise<Record>;
|
63
|
-
|
64
|
-
updateOrInsert(recordId: string, object: Data): Promise<Record>;
|
64
|
+
createOrUpdate(recordId: string, object: Data): Promise<Record>;
|
65
65
|
delete(recordId: string): Promise<void>;
|
66
66
|
query<Result extends XataRecord, Options extends QueryOptions<Record>>(query: Query<Record, Result>, options?: Options): Promise<Page<Record, typeof options extends {
|
67
67
|
columns: SelectableColumn<Data>[];
|
@@ -26,10 +26,11 @@ var __asyncValues = (this && this.__asyncValues) || function (o) {
|
|
26
26
|
function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
|
27
27
|
function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
|
28
28
|
};
|
29
|
-
var _RestRepository_instances, _RestRepository_client, _RestRepository_fetch, _RestRepository_table, _RestRepository_getFetchProps, _BaseClient_links, _BaseClient_branch;
|
29
|
+
var _RestRepository_instances, _RestRepository_client, _RestRepository_fetch, _RestRepository_table, _RestRepository_getFetchProps, _RestRepository_insertRecordWithoutId, _RestRepository_insertRecordWithId, _BaseClient_links, _BaseClient_branch;
|
30
30
|
Object.defineProperty(exports, "__esModule", { value: true });
|
31
31
|
exports.BaseClient = exports.RestRespositoryFactory = exports.RestRepository = exports.Repository = void 0;
|
32
32
|
const api_1 = require("../api");
|
33
|
+
const lang_1 = require("../util/lang");
|
33
34
|
const filters_1 = require("./filters");
|
34
35
|
const pagination_1 = require("./pagination");
|
35
36
|
const query_1 = require("./query");
|
@@ -41,6 +42,7 @@ class Repository extends query_1.Query {
|
|
41
42
|
exports.Repository = Repository;
|
42
43
|
class RestRepository extends Repository {
|
43
44
|
constructor(client, table) {
|
45
|
+
var _a;
|
44
46
|
super(null, table, {});
|
45
47
|
_RestRepository_instances.add(this);
|
46
48
|
_RestRepository_client.set(this, void 0);
|
@@ -49,26 +51,32 @@ class RestRepository extends Repository {
|
|
49
51
|
__classPrivateFieldSet(this, _RestRepository_client, client, "f");
|
50
52
|
__classPrivateFieldSet(this, _RestRepository_table, table, "f");
|
51
53
|
// TODO: Remove when integrating with API client
|
52
|
-
const
|
54
|
+
const globalFetch = typeof fetch !== 'undefined' ? fetch : undefined;
|
55
|
+
const fetchImpl = (_a = __classPrivateFieldGet(this, _RestRepository_client, "f").options.fetch) !== null && _a !== void 0 ? _a : globalFetch;
|
53
56
|
if (!fetchImpl) {
|
57
|
+
/** @todo add a link after docs exist */
|
54
58
|
throw new Error(`The \`fetch\` option passed to the Xata client is resolving to a falsy value and may not be correctly imported.`);
|
55
59
|
}
|
56
60
|
__classPrivateFieldSet(this, _RestRepository_fetch, fetchImpl, "f");
|
57
61
|
}
|
58
|
-
create(
|
62
|
+
create(a, b) {
|
59
63
|
return __awaiter(this, void 0, void 0, function* () {
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
64
|
+
if ((0, lang_1.isString)(a) && (0, lang_1.isObject)(b)) {
|
65
|
+
if (a === '')
|
66
|
+
throw new Error("The id can't be empty");
|
67
|
+
return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_insertRecordWithId).call(this, a, b);
|
68
|
+
}
|
69
|
+
else if ((0, lang_1.isObject)(a) && (0, lang_1.isString)(a.id)) {
|
70
|
+
if (a.id === '')
|
71
|
+
throw new Error("The id can't be empty");
|
72
|
+
return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_insertRecordWithId).call(this, a.id, Object.assign(Object.assign({}, a), { id: undefined }));
|
73
|
+
}
|
74
|
+
else if ((0, lang_1.isObject)(a)) {
|
75
|
+
return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_insertRecordWithoutId).call(this, a);
|
76
|
+
}
|
77
|
+
else {
|
78
|
+
throw new Error('Invalid arguments for create method');
|
70
79
|
}
|
71
|
-
return finalObject;
|
72
80
|
});
|
73
81
|
}
|
74
82
|
createMany(objects) {
|
@@ -100,24 +108,7 @@ class RestRepository extends Repository {
|
|
100
108
|
return item;
|
101
109
|
});
|
102
110
|
}
|
103
|
-
|
104
|
-
return __awaiter(this, void 0, void 0, function* () {
|
105
|
-
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
106
|
-
const record = transformObjectLinks(object);
|
107
|
-
const response = yield (0, api_1.insertRecordWithID)(Object.assign({ pathParams: {
|
108
|
-
workspace: '{workspaceId}',
|
109
|
-
dbBranchName: '{dbBranch}',
|
110
|
-
tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"),
|
111
|
-
recordId
|
112
|
-
}, body: record }, fetchProps));
|
113
|
-
const finalObject = yield this.read(response.id);
|
114
|
-
if (!finalObject) {
|
115
|
-
throw new Error('The server failed to save the record');
|
116
|
-
}
|
117
|
-
return finalObject;
|
118
|
-
});
|
119
|
-
}
|
120
|
-
updateOrInsert(recordId, object) {
|
111
|
+
createOrUpdate(recordId, object) {
|
121
112
|
return __awaiter(this, void 0, void 0, function* () {
|
122
113
|
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
123
114
|
const response = yield (0, api_1.upsertRecordWithID)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId }, body: object }, fetchProps));
|
@@ -169,6 +160,37 @@ _RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _
|
|
169
160
|
}
|
170
161
|
};
|
171
162
|
});
|
163
|
+
}, _RestRepository_insertRecordWithoutId = function _RestRepository_insertRecordWithoutId(object) {
|
164
|
+
return __awaiter(this, void 0, void 0, function* () {
|
165
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
166
|
+
const record = transformObjectLinks(object);
|
167
|
+
const response = yield (0, api_1.insertRecord)(Object.assign({ pathParams: {
|
168
|
+
workspace: '{workspaceId}',
|
169
|
+
dbBranchName: '{dbBranch}',
|
170
|
+
tableName: __classPrivateFieldGet(this, _RestRepository_table, "f")
|
171
|
+
}, body: record }, fetchProps));
|
172
|
+
const finalObject = yield this.read(response.id);
|
173
|
+
if (!finalObject) {
|
174
|
+
throw new Error('The server failed to save the record');
|
175
|
+
}
|
176
|
+
return finalObject;
|
177
|
+
});
|
178
|
+
}, _RestRepository_insertRecordWithId = function _RestRepository_insertRecordWithId(recordId, object) {
|
179
|
+
return __awaiter(this, void 0, void 0, function* () {
|
180
|
+
const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
|
181
|
+
const record = transformObjectLinks(object);
|
182
|
+
const response = yield (0, api_1.insertRecordWithID)(Object.assign({ pathParams: {
|
183
|
+
workspace: '{workspaceId}',
|
184
|
+
dbBranchName: '{dbBranch}',
|
185
|
+
tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"),
|
186
|
+
recordId
|
187
|
+
}, body: record, queryParams: { createOnly: true } }, fetchProps));
|
188
|
+
const finalObject = yield this.read(response.id);
|
189
|
+
if (!finalObject) {
|
190
|
+
throw new Error('The server failed to save the record');
|
191
|
+
}
|
192
|
+
return finalObject;
|
193
|
+
});
|
172
194
|
};
|
173
195
|
class RestRespositoryFactory {
|
174
196
|
createRepository(client, table) {
|
@@ -201,7 +223,7 @@ class BaseClient {
|
|
201
223
|
for (const link of tableLinks) {
|
202
224
|
const [field, linkTable] = link;
|
203
225
|
const value = o[field];
|
204
|
-
if (value &&
|
226
|
+
if (value && (0, lang_1.isObject)(value)) {
|
205
227
|
const { id } = value;
|
206
228
|
if (Object.keys(value).find((col) => col === 'id')) {
|
207
229
|
o[field] = this.initObject(linkTable, value);
|
@@ -210,7 +232,7 @@ class BaseClient {
|
|
210
232
|
o[field] = {
|
211
233
|
id,
|
212
234
|
get: () => {
|
213
|
-
this.db[linkTable].read(id);
|
235
|
+
return this.db[linkTable].read(id);
|
214
236
|
}
|
215
237
|
};
|
216
238
|
}
|
@@ -271,8 +293,8 @@ const isBranchStrategyBuilder = (strategy) => {
|
|
271
293
|
};
|
272
294
|
// TODO: We can find a better implementation for links
|
273
295
|
const transformObjectLinks = (object) => {
|
274
|
-
return Object.entries(object).reduce((acc, [key, value]) => {
|
275
|
-
if (
|
296
|
+
return Object.entries(object !== null && object !== void 0 ? object : {}).reduce((acc, [key, value]) => {
|
297
|
+
if ((0, lang_1.isObject)(value) && (0, lang_1.isString)(value.id)) {
|
276
298
|
return Object.assign(Object.assign({}, acc), { [key]: value.id });
|
277
299
|
}
|
278
300
|
return Object.assign(Object.assign({}, acc), { [key]: value });
|
package/dist/util/lang.d.ts
CHANGED
@@ -1,2 +1,5 @@
|
|
1
1
|
export declare function compact<T>(arr: Array<T | null | undefined>): T[];
|
2
|
+
export declare function compactObject<T>(obj: Record<string, T | null | undefined>): Record<string, T>;
|
2
3
|
export declare type PartialBy<T, K extends keyof T> = Omit<T, K> & Partial<Pick<T, K>>;
|
4
|
+
export declare function isObject(value: any): value is object;
|
5
|
+
export declare function isString(value: any): value is string;
|
package/dist/util/lang.js
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
"use strict";
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
3
|
-
exports.compact = void 0;
|
3
|
+
exports.isString = exports.isObject = exports.compactObject = exports.compact = void 0;
|
4
4
|
function notEmpty(value) {
|
5
5
|
return value !== null && value !== undefined;
|
6
6
|
}
|
@@ -8,3 +8,15 @@ function compact(arr) {
|
|
8
8
|
return arr.filter(notEmpty);
|
9
9
|
}
|
10
10
|
exports.compact = compact;
|
11
|
+
function compactObject(obj) {
|
12
|
+
return Object.fromEntries(Object.entries(obj).filter(([, value]) => notEmpty(value)));
|
13
|
+
}
|
14
|
+
exports.compactObject = compactObject;
|
15
|
+
function isObject(value) {
|
16
|
+
return value !== undefined && value !== null && typeof value === 'object';
|
17
|
+
}
|
18
|
+
exports.isObject = isObject;
|
19
|
+
function isString(value) {
|
20
|
+
return value !== undefined && value !== null && typeof value === 'string';
|
21
|
+
}
|
22
|
+
exports.isString = isString;
|
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@xata.io/client",
|
3
|
-
"version": "0.0.0-beta.
|
3
|
+
"version": "0.0.0-beta.3d1e440",
|
4
4
|
"description": "Xata.io SDK for TypeScript and JavaScript",
|
5
5
|
"main": "./dist/index.js",
|
6
6
|
"types": "./dist/index.d.ts",
|
@@ -20,5 +20,5 @@
|
|
20
20
|
"url": "https://github.com/xataio/client-ts/issues"
|
21
21
|
},
|
22
22
|
"homepage": "https://github.com/xataio/client-ts/blob/main/client/README.md",
|
23
|
-
"gitHead": "
|
23
|
+
"gitHead": "3d1e440610e2b18e76cab792c794491d7fd6c118"
|
24
24
|
}
|