@xata.io/client 0.13.0 → 0.13.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -2798,7 +2798,7 @@ declare class DatabaseApi {
2798
2798
  getGitBranchesMapping(workspace: WorkspaceID, dbName: DBName): Promise<ListGitBranchesResponse>;
2799
2799
  addGitBranchesEntry(workspace: WorkspaceID, dbName: DBName, body: AddGitBranchesEntryRequestBody): Promise<AddGitBranchesEntryResponse>;
2800
2800
  removeGitBranchesEntry(workspace: WorkspaceID, dbName: DBName, gitBranch: string): Promise<void>;
2801
- resolveBranch(workspace: WorkspaceID, dbName: DBName, gitBranch: string): Promise<ResolveBranchResponse>;
2801
+ resolveBranch(workspace: WorkspaceID, dbName: DBName, gitBranch?: string, fallbackBranch?: string): Promise<ResolveBranchResponse>;
2802
2802
  }
2803
2803
  declare class BranchApi {
2804
2804
  private extraProps;
@@ -2873,7 +2873,7 @@ declare type ValueAtColumn<O, P extends SelectableColumn<O>> = P extends '*' ? V
2873
2873
  declare type MAX_RECURSION = 5;
2874
2874
  declare type NestedColumns<O, RecursivePath extends any[]> = RecursivePath['length'] extends MAX_RECURSION ? never : If<IsObject<O>, Values<{
2875
2875
  [K in DataProps<O>]: If<IsArray<RemoveNullable<O[K]>>, K, // If the property is an array, we stop recursion. We don't support object arrays yet
2876
- If<IsObject<RemoveNullable<O[K]>>, RemoveNullable<O[K]> extends XataRecord ? SelectableColumn<RemoveNullable<O[K]>, [...RecursivePath, O[K]]> extends infer Column ? Column extends string ? K | `${K}.${Column}` : never : never : `${K}.${StringKeys<RemoveNullable<O[K]>> | '*'}`, // This allows usage of objects that are not links
2876
+ If<IsObject<RemoveNullable<O[K]>>, RemoveNullable<O[K]> extends XataRecord ? SelectableColumn<RemoveNullable<O[K]>, [...RecursivePath, O[K]]> extends infer Column ? Column extends string ? K | `${K}.${Column}` : never : never : RemoveNullable<O[K]> extends Date ? K : `${K}.${StringKeys<RemoveNullable<O[K]>> | '*'}`, // This allows usage of objects that are not links
2877
2877
  K>>;
2878
2878
  }>, never>;
2879
2879
  declare type DataProps<O> = Exclude<StringKeys<O>, StringKeys<XataRecord>>;
@@ -2950,9 +2950,9 @@ declare function isXataRecord(x: any): x is XataRecord & Record<string, unknown>
2950
2950
  declare type EditableData<O extends BaseData> = {
2951
2951
  [K in keyof O]: O[K] extends XataRecord ? {
2952
2952
  id: string;
2953
- } : NonNullable<O[K]> extends XataRecord ? {
2953
+ } | string : NonNullable<O[K]> extends XataRecord ? {
2954
2954
  id: string;
2955
- } | null | undefined : O[K];
2955
+ } | string | null | undefined : O[K];
2956
2956
  };
2957
2957
 
2958
2958
  /**
@@ -3440,6 +3440,18 @@ declare abstract class Repository<Data extends BaseData, Record extends XataReco
3440
3440
  * @returns The persisted records for the given ids (if a record could not be found it is not returned).
3441
3441
  */
3442
3442
  abstract read(ids: string[]): Promise<Array<Readonly<SelectedPick<Record, ['*']>>>>;
3443
+ /**
3444
+ * Queries a single record from the table by the id in the object.
3445
+ * @param object Object containing the id of the record.
3446
+ * @returns The persisted record for the given id or null if the record could not be found.
3447
+ */
3448
+ abstract read(object: Identifiable): Promise<Readonly<SelectedPick<Record, ['*']> | null>>;
3449
+ /**
3450
+ * Queries multiple records from the table by the ids in the objects.
3451
+ * @param objects Array of objects containing the ids of the records.
3452
+ * @returns The persisted records for the given ids (if a record could not be found it is not returned).
3453
+ */
3454
+ abstract read(objects: Identifiable[]): Promise<Array<Readonly<SelectedPick<Record, ['*']>>>>;
3443
3455
  /**
3444
3456
  * Partially update a single record.
3445
3457
  * @param object An object with its id and the columns to be updated.
@@ -3531,6 +3543,8 @@ declare class RestRepository<Data extends BaseData, Record extends XataRecord =
3531
3543
  create(objects: EditableData<Data>[]): Promise<SelectedPick<Record, ['*']>[]>;
3532
3544
  read(recordId: string): Promise<SelectedPick<Record, ['*']> | null>;
3533
3545
  read(recordIds: string[]): Promise<Array<Readonly<SelectedPick<Record, ['*']>>>>;
3546
+ read(object: Identifiable): Promise<SelectedPick<Record, ['*']> | null>;
3547
+ read(objects: Identifiable[]): Promise<Array<Readonly<SelectedPick<Record, ['*']>>>>;
3534
3548
  update(object: Partial<EditableData<Data>> & Identifiable): Promise<SelectedPick<Record, ['*']>>;
3535
3549
  update(recordId: string, object: Partial<EditableData<Data>>): Promise<SelectedPick<Record, ['*']>>;
3536
3550
  update(objects: Array<Partial<EditableData<Data>> & Identifiable>): Promise<SelectedPick<Record, ['*']>[]>;
package/dist/index.mjs CHANGED
@@ -73,6 +73,8 @@ function getFetchImplementation(userFetch) {
73
73
  return fetchImpl;
74
74
  }
75
75
 
76
+ const VERSION = "0.13.1";
77
+
76
78
  class ErrorWithCause extends Error {
77
79
  constructor(message, options) {
78
80
  super(message, options);
@@ -110,7 +112,12 @@ function getMessage(data) {
110
112
  }
111
113
 
112
114
  const resolveUrl = (url, queryParams = {}, pathParams = {}) => {
113
- const query = new URLSearchParams(queryParams).toString();
115
+ const cleanQueryParams = Object.entries(queryParams).reduce((acc, [key, value]) => {
116
+ if (value === void 0 || value === null)
117
+ return acc;
118
+ return { ...acc, [key]: value };
119
+ }, {});
120
+ const query = new URLSearchParams(cleanQueryParams).toString();
114
121
  const queryString = query.length > 0 ? `?${query}` : "";
115
122
  return url.replace(/\{\w*\}/g, (key) => pathParams[key.slice(1, -1)]) + queryString;
116
123
  };
@@ -150,6 +157,7 @@ async function fetch$1({
150
157
  body: body ? JSON.stringify(body) : void 0,
151
158
  headers: {
152
159
  "Content-Type": "application/json",
160
+ "User-Agent": `Xata client-ts/${VERSION}`,
153
161
  ...headers,
154
162
  ...hostHeader(fullUrl),
155
163
  Authorization: `Bearer ${apiKey}`
@@ -689,10 +697,10 @@ class DatabaseApi {
689
697
  ...this.extraProps
690
698
  });
691
699
  }
692
- resolveBranch(workspace, dbName, gitBranch) {
700
+ resolveBranch(workspace, dbName, gitBranch, fallbackBranch) {
693
701
  return operationsByTag.database.resolveBranch({
694
702
  pathParams: { workspace, dbName },
695
- queryParams: { gitBranch },
703
+ queryParams: { gitBranch, fallbackBranch },
696
704
  ...this.extraProps
697
705
  });
698
706
  }
@@ -1250,9 +1258,29 @@ class RestRepository extends Query {
1250
1258
  if (Array.isArray(a)) {
1251
1259
  if (a.length === 0)
1252
1260
  return [];
1253
- const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
1254
- await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
1255
- return records;
1261
+ const [itemsWithoutIds, itemsWithIds, order] = a.reduce(([accWithoutIds, accWithIds, accOrder], item) => {
1262
+ const condition = isString(item.id);
1263
+ accOrder.push(condition);
1264
+ if (condition) {
1265
+ accWithIds.push(item);
1266
+ } else {
1267
+ accWithoutIds.push(item);
1268
+ }
1269
+ return [accWithoutIds, accWithIds, accOrder];
1270
+ }, [[], [], []]);
1271
+ const recordsWithoutId = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, itemsWithoutIds);
1272
+ await Promise.all(recordsWithoutId.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
1273
+ if (itemsWithIds.length > 100) {
1274
+ console.warn("Bulk create operation with id is not optimized in the Xata API yet, this request might be slow");
1275
+ }
1276
+ const recordsWithId = await Promise.all(itemsWithIds.map((object) => this.create(object)));
1277
+ return order.map((condition) => {
1278
+ if (condition) {
1279
+ return recordsWithId.shift();
1280
+ } else {
1281
+ return recordsWithoutId.shift();
1282
+ }
1283
+ }).filter((record) => !!record);
1256
1284
  }
1257
1285
  if (isString(a) && isObject(b)) {
1258
1286
  if (a === "")
@@ -1279,16 +1307,18 @@ class RestRepository extends Query {
1279
1307
  if (Array.isArray(a)) {
1280
1308
  if (a.length === 0)
1281
1309
  return [];
1282
- return this.getAll({ filter: { id: { $any: a } } });
1310
+ const ids = a.map((item) => isString(item) ? item : item.id).filter((id2) => isString(id2));
1311
+ return this.getAll({ filter: { id: { $any: ids } } });
1283
1312
  }
1284
- if (isString(a)) {
1285
- const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, a);
1313
+ const id = isString(a) ? a : a.id;
1314
+ if (isString(id)) {
1315
+ const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, id);
1286
1316
  if (cacheRecord)
1287
1317
  return cacheRecord;
1288
1318
  const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
1289
1319
  try {
1290
1320
  const response = await getRecord({
1291
- pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: a },
1321
+ pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
1292
1322
  ...fetchProps
1293
1323
  });
1294
1324
  const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
@@ -1460,7 +1490,7 @@ bulkInsertTableRecords_fn = async function(objects) {
1460
1490
  body: { records },
1461
1491
  ...fetchProps
1462
1492
  });
1463
- const finalObjects = await this.any(...response.recordIDs.map((id) => this.filter("id", id))).getAll();
1493
+ const finalObjects = await this.read(response.recordIDs);
1464
1494
  if (finalObjects.length !== objects.length) {
1465
1495
  throw new Error("The server failed to save some records");
1466
1496
  }
@@ -1864,10 +1894,7 @@ async function getDatabaseBranch(branch, options) {
1864
1894
  apiUrl: databaseURL,
1865
1895
  fetchImpl: getFetchImplementation(options?.fetchImpl),
1866
1896
  workspacesApiUrl: `${protocol}//${host}`,
1867
- pathParams: {
1868
- dbBranchName,
1869
- workspace
1870
- }
1897
+ pathParams: { dbBranchName, workspace }
1871
1898
  });
1872
1899
  } catch (err) {
1873
1900
  if (isObject(err) && err.status === 404)