@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/dist/index.cjs CHANGED
@@ -21,7 +21,8 @@ function toBase64(value) {
21
21
  try {
22
22
  return btoa(value);
23
23
  } catch (err) {
24
- return Buffer.from(value).toString("base64");
24
+ const buf = Buffer;
25
+ return buf.from(value).toString("base64");
25
26
  }
26
27
  }
27
28
 
@@ -77,16 +78,28 @@ function getFetchImplementation(userFetch) {
77
78
  return fetchImpl;
78
79
  }
79
80
 
80
- class FetcherError extends Error {
81
- constructor(status, data) {
81
+ const VERSION = "0.0.0-alpha.vfe4ae98";
82
+
83
+ class ErrorWithCause extends Error {
84
+ constructor(message, options) {
85
+ super(message, options);
86
+ }
87
+ }
88
+ class FetcherError extends ErrorWithCause {
89
+ constructor(status, data, requestId) {
82
90
  super(getMessage(data));
83
91
  this.status = status;
84
92
  this.errors = isBulkError(data) ? data.errors : void 0;
93
+ this.requestId = requestId;
85
94
  if (data instanceof Error) {
86
95
  this.stack = data.stack;
87
96
  this.cause = data.cause;
88
97
  }
89
98
  }
99
+ toString() {
100
+ const error = super.toString();
101
+ return `[${this.status}] (${this.requestId ?? "Unknown"}): ${error}`;
102
+ }
90
103
  }
91
104
  function isBulkError(error) {
92
105
  return isObject(error) && Array.isArray(error.errors);
@@ -109,7 +122,12 @@ function getMessage(data) {
109
122
  }
110
123
 
111
124
  const resolveUrl = (url, queryParams = {}, pathParams = {}) => {
112
- const query = new URLSearchParams(queryParams).toString();
125
+ const cleanQueryParams = Object.entries(queryParams).reduce((acc, [key, value]) => {
126
+ if (value === void 0 || value === null)
127
+ return acc;
128
+ return { ...acc, [key]: value };
129
+ }, {});
130
+ const query = new URLSearchParams(cleanQueryParams).toString();
113
131
  const queryString = query.length > 0 ? `?${query}` : "";
114
132
  return url.replace(/\{\w*\}/g, (key) => pathParams[key.slice(1, -1)]) + queryString;
115
133
  };
@@ -149,6 +167,7 @@ async function fetch$1({
149
167
  body: body ? JSON.stringify(body) : void 0,
150
168
  headers: {
151
169
  "Content-Type": "application/json",
170
+ "User-Agent": `Xata client-ts/${VERSION}`,
152
171
  ...headers,
153
172
  ...hostHeader(fullUrl),
154
173
  Authorization: `Bearer ${apiKey}`
@@ -157,14 +176,15 @@ async function fetch$1({
157
176
  if (response.status === 204) {
158
177
  return {};
159
178
  }
179
+ const requestId = response.headers?.get("x-request-id") ?? void 0;
160
180
  try {
161
181
  const jsonResponse = await response.json();
162
182
  if (response.ok) {
163
183
  return jsonResponse;
164
184
  }
165
- throw new FetcherError(response.status, jsonResponse);
185
+ throw new FetcherError(response.status, jsonResponse, requestId);
166
186
  } catch (error) {
167
- throw new FetcherError(response.status, error);
187
+ throw new FetcherError(response.status, error, requestId);
168
188
  }
169
189
  }
170
190
 
@@ -484,7 +504,7 @@ var __privateAdd$7 = (obj, member, value) => {
484
504
  throw TypeError("Cannot add the same private member more than once");
485
505
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
486
506
  };
487
- var __privateSet$6 = (obj, member, value, setter) => {
507
+ var __privateSet$7 = (obj, member, value, setter) => {
488
508
  __accessCheck$7(obj, member, "write to private field");
489
509
  setter ? setter.call(obj, value) : member.set(obj, value);
490
510
  return value;
@@ -499,7 +519,7 @@ class XataApiClient {
499
519
  if (!apiKey) {
500
520
  throw new Error("Could not resolve a valid apiKey");
501
521
  }
502
- __privateSet$6(this, _extraProps, {
522
+ __privateSet$7(this, _extraProps, {
503
523
  apiUrl: getHostUrl(provider, "main"),
504
524
  workspacesApiUrl: getHostUrl(provider, "workspaces"),
505
525
  fetchImpl: getFetchImplementation(options.fetch),
@@ -688,10 +708,10 @@ class DatabaseApi {
688
708
  ...this.extraProps
689
709
  });
690
710
  }
691
- resolveBranch(workspace, dbName, gitBranch) {
711
+ resolveBranch(workspace, dbName, gitBranch, fallbackBranch) {
692
712
  return operationsByTag.database.resolveBranch({
693
713
  pathParams: { workspace, dbName },
694
- queryParams: { gitBranch },
714
+ queryParams: { gitBranch, fallbackBranch },
695
715
  ...this.extraProps
696
716
  });
697
717
  }
@@ -936,18 +956,18 @@ var __privateAdd$6 = (obj, member, value) => {
936
956
  throw TypeError("Cannot add the same private member more than once");
937
957
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
938
958
  };
939
- var __privateSet$5 = (obj, member, value, setter) => {
959
+ var __privateSet$6 = (obj, member, value, setter) => {
940
960
  __accessCheck$6(obj, member, "write to private field");
941
961
  setter ? setter.call(obj, value) : member.set(obj, value);
942
962
  return value;
943
963
  };
944
- var _query;
964
+ var _query, _page;
945
965
  class Page {
946
966
  constructor(query, meta, records = []) {
947
967
  __privateAdd$6(this, _query, void 0);
948
- __privateSet$5(this, _query, query);
968
+ __privateSet$6(this, _query, query);
949
969
  this.meta = meta;
950
- this.records = records;
970
+ this.records = new RecordArray(this, records);
951
971
  }
952
972
  async nextPage(size, offset) {
953
973
  return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
@@ -967,12 +987,50 @@ class Page {
967
987
  }
968
988
  _query = new WeakMap();
969
989
  const PAGINATION_MAX_SIZE = 200;
970
- const PAGINATION_DEFAULT_SIZE = 200;
990
+ const PAGINATION_DEFAULT_SIZE = 20;
971
991
  const PAGINATION_MAX_OFFSET = 800;
972
992
  const PAGINATION_DEFAULT_OFFSET = 0;
973
993
  function isCursorPaginationOptions(options) {
974
994
  return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
975
995
  }
996
+ const _RecordArray = class extends Array {
997
+ constructor(...args) {
998
+ super(..._RecordArray.parseConstructorParams(...args));
999
+ __privateAdd$6(this, _page, void 0);
1000
+ __privateSet$6(this, _page, args[0]);
1001
+ }
1002
+ static parseConstructorParams(...args) {
1003
+ if (args.length === 1 && typeof args[0] === "number") {
1004
+ return new Array(args[0]);
1005
+ }
1006
+ if (args.length <= 2 && isObject(args[0]?.meta) && Array.isArray(args[1] ?? [])) {
1007
+ const result = args[1] ?? args[0].records ?? [];
1008
+ return new Array(...result);
1009
+ }
1010
+ return new Array(...args);
1011
+ }
1012
+ async nextPage(size, offset) {
1013
+ const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
1014
+ return new _RecordArray(newPage);
1015
+ }
1016
+ async previousPage(size, offset) {
1017
+ const newPage = await __privateGet$6(this, _page).previousPage(size, offset);
1018
+ return new _RecordArray(newPage);
1019
+ }
1020
+ async firstPage(size, offset) {
1021
+ const newPage = await __privateGet$6(this, _page).firstPage(size, offset);
1022
+ return new _RecordArray(newPage);
1023
+ }
1024
+ async lastPage(size, offset) {
1025
+ const newPage = await __privateGet$6(this, _page).lastPage(size, offset);
1026
+ return new _RecordArray(newPage);
1027
+ }
1028
+ hasNextPage() {
1029
+ return __privateGet$6(this, _page).meta.page.more;
1030
+ }
1031
+ };
1032
+ let RecordArray = _RecordArray;
1033
+ _page = new WeakMap();
976
1034
 
977
1035
  var __accessCheck$5 = (obj, member, msg) => {
978
1036
  if (!member.has(obj))
@@ -987,25 +1045,26 @@ var __privateAdd$5 = (obj, member, value) => {
987
1045
  throw TypeError("Cannot add the same private member more than once");
988
1046
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
989
1047
  };
990
- var __privateSet$4 = (obj, member, value, setter) => {
1048
+ var __privateSet$5 = (obj, member, value, setter) => {
991
1049
  __accessCheck$5(obj, member, "write to private field");
992
1050
  setter ? setter.call(obj, value) : member.set(obj, value);
993
1051
  return value;
994
1052
  };
995
1053
  var _table$1, _repository, _data;
996
1054
  const _Query = class {
997
- constructor(repository, table, data, parent) {
1055
+ constructor(repository, table, data, rawParent) {
998
1056
  __privateAdd$5(this, _table$1, void 0);
999
1057
  __privateAdd$5(this, _repository, void 0);
1000
1058
  __privateAdd$5(this, _data, { filter: {} });
1001
1059
  this.meta = { page: { cursor: "start", more: true } };
1002
- this.records = [];
1003
- __privateSet$4(this, _table$1, table);
1060
+ this.records = new RecordArray(this, []);
1061
+ __privateSet$5(this, _table$1, table);
1004
1062
  if (repository) {
1005
- __privateSet$4(this, _repository, repository);
1063
+ __privateSet$5(this, _repository, repository);
1006
1064
  } else {
1007
- __privateSet$4(this, _repository, this);
1065
+ __privateSet$5(this, _repository, this);
1008
1066
  }
1067
+ const parent = cleanParent(data, rawParent);
1009
1068
  __privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
1010
1069
  __privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
1011
1070
  __privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
@@ -1087,8 +1146,11 @@ const _Query = class {
1087
1146
  }
1088
1147
  }
1089
1148
  async getMany(options = {}) {
1090
- const { records } = await this.getPaginated(options);
1091
- return records;
1149
+ const page = await this.getPaginated(options);
1150
+ if (page.hasNextPage() && options.pagination?.size === void 0) {
1151
+ console.trace("Calling getMany does not return all results. Paginate to get all results or call getAll.");
1152
+ }
1153
+ return page.records;
1092
1154
  }
1093
1155
  async getAll(options = {}) {
1094
1156
  const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
@@ -1125,12 +1187,20 @@ let Query = _Query;
1125
1187
  _table$1 = new WeakMap();
1126
1188
  _repository = new WeakMap();
1127
1189
  _data = new WeakMap();
1190
+ function cleanParent(data, parent) {
1191
+ if (isCursorPaginationOptions(data.pagination)) {
1192
+ return { ...parent, sorting: void 0, filter: void 0 };
1193
+ }
1194
+ return parent;
1195
+ }
1128
1196
 
1129
1197
  function isIdentifiable(x) {
1130
1198
  return isObject(x) && isString(x?.id);
1131
1199
  }
1132
1200
  function isXataRecord(x) {
1133
- return isIdentifiable(x) && typeof x?.xata === "object" && typeof x?.xata?.version === "number";
1201
+ const record = x;
1202
+ const metadata = record?.getMetadata();
1203
+ return isIdentifiable(x) && isObject(metadata) && typeof metadata.version === "number";
1134
1204
  }
1135
1205
 
1136
1206
  function isSortFilterString(value) {
@@ -1169,7 +1239,7 @@ var __privateAdd$4 = (obj, member, value) => {
1169
1239
  throw TypeError("Cannot add the same private member more than once");
1170
1240
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1171
1241
  };
1172
- var __privateSet$3 = (obj, member, value, setter) => {
1242
+ var __privateSet$4 = (obj, member, value, setter) => {
1173
1243
  __accessCheck$4(obj, member, "write to private field");
1174
1244
  setter ? setter.call(obj, value) : member.set(obj, value);
1175
1245
  return value;
@@ -1178,7 +1248,7 @@ var __privateMethod$2 = (obj, member, method) => {
1178
1248
  __accessCheck$4(obj, member, "access private method");
1179
1249
  return method;
1180
1250
  };
1181
- var _table, _getFetchProps, _cache, _schema$1, _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, _getSchema$1, getSchema_fn$1;
1251
+ 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;
1182
1252
  class Repository extends Query {
1183
1253
  }
1184
1254
  class RestRepository extends Query {
@@ -1195,18 +1265,21 @@ class RestRepository extends Query {
1195
1265
  __privateAdd$4(this, _getCacheRecord);
1196
1266
  __privateAdd$4(this, _setCacheQuery);
1197
1267
  __privateAdd$4(this, _getCacheQuery);
1198
- __privateAdd$4(this, _getSchema$1);
1268
+ __privateAdd$4(this, _getSchemaTables$1);
1199
1269
  __privateAdd$4(this, _table, void 0);
1200
1270
  __privateAdd$4(this, _getFetchProps, void 0);
1201
1271
  __privateAdd$4(this, _cache, void 0);
1202
- __privateAdd$4(this, _schema$1, void 0);
1203
- __privateSet$3(this, _table, options.table);
1204
- __privateSet$3(this, _getFetchProps, options.pluginOptions.getFetchProps);
1272
+ __privateAdd$4(this, _schemaTables$2, void 0);
1273
+ __privateSet$4(this, _table, options.table);
1274
+ __privateSet$4(this, _getFetchProps, options.pluginOptions.getFetchProps);
1205
1275
  this.db = options.db;
1206
- __privateSet$3(this, _cache, options.pluginOptions.cache);
1276
+ __privateSet$4(this, _cache, options.pluginOptions.cache);
1277
+ __privateSet$4(this, _schemaTables$2, options.schemaTables);
1207
1278
  }
1208
1279
  async create(a, b) {
1209
1280
  if (Array.isArray(a)) {
1281
+ if (a.length === 0)
1282
+ return [];
1210
1283
  const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
1211
1284
  await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
1212
1285
  return records;
@@ -1232,27 +1305,38 @@ class RestRepository extends Query {
1232
1305
  }
1233
1306
  throw new Error("Invalid arguments for create method");
1234
1307
  }
1235
- async read(recordId) {
1236
- const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, recordId);
1237
- if (cacheRecord)
1238
- return cacheRecord;
1239
- const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1240
- try {
1241
- const response = await getRecord({
1242
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId },
1243
- ...fetchProps
1244
- });
1245
- const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1246
- return initObject(this.db, schema, __privateGet$4(this, _table), response);
1247
- } catch (e) {
1248
- if (isObject(e) && e.status === 404) {
1249
- return null;
1308
+ async read(a) {
1309
+ if (Array.isArray(a)) {
1310
+ if (a.length === 0)
1311
+ return [];
1312
+ const ids = a.map((item) => isString(item) ? item : item.id).filter((id2) => isString(id2));
1313
+ return this.getAll({ filter: { id: { $any: ids } } });
1314
+ }
1315
+ const id = isString(a) ? a : a.id;
1316
+ if (isString(id)) {
1317
+ const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, id);
1318
+ if (cacheRecord)
1319
+ return cacheRecord;
1320
+ const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1321
+ try {
1322
+ const response = await getRecord({
1323
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
1324
+ ...fetchProps
1325
+ });
1326
+ const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1327
+ return initObject(this.db, schemaTables, __privateGet$4(this, _table), response);
1328
+ } catch (e) {
1329
+ if (isObject(e) && e.status === 404) {
1330
+ return null;
1331
+ }
1332
+ throw e;
1250
1333
  }
1251
- throw e;
1252
1334
  }
1253
1335
  }
1254
1336
  async update(a, b) {
1255
1337
  if (Array.isArray(a)) {
1338
+ if (a.length === 0)
1339
+ return [];
1256
1340
  if (a.length > 100) {
1257
1341
  console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
1258
1342
  }
@@ -1274,6 +1358,8 @@ class RestRepository extends Query {
1274
1358
  }
1275
1359
  async createOrUpdate(a, b) {
1276
1360
  if (Array.isArray(a)) {
1361
+ if (a.length === 0)
1362
+ return [];
1277
1363
  if (a.length > 100) {
1278
1364
  console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
1279
1365
  }
@@ -1295,6 +1381,8 @@ class RestRepository extends Query {
1295
1381
  }
1296
1382
  async delete(a) {
1297
1383
  if (Array.isArray(a)) {
1384
+ if (a.length === 0)
1385
+ return;
1298
1386
  if (a.length > 100) {
1299
1387
  console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
1300
1388
  }
@@ -1320,24 +1408,22 @@ class RestRepository extends Query {
1320
1408
  body: {
1321
1409
  query,
1322
1410
  fuzziness: options.fuzziness,
1411
+ highlight: options.highlight,
1323
1412
  filter: options.filter
1324
1413
  },
1325
1414
  ...fetchProps
1326
1415
  });
1327
- const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1328
- return records.map((item) => initObject(this.db, schema, __privateGet$4(this, _table), item));
1416
+ const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1417
+ return records.map((item) => initObject(this.db, schemaTables, __privateGet$4(this, _table), item));
1329
1418
  }
1330
1419
  async query(query) {
1331
1420
  const cacheQuery = await __privateMethod$2(this, _getCacheQuery, getCacheQuery_fn).call(this, query);
1332
1421
  if (cacheQuery)
1333
1422
  return new Page(query, cacheQuery.meta, cacheQuery.records);
1334
1423
  const data = query.getQueryOptions();
1335
- const filter = Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0;
1336
- const sort = data.sort !== void 0 ? buildSortFilter(data.sort) : void 0;
1337
- const isCursorPagination = isCursorPaginationOptions(data.pagination);
1338
1424
  const body = {
1339
- filter: isCursorPagination ? void 0 : filter,
1340
- sort: isCursorPagination ? void 0 : sort,
1425
+ filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
1426
+ sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
1341
1427
  page: data.pagination,
1342
1428
  columns: data.columns
1343
1429
  };
@@ -1347,8 +1433,8 @@ class RestRepository extends Query {
1347
1433
  body,
1348
1434
  ...fetchProps
1349
1435
  });
1350
- const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
1351
- const records = objects.map((record) => initObject(this.db, schema, __privateGet$4(this, _table), record));
1436
+ const schemaTables = await __privateMethod$2(this, _getSchemaTables$1, getSchemaTables_fn$1).call(this);
1437
+ const records = objects.map((record) => initObject(this.db, schemaTables, __privateGet$4(this, _table), record));
1352
1438
  await __privateMethod$2(this, _setCacheQuery, setCacheQuery_fn).call(this, query, meta, records);
1353
1439
  return new Page(query, meta, records);
1354
1440
  }
@@ -1356,7 +1442,7 @@ class RestRepository extends Query {
1356
1442
  _table = new WeakMap();
1357
1443
  _getFetchProps = new WeakMap();
1358
1444
  _cache = new WeakMap();
1359
- _schema$1 = new WeakMap();
1445
+ _schemaTables$2 = new WeakMap();
1360
1446
  _insertRecordWithoutId = new WeakSet();
1361
1447
  insertRecordWithoutId_fn = async function(object) {
1362
1448
  const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
@@ -1401,16 +1487,20 @@ _bulkInsertTableRecords = new WeakSet();
1401
1487
  bulkInsertTableRecords_fn = async function(objects) {
1402
1488
  const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1403
1489
  const records = objects.map((object) => transformObjectLinks(object));
1404
- const response = await bulkInsertTableRecords({
1490
+ const { recordIDs } = await bulkInsertTableRecords({
1405
1491
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
1406
1492
  body: { records },
1407
1493
  ...fetchProps
1408
1494
  });
1409
- const finalObjects = await this.any(...response.recordIDs.map((id) => this.filter("id", id))).getAll();
1495
+ const finalObjects = await this.read(recordIDs);
1410
1496
  if (finalObjects.length !== objects.length) {
1411
1497
  throw new Error("The server failed to save some records");
1412
1498
  }
1413
- return finalObjects;
1499
+ const dictionary = finalObjects.reduce((acc, object) => {
1500
+ acc[object.id] = object;
1501
+ return acc;
1502
+ }, {});
1503
+ return recordIDs.map((id) => dictionary[id]);
1414
1504
  };
1415
1505
  _updateRecordWithID = new WeakSet();
1416
1506
  updateRecordWithID_fn = async function(recordId, object) {
@@ -1486,17 +1576,17 @@ getCacheQuery_fn = async function(query) {
1486
1576
  const hasExpired = result.date.getTime() + ttl < Date.now();
1487
1577
  return hasExpired ? null : result;
1488
1578
  };
1489
- _getSchema$1 = new WeakSet();
1490
- getSchema_fn$1 = async function() {
1491
- if (__privateGet$4(this, _schema$1))
1492
- return __privateGet$4(this, _schema$1);
1579
+ _getSchemaTables$1 = new WeakSet();
1580
+ getSchemaTables_fn$1 = async function() {
1581
+ if (__privateGet$4(this, _schemaTables$2))
1582
+ return __privateGet$4(this, _schemaTables$2);
1493
1583
  const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1494
1584
  const { schema } = await getBranchDetails({
1495
1585
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1496
1586
  ...fetchProps
1497
1587
  });
1498
- __privateSet$3(this, _schema$1, schema);
1499
- return schema;
1588
+ __privateSet$4(this, _schemaTables$2, schema.tables);
1589
+ return schema.tables;
1500
1590
  };
1501
1591
  const transformObjectLinks = (object) => {
1502
1592
  return Object.entries(object).reduce((acc, [key, value]) => {
@@ -1505,20 +1595,21 @@ const transformObjectLinks = (object) => {
1505
1595
  return { ...acc, [key]: isIdentifiable(value) ? value.id : value };
1506
1596
  }, {});
1507
1597
  };
1508
- const initObject = (db, schema, table, object) => {
1598
+ const initObject = (db, schemaTables, table, object) => {
1509
1599
  const result = {};
1510
- Object.assign(result, object);
1511
- const { columns } = schema.tables.find(({ name }) => name === table) ?? {};
1600
+ const { xata, ...rest } = object ?? {};
1601
+ Object.assign(result, rest);
1602
+ const { columns } = schemaTables.find(({ name }) => name === table) ?? {};
1512
1603
  if (!columns)
1513
1604
  console.error(`Table ${table} not found in schema`);
1514
1605
  for (const column of columns ?? []) {
1515
1606
  const value = result[column.name];
1516
1607
  switch (column.type) {
1517
1608
  case "datetime": {
1518
- const date = new Date(value);
1519
- if (isNaN(date.getTime())) {
1609
+ const date = value !== void 0 ? new Date(value) : void 0;
1610
+ if (date && isNaN(date.getTime())) {
1520
1611
  console.error(`Failed to parse date ${value} for field ${column.name}`);
1521
- } else {
1612
+ } else if (date) {
1522
1613
  result[column.name] = date;
1523
1614
  }
1524
1615
  break;
@@ -1528,7 +1619,7 @@ const initObject = (db, schema, table, object) => {
1528
1619
  if (!linkTable) {
1529
1620
  console.error(`Failed to parse link for field ${column.name}`);
1530
1621
  } else if (isObject(value)) {
1531
- result[column.name] = initObject(db, schema, linkTable, value);
1622
+ result[column.name] = initObject(db, schemaTables, linkTable, value);
1532
1623
  }
1533
1624
  break;
1534
1625
  }
@@ -1543,7 +1634,10 @@ const initObject = (db, schema, table, object) => {
1543
1634
  result.delete = function() {
1544
1635
  return db[table].delete(result["id"]);
1545
1636
  };
1546
- for (const prop of ["read", "update", "delete"]) {
1637
+ result.getMetadata = function() {
1638
+ return xata;
1639
+ };
1640
+ for (const prop of ["read", "update", "delete", "getMetadata"]) {
1547
1641
  Object.defineProperty(result, prop, { enumerable: false });
1548
1642
  }
1549
1643
  Object.freeze(result);
@@ -1572,7 +1666,7 @@ var __privateAdd$3 = (obj, member, value) => {
1572
1666
  throw TypeError("Cannot add the same private member more than once");
1573
1667
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1574
1668
  };
1575
- var __privateSet$2 = (obj, member, value, setter) => {
1669
+ var __privateSet$3 = (obj, member, value, setter) => {
1576
1670
  __accessCheck$3(obj, member, "write to private field");
1577
1671
  setter ? setter.call(obj, value) : member.set(obj, value);
1578
1672
  return value;
@@ -1581,7 +1675,7 @@ var _map;
1581
1675
  class SimpleCache {
1582
1676
  constructor(options = {}) {
1583
1677
  __privateAdd$3(this, _map, void 0);
1584
- __privateSet$2(this, _map, /* @__PURE__ */ new Map());
1678
+ __privateSet$3(this, _map, /* @__PURE__ */ new Map());
1585
1679
  this.capacity = options.max ?? 500;
1586
1680
  this.cacheRecords = options.cacheRecords ?? true;
1587
1681
  this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
@@ -1641,12 +1735,18 @@ var __privateAdd$2 = (obj, member, value) => {
1641
1735
  throw TypeError("Cannot add the same private member more than once");
1642
1736
  member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
1643
1737
  };
1644
- var _tables;
1738
+ var __privateSet$2 = (obj, member, value, setter) => {
1739
+ __accessCheck$2(obj, member, "write to private field");
1740
+ setter ? setter.call(obj, value) : member.set(obj, value);
1741
+ return value;
1742
+ };
1743
+ var _tables, _schemaTables$1;
1645
1744
  class SchemaPlugin extends XataPlugin {
1646
- constructor(tableNames) {
1745
+ constructor(schemaTables) {
1647
1746
  super();
1648
- this.tableNames = tableNames;
1649
1747
  __privateAdd$2(this, _tables, {});
1748
+ __privateAdd$2(this, _schemaTables$1, void 0);
1749
+ __privateSet$2(this, _schemaTables$1, schemaTables);
1650
1750
  }
1651
1751
  build(pluginOptions) {
1652
1752
  const db = new Proxy({}, {
@@ -1659,13 +1759,15 @@ class SchemaPlugin extends XataPlugin {
1659
1759
  return __privateGet$2(this, _tables)[table];
1660
1760
  }
1661
1761
  });
1662
- for (const table of this.tableNames ?? []) {
1663
- db[table] = new RestRepository({ db, pluginOptions, table });
1762
+ const tableNames = __privateGet$2(this, _schemaTables$1)?.map(({ name }) => name) ?? [];
1763
+ for (const table of tableNames) {
1764
+ db[table] = new RestRepository({ db, pluginOptions, table, schemaTables: __privateGet$2(this, _schemaTables$1) });
1664
1765
  }
1665
1766
  return db;
1666
1767
  }
1667
1768
  }
1668
1769
  _tables = new WeakMap();
1770
+ _schemaTables$1 = new WeakMap();
1669
1771
 
1670
1772
  var __accessCheck$1 = (obj, member, msg) => {
1671
1773
  if (!member.has(obj))
@@ -1689,61 +1791,62 @@ var __privateMethod$1 = (obj, member, method) => {
1689
1791
  __accessCheck$1(obj, member, "access private method");
1690
1792
  return method;
1691
1793
  };
1692
- var _schema, _search, search_fn, _getSchema, getSchema_fn;
1794
+ var _schemaTables, _search, search_fn, _getSchemaTables, getSchemaTables_fn;
1693
1795
  class SearchPlugin extends XataPlugin {
1694
- constructor(db) {
1796
+ constructor(db, schemaTables) {
1695
1797
  super();
1696
1798
  this.db = db;
1697
1799
  __privateAdd$1(this, _search);
1698
- __privateAdd$1(this, _getSchema);
1699
- __privateAdd$1(this, _schema, void 0);
1800
+ __privateAdd$1(this, _getSchemaTables);
1801
+ __privateAdd$1(this, _schemaTables, void 0);
1802
+ __privateSet$1(this, _schemaTables, schemaTables);
1700
1803
  }
1701
1804
  build({ getFetchProps }) {
1702
1805
  return {
1703
1806
  all: async (query, options = {}) => {
1704
1807
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1705
- const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
1808
+ const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
1706
1809
  return records.map((record) => {
1707
1810
  const { table = "orphan" } = record.xata;
1708
- return { table, record: initObject(this.db, schema, table, record) };
1811
+ return { table, record: initObject(this.db, schemaTables, table, record) };
1709
1812
  });
1710
1813
  },
1711
1814
  byTable: async (query, options = {}) => {
1712
1815
  const records = await __privateMethod$1(this, _search, search_fn).call(this, query, options, getFetchProps);
1713
- const schema = await __privateMethod$1(this, _getSchema, getSchema_fn).call(this, getFetchProps);
1816
+ const schemaTables = await __privateMethod$1(this, _getSchemaTables, getSchemaTables_fn).call(this, getFetchProps);
1714
1817
  return records.reduce((acc, record) => {
1715
1818
  const { table = "orphan" } = record.xata;
1716
1819
  const items = acc[table] ?? [];
1717
- const item = initObject(this.db, schema, table, record);
1820
+ const item = initObject(this.db, schemaTables, table, record);
1718
1821
  return { ...acc, [table]: [...items, item] };
1719
1822
  }, {});
1720
1823
  }
1721
1824
  };
1722
1825
  }
1723
1826
  }
1724
- _schema = new WeakMap();
1827
+ _schemaTables = new WeakMap();
1725
1828
  _search = new WeakSet();
1726
1829
  search_fn = async function(query, options, getFetchProps) {
1727
1830
  const fetchProps = await getFetchProps();
1728
- const { tables, fuzziness } = options ?? {};
1831
+ const { tables, fuzziness, highlight } = options ?? {};
1729
1832
  const { records } = await searchBranch({
1730
1833
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1731
- body: { tables, query, fuzziness },
1834
+ body: { tables, query, fuzziness, highlight },
1732
1835
  ...fetchProps
1733
1836
  });
1734
1837
  return records;
1735
1838
  };
1736
- _getSchema = new WeakSet();
1737
- getSchema_fn = async function(getFetchProps) {
1738
- if (__privateGet$1(this, _schema))
1739
- return __privateGet$1(this, _schema);
1839
+ _getSchemaTables = new WeakSet();
1840
+ getSchemaTables_fn = async function(getFetchProps) {
1841
+ if (__privateGet$1(this, _schemaTables))
1842
+ return __privateGet$1(this, _schemaTables);
1740
1843
  const fetchProps = await getFetchProps();
1741
1844
  const { schema } = await getBranchDetails({
1742
1845
  pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
1743
1846
  ...fetchProps
1744
1847
  });
1745
- __privateSet$1(this, _schema, schema);
1746
- return schema;
1848
+ __privateSet$1(this, _schemaTables, schema.tables);
1849
+ return schema.tables;
1747
1850
  };
1748
1851
 
1749
1852
  const isBranchStrategyBuilder = (strategy) => {
@@ -1806,10 +1909,7 @@ async function getDatabaseBranch(branch, options) {
1806
1909
  apiUrl: databaseURL,
1807
1910
  fetchImpl: getFetchImplementation(options?.fetchImpl),
1808
1911
  workspacesApiUrl: `${protocol}//${host}`,
1809
- pathParams: {
1810
- dbBranchName,
1811
- workspace
1812
- }
1912
+ pathParams: { dbBranchName, workspace }
1813
1913
  });
1814
1914
  } catch (err) {
1815
1915
  if (isObject(err) && err.status === 404)
@@ -1862,7 +1962,7 @@ var __privateMethod = (obj, member, method) => {
1862
1962
  const buildClient = (plugins) => {
1863
1963
  var _branch, _parseOptions, parseOptions_fn, _getFetchProps, getFetchProps_fn, _evaluateBranch, evaluateBranch_fn, _a;
1864
1964
  return _a = class {
1865
- constructor(options = {}, tables) {
1965
+ constructor(options = {}, schemaTables) {
1866
1966
  __privateAdd(this, _parseOptions);
1867
1967
  __privateAdd(this, _getFetchProps);
1868
1968
  __privateAdd(this, _evaluateBranch);
@@ -1872,8 +1972,8 @@ const buildClient = (plugins) => {
1872
1972
  getFetchProps: () => __privateMethod(this, _getFetchProps, getFetchProps_fn).call(this, safeOptions),
1873
1973
  cache: safeOptions.cache
1874
1974
  };
1875
- const db = new SchemaPlugin(tables).build(pluginOptions);
1876
- const search = new SearchPlugin(db).build(pluginOptions);
1975
+ const db = new SchemaPlugin(schemaTables).build(pluginOptions);
1976
+ const search = new SearchPlugin(db, schemaTables).build(pluginOptions);
1877
1977
  this.db = db;
1878
1978
  this.search = search;
1879
1979
  for (const [key, namespace] of Object.entries(plugins ?? {})) {
@@ -1954,6 +2054,7 @@ exports.PAGINATION_MAX_OFFSET = PAGINATION_MAX_OFFSET;
1954
2054
  exports.PAGINATION_MAX_SIZE = PAGINATION_MAX_SIZE;
1955
2055
  exports.Page = Page;
1956
2056
  exports.Query = Query;
2057
+ exports.RecordArray = RecordArray;
1957
2058
  exports.Repository = Repository;
1958
2059
  exports.RestRepository = RestRepository;
1959
2060
  exports.SchemaPlugin = SchemaPlugin;