@xata.io/client 0.11.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/CHANGELOG.md +38 -0
- package/README.md +265 -1
- package/Usage.md +380 -0
- package/dist/index.cjs +92 -27
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +184 -35
- package/dist/index.mjs +92 -28
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -3
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
|
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
|
}
|
@@ -942,13 +950,13 @@ var __privateSet$5 = (obj, member, value, setter) => {
|
|
942
950
|
setter ? setter.call(obj, value) : member.set(obj, value);
|
943
951
|
return value;
|
944
952
|
};
|
945
|
-
var _query;
|
953
|
+
var _query, _page;
|
946
954
|
class Page {
|
947
955
|
constructor(query, meta, records = []) {
|
948
956
|
__privateAdd$6(this, _query, void 0);
|
949
957
|
__privateSet$5(this, _query, query);
|
950
958
|
this.meta = meta;
|
951
|
-
this.records = records;
|
959
|
+
this.records = new RecordArray(this, records);
|
952
960
|
}
|
953
961
|
async nextPage(size, offset) {
|
954
962
|
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
|
@@ -968,12 +976,40 @@ class Page {
|
|
968
976
|
}
|
969
977
|
_query = new WeakMap();
|
970
978
|
const PAGINATION_MAX_SIZE = 200;
|
971
|
-
const PAGINATION_DEFAULT_SIZE =
|
979
|
+
const PAGINATION_DEFAULT_SIZE = 20;
|
972
980
|
const PAGINATION_MAX_OFFSET = 800;
|
973
981
|
const PAGINATION_DEFAULT_OFFSET = 0;
|
974
982
|
function isCursorPaginationOptions(options) {
|
975
983
|
return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
|
976
984
|
}
|
985
|
+
const _RecordArray = class extends Array {
|
986
|
+
constructor(page, overrideRecords) {
|
987
|
+
super(...overrideRecords ?? page.records);
|
988
|
+
__privateAdd$6(this, _page, void 0);
|
989
|
+
__privateSet$5(this, _page, page);
|
990
|
+
}
|
991
|
+
async nextPage(size, offset) {
|
992
|
+
const newPage = await __privateGet$6(this, _page).nextPage(size, offset);
|
993
|
+
return new _RecordArray(newPage);
|
994
|
+
}
|
995
|
+
async previousPage(size, offset) {
|
996
|
+
const newPage = await __privateGet$6(this, _page).previousPage(size, offset);
|
997
|
+
return new _RecordArray(newPage);
|
998
|
+
}
|
999
|
+
async firstPage(size, offset) {
|
1000
|
+
const newPage = await __privateGet$6(this, _page).firstPage(size, offset);
|
1001
|
+
return new _RecordArray(newPage);
|
1002
|
+
}
|
1003
|
+
async lastPage(size, offset) {
|
1004
|
+
const newPage = await __privateGet$6(this, _page).lastPage(size, offset);
|
1005
|
+
return new _RecordArray(newPage);
|
1006
|
+
}
|
1007
|
+
hasNextPage() {
|
1008
|
+
return __privateGet$6(this, _page).meta.page.more;
|
1009
|
+
}
|
1010
|
+
};
|
1011
|
+
let RecordArray = _RecordArray;
|
1012
|
+
_page = new WeakMap();
|
977
1013
|
|
978
1014
|
var __accessCheck$5 = (obj, member, msg) => {
|
979
1015
|
if (!member.has(obj))
|
@@ -1000,7 +1036,7 @@ const _Query = class {
|
|
1000
1036
|
__privateAdd$5(this, _repository, void 0);
|
1001
1037
|
__privateAdd$5(this, _data, { filter: {} });
|
1002
1038
|
this.meta = { page: { cursor: "start", more: true } };
|
1003
|
-
this.records = [];
|
1039
|
+
this.records = new RecordArray(this, []);
|
1004
1040
|
__privateSet$4(this, _table$1, table);
|
1005
1041
|
if (repository) {
|
1006
1042
|
__privateSet$4(this, _repository, repository);
|
@@ -1089,8 +1125,11 @@ const _Query = class {
|
|
1089
1125
|
}
|
1090
1126
|
}
|
1091
1127
|
async getMany(options = {}) {
|
1092
|
-
const
|
1093
|
-
|
1128
|
+
const page = await this.getPaginated(options);
|
1129
|
+
if (page.hasNextPage() && options.pagination?.size === void 0) {
|
1130
|
+
console.trace("Calling getMany does not return all results. Paginate to get all results or call getAll.");
|
1131
|
+
}
|
1132
|
+
return page.records;
|
1094
1133
|
}
|
1095
1134
|
async getAll(options = {}) {
|
1096
1135
|
const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
|
@@ -1138,7 +1177,9 @@ function isIdentifiable(x) {
|
|
1138
1177
|
return isObject(x) && isString(x?.id);
|
1139
1178
|
}
|
1140
1179
|
function isXataRecord(x) {
|
1141
|
-
|
1180
|
+
const record = x;
|
1181
|
+
const metadata = record?.getMetadata();
|
1182
|
+
return isIdentifiable(x) && isObject(metadata) && typeof metadata.version === "number";
|
1142
1183
|
}
|
1143
1184
|
|
1144
1185
|
function isSortFilterString(value) {
|
@@ -1217,9 +1258,29 @@ class RestRepository extends Query {
|
|
1217
1258
|
if (Array.isArray(a)) {
|
1218
1259
|
if (a.length === 0)
|
1219
1260
|
return [];
|
1220
|
-
const
|
1221
|
-
|
1222
|
-
|
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);
|
1223
1284
|
}
|
1224
1285
|
if (isString(a) && isObject(b)) {
|
1225
1286
|
if (a === "")
|
@@ -1246,16 +1307,18 @@ class RestRepository extends Query {
|
|
1246
1307
|
if (Array.isArray(a)) {
|
1247
1308
|
if (a.length === 0)
|
1248
1309
|
return [];
|
1249
|
-
|
1310
|
+
const ids = a.map((item) => isString(item) ? item : item.id).filter((id2) => isString(id2));
|
1311
|
+
return this.getAll({ filter: { id: { $any: ids } } });
|
1250
1312
|
}
|
1251
|
-
|
1252
|
-
|
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);
|
1253
1316
|
if (cacheRecord)
|
1254
1317
|
return cacheRecord;
|
1255
1318
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1256
1319
|
try {
|
1257
1320
|
const response = await getRecord({
|
1258
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId:
|
1321
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: id },
|
1259
1322
|
...fetchProps
|
1260
1323
|
});
|
1261
1324
|
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
@@ -1427,7 +1490,7 @@ bulkInsertTableRecords_fn = async function(objects) {
|
|
1427
1490
|
body: { records },
|
1428
1491
|
...fetchProps
|
1429
1492
|
});
|
1430
|
-
const finalObjects = await this.
|
1493
|
+
const finalObjects = await this.read(response.recordIDs);
|
1431
1494
|
if (finalObjects.length !== objects.length) {
|
1432
1495
|
throw new Error("The server failed to save some records");
|
1433
1496
|
}
|
@@ -1528,7 +1591,8 @@ const transformObjectLinks = (object) => {
|
|
1528
1591
|
};
|
1529
1592
|
const initObject = (db, schema, table, object) => {
|
1530
1593
|
const result = {};
|
1531
|
-
|
1594
|
+
const { xata, ...rest } = object ?? {};
|
1595
|
+
Object.assign(result, rest);
|
1532
1596
|
const { columns } = schema.tables.find(({ name }) => name === table) ?? {};
|
1533
1597
|
if (!columns)
|
1534
1598
|
console.error(`Table ${table} not found in schema`);
|
@@ -1536,10 +1600,10 @@ const initObject = (db, schema, table, object) => {
|
|
1536
1600
|
const value = result[column.name];
|
1537
1601
|
switch (column.type) {
|
1538
1602
|
case "datetime": {
|
1539
|
-
const date = new Date(value);
|
1540
|
-
if (isNaN(date.getTime())) {
|
1603
|
+
const date = value !== void 0 ? new Date(value) : void 0;
|
1604
|
+
if (date && isNaN(date.getTime())) {
|
1541
1605
|
console.error(`Failed to parse date ${value} for field ${column.name}`);
|
1542
|
-
} else {
|
1606
|
+
} else if (date) {
|
1543
1607
|
result[column.name] = date;
|
1544
1608
|
}
|
1545
1609
|
break;
|
@@ -1564,7 +1628,10 @@ const initObject = (db, schema, table, object) => {
|
|
1564
1628
|
result.delete = function() {
|
1565
1629
|
return db[table].delete(result["id"]);
|
1566
1630
|
};
|
1567
|
-
|
1631
|
+
result.getMetadata = function() {
|
1632
|
+
return xata;
|
1633
|
+
};
|
1634
|
+
for (const prop of ["read", "update", "delete", "getMetadata"]) {
|
1568
1635
|
Object.defineProperty(result, prop, { enumerable: false });
|
1569
1636
|
}
|
1570
1637
|
Object.freeze(result);
|
@@ -1827,10 +1894,7 @@ async function getDatabaseBranch(branch, options) {
|
|
1827
1894
|
apiUrl: databaseURL,
|
1828
1895
|
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1829
1896
|
workspacesApiUrl: `${protocol}//${host}`,
|
1830
|
-
pathParams: {
|
1831
|
-
dbBranchName,
|
1832
|
-
workspace
|
1833
|
-
}
|
1897
|
+
pathParams: { dbBranchName, workspace }
|
1834
1898
|
});
|
1835
1899
|
} catch (err) {
|
1836
1900
|
if (isObject(err) && err.status === 404)
|
@@ -1967,5 +2031,5 @@ class XataError extends Error {
|
|
1967
2031
|
}
|
1968
2032
|
}
|
1969
2033
|
|
1970
|
-
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 };
|
2034
|
+
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 };
|
1971
2035
|
//# sourceMappingURL=index.mjs.map
|