@xata.io/client 0.0.0-alpha.vf6d8daa → 0.0.0-alpha.vf6f2567

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.cjs CHANGED
@@ -949,7 +949,7 @@ const _Query = class {
949
949
  __privateGet$4(this, _data).sort = data.sort ?? parent?.sort;
950
950
  __privateGet$4(this, _data).columns = data.columns ?? parent?.columns ?? ["*"];
951
951
  __privateGet$4(this, _data).page = data.page ?? parent?.page;
952
- __privateGet$4(this, _data).ttl = data.ttl ?? parent?.ttl;
952
+ __privateGet$4(this, _data).cache = data.cache ?? parent?.cache;
953
953
  this.any = this.any.bind(this);
954
954
  this.all = this.all.bind(this);
955
955
  this.not = this.not.bind(this);
@@ -1035,6 +1035,9 @@ const _Query = class {
1035
1035
  const records = await this.getMany({ ...options, page: { size: 1 } });
1036
1036
  return records[0] || null;
1037
1037
  }
1038
+ cache(ttl) {
1039
+ return new _Query(__privateGet$4(this, _repository), __privateGet$4(this, _table$1), { cache: ttl }, __privateGet$4(this, _data));
1040
+ }
1038
1041
  nextPage(size, offset) {
1039
1042
  return this.firstPage(size, offset);
1040
1043
  }
@@ -1108,7 +1111,7 @@ var __privateMethod$2 = (obj, member, method) => {
1108
1111
  __accessCheck$4(obj, member, "access private method");
1109
1112
  return method;
1110
1113
  };
1111
- var _table, _links, _getFetchProps, _insertRecordWithoutId, insertRecordWithoutId_fn, _insertRecordWithId, insertRecordWithId_fn, _bulkInsertTableRecords, bulkInsertTableRecords_fn, _updateRecordWithID, updateRecordWithID_fn, _upsertRecordWithID, upsertRecordWithID_fn, _deleteRecord, deleteRecord_fn, _invalidateCache, invalidateCache_fn, _setCacheRecord, setCacheRecord_fn, _getCacheRecord, getCacheRecord_fn, _setCacheQuery, setCacheQuery_fn, _getCacheQuery, getCacheQuery_fn;
1114
+ var _table, _links, _getFetchProps, _cache, _insertRecordWithoutId, insertRecordWithoutId_fn, _insertRecordWithId, insertRecordWithId_fn, _bulkInsertTableRecords, bulkInsertTableRecords_fn, _updateRecordWithID, updateRecordWithID_fn, _upsertRecordWithID, upsertRecordWithID_fn, _deleteRecord, deleteRecord_fn, _invalidateCache, invalidateCache_fn, _setCacheRecord, setCacheRecord_fn, _getCacheRecord, getCacheRecord_fn, _setCacheQuery, setCacheQuery_fn, _getCacheQuery, getCacheQuery_fn;
1112
1115
  class Repository extends Query {
1113
1116
  }
1114
1117
  class RestRepository extends Query {
@@ -1128,11 +1131,12 @@ class RestRepository extends Query {
1128
1131
  __privateAdd$4(this, _table, void 0);
1129
1132
  __privateAdd$4(this, _links, void 0);
1130
1133
  __privateAdd$4(this, _getFetchProps, void 0);
1134
+ __privateAdd$4(this, _cache, void 0);
1131
1135
  __privateSet$2(this, _table, options.table);
1132
1136
  __privateSet$2(this, _links, options.links ?? {});
1133
1137
  __privateSet$2(this, _getFetchProps, options.pluginOptions.getFetchProps);
1134
1138
  this.db = options.db;
1135
- this.cache = options.pluginOptions.cache;
1139
+ __privateSet$2(this, _cache, options.pluginOptions.cache);
1136
1140
  }
1137
1141
  async create(a, b) {
1138
1142
  if (Array.isArray(a)) {
@@ -1275,6 +1279,7 @@ class RestRepository extends Query {
1275
1279
  _table = new WeakMap();
1276
1280
  _links = new WeakMap();
1277
1281
  _getFetchProps = new WeakMap();
1282
+ _cache = new WeakMap();
1278
1283
  _insertRecordWithoutId = new WeakSet();
1279
1284
  insertRecordWithoutId_fn = async function(object) {
1280
1285
  const fetchProps = await __privateGet$3(this, _getFetchProps).call(this);
@@ -1367,34 +1372,38 @@ deleteRecord_fn = async function(recordId) {
1367
1372
  };
1368
1373
  _invalidateCache = new WeakSet();
1369
1374
  invalidateCache_fn = async function(recordId) {
1370
- await this.cache.delete(`rec_${__privateGet$3(this, _table)}:${recordId}`);
1371
- const cacheItems = await this.cache.getAll();
1375
+ await __privateGet$3(this, _cache).delete(`rec_${__privateGet$3(this, _table)}:${recordId}`);
1376
+ const cacheItems = await __privateGet$3(this, _cache).getAll();
1372
1377
  const queries = Object.entries(cacheItems).filter(([key]) => key.startsWith("query_"));
1373
1378
  for (const [key, value] of queries) {
1374
1379
  const ids = getIds(value);
1375
1380
  if (ids.includes(recordId))
1376
- await this.cache.delete(key);
1381
+ await __privateGet$3(this, _cache).delete(key);
1377
1382
  }
1378
1383
  };
1379
1384
  _setCacheRecord = new WeakSet();
1380
1385
  setCacheRecord_fn = async function(record) {
1381
- await this.cache.set(`rec_${__privateGet$3(this, _table)}:${record.id}`, record);
1386
+ if (!__privateGet$3(this, _cache).cacheRecords)
1387
+ return;
1388
+ await __privateGet$3(this, _cache).set(`rec_${__privateGet$3(this, _table)}:${record.id}`, record);
1382
1389
  };
1383
1390
  _getCacheRecord = new WeakSet();
1384
1391
  getCacheRecord_fn = async function(recordId) {
1385
- return this.cache.get(`rec_${__privateGet$3(this, _table)}:${recordId}`);
1392
+ if (!__privateGet$3(this, _cache).cacheRecords)
1393
+ return null;
1394
+ return __privateGet$3(this, _cache).get(`rec_${__privateGet$3(this, _table)}:${recordId}`);
1386
1395
  };
1387
1396
  _setCacheQuery = new WeakSet();
1388
1397
  setCacheQuery_fn = async function(query, meta, records) {
1389
- await this.cache.set(`query_${__privateGet$3(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
1398
+ await __privateGet$3(this, _cache).set(`query_${__privateGet$3(this, _table)}:${query.key()}`, { date: new Date(), meta, records });
1390
1399
  };
1391
1400
  _getCacheQuery = new WeakSet();
1392
1401
  getCacheQuery_fn = async function(query) {
1393
1402
  const key = `query_${__privateGet$3(this, _table)}:${query.key()}`;
1394
- const result = await this.cache.get(key);
1403
+ const result = await __privateGet$3(this, _cache).get(key);
1395
1404
  if (!result)
1396
1405
  return null;
1397
- const { ttl = 0 } = query.getQueryOptions();
1406
+ const { cache: ttl = __privateGet$3(this, _cache).defaultQueryTTL } = query.getQueryOptions();
1398
1407
  if (!ttl || ttl < 0)
1399
1408
  return result;
1400
1409
  const hasExpired = result.date.getTime() + ttl < Date.now();
@@ -1463,9 +1472,12 @@ var __privateSet$1 = (obj, member, value, setter) => {
1463
1472
  };
1464
1473
  var _map;
1465
1474
  class SimpleCache {
1466
- constructor() {
1475
+ constructor(options = {}) {
1467
1476
  __privateAdd$3(this, _map, void 0);
1468
1477
  __privateSet$1(this, _map, /* @__PURE__ */ new Map());
1478
+ this.capacity = options.max ?? 500;
1479
+ this.cacheRecords = options.cacheRecords ?? true;
1480
+ this.defaultQueryTTL = options.defaultQueryTTL ?? 60 * 1e3;
1469
1481
  }
1470
1482
  async getAll() {
1471
1483
  return Object.fromEntries(__privateGet$2(this, _map));
@@ -1474,7 +1486,12 @@ class SimpleCache {
1474
1486
  return __privateGet$2(this, _map).get(key) ?? null;
1475
1487
  }
1476
1488
  async set(key, value) {
1489
+ await this.delete(key);
1477
1490
  __privateGet$2(this, _map).set(key, value);
1491
+ if (__privateGet$2(this, _map).size > this.capacity) {
1492
+ const leastRecentlyUsed = __privateGet$2(this, _map).keys().next().value;
1493
+ await this.delete(leastRecentlyUsed);
1494
+ }
1478
1495
  }
1479
1496
  async delete(key) {
1480
1497
  __privateGet$2(this, _map).delete(key);
@@ -1737,7 +1754,7 @@ const buildClient = (plugins) => {
1737
1754
  const fetch = getFetchImplementation(options?.fetch);
1738
1755
  const databaseURL = options?.databaseURL || getDatabaseURL();
1739
1756
  const apiKey = options?.apiKey || getAPIKey();
1740
- const cache = options?.cache ?? new SimpleCache();
1757
+ const cache = options?.cache ?? new SimpleCache({ cacheRecords: false, defaultQueryTTL: 0 });
1741
1758
  const branch = async () => options?.branch ? await __privateMethod(this, _evaluateBranch, evaluateBranch_fn).call(this, options.branch) : await getCurrentBranchName({ apiKey, databaseURL, fetchImpl: options?.fetch });
1742
1759
  if (!databaseURL || !apiKey) {
1743
1760
  throw new Error("Options databaseURL and apiKey are required");