@xata.io/client 0.0.0-alpha.vf7b5320 → 0.0.0-alpha.vf957886
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 +32 -0
- package/dist/index.cjs +97 -49
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +153 -80
- package/dist/index.mjs +96 -50
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/tsconfig.json +1 -0
package/.eslintrc.cjs
CHANGED
@@ -6,8 +6,7 @@ module.exports = {
|
|
6
6
|
project: 'packages/client/tsconfig.json'
|
7
7
|
},
|
8
8
|
rules: {
|
9
|
-
'@typescript-eslint/no-explicit-any': 'off',
|
10
|
-
'@typescript-eslint/ban-types': 'off',
|
11
9
|
'@typescript-eslint/no-floating-promises': 'error',
|
10
|
+
"@typescript-eslint/strict-boolean-expressions": ["error", { allowNullableString: true, allowNullableObject: true }],
|
12
11
|
}
|
13
12
|
};
|
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,37 @@
|
|
1
1
|
# @xata.io/client
|
2
2
|
|
3
|
+
## 0.10.2
|
4
|
+
|
5
|
+
### Patch Changes
|
6
|
+
|
7
|
+
- [#312](https://github.com/xataio/client-ts/pull/312) [`0edf1af`](https://github.com/xataio/client-ts/commit/0edf1af2205c4761d53a02c74ddaab3168d69775) Thanks [@SferaDev](https://github.com/SferaDev)! - Add filtering to search by table
|
8
|
+
|
9
|
+
* [#312](https://github.com/xataio/client-ts/pull/312) [`66ad7cc`](https://github.com/xataio/client-ts/commit/66ad7cc0365046c5d039c37117feac04428d8373) Thanks [@SferaDev](https://github.com/SferaDev)! - Add new API method for searching in a given table
|
10
|
+
|
11
|
+
## 0.10.1
|
12
|
+
|
13
|
+
### Patch Changes
|
14
|
+
|
15
|
+
- [#271](https://github.com/xataio/client-ts/pull/271) [`0bb17b8`](https://github.com/xataio/client-ts/commit/0bb17b88d49f1c8be32d2d6b0b3a5918890876cb) Thanks [@SferaDev](https://github.com/SferaDev)! - Link and resolve branches from git
|
16
|
+
|
17
|
+
## 0.10.0
|
18
|
+
|
19
|
+
### Minor Changes
|
20
|
+
|
21
|
+
- [#272](https://github.com/xataio/client-ts/pull/272) [`6d76275`](https://github.com/xataio/client-ts/commit/6d7627555a404a4c2da42f4187df6f8300f9a46f) Thanks [@SferaDev](https://github.com/SferaDev)! - Rename page options to pagination
|
22
|
+
|
23
|
+
* [#270](https://github.com/xataio/client-ts/pull/270) [`1864742`](https://github.com/xataio/client-ts/commit/18647428d8608841de514c3784fb711c39dccc6d) Thanks [@SferaDev](https://github.com/SferaDev)! - Move chunk to options object
|
24
|
+
|
25
|
+
### Patch Changes
|
26
|
+
|
27
|
+
- [#281](https://github.com/xataio/client-ts/pull/281) [`d1ec0df`](https://github.com/xataio/client-ts/commit/d1ec0df14834088a816919bfc68216f3f9b2d9ef) Thanks [@SferaDev](https://github.com/SferaDev)! - Do not send from param on undefined
|
28
|
+
|
29
|
+
* [#282](https://github.com/xataio/client-ts/pull/282) [`1af6f1a`](https://github.com/xataio/client-ts/commit/1af6f1aaa1123e77a895961581c87f06a88db698) Thanks [@SferaDev](https://github.com/SferaDev)! - Do not allow filter/sort with cursor navigation
|
30
|
+
|
31
|
+
- [#284](https://github.com/xataio/client-ts/pull/284) [`be4eda8`](https://github.com/xataio/client-ts/commit/be4eda8f73037d97fef7de28b56d7471dd867875) Thanks [@SferaDev](https://github.com/SferaDev)! - Fix cache ttl with 0 value
|
32
|
+
|
33
|
+
* [#265](https://github.com/xataio/client-ts/pull/265) [`99be734`](https://github.com/xataio/client-ts/commit/99be734827576d888aa12a579ed1983a0a8a8e83) Thanks [@SferaDev](https://github.com/SferaDev)! - Add datetime field type support
|
34
|
+
|
3
35
|
## 0.9.1
|
4
36
|
|
5
37
|
### Patch Changes
|
package/dist/index.cjs
CHANGED
@@ -11,8 +11,11 @@ function compact(arr) {
|
|
11
11
|
function isObject(value) {
|
12
12
|
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
13
13
|
}
|
14
|
+
function isDefined(value) {
|
15
|
+
return value !== null && value !== void 0;
|
16
|
+
}
|
14
17
|
function isString(value) {
|
15
|
-
return value
|
18
|
+
return isDefined(value) && typeof value === "string";
|
16
19
|
}
|
17
20
|
function toBase64(value) {
|
18
21
|
try {
|
@@ -74,7 +77,12 @@ function getFetchImplementation(userFetch) {
|
|
74
77
|
return fetchImpl;
|
75
78
|
}
|
76
79
|
|
77
|
-
class
|
80
|
+
class ErrorWithCause extends Error {
|
81
|
+
constructor(message, options) {
|
82
|
+
super(message, options);
|
83
|
+
}
|
84
|
+
}
|
85
|
+
class FetcherError extends ErrorWithCause {
|
78
86
|
constructor(status, data) {
|
79
87
|
super(getMessage(data));
|
80
88
|
this.status = status;
|
@@ -370,6 +378,11 @@ const queryTable = (variables) => fetch$1({
|
|
370
378
|
method: "post",
|
371
379
|
...variables
|
372
380
|
});
|
381
|
+
const searchTable = (variables) => fetch$1({
|
382
|
+
url: "/db/{dbBranchName}/tables/{tableName}/search",
|
383
|
+
method: "post",
|
384
|
+
...variables
|
385
|
+
});
|
373
386
|
const searchBranch = (variables) => fetch$1({
|
374
387
|
url: "/db/{dbBranchName}/search",
|
375
388
|
method: "post",
|
@@ -433,6 +446,7 @@ const operationsByTag = {
|
|
433
446
|
getRecord,
|
434
447
|
bulkInsertTableRecords,
|
435
448
|
queryTable,
|
449
|
+
searchTable,
|
436
450
|
searchBranch
|
437
451
|
}
|
438
452
|
};
|
@@ -703,10 +717,10 @@ class BranchApi {
|
|
703
717
|
...this.extraProps
|
704
718
|
});
|
705
719
|
}
|
706
|
-
createBranch(workspace, database, branch, from
|
720
|
+
createBranch(workspace, database, branch, from, options = {}) {
|
707
721
|
return operationsByTag.branch.createBranch({
|
708
722
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
709
|
-
queryParams: { from },
|
723
|
+
queryParams: isString(from) ? { from } : void 0,
|
710
724
|
body: options,
|
711
725
|
...this.extraProps
|
712
726
|
});
|
@@ -888,6 +902,13 @@ class RecordsApi {
|
|
888
902
|
...this.extraProps
|
889
903
|
});
|
890
904
|
}
|
905
|
+
searchTable(workspace, database, branch, tableName, query) {
|
906
|
+
return operationsByTag.records.searchTable({
|
907
|
+
pathParams: { workspace, dbBranchName: `${database}:${branch}`, tableName },
|
908
|
+
body: query,
|
909
|
+
...this.extraProps
|
910
|
+
});
|
911
|
+
}
|
891
912
|
searchBranch(workspace, database, branch, query) {
|
892
913
|
return operationsByTag.records.searchBranch({
|
893
914
|
pathParams: { workspace, dbBranchName: `${database}:${branch}` },
|
@@ -954,6 +975,9 @@ const PAGINATION_MAX_SIZE = 200;
|
|
954
975
|
const PAGINATION_DEFAULT_SIZE = 200;
|
955
976
|
const PAGINATION_MAX_OFFSET = 800;
|
956
977
|
const PAGINATION_DEFAULT_OFFSET = 0;
|
978
|
+
function isCursorPaginationOptions(options) {
|
979
|
+
return isDefined(options) && (isDefined(options.first) || isDefined(options.last) || isDefined(options.after) || isDefined(options.before));
|
980
|
+
}
|
957
981
|
|
958
982
|
var __accessCheck$5 = (obj, member, msg) => {
|
959
983
|
if (!member.has(obj))
|
@@ -975,7 +999,7 @@ var __privateSet$4 = (obj, member, value, setter) => {
|
|
975
999
|
};
|
976
1000
|
var _table$1, _repository, _data;
|
977
1001
|
const _Query = class {
|
978
|
-
constructor(repository, table, data,
|
1002
|
+
constructor(repository, table, data, rawParent) {
|
979
1003
|
__privateAdd$5(this, _table$1, void 0);
|
980
1004
|
__privateAdd$5(this, _repository, void 0);
|
981
1005
|
__privateAdd$5(this, _data, { filter: {} });
|
@@ -987,6 +1011,7 @@ const _Query = class {
|
|
987
1011
|
} else {
|
988
1012
|
__privateSet$4(this, _repository, this);
|
989
1013
|
}
|
1014
|
+
const parent = cleanParent(data, rawParent);
|
990
1015
|
__privateGet$5(this, _data).filter = data.filter ?? parent?.filter ?? {};
|
991
1016
|
__privateGet$5(this, _data).filter.$any = data.filter?.$any ?? parent?.filter?.$any;
|
992
1017
|
__privateGet$5(this, _data).filter.$all = data.filter?.$all ?? parent?.filter?.$all;
|
@@ -1052,34 +1077,36 @@ const _Query = class {
|
|
1052
1077
|
return __privateGet$5(this, _repository).query(query);
|
1053
1078
|
}
|
1054
1079
|
async *[Symbol.asyncIterator]() {
|
1055
|
-
for await (const [record] of this.getIterator(1)) {
|
1080
|
+
for await (const [record] of this.getIterator({ batchSize: 1 })) {
|
1056
1081
|
yield record;
|
1057
1082
|
}
|
1058
1083
|
}
|
1059
|
-
async *getIterator(
|
1060
|
-
|
1061
|
-
let
|
1062
|
-
|
1063
|
-
|
1064
|
-
|
1065
|
-
|
1066
|
-
|
1084
|
+
async *getIterator(options = {}) {
|
1085
|
+
const { batchSize = 1 } = options;
|
1086
|
+
let page = await this.getPaginated({ ...options, pagination: { size: batchSize, offset: 0 } });
|
1087
|
+
let more = page.hasNextPage();
|
1088
|
+
yield page.records;
|
1089
|
+
while (more) {
|
1090
|
+
page = await page.nextPage();
|
1091
|
+
more = page.hasNextPage();
|
1092
|
+
yield page.records;
|
1067
1093
|
}
|
1068
1094
|
}
|
1069
1095
|
async getMany(options = {}) {
|
1070
1096
|
const { records } = await this.getPaginated(options);
|
1071
1097
|
return records;
|
1072
1098
|
}
|
1073
|
-
async getAll(
|
1099
|
+
async getAll(options = {}) {
|
1100
|
+
const { batchSize = PAGINATION_MAX_SIZE, ...rest } = options;
|
1074
1101
|
const results = [];
|
1075
|
-
for await (const page of this.getIterator(
|
1102
|
+
for await (const page of this.getIterator({ ...rest, batchSize })) {
|
1076
1103
|
results.push(...page);
|
1077
1104
|
}
|
1078
1105
|
return results;
|
1079
1106
|
}
|
1080
1107
|
async getFirst(options = {}) {
|
1081
1108
|
const records = await this.getMany({ ...options, pagination: { size: 1 } });
|
1082
|
-
return records[0]
|
1109
|
+
return records[0] ?? null;
|
1083
1110
|
}
|
1084
1111
|
cache(ttl) {
|
1085
1112
|
return new _Query(__privateGet$5(this, _repository), __privateGet$5(this, _table$1), { cache: ttl }, __privateGet$5(this, _data));
|
@@ -1104,6 +1131,12 @@ let Query = _Query;
|
|
1104
1131
|
_table$1 = new WeakMap();
|
1105
1132
|
_repository = new WeakMap();
|
1106
1133
|
_data = new WeakMap();
|
1134
|
+
function cleanParent(data, parent) {
|
1135
|
+
if (isCursorPaginationOptions(data.pagination)) {
|
1136
|
+
return { ...parent, sorting: void 0, filter: void 0 };
|
1137
|
+
}
|
1138
|
+
return parent;
|
1139
|
+
}
|
1107
1140
|
|
1108
1141
|
function isIdentifiable(x) {
|
1109
1142
|
return isObject(x) && isString(x?.id);
|
@@ -1294,9 +1327,13 @@ class RestRepository extends Query {
|
|
1294
1327
|
}
|
1295
1328
|
async search(query, options = {}) {
|
1296
1329
|
const fetchProps = await __privateGet$4(this, _getFetchProps).call(this);
|
1297
|
-
const { records } = await
|
1298
|
-
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}" },
|
1299
|
-
body: {
|
1330
|
+
const { records } = await searchTable({
|
1331
|
+
pathParams: { workspace: "{workspaceId}", dbBranchName: "{dbBranch}", tableName: __privateGet$4(this, _table) },
|
1332
|
+
body: {
|
1333
|
+
query,
|
1334
|
+
fuzziness: options.fuzziness,
|
1335
|
+
filter: options.filter
|
1336
|
+
},
|
1300
1337
|
...fetchProps
|
1301
1338
|
});
|
1302
1339
|
const schema = await __privateMethod$2(this, _getSchema$1, getSchema_fn$1).call(this);
|
@@ -1309,7 +1346,7 @@ class RestRepository extends Query {
|
|
1309
1346
|
const data = query.getQueryOptions();
|
1310
1347
|
const body = {
|
1311
1348
|
filter: Object.values(data.filter ?? {}).some(Boolean) ? data.filter : void 0,
|
1312
|
-
sort: data.sort ? buildSortFilter(data.sort) : void 0,
|
1349
|
+
sort: data.sort !== void 0 ? buildSortFilter(data.sort) : void 0,
|
1313
1350
|
page: data.pagination,
|
1314
1351
|
columns: data.columns
|
1315
1352
|
};
|
@@ -1453,8 +1490,8 @@ getCacheQuery_fn = async function(query) {
|
|
1453
1490
|
if (!result)
|
1454
1491
|
return null;
|
1455
1492
|
const { cache: ttl = __privateGet$4(this, _cache).defaultQueryTTL } = query.getQueryOptions();
|
1456
|
-
if (
|
1457
|
-
return
|
1493
|
+
if (ttl < 0)
|
1494
|
+
return null;
|
1458
1495
|
const hasExpired = result.date.getTime() + ttl < Date.now();
|
1459
1496
|
return hasExpired ? null : result;
|
1460
1497
|
};
|
@@ -1499,7 +1536,7 @@ const initObject = (db, schema, table, object) => {
|
|
1499
1536
|
const linkTable = column.link?.table;
|
1500
1537
|
if (!linkTable) {
|
1501
1538
|
console.error(`Failed to parse link for field ${column.name}`);
|
1502
|
-
} else if (
|
1539
|
+
} else if (isObject(value)) {
|
1503
1540
|
result[column.name] = initObject(db, schema, linkTable, value);
|
1504
1541
|
}
|
1505
1542
|
break;
|
@@ -1625,7 +1662,7 @@ class SchemaPlugin extends XataPlugin {
|
|
1625
1662
|
get: (_target, table) => {
|
1626
1663
|
if (!isString(table))
|
1627
1664
|
throw new Error("Invalid table name");
|
1628
|
-
if (
|
1665
|
+
if (__privateGet$2(this, _tables)[table] === void 0) {
|
1629
1666
|
__privateGet$2(this, _tables)[table] = new RestRepository({ db, pluginOptions, table });
|
1630
1667
|
}
|
1631
1668
|
return __privateGet$2(this, _tables)[table];
|
@@ -1728,30 +1765,39 @@ const envBranchNames = [
|
|
1728
1765
|
"CF_PAGES_BRANCH",
|
1729
1766
|
"BRANCH"
|
1730
1767
|
];
|
1731
|
-
const defaultBranch = "main";
|
1732
1768
|
async function getCurrentBranchName(options) {
|
1733
|
-
const env =
|
1734
|
-
if (env)
|
1735
|
-
|
1736
|
-
|
1737
|
-
|
1738
|
-
|
1739
|
-
|
1740
|
-
|
1741
|
-
|
1742
|
-
return defaultBranch;
|
1769
|
+
const env = getBranchByEnvVariable();
|
1770
|
+
if (env) {
|
1771
|
+
const details = await getDatabaseBranch(env, options);
|
1772
|
+
if (details)
|
1773
|
+
return env;
|
1774
|
+
console.warn(`Branch ${env} not found in Xata. Ignoring...`);
|
1775
|
+
}
|
1776
|
+
const gitBranch = await getGitBranch();
|
1777
|
+
return resolveXataBranch(gitBranch, options);
|
1743
1778
|
}
|
1744
1779
|
async function getCurrentBranchDetails(options) {
|
1745
|
-
const
|
1746
|
-
|
1747
|
-
|
1748
|
-
|
1749
|
-
|
1750
|
-
|
1751
|
-
|
1752
|
-
|
1753
|
-
|
1754
|
-
|
1780
|
+
const branch = await getCurrentBranchName(options);
|
1781
|
+
return getDatabaseBranch(branch, options);
|
1782
|
+
}
|
1783
|
+
async function resolveXataBranch(gitBranch, options) {
|
1784
|
+
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1785
|
+
const apiKey = options?.apiKey || getAPIKey();
|
1786
|
+
if (!databaseURL)
|
1787
|
+
throw new Error("A databaseURL was not defined. Either set the XATA_DATABASE_URL env variable or pass the argument explicitely");
|
1788
|
+
if (!apiKey)
|
1789
|
+
throw new Error("An API key was not defined. Either set the XATA_API_KEY env variable or pass the argument explicitely");
|
1790
|
+
const [protocol, , host, , dbName] = databaseURL.split("/");
|
1791
|
+
const [workspace] = host.split(".");
|
1792
|
+
const { branch } = await resolveBranch({
|
1793
|
+
apiKey,
|
1794
|
+
apiUrl: databaseURL,
|
1795
|
+
fetchImpl: getFetchImplementation(options?.fetchImpl),
|
1796
|
+
workspacesApiUrl: `${protocol}//${host}`,
|
1797
|
+
pathParams: { dbName, workspace },
|
1798
|
+
queryParams: { gitBranch, fallbackBranch: getEnvVariable("XATA_FALLBACK_BRANCH") }
|
1799
|
+
});
|
1800
|
+
return branch;
|
1755
1801
|
}
|
1756
1802
|
async function getDatabaseBranch(branch, options) {
|
1757
1803
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
@@ -1840,7 +1886,7 @@ const buildClient = (plugins) => {
|
|
1840
1886
|
this.db = db;
|
1841
1887
|
this.search = search;
|
1842
1888
|
for (const [key, namespace] of Object.entries(plugins ?? {})) {
|
1843
|
-
if (
|
1889
|
+
if (namespace === void 0)
|
1844
1890
|
continue;
|
1845
1891
|
const result = namespace.build(pluginOptions);
|
1846
1892
|
if (result instanceof Promise) {
|
@@ -1857,7 +1903,7 @@ const buildClient = (plugins) => {
|
|
1857
1903
|
const databaseURL = options?.databaseURL || getDatabaseURL();
|
1858
1904
|
const apiKey = options?.apiKey || getAPIKey();
|
1859
1905
|
const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
|
1860
|
-
const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
1906
|
+
const branch = async () => options?.branch !== void 0 ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
|
1861
1907
|
if (!databaseURL || !apiKey) {
|
1862
1908
|
throw new Error("Options databaseURL and apiKey are required");
|
1863
1909
|
}
|
@@ -1884,7 +1930,7 @@ const buildClient = (plugins) => {
|
|
1884
1930
|
}, _evaluateBranch = new WeakSet(), evaluateBranch_fn = async function(param) {
|
1885
1931
|
if (__privateGet(this, _branch))
|
1886
1932
|
return __privateGet(this, _branch);
|
1887
|
-
if (
|
1933
|
+
if (param === void 0)
|
1888
1934
|
return void 0;
|
1889
1935
|
const strategies = Array.isArray(param) ? [...param] : [param];
|
1890
1936
|
const evaluateBranch = async (strategy) => {
|
@@ -1981,6 +2027,7 @@ exports.insertRecord = insertRecord;
|
|
1981
2027
|
exports.insertRecordWithID = insertRecordWithID;
|
1982
2028
|
exports.inviteWorkspaceMember = inviteWorkspaceMember;
|
1983
2029
|
exports.is = is;
|
2030
|
+
exports.isCursorPaginationOptions = isCursorPaginationOptions;
|
1984
2031
|
exports.isIdentifiable = isIdentifiable;
|
1985
2032
|
exports.isNot = isNot;
|
1986
2033
|
exports.isXataRecord = isXataRecord;
|
@@ -1996,6 +2043,7 @@ exports.removeWorkspaceMember = removeWorkspaceMember;
|
|
1996
2043
|
exports.resendWorkspaceMemberInvite = resendWorkspaceMemberInvite;
|
1997
2044
|
exports.resolveBranch = resolveBranch;
|
1998
2045
|
exports.searchBranch = searchBranch;
|
2046
|
+
exports.searchTable = searchTable;
|
1999
2047
|
exports.setTableSchema = setTableSchema;
|
2000
2048
|
exports.startsWith = startsWith;
|
2001
2049
|
exports.updateBranchMetadata = updateBranchMetadata;
|