@xata.io/client 0.0.0-alpha.vfde9dcf → 0.0.0-alpha.vfe4ae98
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 +80 -0
- package/README.md +271 -1
- package/Usage.md +395 -0
- package/dist/index.cjs +206 -105
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +316 -63
- package/dist/index.mjs +206 -106
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -3
package/dist/index.mjs
CHANGED
@@ -17,7 +17,8 @@ function toBase64(value) {
|
|
17
17
|
try {
|
18
18
|
return btoa(value);
|
19
19
|
} catch (err) {
|
20
|
-
|
20
|
+
const buf = Buffer;
|
21
|
+
return buf.from(value).toString("base64");
|
21
22
|
}
|
22
23
|
}
|
23
24
|
|
@@ -73,16 +74,28 @@ function getFetchImplementation(userFetch) {
|
|
73
74
|
return fetchImpl;
|
74
75
|
}
|
75
76
|
|
76
|
-
|
77
|
-
|
77
|
+
const VERSION = "0.0.0-alpha.vfe4ae98";
|
78
|
+
|
79
|
+
class ErrorWithCause extends Error {
|
80
|
+
constructor(message, options) {
|
81
|
+
super(message, options);
|
82
|
+
}
|
83
|
+
}
|
84
|
+
class FetcherError extends ErrorWithCause {
|
85
|
+
constructor(status, data, requestId) {
|
78
86
|
super(getMessage(data));
|
79
87
|
this.status = status;
|
80
88
|
this.errors = isBulkError(data) ? data.errors : void 0;
|
89
|
+
this.requestId = requestId;
|
81
90
|
if (data instanceof Error) {
|
82
91
|
this.stack = data.stack;
|
83
92
|
this.cause = data.cause;
|
84
93
|
}
|
85
94
|
}
|
95
|
+
toString() {
|
96
|
+
const error = super.toString();
|
97
|
+
return `[${this.status}] (${this.requestId ?? "Unknown"}): ${error}`;
|
98
|
+
}
|
86
99
|
}
|
87
100
|
function isBulkError(error) {
|
88
101
|
return isObject(error) && Array.isArray(error.errors);
|
@@ -105,7 +118,12 @@ function getMessage(data) {
|
|
105
118
|
}
|
106
119
|
|
107
120
|
const resolveUrl = (url, queryParams = {}, pathParams = {}) => {
|
108
|
-
const
|
121
|
+
const cleanQueryParams = Object.entries(queryParams).reduce((acc, [key, value]) => {
|
122
|
+
if (value === void 0 || value === null)
|
123
|
+
return acc;
|
124
|
+
return { ...acc, [key]: value };
|
125
|
+
}, {});
|
126
|
+
const query = new URLSearchParams(cleanQueryParams).toString();
|
109
127
|
const queryString = query.length > 0 ? `?${query}` : "";
|
110
128
|
return url.replace(/\{\w*\}/g, (key) => pathParams[key.slice(1, -1)]) + queryString;
|
111
129
|
};
|
@@ -145,6 +163,7 @@ async function fetch$1({
|
|
145
163
|
body: body ? JSON.stringify(body) : void 0,
|
146
164
|
headers: {
|
147
165
|
"Content-Type": "application/json",
|
166
|
+
"User-Agent": `Xata client-ts/${VERSION}`,
|
148
167
|
...headers,
|
149
168
|
...hostHeader(fullUrl),
|
150
169
|
Authorization: `Bearer ${apiKey}`
|
@@ -153,14 +172,15 @@ async function fetch$1({
|
|
153
172
|
if (response.status === 204) {
|
154
173
|
return {};
|
155
174
|
}
|
175
|
+
const requestId = response.headers?.get("x-request-id") ?? void 0;
|
156
176
|
try {
|
157
177
|
const jsonResponse = await response.json();
|
158
178
|
if (response.ok) {
|
159
179
|
return jsonResponse;
|
160
180
|
}
|
161
|
-
throw new FetcherError(response.status, jsonResponse);
|
181
|
+
throw new FetcherError(response.status, jsonResponse, requestId);
|
162
182
|
} catch (error) {
|
163
|
-
throw new FetcherError(response.status, error);
|
183
|
+
throw new FetcherError(response.status, error, requestId);
|
164
184
|
}
|
165
185
|
}
|
166
186
|
|
@@ -480,7 +500,7 @@ var __privateAdd$7 = (obj, member, value) => {
|
|
480
500
|
throw TypeError("Cannot add the same private member more than once");
|
481
501
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
482
502
|
};
|
483
|
-
var __privateSet$
|
503
|
+
var __privateSet$7 = (obj, member, value, setter) => {
|
484
504
|
__accessCheck$7(obj, member, "write to private field");
|
485
505
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
486
506
|
return value;
|
@@ -495,7 +515,7 @@ class XataApiClient {
|
|
495
515
|
if (!apiKey) {
|
496
516
|
throw new Error("Could not resolve a valid apiKey");
|
497
517
|
}
|
498
|
-
__privateSet$
|
518
|
+
__privateSet$7(this, _extraProps, {
|
499
519
|
apiUrl: getHostUrl(provider, "main"),
|
500
520
|
workspacesApiUrl: getHostUrl(provider, "workspaces"),
|
501
521
|
fetchImpl: getFetchImplementation(options.fetch),
|
@@ -684,10 +704,10 @@ class DatabaseApi {
|
|
684
704
|
...this.extraProps
|
685
705
|
});
|
686
706
|
}
|
687
|
-
resolveBranch(workspace, dbName, gitBranch) {
|
707
|
+
resolveBranch(workspace, dbName, gitBranch, fallbackBranch) {
|
688
708
|
return operationsByTag.database.resolveBranch({
|
689
709
|
pathParams: { workspace, dbName },
|
690
|
-
queryParams: { gitBranch },
|
710
|
+
queryParams: { gitBranch, fallbackBranch },
|
691
711
|
...this.extraProps
|
692
712
|
});
|
693
713
|
}
|
@@ -932,18 +952,18 @@ var __privateAdd$6 = (obj, member, value) => {
|
|
932
952
|
throw TypeError("Cannot add the same private member more than once");
|
933
953
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
934
954
|
};
|
935
|
-
var __privateSet$
|
955
|
+
var __privateSet$6 = (obj, member, value, setter) => {
|
936
956
|
__accessCheck$6(obj, member, "write to private field");
|
937
957
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
938
958
|
return value;
|
939
959
|
};
|
940
|
-
var _query;
|
960
|
+
var _query, _page;
|
941
961
|
class Page {
|
942
962
|
constructor(query, meta, records = []) {
|
943
963
|
__privateAdd$6(this, _query, void 0);
|
944
|
-
__privateSet$
|
964
|
+
__privateSet$6(this, _query, query);
|
945
965
|
this.meta = meta;
|
946
|
-
this.records = records;
|
966
|
+
this.records = new RecordArray(this, records);
|
947
967
|
}
|
948
968
|
async nextPage(size, offset) {
|
949
969
|
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
|
@@ -963,12 +983,50 @@ class Page {
|
|
963
983
|
}
|
964
984
|
_query = new WeakMap();
|
965
985
|
const PAGINATION_MAX_SIZE = 200;
|
966
|
-
const PAGINATION_DEFAULT_SIZE =
|
986
|
+
const PAGINATION_DEFAULT_SIZE = 20;
|
967
987
|
const PAGINATION_MAX_OFFSET = 800;
|
968
988
|
const PAGINATION_DEFAULT_OFFSET = 0;
|
969
989
|
function isCursorPaginationOptions(options) {
|
970
990
|
return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
|
971
991
|
}
|
992
|
+
const _RecordArray = class extends Array {
|
993
|
+
constructor(...args) {
|
994
|
+
super(..._RecordArray.parseConstructorParams(...args));
|
995
|
+
__privateAdd$6(this, _page, void 0);
|
996
|
+
__privateSet$6(this, _page, args[0]);
|
997
|
+
}
|
998
|
+
static parseConstructorParams(...args) {
|
999
|
+
if (args.length === 1 && typeof args[0] === "number") {
|
1000
|
+
return new Array(args[0]);
|
1001
|
+
}
|
1002
|
+
if (args.length <= 2 && isObject(args[0]?.meta) && Array.isArray(args[1] ?? [])) {
|
1003
|
+
const result = args[1] ?? args[0].records ?? [];
|
1004
|
+
return new Array(...result);
|
1005
|
+
}
|
1006
|
+
return new Array(...args);
|
1007
|
+
}
|
1008
|
+
async nextPage(size, offset) {
|
1009
|
+
const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
|
1010
|
+
return new _RecordArray(newPage);
|
1011
|
+
}
|
1012
|
+
async previousPage(size, offset) {
|
1013
|
+
const newPage = await __privateGet$6(this, _page).previousPage(size, offset);
|
1014
|
+
return new _RecordArray(newPage);
|
1015
|
+
}
|
1016
|
+
async firstPage(size, offset) {
|
1017
|
+
const newPage = await __privateGet$6(this, _page).firstPage(size, offset);
|
1018
|
+
return new _RecordArray(newPage);
|
1019
|
+
}
|
1020
|
+
async lastPage(size, offset) {
|
1021
|
+
const newPage = await __privateGet$6(this, _page).lastPage(size, offset);
|
1022
|
+
return new _RecordArray(newPage);
|
1023
|
+
}
|
1024
|
+
hasNextPage() {
|
1025
|
+
return __privateGet$6(this, _page).meta.page.more;
|
1026
|
+
}
|
1027
|
+
};
|
1028
|
+
let RecordArray = _RecordArray;
|
1029
|
+
_page = new WeakMap();
|
972
1030
|
|
973
1031
|
var __accessCheck$5 = (obj, member, msg) => {
|
974
1032
|
if (!member.has(obj))
|
@@ -983,25 +1041,26 @@ var __privateAdd$5 = (obj, member, value) => {
|
|
983
1041
|
throw TypeError("Cannot add the same private member more than once");
|
984
1042
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
985
1043
|
};
|
986
|
-
var __privateSet$
|
1044
|
+
var __privateSet$5 = (obj, member, value, setter) => {
|
987
1045
|
__accessCheck$5(obj, member, "write to private field");
|
988
1046
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
989
1047
|
return value;
|
990
1048
|
};
|
991
1049
|
var _table$1, _repository, _data;
|
992
1050
|
const _Query = class {
|
993
|
-
constructor(repository, table, data,
|
1051
|
+
constructor(repository, table, data, rawParent) {
|
994
1052
|
__privateAdd$5(this, _table$1, void 0);
|
995
1053
|
__privateAdd$5(this, _repository, void 0);
|
996
1054
|
__privateAdd$5(this, _data, { filter: {} });
|
997
1055
|
this.meta = { page: { cursor: "start", more: true } };
|
998
|
-
this.records = [];
|
999
|
-
__privateSet$
|
1056
|
+
this.records = new RecordArray(this, []);
|
1057
|
+
__privateSet$5(this, _table$1, table);
|
1000
1058
|
if (repository) {
|
1001
|
-
__privateSet$
|
1059
|
+
__privateSet$5(this, _repository, repository);
|
1002
1060
|
} else {
|
1003
|
-
__privateSet$
|
1061
|
+
__privateSet$5(this, _repository, this);
|
1004
1062
|
}
|
1063
|
+
const parent = cleanParent(data, rawParent);
|
1005
1064
|
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
1006
1065
|
__privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
|
1007
1066
|
__privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
|
@@ -1083,8 +1142,11 @@ const _Query = class {
|
|
1083
1142
|
}
|
1084
1143
|
}
|
1085
1144
|
async getMany(options = {}) {
|
1086
|
-
const
|
1087
|
-
|
1145
|
+
const page = await this.getPaginated(options);
|
1146
|
+
if (page.hasNextPage() && options.pagination?.size === void 0) {
|
1147
|
+
console.trace("Calling getMany does not return all results. Paginate to get all results or call getAll.");
|
1148
|
+
}
|
1149
|
+
return page.records;
|
1088
1150
|
}
|
1089
1151
|
async getAll(options = {}) {
|
1090
1152
|
const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
|
@@ -1121,12 +1183,20 @@ let Query = _Query;
|
|
1121
1183
|
_table$1 = new WeakMap();
|
1122
1184
|
_repository = new WeakMap();
|
1123
1185
|
_data = new WeakMap();
|
1186
|
+
function cleanParent(data, parent) {
|
1187
|
+
if (isCursorPaginationOptions(data.pagination)) {
|
1188
|
+
return { ...parent, sorting: void 0, filter: void 0 };
|
1189
|
+
}
|
1190
|
+
return parent;
|
1191
|
+
}
|
1124
1192
|
|
1125
1193
|
function isIdentifiable(x) {
|
1126
1194
|
return isObject(x) && isString(x?.id);
|
1127
1195
|
}
|
1128
1196
|
function isXataRecord(x) {
|
1129
|
-
|
1197
|
+
const record = x;
|
1198
|
+
const metadata = record?.getMetadata();
|
1199
|
+
return isIdentifiable(x) && isObject(metadata) && typeof metadata.version === "number";
|
1130
1200
|
}
|
1131
1201
|
|
1132
1202
|
function isSortFilterString(value) {
|
@@ -1165,7 +1235,7 @@ var __privateAdd$4 = (obj, member, value) => {
|
|
1165
1235
|
throw TypeError("Cannot add the same private member more than once");
|
1166
1236
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1167
1237
|
};
|
1168
|
-
var __privateSet$
|
1238
|
+
var __privateSet$4 = (obj, member, value, setter) => {
|
1169
1239
|
__accessCheck$4(obj, member, "write to private field");
|
1170
1240
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1171
1241
|
return value;
|
@@ -1174,7 +1244,7 @@ var __privateMethod$2 = (obj, member, method) => {
|
|
1174
1244
|
__accessCheck$4(obj, member, "access private method");
|
1175
1245
|
return method;
|
1176
1246
|
};
|
1177
|
-
var _table, _getFetchProps, _cache,
|
1247
|
+
var _table, _getFetchProps, _cache, _schemaTables$2, _insertRecordWithoutId, insertRecordWithoutId_fn, _insertRecordWithId, insertRecordWithId_fn, _bulkInsertTableRecords, bulkInsertTableRecords_fn, _updateRecordWithID, updateRecordWithID_fn, _upsertRecordWithID, upsertRecordWithID_fn, _deleteRecord, deleteRecord_fn, _invalidateCache, invalidateCache_fn, _setCacheRecord, setCacheRecord_fn, _getCacheRecord, getCacheRecord_fn, _setCacheQuery, setCacheQuery_fn, _getCacheQuery, getCacheQuery_fn, _getSchemaTables$1, getSchemaTables_fn$1;
|
1178
1248
|
class Repository extends Query {
|
1179
1249
|
}
|
1180
1250
|
class RestRepository extends Query {
|
@@ -1191,18 +1261,21 @@ class RestRepository extends Query {
|
|
1191
1261
|
__privateAdd$4(this, _getCacheRecord);
|
1192
1262
|
__privateAdd$4(this, _setCacheQuery);
|
1193
1263
|
__privateAdd$4(this, _getCacheQuery);
|
1194
|
-
__privateAdd$4(this,
|
1264
|
+
__privateAdd$4(this, _getSchemaTables$1);
|
1195
1265
|
__privateAdd$4(this, _table, void 0);
|
1196
1266
|
__privateAdd$4(this, _getFetchProps, void 0);
|
1197
1267
|
__privateAdd$4(this, _cache, void 0);
|
1198
|
-
__privateAdd$4(this,
|
1199
|
-
__privateSet$
|
1200
|
-
__privateSet$
|
1268
|
+
__privateAdd$4(this, _schemaTables$2, void 0);
|
1269
|
+
__privateSet$4(this, _table, options.table);
|
1270
|
+
__privateSet$4(this, _getFetchProps, options.pluginOptions.getFetchProps);
|
1201
1271
|
this.db = options.db;
|
1202
|
-
__privateSet$
|
1272
|
+
__privateSet$4(this, _cache, options.pluginOptions.cache);
|
1273
|
+
__privateSet$4(this, _schemaTables$2, options.schemaTables);
|
1203
1274
|
}
|
1204
1275
|
async create(a, b) {
|
1205
1276
|
if (Array.isArray(a)) {
|
1277
|
+
if (a.length === 0)
|
1278
|
+
return [];
|
1206
1279
|
const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
|
1207
1280
|
await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
|
1208
1281
|
return records;
|
@@ -1228,27 +1301,38 @@ class RestRepository extends Query {
|
|
1228
1301
|
}
|
1229
1302
|
throw new Error("Invalid arguments for create method");
|
1230
1303
|
}
|
1231
|
-
async read(
|
1232
|
-
|
1233
|
-
|
1234
|
-
|
1235
|
-
|
1236
|
-
|
1237
|
-
|
1238
|
-
|
1239
|
-
|
1240
|
-
|
1241
|
-
|
1242
|
-
|
1243
|
-
|
1244
|
-
|
1245
|
-
|
1304
|
+
async read(a) {
|
1305
|
+
if (Array.isArray(a)) {
|
1306
|
+
if (a.length === 0)
|
1307
|
+
return [];
|
1308
|
+
const ids = a.map((item) => isString(item) ? item : item.id).filter((id2) => isString(id2));
|
1309
|
+
return this.getAll({ filter: { id: { $any: ids } } });
|
1310
|
+
}
|
1311
|
+
const id = isString(a) ? a : a.id;
|
1312
|
+
if (isString(id)) {
|
1313
|
+
const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, id);
|
1314
|
+
if (cacheRecord)
|
1315
|
+
return cacheRecord;
|
1316
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1317
|
+
try {
|
1318
|
+
const response = await getRecord({
|
1319
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
|
1320
|
+
...fetchProps
|
1321
|
+
});
|
1322
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1323
|
+
return initObject(this.db, schemaTables, __privateGet$4(this, _table), response);
|
1324
|
+
} catch (e) {
|
1325
|
+
if (isObject(e) && e.status === 404) {
|
1326
|
+
return null;
|
1327
|
+
}
|
1328
|
+
throw e;
|
1246
1329
|
}
|
1247
|
-
throw e;
|
1248
1330
|
}
|
1249
1331
|
}
|
1250
1332
|
async update(a, b) {
|
1251
1333
|
if (Array.isArray(a)) {
|
1334
|
+
if (a.length === 0)
|
1335
|
+
return [];
|
1252
1336
|
if (a.length > 100) {
|
1253
1337
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1254
1338
|
}
|
@@ -1270,6 +1354,8 @@ class RestRepository extends Query {
|
|
1270
1354
|
}
|
1271
1355
|
async createOrUpdate(a, b) {
|
1272
1356
|
if (Array.isArray(a)) {
|
1357
|
+
if (a.length === 0)
|
1358
|
+
return [];
|
1273
1359
|
if (a.length > 100) {
|
1274
1360
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1275
1361
|
}
|
@@ -1291,6 +1377,8 @@ class RestRepository extends Query {
|
|
1291
1377
|
}
|
1292
1378
|
async delete(a) {
|
1293
1379
|
if (Array.isArray(a)) {
|
1380
|
+
if (a.length === 0)
|
1381
|
+
return;
|
1294
1382
|
if (a.length > 100) {
|
1295
1383
|
console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
|
1296
1384
|
}
|
@@ -1316,24 +1404,22 @@ class RestRepository extends Query {
|
|
1316
1404
|
body: {
|
1317
1405
|
query,
|
1318
1406
|
fuzziness: options.fuzziness,
|
1407
|
+
highlight: options.highlight,
|
1319
1408
|
filter: options.filter
|
1320
1409
|
},
|
1321
1410
|
...fetchProps
|
1322
1411
|
});
|
1323
|
-
const
|
1324
|
-
return records.map((item) => initObject(this.db,
|
1412
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1413
|
+
return records.map((item) => initObject(this.db, schemaTables, __privateGet$4(this, _table), item));
|
1325
1414
|
}
|
1326
1415
|
async query(query) {
|
1327
1416
|
const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
|
1328
1417
|
if (cacheQuery)
|
1329
1418
|
return new Page(query, cacheQuery.meta, cacheQuery.records);
|
1330
1419
|
const data = query.getQueryOptions();
|
1331
|
-
const filter = Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0;
|
1332
|
-
const sort = data.sort !== void 0 ? buildSortFilter(data.sort) : void 0;
|
1333
|
-
const isCursorPagination = isCursorPaginationOptions(data.pagination);
|
1334
1420
|
const body = {
|
1335
|
-
filter:
|
1336
|
-
sort:
|
1421
|
+
filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
|
1422
|
+
sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
|
1337
1423
|
page: data.pagination,
|
1338
1424
|
columns: data.columns
|
1339
1425
|
};
|
@@ -1343,8 +1429,8 @@ class RestRepository extends Query {
|
|
1343
1429
|
body,
|
1344
1430
|
...fetchProps
|
1345
1431
|
});
|
1346
|
-
const
|
1347
|
-
const records = objects.map((record) => initObject(this.db,
|
1432
|
+
const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
|
1433
|
+
const records = objects.map((record) => initObject(this.db, schemaTables, __privateGet$4(this, _table), record));
|
1348
1434
|
await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
|
1349
1435
|
return new Page(query, meta, records);
|
1350
1436
|
}
|
@@ -1352,7 +1438,7 @@ class RestRepository extends Query {
|
|
1352
1438
|
_table = new WeakMap();
|
1353
1439
|
_getFetchProps = new WeakMap();
|
1354
1440
|
_cache = new WeakMap();
|
1355
|
-
|
1441
|
+
_schemaTables$2 = new WeakMap();
|
1356
1442
|
_insertRecordWithoutId = new WeakSet();
|
1357
1443
|
insertRecordWithoutId_fn = async function(object) {
|
1358
1444
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
@@ -1397,16 +1483,20 @@ _bulkInsertTableRecords = new WeakSet();
|
|
1397
1483
|
bulkInsertTableRecords_fn = async function(objects) {
|
1398
1484
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1399
1485
|
const records = objects.map((object) => transformObjectLinks(object));
|
1400
|
-
const
|
1486
|
+
const { recordIDs } = await bulkInsertTableRecords({
|
1401
1487
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1402
1488
|
body: { records },
|
1403
1489
|
...fetchProps
|
1404
1490
|
});
|
1405
|
-
const finalObjects = await this.
|
1491
|
+
const finalObjects = await this.read(recordIDs);
|
1406
1492
|
if (finalObjects.length !== objects.length) {
|
1407
1493
|
throw new Error("The server failed to save some records");
|
1408
1494
|
}
|
1409
|
-
|
1495
|
+
const dictionary = finalObjects.reduce((acc, object) => {
|
1496
|
+
acc[object.id] = object;
|
1497
|
+
return acc;
|
1498
|
+
}, {});
|
1499
|
+
return recordIDs.map((id) => dictionary[id]);
|
1410
1500
|
};
|
1411
1501
|
_updateRecordWithID = new WeakSet();
|
1412
1502
|
updateRecordWithID_fn = async function(recordId, object) {
|
@@ -1482,17 +1572,17 @@ getCacheQuery_fn = async function(query) {
|
|
1482
1572
|
const hasExpired = result.date.getTime() + ttl < Date.now();
|
1483
1573
|
return hasExpired ? null : result;
|
1484
1574
|
};
|
1485
|
-
|
1486
|
-
|
1487
|
-
if (__privateGet$4(this,
|
1488
|
-
return __privateGet$4(this,
|
1575
|
+
_getSchemaTables$1 = new WeakSet();
|
1576
|
+
getSchemaTables_fn$1 = async function() {
|
1577
|
+
if (__privateGet$4(this, _schemaTables$2))
|
1578
|
+
return __privateGet$4(this, _schemaTables$2);
|
1489
1579
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1490
1580
|
const { schema } = await getBranchDetails({
|
1491
1581
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1492
1582
|
...fetchProps
|
1493
1583
|
});
|
1494
|
-
__privateSet$
|
1495
|
-
return schema;
|
1584
|
+
__privateSet$4(this, _schemaTables$2, schema.tables);
|
1585
|
+
return schema.tables;
|
1496
1586
|
};
|
1497
1587
|
const transformObjectLinks = (object) => {
|
1498
1588
|
return Object.entries(object).reduce((acc, [key, value]) => {
|
@@ -1501,20 +1591,21 @@ const transformObjectLinks = (object) => {
|
|
1501
1591
|
return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
|
1502
1592
|
}, {});
|
1503
1593
|
};
|
1504
|
-
const initObject = (db,
|
1594
|
+
const initObject = (db, schemaTables, table, object) => {
|
1505
1595
|
const result = {};
|
1506
|
-
|
1507
|
-
|
1596
|
+
const { xata, ...rest } = object ?? {};
|
1597
|
+
Object.assign(result, rest);
|
1598
|
+
const { columns } = schemaTables.find(({ name }) => name === table) ?? {};
|
1508
1599
|
if (!columns)
|
1509
1600
|
console.error(`Table ${table} not found in schema`);
|
1510
1601
|
for (const column of columns ?? []) {
|
1511
1602
|
const value = result[column.name];
|
1512
1603
|
switch (column.type) {
|
1513
1604
|
case "datetime": {
|
1514
|
-
const date = new Date(value);
|
1515
|
-
if (isNaN(date.getTime())) {
|
1605
|
+
const date = value !== void 0 ? new Date(value) : void 0;
|
1606
|
+
if (date && isNaN(date.getTime())) {
|
1516
1607
|
console.error(`Failed to parse date ${value} for field ${column.name}`);
|
1517
|
-
} else {
|
1608
|
+
} else if (date) {
|
1518
1609
|
result[column.name] = date;
|
1519
1610
|
}
|
1520
1611
|
break;
|
@@ -1524,7 +1615,7 @@ const initObject = (db, schema, table, object) => {
|
|
1524
1615
|
if (!linkTable) {
|
1525
1616
|
console.error(`Failed to parse link for field ${column.name}`);
|
1526
1617
|
} else if (isObject(value)) {
|
1527
|
-
result[column.name] = initObject(db,
|
1618
|
+
result[column.name] = initObject(db, schemaTables, linkTable, value);
|
1528
1619
|
}
|
1529
1620
|
break;
|
1530
1621
|
}
|
@@ -1539,7 +1630,10 @@ const initObject = (db, schema, table, object) => {
|
|
1539
1630
|
result.delete = function() {
|
1540
1631
|
return db[table].delete(result["id"]);
|
1541
1632
|
};
|
1542
|
-
|
1633
|
+
result.getMetadata = function() {
|
1634
|
+
return xata;
|
1635
|
+
};
|
1636
|
+
for (const prop of ["read", "update", "delete", "getMetadata"]) {
|
1543
1637
|
Object.defineProperty(result, prop, { enumerable: false });
|
1544
1638
|
}
|
1545
1639
|
Object.freeze(result);
|
@@ -1568,7 +1662,7 @@ var __privateAdd$3 = (obj, member, value) => {
|
|
1568
1662
|
throw TypeError("Cannot add the same private member more than once");
|
1569
1663
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1570
1664
|
};
|
1571
|
-
var __privateSet$
|
1665
|
+
var __privateSet$3 = (obj, member, value, setter) => {
|
1572
1666
|
__accessCheck$3(obj, member, "write to private field");
|
1573
1667
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
1574
1668
|
return value;
|
@@ -1577,7 +1671,7 @@ var _map;
|
|
1577
1671
|
class SimpleCache {
|
1578
1672
|
constructor(options = {}) {
|
1579
1673
|
__privateAdd$3(this, _map, void 0);
|
1580
|
-
__privateSet$
|
1674
|
+
__privateSet$3(this, _map, /* @__PURE__ */ new Map());
|
1581
1675
|
this.capacity = options.max ?? 500;
|
1582
1676
|
this.cacheRecords = options.cacheRecords ?? true;
|
1583
1677
|
this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
|
@@ -1637,12 +1731,18 @@ var __privateAdd$2 = (obj, member, value) => {
|
|
1637
1731
|
throw TypeError("Cannot add the same private member more than once");
|
1638
1732
|
member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
1639
1733
|
};
|
1640
|
-
var
|
1734
|
+
var __privateSet$2 = (obj, member, value, setter) => {
|
1735
|
+
__accessCheck$2(obj, member, "write to private field");
|
1736
|
+
setter ? setter.call(obj, value) : member.set(obj, value);
|
1737
|
+
return value;
|
1738
|
+
};
|
1739
|
+
var _tables, _schemaTables$1;
|
1641
1740
|
class SchemaPlugin extends XataPlugin {
|
1642
|
-
constructor(
|
1741
|
+
constructor(schemaTables) {
|
1643
1742
|
super();
|
1644
|
-
this.tableNames = tableNames;
|
1645
1743
|
__privateAdd$2(this, _tables, {});
|
1744
|
+
__privateAdd$2(this, _schemaTables$1, void 0);
|
1745
|
+
__privateSet$2(this, _schemaTables$1, schemaTables);
|
1646
1746
|
}
|
1647
1747
|
build(pluginOptions) {
|
1648
1748
|
const db = new Proxy({}, {
|
@@ -1655,13 +1755,15 @@ class SchemaPlugin extends XataPlugin {
|
|
1655
1755
|
return __privateGet$2(this, _tables)[table];
|
1656
1756
|
}
|
1657
1757
|
});
|
1658
|
-
|
1659
|
-
|
1758
|
+
const tableNames = __privateGet$2(this, _schemaTables$1)?.map(({ name }) => name) ?? [];
|
1759
|
+
for (const table of tableNames) {
|
1760
|
+
db[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
|
1660
1761
|
}
|
1661
1762
|
return db;
|
1662
1763
|
}
|
1663
1764
|
}
|
1664
1765
|
_tables = new WeakMap();
|
1766
|
+
_schemaTables$1 = new WeakMap();
|
1665
1767
|
|
1666
1768
|
var __accessCheck$1 = (obj, member, msg) => {
|
1667
1769
|
if (!member.has(obj))
|
@@ -1685,61 +1787,62 @@ var __privateMethod$1 = (obj, member, method) => {
|
|
1685
1787
|
__accessCheck$1(obj, member, "access private method");
|
1686
1788
|
return method;
|
1687
1789
|
};
|
1688
|
-
var
|
1790
|
+
var _schemaTables, _search, search_fn, _getSchemaTables, getSchemaTables_fn;
|
1689
1791
|
class SearchPlugin extends XataPlugin {
|
1690
|
-
constructor(db) {
|
1792
|
+
constructor(db, schemaTables) {
|
1691
1793
|
super();
|
1692
1794
|
this.db = db;
|
1693
1795
|
__privateAdd$1(this, _search);
|
1694
|
-
__privateAdd$1(this,
|
1695
|
-
__privateAdd$1(this,
|
1796
|
+
__privateAdd$1(this, _getSchemaTables);
|
1797
|
+
__privateAdd$1(this, _schemaTables, void 0);
|
1798
|
+
__privateSet$1(this, _schemaTables, schemaTables);
|
1696
1799
|
}
|
1697
1800
|
build({ getFetchProps }) {
|
1698
1801
|
return {
|
1699
1802
|
all: async (query, options = {}) => {
|
1700
1803
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1701
|
-
const
|
1804
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1702
1805
|
return records.map((record) => {
|
1703
1806
|
const { table = "orphan" } = record.xata;
|
1704
|
-
return { table, record: initObject(this.db,
|
1807
|
+
return { table, record: initObject(this.db, schemaTables, table, record) };
|
1705
1808
|
});
|
1706
1809
|
},
|
1707
1810
|
byTable: async (query, options = {}) => {
|
1708
1811
|
const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
|
1709
|
-
const
|
1812
|
+
const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
|
1710
1813
|
return records.reduce((acc, record) => {
|
1711
1814
|
const { table = "orphan" } = record.xata;
|
1712
1815
|
const items = acc[table] ?? [];
|
1713
|
-
const item = initObject(this.db,
|
1816
|
+
const item = initObject(this.db, schemaTables, table, record);
|
1714
1817
|
return { ...acc, [table]: [...items, item] };
|
1715
1818
|
}, {});
|
1716
1819
|
}
|
1717
1820
|
};
|
1718
1821
|
}
|
1719
1822
|
}
|
1720
|
-
|
1823
|
+
_schemaTables = new WeakMap();
|
1721
1824
|
_search = new WeakSet();
|
1722
1825
|
search_fn = async function(query, options, getFetchProps) {
|
1723
1826
|
const fetchProps = await getFetchProps();
|
1724
|
-
const { tables, fuzziness } = options ?? {};
|
1827
|
+
const { tables, fuzziness, highlight } = options ?? {};
|
1725
1828
|
const { records } = await searchBranch({
|
1726
1829
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1727
|
-
body: { tables, query, fuzziness },
|
1830
|
+
body: { tables, query, fuzziness, highlight },
|
1728
1831
|
...fetchProps
|
1729
1832
|
});
|
1730
1833
|
return records;
|
1731
1834
|
};
|
1732
|
-
|
1733
|
-
|
1734
|
-
if (__privateGet$1(this,
|
1735
|
-
return __privateGet$1(this,
|
1835
|
+
_getSchemaTables = new WeakSet();
|
1836
|
+
getSchemaTables_fn = async function(getFetchProps) {
|
1837
|
+
if (__privateGet$1(this, _schemaTables))
|
1838
|
+
return __privateGet$1(this, _schemaTables);
|
1736
1839
|
const fetchProps = await getFetchProps();
|
1737
1840
|
const { schema } = await getBranchDetails({
|
1738
1841
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1739
1842
|
...fetchProps
|
1740
1843
|
});
|
1741
|
-
__privateSet$1(this,
|
1742
|
-
return schema;
|
1844
|
+
__privateSet$1(this, _schemaTables, schema.tables);
|
1845
|
+
return schema.tables;
|
1743
1846
|
};
|
1744
1847
|
|
1745
1848
|
const isBranchStrategyBuilder = (strategy) => {
|
@@ -1802,10 +1905,7 @@ async function getDatabaseBranch(branch, options) {
|
|
1802
1905
|
apiUrl: databaseURL,
|
1803
1906
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1804
1907
|
workspacesApiUrl: `${protocol}//${host}`,
|
1805
|
-
pathParams: {
|
1806
|
-
dbBranchName,
|
1807
|
-
workspace
|
1808
|
-
}
|
1908
|
+
pathParams: { dbBranchName, workspace }
|
1809
1909
|
});
|
1810
1910
|
} catch (err) {
|
1811
1911
|
if (isObject(err) && err.status === 404)
|
@@ -1858,7 +1958,7 @@ var __privateMethod = (obj, member, method) => {
|
|
1858
1958
|
const buildClient = (plugins) => {
|
1859
1959
|
var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
|
1860
1960
|
return _a = class {
|
1861
|
-
constructor(options = {},
|
1961
|
+
constructor(options = {}, schemaTables) {
|
1862
1962
|
__privateAdd(this, _parseOptions);
|
1863
1963
|
__privateAdd(this, _getFetchProps);
|
1864
1964
|
__privateAdd(this, _evaluateBranch);
|
@@ -1868,8 +1968,8 @@ const buildClient = (plugins) => {
|
|
1868
1968
|
getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
|
1869
1969
|
cache: safeOptions.cache
|
1870
1970
|
};
|
1871
|
-
const db = new SchemaPlugin(
|
1872
|
-
const search = new SearchPlugin(db).build(pluginOptions);
|
1971
|
+
const db = new SchemaPlugin(schemaTables).build(pluginOptions);
|
1972
|
+
const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
|
1873
1973
|
this.db = db;
|
1874
1974
|
this.search = search;
|
1875
1975
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|
@@ -1942,5 +2042,5 @@ class XataError extends Error {
|
|
1942
2042
|
}
|
1943
2043
|
}
|
1944
2044
|
|
1945
|
-
export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, Repository, RestRepository, SchemaPlugin, SearchPlugin, SimpleCache, XataApiClient, XataApiPlugin, XataError, XataPlugin, acceptWorkspaceMemberInvite, addGitBranchesEntry, addTableColumn, buildClient, bulkInsertTableRecords, cancelWorkspaceMemberInvite, contains, createBranch, createDatabase, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteWorkspace, endsWith, executeBranchMigrationPlan, exists, ge, getAPIKey, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchStats, getColumn, getCurrentBranchDetails, getCurrentBranchName, getDatabaseList, getDatabaseURL, getGitBranchesMapping, getRecord, getTableColumns, getTableSchema, getUser, getUserAPIKeys, getWorkspace, getWorkspaceMembersList, getWorkspacesList, gt, gte, includes, includesAll, includesAny, includesNone, insertRecord, insertRecordWithID, inviteWorkspaceMember, is, isCursorPaginationOptions, isIdentifiable, isNot, isXataRecord, le, lt, lte, notExists, operationsByTag, pattern, queryTable, removeGitBranchesEntry, removeWorkspaceMember, resendWorkspaceMemberInvite, resolveBranch, searchBranch, searchTable, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberRole, upsertRecordWithID };
|
2045
|
+
export { BaseClient, operationsByTag as Operations, PAGINATION_DEFAULT_OFFSET, PAGINATION_DEFAULT_SIZE, PAGINATION_MAX_OFFSET, PAGINATION_MAX_SIZE, Page, Query, RecordArray, Repository, RestRepository, SchemaPlugin, SearchPlugin, SimpleCache, XataApiClient, XataApiPlugin, XataError, XataPlugin, acceptWorkspaceMemberInvite, addGitBranchesEntry, addTableColumn, buildClient, bulkInsertTableRecords, cancelWorkspaceMemberInvite, contains, createBranch, createDatabase, createTable, createUserAPIKey, createWorkspace, deleteBranch, deleteColumn, deleteDatabase, deleteRecord, deleteTable, deleteUser, deleteUserAPIKey, deleteWorkspace, endsWith, executeBranchMigrationPlan, exists, ge, getAPIKey, getBranchDetails, getBranchList, getBranchMetadata, getBranchMigrationHistory, getBranchMigrationPlan, getBranchStats, getColumn, getCurrentBranchDetails, getCurrentBranchName, getDatabaseList, getDatabaseURL, getGitBranchesMapping, getRecord, getTableColumns, getTableSchema, getUser, getUserAPIKeys, getWorkspace, getWorkspaceMembersList, getWorkspacesList, gt, gte, includes, includesAll, includesAny, includesNone, insertRecord, insertRecordWithID, inviteWorkspaceMember, is, isCursorPaginationOptions, isIdentifiable, isNot, isXataRecord, le, lt, lte, notExists, operationsByTag, pattern, queryTable, removeGitBranchesEntry, removeWorkspaceMember, resendWorkspaceMemberInvite, resolveBranch, searchBranch, searchTable, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberRole, upsertRecordWithID };
|
1946
2046
|
//# sourceMappingURL=index.mjs.map
|