@xata.io/client 0.10.0 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.eslintrc.cjs +1 -2
- package/CHANGELOG.md +34 -0
- package/dist/index.cjs +121 -59
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +183 -88
- package/dist/index.mjs +120 -60
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/tsconfig.json +1 -0
package/dist/index.mjs
CHANGED
@@ -7,8 +7,11 @@ function compact(arr) {
|
|
7
7
|
function isObject(value) {
|
8
8
|
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
9
9
|
}
|
10
|
+
function isDefined(value) {
|
11
|
+
return value !== null && value !== void 0;
|
12
|
+
}
|
10
13
|
function isString(value) {
|
11
|
-
return value
|
14
|
+
return isDefined(value) && typeof value === "string";
|
12
15
|
}
|
13
16
|
function toBase64(value) {
|
14
17
|
try {
|
@@ -70,7 +73,12 @@ function getFetchImplementation(userFetch) {
|
|
70
73
|
return fetchImpl;
|
71
74
|
}
|
72
75
|
|
73
|
-
class
|
76
|
+
class ErrorWithCause extends Error {
|
77
|
+
constructor(message, options) {
|
78
|
+
super(message, options);
|
79
|
+
}
|
80
|
+
}
|
81
|
+
class FetcherError extends ErrorWithCause {
|
74
82
|
constructor(status, data) {
|
75
83
|
super(getMessage(data));
|
76
84
|
this.status = status;
|
@@ -366,6 +374,11 @@ const queryTable = (variables) => fetch$1({
|
|
366
374
|
method: "post",
|
367
375
|
...variables
|
368
376
|
});
|
377
|
+
const searchTable = (variables) => fetch$1({
|
378
|
+
url: "/db/{dbBranchName}/tables/{tableName}/search",
|
379
|
+
method: "post",
|
380
|
+
...variables
|
381
|
+
});
|
369
382
|
const searchBranch = (variables) => fetch$1({
|
370
383
|
url: "/db/{dbBranchName}/search",
|
371
384
|
method: "post",
|
@@ -429,6 +442,7 @@ const operationsByTag = {
|
|
429
442
|
getRecord,
|
430
443
|
bulkInsertTableRecords,
|
431
444
|
queryTable,
|
445
|
+
searchTable,
|
432
446
|
searchBranch
|
433
447
|
}
|
434
448
|
};
|
@@ -884,6 +898,13 @@ class RecordsApi {
|
|
884
898
|
...this.extraProps
|
885
899
|
});
|
886
900
|
}
|
901
|
+
searchTable(workspace, database, branch, tableName, query) {
|
902
|
+
return operationsByTag.records.searchTable({
|
903
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
904
|
+
body: query,
|
905
|
+
...this.extraProps
|
906
|
+
});
|
907
|
+
}
|
887
908
|
searchBranch(workspace, database, branch, query) {
|
888
909
|
return operationsByTag.records.searchBranch({
|
889
910
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
@@ -950,6 +971,9 @@ const PAGINATION_MAX_SIZE = 200;
|
|
950
971
|
const PAGINATION_DEFAULT_SIZE = 200;
|
951
972
|
const PAGINATION_MAX_OFFSET = 800;
|
952
973
|
const PAGINATION_DEFAULT_OFFSET = 0;
|
974
|
+
function isCursorPaginationOptions(options) {
|
975
|
+
return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
|
976
|
+
}
|
953
977
|
|
954
978
|
var __accessCheck$5 = (obj, member, msg) => {
|
955
979
|
if (!member.has(obj))
|
@@ -971,7 +995,7 @@ var __privateSet$4 = (obj, member, value, setter) => {
|
|
971
995
|
};
|
972
996
|
var _table$1, _repository, _data;
|
973
997
|
const _Query = class {
|
974
|
-
constructor(repository, table, data,
|
998
|
+
constructor(repository, table, data, rawParent) {
|
975
999
|
__privateAdd$5(this, _table$1, void 0);
|
976
1000
|
__privateAdd$5(this, _repository, void 0);
|
977
1001
|
__privateAdd$5(this, _data, { filter: {} });
|
@@ -983,6 +1007,7 @@ const _Query = class {
|
|
983
1007
|
} else {
|
984
1008
|
__privateSet$4(this, _repository, this);
|
985
1009
|
}
|
1010
|
+
const parent = cleanParent(data, rawParent);
|
986
1011
|
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
987
1012
|
__privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
|
988
1013
|
__privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
|
@@ -1054,13 +1079,13 @@ const _Query = class {
|
|
1054
1079
|
}
|
1055
1080
|
async *getIterator(options = {}) {
|
1056
1081
|
const { batchSize = 1 } = options;
|
1057
|
-
let
|
1058
|
-
let
|
1059
|
-
|
1060
|
-
|
1061
|
-
|
1062
|
-
|
1063
|
-
|
1082
|
+
let page = await this.getPaginated({ ...options, pagination: { size: batchSize, offset: 0 } });
|
1083
|
+
let more = page.hasNextPage();
|
1084
|
+
yield page.records;
|
1085
|
+
while (more) {
|
1086
|
+
page = await page.nextPage();
|
1087
|
+
more = page.hasNextPage();
|
1088
|
+
yield page.records;
|
1064
1089
|
}
|
1065
1090
|
}
|
1066
1091
|
async getMany(options = {}) {
|
@@ -1077,7 +1102,7 @@ const _Query = class {
|
|
1077
1102
|
}
|
1078
1103
|
async getFirst(options = {}) {
|
1079
1104
|
const records = await this.getMany({ ...options, pagination: { size: 1 } });
|
1080
|
-
return records[0]
|
1105
|
+
return records[0] ?? null;
|
1081
1106
|
}
|
1082
1107
|
cache(ttl) {
|
1083
1108
|
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
|
@@ -1102,6 +1127,12 @@ let Query = _Query;
|
|
1102
1127
|
_table$1 = new WeakMap();
|
1103
1128
|
_repository = new WeakMap();
|
1104
1129
|
_data = new WeakMap();
|
1130
|
+
function cleanParent(data, parent) {
|
1131
|
+
if (isCursorPaginationOptions(data.pagination)) {
|
1132
|
+
return { ...parent, sorting: void 0, filter: void 0 };
|
1133
|
+
}
|
1134
|
+
return parent;
|
1135
|
+
}
|
1105
1136
|
|
1106
1137
|
function isIdentifiable(x) {
|
1107
1138
|
return isObject(x) && isString(x?.id);
|
@@ -1184,6 +1215,8 @@ class RestRepository extends Query {
|
|
1184
1215
|
}
|
1185
1216
|
async create(a, b) {
|
1186
1217
|
if (Array.isArray(a)) {
|
1218
|
+
if (a.length === 0)
|
1219
|
+
return [];
|
1187
1220
|
const records = await __privateMethod$2(this, _bulkInsertTableRecords, bulkInsertTableRecords_fn).call(this, a);
|
1188
1221
|
await Promise.all(records.map((record) => __privateMethod$2(this, _setCacheRecord, setCacheRecord_fn).call(this, record)));
|
1189
1222
|
return records;
|
@@ -1209,27 +1242,36 @@ class RestRepository extends Query {
|
|
1209
1242
|
}
|
1210
1243
|
throw new Error("Invalid arguments for create method");
|
1211
1244
|
}
|
1212
|
-
async read(
|
1213
|
-
|
1214
|
-
|
1215
|
-
|
1216
|
-
|
1217
|
-
|
1218
|
-
|
1219
|
-
|
1220
|
-
|
1221
|
-
|
1222
|
-
const
|
1223
|
-
|
1224
|
-
|
1225
|
-
|
1226
|
-
|
1245
|
+
async read(a) {
|
1246
|
+
if (Array.isArray(a)) {
|
1247
|
+
if (a.length === 0)
|
1248
|
+
return [];
|
1249
|
+
return this.getAll({ filter: { id: { $any: a } } });
|
1250
|
+
}
|
1251
|
+
if (isString(a)) {
|
1252
|
+
const cacheRecord = await __privateMethod$2(this, _getCacheRecord, getCacheRecord_fn).call(this, a);
|
1253
|
+
if (cacheRecord)
|
1254
|
+
return cacheRecord;
|
1255
|
+
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1256
|
+
try {
|
1257
|
+
const response = await getRecord({
|
1258
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table), recordId: a },
|
1259
|
+
...fetchProps
|
1260
|
+
});
|
1261
|
+
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
1262
|
+
return initObject(this.db, schema, __privateGet$4(this, _table), response);
|
1263
|
+
} catch (e) {
|
1264
|
+
if (isObject(e) && e.status === 404) {
|
1265
|
+
return null;
|
1266
|
+
}
|
1267
|
+
throw e;
|
1227
1268
|
}
|
1228
|
-
throw e;
|
1229
1269
|
}
|
1230
1270
|
}
|
1231
1271
|
async update(a, b) {
|
1232
1272
|
if (Array.isArray(a)) {
|
1273
|
+
if (a.length === 0)
|
1274
|
+
return [];
|
1233
1275
|
if (a.length > 100) {
|
1234
1276
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1235
1277
|
}
|
@@ -1251,6 +1293,8 @@ class RestRepository extends Query {
|
|
1251
1293
|
}
|
1252
1294
|
async createOrUpdate(a, b) {
|
1253
1295
|
if (Array.isArray(a)) {
|
1296
|
+
if (a.length === 0)
|
1297
|
+
return [];
|
1254
1298
|
if (a.length > 100) {
|
1255
1299
|
console.warn("Bulk update operation is not optimized in the Xata API yet, this request might be slow");
|
1256
1300
|
}
|
@@ -1272,6 +1316,8 @@ class RestRepository extends Query {
|
|
1272
1316
|
}
|
1273
1317
|
async delete(a) {
|
1274
1318
|
if (Array.isArray(a)) {
|
1319
|
+
if (a.length === 0)
|
1320
|
+
return;
|
1275
1321
|
if (a.length > 100) {
|
1276
1322
|
console.warn("Bulk delete operation is not optimized in the Xata API yet, this request might be slow");
|
1277
1323
|
}
|
@@ -1292,9 +1338,14 @@ class RestRepository extends Query {
|
|
1292
1338
|
}
|
1293
1339
|
async search(query, options = {}) {
|
1294
1340
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1295
|
-
const { records } = await
|
1296
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1297
|
-
body: {
|
1341
|
+
const { records } = await searchTable({
|
1342
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1343
|
+
body: {
|
1344
|
+
query,
|
1345
|
+
fuzziness: options.fuzziness,
|
1346
|
+
highlight: options.highlight,
|
1347
|
+
filter: options.filter
|
1348
|
+
},
|
1298
1349
|
...fetchProps
|
1299
1350
|
});
|
1300
1351
|
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
@@ -1307,7 +1358,7 @@ class RestRepository extends Query {
|
|
1307
1358
|
const data = query.getQueryOptions();
|
1308
1359
|
const body = {
|
1309
1360
|
filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
|
1310
|
-
sort: data.sort ? buildSortFilter(data.sort) : void 0,
|
1361
|
+
sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
|
1311
1362
|
page: data.pagination,
|
1312
1363
|
columns: data.columns
|
1313
1364
|
};
|
@@ -1497,7 +1548,7 @@ const initObject = (db, schema, table, object) => {
|
|
1497
1548
|
const linkTable = column.link?.table;
|
1498
1549
|
if (!linkTable) {
|
1499
1550
|
console.error(`Failed to parse link for field ${column.name}`);
|
1500
|
-
} else if (
|
1551
|
+
} else if (isObject(value)) {
|
1501
1552
|
result[column.name] = initObject(db, schema, linkTable, value);
|
1502
1553
|
}
|
1503
1554
|
break;
|
@@ -1623,7 +1674,7 @@ class SchemaPlugin extends XataPlugin {
|
|
1623
1674
|
get: (_target, table) => {
|
1624
1675
|
if (!isString(table))
|
1625
1676
|
throw new Error("Invalid table name");
|
1626
|
-
if (
|
1677
|
+
if (__privateGet$2(this, _tables)[table] === void 0) {
|
1627
1678
|
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
|
1628
1679
|
}
|
1629
1680
|
return __privateGet$2(this, _tables)[table];
|
@@ -1695,10 +1746,10 @@ _schema = new WeakMap();
|
|
1695
1746
|
_search = new WeakSet();
|
1696
1747
|
search_fn = async function(query, options, getFetchProps) {
|
1697
1748
|
const fetchProps = await getFetchProps();
|
1698
|
-
const { tables, fuzziness } = options ?? {};
|
1749
|
+
const { tables, fuzziness, highlight } = options ?? {};
|
1699
1750
|
const { records } = await searchBranch({
|
1700
1751
|
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1701
|
-
body: { tables, query, fuzziness },
|
1752
|
+
body: { tables, query, fuzziness, highlight },
|
1702
1753
|
...fetchProps
|
1703
1754
|
});
|
1704
1755
|
return records;
|
@@ -1726,30 +1777,39 @@ const envBranchNames = [
|
|
1726
1777
|
"CF_PAGES_BRANCH",
|
1727
1778
|
"BRANCH"
|
1728
1779
|
];
|
1729
|
-
const defaultBranch = "main";
|
1730
1780
|
async function getCurrentBranchName(options) {
|
1731
|
-
const env =
|
1732
|
-
if (env)
|
1733
|
-
|
1734
|
-
|
1735
|
-
|
1736
|
-
|
1737
|
-
|
1738
|
-
|
1739
|
-
|
1740
|
-
return defaultBranch;
|
1781
|
+
const env = getBranchByEnvVariable();
|
1782
|
+
if (env) {
|
1783
|
+
const details = await getDatabaseBranch(env, options);
|
1784
|
+
if (details)
|
1785
|
+
return env;
|
1786
|
+
console.warn(`Branch ${env} not found in Xata. Ignoring...`);
|
1787
|
+
}
|
1788
|
+
const gitBranch = await getGitBranch();
|
1789
|
+
return resolveXataBranch(gitBranch, options);
|
1741
1790
|
}
|
1742
1791
|
async function getCurrentBranchDetails(options) {
|
1743
|
-
const
|
1744
|
-
|
1745
|
-
|
1746
|
-
|
1747
|
-
|
1748
|
-
|
1749
|
-
|
1750
|
-
|
1751
|
-
|
1752
|
-
|
1792
|
+
const branch = await getCurrentBranchName(options);
|
1793
|
+
return getDatabaseBranch(branch, options);
|
1794
|
+
}
|
1795
|
+
async function resolveXataBranch(gitBranch, options) {
|
1796
|
+
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1797
|
+
const apiKey = options?.apiKey || getAPIKey();
|
1798
|
+
if (!databaseURL)
|
1799
|
+
throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
|
1800
|
+
if (!apiKey)
|
1801
|
+
throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
|
1802
|
+
const [protocol, , host, , dbName] = databaseURL.split("/");
|
1803
|
+
const [workspace] = host.split(".");
|
1804
|
+
const { branch } = await resolveBranch({
|
1805
|
+
apiKey,
|
1806
|
+
apiUrl: databaseURL,
|
1807
|
+
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1808
|
+
workspacesApiUrl: `${protocol}//${host}`,
|
1809
|
+
pathParams: { dbName, workspace },
|
1810
|
+
queryParams: { gitBranch, fallbackBranch: getEnvVariable("XATA_FALLBACK_BRANCH") }
|
1811
|
+
});
|
1812
|
+
return branch;
|
1753
1813
|
}
|
1754
1814
|
async function getDatabaseBranch(branch, options) {
|
1755
1815
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
@@ -1838,7 +1898,7 @@ const buildClient = (plugins) => {
|
|
1838
1898
|
this.db = db;
|
1839
1899
|
this.search = search;
|
1840
1900
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|
1841
|
-
if (
|
1901
|
+
if (namespace === void 0)
|
1842
1902
|
continue;
|
1843
1903
|
const result = namespace.build(pluginOptions);
|
1844
1904
|
if (result instanceof Promise) {
|
@@ -1855,7 +1915,7 @@ const buildClient = (plugins) => {
|
|
1855
1915
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1856
1916
|
const apiKey = options?.apiKey || getAPIKey();
|
1857
1917
|
const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
|
1858
|
-
const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
1918
|
+
const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
1859
1919
|
if (!databaseURL || !apiKey) {
|
1860
1920
|
throw new Error("Options databaseURL and apiKey are required");
|
1861
1921
|
}
|
@@ -1882,7 +1942,7 @@ const buildClient = (plugins) => {
|
|
1882
1942
|
}, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
|
1883
1943
|
if (__privateGet(this, _branch))
|
1884
1944
|
return __privateGet(this, _branch);
|
1885
|
-
if (
|
1945
|
+
if (param === void 0)
|
1886
1946
|
return void 0;
|
1887
1947
|
const strategies = Array.isArray(param) ? [...param] : [param];
|
1888
1948
|
const evaluateBranch = async (strategy) => {
|
@@ -1907,5 +1967,5 @@ class XataError extends Error {
|
|
1907
1967
|
}
|
1908
1968
|
}
|
1909
1969
|
|
1910
|
-
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, isIdentifiable, isNot, isXataRecord, le, lt, lte, notExists, operationsByTag, pattern, queryTable, removeGitBranchesEntry, removeWorkspaceMember, resendWorkspaceMemberInvite, resolveBranch, searchBranch, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberRole, upsertRecordWithID };
|
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 };
|
1911
1971
|
//# sourceMappingURL=index.mjs.map
|