@xata.io/client 0.0.0-alpha.vf0ac0ad → 0.0.0-alpha.vf221157
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 +40 -0
- package/dist/index.cjs +81 -52
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +147 -78
- package/dist/index.mjs +81 -53
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/tsconfig.json +1 -0
package/dist/index.mjs
CHANGED
@@ -366,6 +366,11 @@ const queryTable = (variables) => fetch$1({
|
|
366
366
|
method: "post",
|
367
367
|
...variables
|
368
368
|
});
|
369
|
+
const searchTable = (variables) => fetch$1({
|
370
|
+
url: "/db/{dbBranchName}/tables/{tableName}/search",
|
371
|
+
method: "post",
|
372
|
+
...variables
|
373
|
+
});
|
369
374
|
const searchBranch = (variables) => fetch$1({
|
370
375
|
url: "/db/{dbBranchName}/search",
|
371
376
|
method: "post",
|
@@ -429,6 +434,7 @@ const operationsByTag = {
|
|
429
434
|
getRecord,
|
430
435
|
bulkInsertTableRecords,
|
431
436
|
queryTable,
|
437
|
+
searchTable,
|
432
438
|
searchBranch
|
433
439
|
}
|
434
440
|
};
|
@@ -699,10 +705,10 @@ class BranchApi {
|
|
699
705
|
...this.extraProps
|
700
706
|
});
|
701
707
|
}
|
702
|
-
createBranch(workspace, database, branch, from
|
708
|
+
createBranch(workspace, database, branch, from, options = {}) {
|
703
709
|
return operationsByTag.branch.createBranch({
|
704
710
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
705
|
-
queryParams: { from },
|
711
|
+
queryParams: isString(from) ? { from } : void 0,
|
706
712
|
body: options,
|
707
713
|
...this.extraProps
|
708
714
|
});
|
@@ -884,6 +890,13 @@ class RecordsApi {
|
|
884
890
|
...this.extraProps
|
885
891
|
});
|
886
892
|
}
|
893
|
+
searchTable(workspace, database, branch, tableName, query) {
|
894
|
+
return operationsByTag.records.searchTable({
|
895
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
896
|
+
body: query,
|
897
|
+
...this.extraProps
|
898
|
+
});
|
899
|
+
}
|
887
900
|
searchBranch(workspace, database, branch, query) {
|
888
901
|
return operationsByTag.records.searchBranch({
|
889
902
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
@@ -930,16 +943,16 @@ class Page {
|
|
930
943
|
this.records = records;
|
931
944
|
}
|
932
945
|
async nextPage(size, offset) {
|
933
|
-
return __privateGet$6(this, _query).getPaginated({
|
946
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, after: this.meta.page.cursor } });
|
934
947
|
}
|
935
948
|
async previousPage(size, offset) {
|
936
|
-
return __privateGet$6(this, _query).getPaginated({
|
949
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, before: this.meta.page.cursor } });
|
937
950
|
}
|
938
951
|
async firstPage(size, offset) {
|
939
|
-
return __privateGet$6(this, _query).getPaginated({
|
952
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, first: this.meta.page.cursor } });
|
940
953
|
}
|
941
954
|
async lastPage(size, offset) {
|
942
|
-
return __privateGet$6(this, _query).getPaginated({
|
955
|
+
return __privateGet$6(this, _query).getPaginated({ pagination: { size, offset, last: this.meta.page.cursor } });
|
943
956
|
}
|
944
957
|
hasNextPage() {
|
945
958
|
return this.meta.page.more;
|
@@ -990,7 +1003,7 @@ const _Query = class {
|
|
990
1003
|
__privateGet$5(this, _data).filter.$none = data.filter?.$none ?? parent?.filter?.$none;
|
991
1004
|
__privateGet$5(this, _data).sort = data.sort ?? parent?.sort;
|
992
1005
|
__privateGet$5(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
|
993
|
-
__privateGet$5(this, _data).
|
1006
|
+
__privateGet$5(this, _data).pagination = data.pagination ?? parent?.pagination;
|
994
1007
|
__privateGet$5(this, _data).cache = data.cache ?? parent?.cache;
|
995
1008
|
this.any = this.any.bind(this);
|
996
1009
|
this.all = this.all.bind(this);
|
@@ -1005,8 +1018,8 @@ const _Query = class {
|
|
1005
1018
|
return __privateGet$5(this, _data);
|
1006
1019
|
}
|
1007
1020
|
key() {
|
1008
|
-
const { columns = [], filter = {}, sort = [],
|
1009
|
-
const key = JSON.stringify({ columns, filter, sort,
|
1021
|
+
const { columns = [], filter = {}, sort = [], pagination = {} } = __privateGet$5(this, _data);
|
1022
|
+
const key = JSON.stringify({ columns, filter, sort, pagination });
|
1010
1023
|
return toBase64(key);
|
1011
1024
|
}
|
1012
1025
|
any(...queries) {
|
@@ -1048,17 +1061,18 @@ const _Query = class {
|
|
1048
1061
|
return __privateGet$5(this, _repository).query(query);
|
1049
1062
|
}
|
1050
1063
|
async *[Symbol.asyncIterator]() {
|
1051
|
-
for await (const [record] of this.getIterator(1)) {
|
1064
|
+
for await (const [record] of this.getIterator({ batchSize: 1 })) {
|
1052
1065
|
yield record;
|
1053
1066
|
}
|
1054
1067
|
}
|
1055
|
-
async *getIterator(
|
1068
|
+
async *getIterator(options = {}) {
|
1069
|
+
const { batchSize = 1 } = options;
|
1056
1070
|
let offset = 0;
|
1057
1071
|
let end = false;
|
1058
1072
|
while (!end) {
|
1059
|
-
const { records, meta } = await this.getPaginated({ ...options,
|
1073
|
+
const { records, meta } = await this.getPaginated({ ...options, pagination: { size: batchSize, offset } });
|
1060
1074
|
yield records;
|
1061
|
-
offset +=
|
1075
|
+
offset += batchSize;
|
1062
1076
|
end = !meta.page.more;
|
1063
1077
|
}
|
1064
1078
|
}
|
@@ -1066,16 +1080,17 @@ const _Query = class {
|
|
1066
1080
|
const { records } = await this.getPaginated(options);
|
1067
1081
|
return records;
|
1068
1082
|
}
|
1069
|
-
async getAll(
|
1083
|
+
async getAll(options = {}) {
|
1084
|
+
const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
|
1070
1085
|
const results = [];
|
1071
|
-
for await (const page of this.getIterator(
|
1086
|
+
for await (const page of this.getIterator({ ...rest, batchSize })) {
|
1072
1087
|
results.push(...page);
|
1073
1088
|
}
|
1074
1089
|
return results;
|
1075
1090
|
}
|
1076
1091
|
async getFirst(options = {}) {
|
1077
|
-
const records = await this.getMany({ ...options,
|
1078
|
-
return records[0]
|
1092
|
+
const records = await this.getMany({ ...options, pagination: { size: 1 } });
|
1093
|
+
return records[0] ?? null;
|
1079
1094
|
}
|
1080
1095
|
cache(ttl) {
|
1081
1096
|
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
|
@@ -1087,10 +1102,10 @@ const _Query = class {
|
|
1087
1102
|
return this.firstPage(size, offset);
|
1088
1103
|
}
|
1089
1104
|
firstPage(size, offset) {
|
1090
|
-
return this.getPaginated({
|
1105
|
+
return this.getPaginated({ pagination: { size, offset } });
|
1091
1106
|
}
|
1092
1107
|
lastPage(size, offset) {
|
1093
|
-
return this.getPaginated({
|
1108
|
+
return this.getPaginated({ pagination: { size, offset, before: "end" } });
|
1094
1109
|
}
|
1095
1110
|
hasNextPage() {
|
1096
1111
|
return this.meta.page.more;
|
@@ -1290,9 +1305,13 @@ class RestRepository extends Query {
|
|
1290
1305
|
}
|
1291
1306
|
async search(query, options = {}) {
|
1292
1307
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1293
|
-
const { records } = await
|
1294
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1295
|
-
body: {
|
1308
|
+
const { records } = await searchTable({
|
1309
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1310
|
+
body: {
|
1311
|
+
query,
|
1312
|
+
fuzziness: options.fuzziness,
|
1313
|
+
filter: options.filter
|
1314
|
+
},
|
1296
1315
|
...fetchProps
|
1297
1316
|
});
|
1298
1317
|
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
@@ -1305,8 +1324,8 @@ class RestRepository extends Query {
|
|
1305
1324
|
const data = query.getQueryOptions();
|
1306
1325
|
const body = {
|
1307
1326
|
filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
|
1308
|
-
sort: data.sort ? buildSortFilter(data.sort) : void 0,
|
1309
|
-
page: data.
|
1327
|
+
sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
|
1328
|
+
page: data.pagination,
|
1310
1329
|
columns: data.columns
|
1311
1330
|
};
|
1312
1331
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
@@ -1449,8 +1468,8 @@ getCacheQuery_fn = async function(query) {
|
|
1449
1468
|
if (!result)
|
1450
1469
|
return null;
|
1451
1470
|
const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
|
1452
|
-
if (
|
1453
|
-
return
|
1471
|
+
if (ttl < 0)
|
1472
|
+
return null;
|
1454
1473
|
const hasExpired = result.date.getTime() + ttl < Date.now();
|
1455
1474
|
return hasExpired ? null : result;
|
1456
1475
|
};
|
@@ -1495,7 +1514,7 @@ const initObject = (db, schema, table, object) => {
|
|
1495
1514
|
const linkTable = column.link?.table;
|
1496
1515
|
if (!linkTable) {
|
1497
1516
|
console.error(`Failed to parse link for field ${column.name}`);
|
1498
|
-
} else if (
|
1517
|
+
} else if (isObject(value)) {
|
1499
1518
|
result[column.name] = initObject(db, schema, linkTable, value);
|
1500
1519
|
}
|
1501
1520
|
break;
|
@@ -1621,7 +1640,7 @@ class SchemaPlugin extends XataPlugin {
|
|
1621
1640
|
get: (_target, table) => {
|
1622
1641
|
if (!isString(table))
|
1623
1642
|
throw new Error("Invalid table name");
|
1624
|
-
if (
|
1643
|
+
if (__privateGet$2(this, _tables)[table] === void 0) {
|
1625
1644
|
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
|
1626
1645
|
}
|
1627
1646
|
return __privateGet$2(this, _tables)[table];
|
@@ -1724,30 +1743,39 @@ const envBranchNames = [
|
|
1724
1743
|
"CF_PAGES_BRANCH",
|
1725
1744
|
"BRANCH"
|
1726
1745
|
];
|
1727
|
-
const defaultBranch = "main";
|
1728
1746
|
async function getCurrentBranchName(options) {
|
1729
|
-
const env =
|
1730
|
-
if (env)
|
1731
|
-
|
1732
|
-
|
1733
|
-
|
1734
|
-
|
1735
|
-
|
1736
|
-
|
1737
|
-
|
1738
|
-
return defaultBranch;
|
1747
|
+
const env = getBranchByEnvVariable();
|
1748
|
+
if (env) {
|
1749
|
+
const details = await getDatabaseBranch(env, options);
|
1750
|
+
if (details)
|
1751
|
+
return env;
|
1752
|
+
console.warn(`Branch ${env} not found in Xata. Ignoring...`);
|
1753
|
+
}
|
1754
|
+
const gitBranch = await getGitBranch();
|
1755
|
+
return resolveXataBranch(gitBranch, options);
|
1739
1756
|
}
|
1740
1757
|
async function getCurrentBranchDetails(options) {
|
1741
|
-
const
|
1742
|
-
|
1743
|
-
|
1744
|
-
|
1745
|
-
|
1746
|
-
|
1747
|
-
|
1748
|
-
|
1749
|
-
|
1750
|
-
|
1758
|
+
const branch = await getCurrentBranchName(options);
|
1759
|
+
return getDatabaseBranch(branch, options);
|
1760
|
+
}
|
1761
|
+
async function resolveXataBranch(gitBranch, options) {
|
1762
|
+
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1763
|
+
const apiKey = options?.apiKey || getAPIKey();
|
1764
|
+
if (!databaseURL)
|
1765
|
+
throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
|
1766
|
+
if (!apiKey)
|
1767
|
+
throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
|
1768
|
+
const [protocol, , host, , dbName] = databaseURL.split("/");
|
1769
|
+
const [workspace] = host.split(".");
|
1770
|
+
const { branch } = await resolveBranch({
|
1771
|
+
apiKey,
|
1772
|
+
apiUrl: databaseURL,
|
1773
|
+
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1774
|
+
workspacesApiUrl: `${protocol}//${host}`,
|
1775
|
+
pathParams: { dbName, workspace },
|
1776
|
+
queryParams: { gitBranch, fallbackBranch: getEnvVariable("XATA_FALLBACK_BRANCH") }
|
1777
|
+
});
|
1778
|
+
return branch;
|
1751
1779
|
}
|
1752
1780
|
async function getDatabaseBranch(branch, options) {
|
1753
1781
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
@@ -1836,7 +1864,7 @@ const buildClient = (plugins) => {
|
|
1836
1864
|
this.db = db;
|
1837
1865
|
this.search = search;
|
1838
1866
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|
1839
|
-
if (
|
1867
|
+
if (namespace === void 0)
|
1840
1868
|
continue;
|
1841
1869
|
const result = namespace.build(pluginOptions);
|
1842
1870
|
if (result instanceof Promise) {
|
@@ -1853,7 +1881,7 @@ const buildClient = (plugins) => {
|
|
1853
1881
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1854
1882
|
const apiKey = options?.apiKey || getAPIKey();
|
1855
1883
|
const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
|
1856
|
-
const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
1884
|
+
const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
1857
1885
|
if (!databaseURL || !apiKey) {
|
1858
1886
|
throw new Error("Options databaseURL and apiKey are required");
|
1859
1887
|
}
|
@@ -1880,7 +1908,7 @@ const buildClient = (plugins) => {
|
|
1880
1908
|
}, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
|
1881
1909
|
if (__privateGet(this, _branch))
|
1882
1910
|
return __privateGet(this, _branch);
|
1883
|
-
if (
|
1911
|
+
if (param === void 0)
|
1884
1912
|
return void 0;
|
1885
1913
|
const strategies = Array.isArray(param) ? [...param] : [param];
|
1886
1914
|
const evaluateBranch = async (strategy) => {
|
@@ -1905,5 +1933,5 @@ class XataError extends Error {
|
|
1905
1933
|
}
|
1906
1934
|
}
|
1907
1935
|
|
1908
|
-
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 };
|
1936
|
+
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, searchTable, setTableSchema, startsWith, updateBranchMetadata, updateColumn, updateRecordWithID, updateTable, updateUser, updateWorkspace, updateWorkspaceMemberRole, upsertRecordWithID };
|
1909
1937
|
//# sourceMappingURL=index.mjs.map
|