@xata.io/client 0.0.0-beta.9d62fb3 → 0.0.0-beta.bdce130

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.js CHANGED
@@ -1,4 +1,14 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
2
12
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
13
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
14
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -8,6 +18,17 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
18
  step((generator = generator.apply(thisArg, _arguments || [])).next());
9
19
  });
10
20
  };
21
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
22
+ if (kind === "m") throw new TypeError("Private method is not writable");
23
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
24
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
25
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
26
+ };
27
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
28
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
29
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
30
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
31
+ };
11
32
  var __asyncValues = (this && this.__asyncValues) || function (o) {
12
33
  if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
13
34
  var m = o[Symbol.asyncIterator], i;
@@ -15,185 +36,36 @@ var __asyncValues = (this && this.__asyncValues) || function (o) {
15
36
  function verb(n) { i[n] = o[n] && function (v) { return new Promise(function (resolve, reject) { v = o[n](v), settle(resolve, reject, v.done, v.value); }); }; }
16
37
  function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
17
38
  };
39
+ var _RestRepository_client, _RestRepository_fetch, _RestRepository_table;
18
40
  Object.defineProperty(exports, "__esModule", { value: true });
19
- exports.XataError = exports.BaseClient = exports.RestRespositoryFactory = exports.RestRepository = exports.Repository = exports.Query = exports.includesAll = exports.includesPattern = exports.includesSubstring = exports.includes = exports.contains = exports.isNot = exports.is = exports.pattern = exports.endsWith = exports.startsWith = exports.notExists = exports.exists = exports.le = exports.lte = exports.lt = exports.gte = exports.ge = exports.gt = void 0;
20
- const gt = (value) => ({ $gt: value });
21
- exports.gt = gt;
22
- const ge = (value) => ({ $ge: value });
23
- exports.ge = ge;
24
- const gte = (value) => ({ $ge: value });
25
- exports.gte = gte;
26
- const lt = (value) => ({ $lt: value });
27
- exports.lt = lt;
28
- const lte = (value) => ({ $le: value });
29
- exports.lte = lte;
30
- const le = (value) => ({ $le: value });
31
- exports.le = le;
32
- const exists = (column) => ({ $exists: column });
33
- exports.exists = exists;
34
- const notExists = (column) => ({ $notExists: column });
35
- exports.notExists = notExists;
36
- const startsWith = (value) => ({ $startsWith: value });
37
- exports.startsWith = startsWith;
38
- const endsWith = (value) => ({ $endsWith: value });
39
- exports.endsWith = endsWith;
40
- const pattern = (value) => ({ $pattern: value });
41
- exports.pattern = pattern;
42
- const is = (value) => ({ $is: value });
43
- exports.is = is;
44
- const isNot = (value) => ({ $isNot: value });
45
- exports.isNot = isNot;
46
- const contains = (value) => ({ $contains: value });
47
- exports.contains = contains;
48
- // TODO: these can only be applied to columns of type "multiple"
49
- const includes = (value) => ({ $includes: value });
50
- exports.includes = includes;
51
- const includesSubstring = (value) => ({ $includesSubstring: value });
52
- exports.includesSubstring = includesSubstring;
53
- const includesPattern = (value) => ({ $includesPattern: value });
54
- exports.includesPattern = includesPattern;
55
- const includesAll = (value) => ({ $includesAll: value });
56
- exports.includesAll = includesAll;
57
- class Query {
58
- constructor(repository, table, data, parent) {
59
- if (repository) {
60
- this.repository = repository;
61
- }
62
- else {
63
- this.repository = this;
64
- }
65
- this.table = table;
66
- // For some reason Object.assign(this, parent) didn't work in this case
67
- // so doing all this manually:
68
- this.$any = parent === null || parent === void 0 ? void 0 : parent.$any;
69
- this.$all = parent === null || parent === void 0 ? void 0 : parent.$all;
70
- this.$not = parent === null || parent === void 0 ? void 0 : parent.$not;
71
- this.$none = parent === null || parent === void 0 ? void 0 : parent.$none;
72
- this.$sort = parent === null || parent === void 0 ? void 0 : parent.$sort;
73
- Object.assign(this, data);
74
- // These bindings are used to support deconstructing
75
- // const { any, not, filter, sort } = xata.users.query()
76
- this.any = this.any.bind(this);
77
- this.all = this.all.bind(this);
78
- this.not = this.not.bind(this);
79
- this.filter = this.filter.bind(this);
80
- this.sort = this.sort.bind(this);
81
- this.none = this.none.bind(this);
82
- Object.defineProperty(this, 'table', { enumerable: false });
83
- Object.defineProperty(this, 'repository', { enumerable: false });
84
- }
85
- any(...queries) {
86
- return new Query(this.repository, this.table, {
87
- $any: (this.$any || []).concat(queries)
88
- }, this);
89
- }
90
- all(...queries) {
91
- return new Query(this.repository, this.table, {
92
- $all: (this.$all || []).concat(queries)
93
- }, this);
94
- }
95
- not(...queries) {
96
- return new Query(this.repository, this.table, {
97
- $not: (this.$not || []).concat(queries)
98
- }, this);
99
- }
100
- none(...queries) {
101
- return new Query(this.repository, this.table, {
102
- $none: (this.$none || []).concat(queries)
103
- }, this);
104
- }
105
- filter(a, b) {
106
- if (arguments.length === 1) {
107
- const constraints = a;
108
- const queries = [];
109
- for (const [column, constraint] of Object.entries(constraints)) {
110
- queries.push({ [column]: constraint });
111
- }
112
- return new Query(this.repository, this.table, {
113
- $all: (this.$all || []).concat(queries)
114
- }, this);
115
- }
116
- else {
117
- const column = a;
118
- const value = b;
119
- return new Query(this.repository, this.table, {
120
- $all: (this.$all || []).concat({ [column]: value })
121
- }, this);
122
- }
123
- }
124
- sort(column, direction) {
125
- const sort = Object.assign(Object.assign({}, this.$sort), { [column]: direction });
126
- const q = new Query(this.repository, this.table, {
127
- $sort: sort
128
- }, this);
129
- return q;
130
- }
131
- // TODO: pagination. Maybe implement different methods for different type of paginations
132
- // and one to simply get the first records returned by the query with no pagination.
133
- getMany(options) {
134
- return __awaiter(this, void 0, void 0, function* () {
135
- // TODO: use options
136
- return this.repository.query(this);
137
- });
138
- }
139
- getOne(options) {
140
- return __awaiter(this, void 0, void 0, function* () {
141
- // TODO: use options
142
- const arr = yield this.getMany(); // TODO, limit to 1
143
- return arr[0] || null;
144
- });
145
- }
146
- deleteAll() {
147
- return __awaiter(this, void 0, void 0, function* () {
148
- // Return number of affected rows
149
- return 0;
150
- });
151
- }
152
- include(columns) {
153
- // TODO
154
- return this;
155
- }
156
- }
157
- exports.Query = Query;
158
- class Repository extends Query {
159
- select(...columns) {
160
- return new Query(this.repository, this.table, {});
161
- }
41
+ exports.XataError = exports.BaseClient = exports.RestRespositoryFactory = exports.RestRepository = exports.Repository = void 0;
42
+ const filters_1 = require("./schema/filters");
43
+ const pagination_1 = require("./schema/pagination");
44
+ const query_1 = require("./schema/query");
45
+ class Repository extends query_1.Query {
162
46
  }
163
47
  exports.Repository = Repository;
164
48
  class RestRepository extends Repository {
165
49
  constructor(client, table) {
166
50
  super(null, table, {});
167
- this.client = client;
168
- const { fetch } = client.options;
169
- if (fetch) {
170
- this.fetch = fetch;
171
- }
172
- else if (typeof window === 'object') {
173
- this.fetch = window.fetch;
51
+ _RestRepository_client.set(this, void 0);
52
+ _RestRepository_fetch.set(this, void 0);
53
+ _RestRepository_table.set(this, void 0);
54
+ __classPrivateFieldSet(this, _RestRepository_client, client, "f");
55
+ __classPrivateFieldSet(this, _RestRepository_table, table, "f");
56
+ // TODO: Remove when integrating with API client
57
+ const fetchImpl = typeof fetch !== 'undefined' ? fetch : __classPrivateFieldGet(this, _RestRepository_client, "f").options.fetch;
58
+ if (!fetchImpl) {
59
+ throw new Error(`The \`fetch\` option passed to the Xata client is resolving to a falsy value and may not be correctly imported.`);
174
60
  }
175
- else if (typeof require === 'function') {
176
- try {
177
- this.fetch = require('node-fetch');
178
- }
179
- catch (err) {
180
- try {
181
- this.fetch = require('cross-fetch');
182
- }
183
- catch (err) {
184
- throw new Error('No fetch implementation found. Please provide one in the constructor');
185
- }
186
- }
187
- }
188
- Object.defineProperty(this, 'client', { enumerable: false });
189
- Object.defineProperty(this, 'fetch', { enumerable: false });
190
- Object.defineProperty(this, 'hostname', { enumerable: false });
61
+ __classPrivateFieldSet(this, _RestRepository_fetch, fetchImpl, "f");
191
62
  }
192
63
  request(method, path, body) {
193
64
  return __awaiter(this, void 0, void 0, function* () {
194
- const { databaseURL, apiKey } = this.client.options;
195
- const branch = yield this.client.getBranch();
196
- const resp = yield this.fetch(`${databaseURL}:${branch}${path}`, {
65
+ const { databaseURL, apiKey } = __classPrivateFieldGet(this, _RestRepository_client, "f").options;
66
+ const branch = yield __classPrivateFieldGet(this, _RestRepository_client, "f").getBranch();
67
+ const fetchImpl = __classPrivateFieldGet(this, _RestRepository_fetch, "f");
68
+ const resp = yield fetchImpl(`${databaseURL}:${branch}${path}`, {
197
69
  method,
198
70
  headers: {
199
71
  Accept: '*/*',
@@ -219,31 +91,46 @@ class RestRepository extends Repository {
219
91
  throw new XataError(resp.statusText, resp.status);
220
92
  }
221
93
  if (resp.status === 204)
222
- return;
94
+ return undefined;
223
95
  return resp.json();
224
96
  });
225
97
  }
226
- select(...columns) {
227
- return new Query(this.repository, this.table, {});
228
- }
229
98
  create(object) {
230
99
  return __awaiter(this, void 0, void 0, function* () {
231
- const body = Object.assign({}, object);
232
- for (const key of Object.keys(body)) {
233
- const value = body[key];
234
- if (value && typeof value === 'object' && typeof value.id === 'string') {
235
- body[key] = value.id;
236
- }
100
+ const record = transformObjectLinks(object);
101
+ const response = yield this.request('POST', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/data`, record);
102
+ if (!response) {
103
+ throw new Error("The server didn't return any data for the query");
237
104
  }
238
- const obj = yield this.request('POST', `/tables/${this.table}/data`, body);
239
- return this.client.initObject(this.table, obj);
105
+ const finalObject = yield this.read(response.id);
106
+ if (!finalObject) {
107
+ throw new Error('The server failed to save the record');
108
+ }
109
+ return finalObject;
110
+ });
111
+ }
112
+ createMany(objects) {
113
+ return __awaiter(this, void 0, void 0, function* () {
114
+ const records = objects.map((object) => transformObjectLinks(object));
115
+ const response = yield this.request('POST', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/bulk`, { records });
116
+ if (!response) {
117
+ throw new Error("The server didn't return any data for the query");
118
+ }
119
+ // TODO: Use filer.$any() to get all the records
120
+ const finalObjects = yield Promise.all(response.recordIDs.map((id) => this.read(id)));
121
+ if (finalObjects.some((object) => !object)) {
122
+ throw new Error('The server failed to save the record');
123
+ }
124
+ return finalObjects;
240
125
  });
241
126
  }
242
127
  read(id) {
243
128
  return __awaiter(this, void 0, void 0, function* () {
244
129
  try {
245
- const obj = yield this.request('GET', `/tables/${this.table}/data/${id}`);
246
- return this.client.initObject(this.table, obj);
130
+ const response = yield this.request('GET', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/data/${id}`);
131
+ if (!response)
132
+ return null;
133
+ return __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), response);
247
134
  }
248
135
  catch (err) {
249
136
  if (err.status === 404)
@@ -254,33 +141,42 @@ class RestRepository extends Repository {
254
141
  }
255
142
  update(id, object) {
256
143
  return __awaiter(this, void 0, void 0, function* () {
257
- const obj = yield this.request('PUT', `/tables/${this.table}/data/${id}`, object);
258
- return this.client.initObject(this.table, obj);
144
+ const response = yield this.request('PUT', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/data/${id}`, object);
145
+ if (!response) {
146
+ throw new Error("The server didn't return any data for the query");
147
+ }
148
+ // TODO: Review this, not sure we are properly initializing the object
149
+ return __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), response);
259
150
  });
260
151
  }
261
152
  delete(id) {
262
153
  return __awaiter(this, void 0, void 0, function* () {
263
- yield this.request('DELETE', `/tables/${this.table}/data/${id}`);
154
+ yield this.request('DELETE', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/data/${id}`);
264
155
  });
265
156
  }
266
- query(query) {
157
+ query(query, options) {
158
+ var _a, _b, _c;
267
159
  return __awaiter(this, void 0, void 0, function* () {
268
- const filter = {
269
- $any: query.$any,
270
- $all: query.$all,
271
- $not: query.$not,
272
- $none: query.$none
273
- };
160
+ const data = query.getQueryOptions();
274
161
  const body = {
275
- filter: Object.values(filter).some(Boolean) ? filter : undefined,
276
- sort: query.$sort
162
+ filter: Object.values(data.filter).some(Boolean) ? data.filter : undefined,
163
+ sort: (_a = (0, filters_1.buildSortFilter)(options === null || options === void 0 ? void 0 : options.sort)) !== null && _a !== void 0 ? _a : data.sort,
164
+ page: (_b = options === null || options === void 0 ? void 0 : options.page) !== null && _b !== void 0 ? _b : data.page,
165
+ columns: (_c = options === null || options === void 0 ? void 0 : options.columns) !== null && _c !== void 0 ? _c : data.columns
277
166
  };
278
- const result = yield this.request('POST', `/tables/${this.table}/query`, body);
279
- return result.records.map((record) => this.client.initObject(this.table, record));
167
+ const response = yield this.request('POST', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/query`, body);
168
+ if (!response) {
169
+ throw new Error("The server didn't return any data for the query");
170
+ }
171
+ const { meta, records: objects } = response;
172
+ const records = objects.map((record) => __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), record));
173
+ // TODO: We should properly type this any
174
+ return new pagination_1.Page(query, meta, records);
280
175
  });
281
176
  }
282
177
  }
283
178
  exports.RestRepository = RestRepository;
179
+ _RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _RestRepository_table = new WeakMap();
284
180
  class RestRespositoryFactory {
285
181
  createRepository(client, table) {
286
182
  return new RestRepository(client, table);
@@ -376,3 +272,14 @@ exports.XataError = XataError;
376
272
  const isBranchStrategyBuilder = (strategy) => {
377
273
  return typeof strategy === 'function';
378
274
  };
275
+ // TODO: We can find a better implementation for links
276
+ const transformObjectLinks = (object) => {
277
+ return Object.entries(object).reduce((acc, [key, value]) => {
278
+ if (value && typeof value === 'object' && typeof value.id === 'string') {
279
+ return Object.assign(Object.assign({}, acc), { [key]: value.id });
280
+ }
281
+ return Object.assign(Object.assign({}, acc), { [key]: value });
282
+ }, {});
283
+ };
284
+ __exportStar(require("./api"), exports);
285
+ __exportStar(require("./schema"), exports);
@@ -0,0 +1,20 @@
1
+ export declare type SortDirection = 'asc' | 'desc';
2
+ export declare type SortFilterExtended<T> = {
3
+ column: keyof T;
4
+ direction?: SortDirection;
5
+ };
6
+ export declare type SortFilter<T> = SortFilterExtended<T> | keyof T;
7
+ export declare function isSortFilterObject<T>(filter: SortFilter<T>): filter is SortFilterExtended<T>;
8
+ export declare type FilterOperator = '$gt' | '$lt' | '$ge' | '$le' | '$exists' | '$notExists' | '$endsWith' | '$startsWith' | '$pattern' | '$is' | '$isNot' | '$contains' | '$includes' | '$includesSubstring' | '$includesPattern' | '$includesAll';
9
+ export declare function buildSortFilter<T>(filter?: SortFilter<T> | SortFilter<T>[]): {
10
+ [key: string]: SortDirection;
11
+ } | undefined;
12
+ export declare type Constraint<T> = {
13
+ [key in FilterOperator]?: T;
14
+ };
15
+ export declare type DeepConstraint<T> = T extends Record<string, any> ? {
16
+ [key in keyof T]?: T[key] | DeepConstraint<T[key]>;
17
+ } : Constraint<T>;
18
+ export declare type FilterConstraints<T> = {
19
+ [key in keyof T]?: T[key] extends Record<string, any> ? FilterConstraints<T[key]> : T[key] | DeepConstraint<T[key]>;
20
+ };
@@ -0,0 +1,24 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.buildSortFilter = exports.isSortFilterObject = void 0;
4
+ function isSortFilterObject(filter) {
5
+ return typeof filter === 'object' && filter.column !== undefined;
6
+ }
7
+ exports.isSortFilterObject = isSortFilterObject;
8
+ function buildSortFilter(filter) {
9
+ if (!filter)
10
+ return undefined;
11
+ const filters = Array.isArray(filter) ? filter : [filter];
12
+ return filters.reduce((acc, item) => {
13
+ if (typeof item === 'string') {
14
+ return Object.assign(Object.assign({}, acc), { [item]: 'asc' });
15
+ }
16
+ else if (isSortFilterObject(item)) {
17
+ return Object.assign(Object.assign({}, acc), { [item.column]: item.direction });
18
+ }
19
+ else {
20
+ return acc;
21
+ }
22
+ }, {});
23
+ }
24
+ exports.buildSortFilter = buildSortFilter;
@@ -0,0 +1 @@
1
+ export * from './operators';
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
12
+ Object.defineProperty(exports, "__esModule", { value: true });
13
+ __exportStar(require("./operators"), exports);
@@ -0,0 +1,21 @@
1
+ import { Constraint } from './filters';
2
+ declare type ComparableType = number | Date;
3
+ export declare const gt: <T extends ComparableType>(value: T) => Constraint<T>;
4
+ export declare const ge: <T extends ComparableType>(value: T) => Constraint<T>;
5
+ export declare const gte: <T extends ComparableType>(value: T) => Constraint<T>;
6
+ export declare const lt: <T extends ComparableType>(value: T) => Constraint<T>;
7
+ export declare const lte: <T extends ComparableType>(value: T) => Constraint<T>;
8
+ export declare const le: <T extends ComparableType>(value: T) => Constraint<T>;
9
+ export declare const exists: (column: string) => Constraint<string>;
10
+ export declare const notExists: (column: string) => Constraint<string>;
11
+ export declare const startsWith: (value: string) => Constraint<string>;
12
+ export declare const endsWith: (value: string) => Constraint<string>;
13
+ export declare const pattern: (value: string) => Constraint<string>;
14
+ export declare const is: <T>(value: T) => Constraint<T>;
15
+ export declare const isNot: <T>(value: T) => Constraint<T>;
16
+ export declare const contains: <T>(value: T) => Constraint<T>;
17
+ export declare const includes: (value: string) => Constraint<string>;
18
+ export declare const includesSubstring: (value: string) => Constraint<string>;
19
+ export declare const includesPattern: (value: string) => Constraint<string>;
20
+ export declare const includesAll: (value: string) => Constraint<string>;
21
+ export {};
@@ -0,0 +1,40 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.includesAll = exports.includesPattern = exports.includesSubstring = exports.includes = exports.contains = exports.isNot = exports.is = exports.pattern = exports.endsWith = exports.startsWith = exports.notExists = exports.exists = exports.le = exports.lte = exports.lt = exports.gte = exports.ge = exports.gt = void 0;
4
+ const gt = (value) => ({ $gt: value });
5
+ exports.gt = gt;
6
+ const ge = (value) => ({ $ge: value });
7
+ exports.ge = ge;
8
+ const gte = (value) => ({ $ge: value });
9
+ exports.gte = gte;
10
+ const lt = (value) => ({ $lt: value });
11
+ exports.lt = lt;
12
+ const lte = (value) => ({ $le: value });
13
+ exports.lte = lte;
14
+ const le = (value) => ({ $le: value });
15
+ exports.le = le;
16
+ const exists = (column) => ({ $exists: column });
17
+ exports.exists = exists;
18
+ const notExists = (column) => ({ $notExists: column });
19
+ exports.notExists = notExists;
20
+ const startsWith = (value) => ({ $startsWith: value });
21
+ exports.startsWith = startsWith;
22
+ const endsWith = (value) => ({ $endsWith: value });
23
+ exports.endsWith = endsWith;
24
+ const pattern = (value) => ({ $pattern: value });
25
+ exports.pattern = pattern;
26
+ const is = (value) => ({ $is: value });
27
+ exports.is = is;
28
+ const isNot = (value) => ({ $isNot: value });
29
+ exports.isNot = isNot;
30
+ const contains = (value) => ({ $contains: value });
31
+ exports.contains = contains;
32
+ // TODO: these can only be applied to columns of type "multiple"
33
+ const includes = (value) => ({ $includes: value });
34
+ exports.includes = includes;
35
+ const includesSubstring = (value) => ({ $includesSubstring: value });
36
+ exports.includesSubstring = includesSubstring;
37
+ const includesPattern = (value) => ({ $includesPattern: value });
38
+ exports.includesPattern = includesPattern;
39
+ const includesAll = (value) => ({ $includesAll: value });
40
+ exports.includesAll = includesAll;
@@ -0,0 +1,41 @@
1
+ import { XataRecord } from '..';
2
+ import { Query } from './query';
3
+ export declare type PaginationQueryMeta = {
4
+ page: {
5
+ cursor: string;
6
+ more: boolean;
7
+ };
8
+ };
9
+ export interface Paginable<T extends XataRecord, R extends XataRecord = T> {
10
+ meta: PaginationQueryMeta;
11
+ records: R[];
12
+ nextPage(size?: number, offset?: number): Promise<Page<T, R>>;
13
+ previousPage(size?: number, offset?: number): Promise<Page<T, R>>;
14
+ firstPage(size?: number, offset?: number): Promise<Page<T, R>>;
15
+ lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
16
+ hasNextPage(): boolean;
17
+ }
18
+ export declare class Page<T extends XataRecord, R extends XataRecord> implements Paginable<T, R> {
19
+ #private;
20
+ readonly meta: PaginationQueryMeta;
21
+ readonly records: R[];
22
+ constructor(query: Query<T, R>, meta: PaginationQueryMeta, records?: R[]);
23
+ nextPage(size?: number, offset?: number): Promise<Page<T, R>>;
24
+ previousPage(size?: number, offset?: number): Promise<Page<T, R>>;
25
+ firstPage(size?: number, offset?: number): Promise<Page<T, R>>;
26
+ lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
27
+ hasNextPage(): boolean;
28
+ }
29
+ export declare type CursorNavigationOptions = {
30
+ first?: string;
31
+ } | {
32
+ last?: string;
33
+ } | {
34
+ after?: string;
35
+ before?: string;
36
+ };
37
+ export declare type OffsetNavigationOptions = {
38
+ size?: number;
39
+ offset?: number;
40
+ };
41
+ export declare type PaginationOptions = CursorNavigationOptions & OffsetNavigationOptions;
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
12
+ if (kind === "m") throw new TypeError("Private method is not writable");
13
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
14
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
15
+ return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
16
+ };
17
+ var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
18
+ if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
19
+ if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
20
+ return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
21
+ };
22
+ var _Page_query;
23
+ Object.defineProperty(exports, "__esModule", { value: true });
24
+ exports.Page = void 0;
25
+ class Page {
26
+ constructor(query, meta, records = []) {
27
+ _Page_query.set(this, void 0);
28
+ __classPrivateFieldSet(this, _Page_query, query, "f");
29
+ this.meta = meta;
30
+ this.records = records;
31
+ }
32
+ nextPage(size, offset) {
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ return __classPrivateFieldGet(this, _Page_query, "f").getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
35
+ });
36
+ }
37
+ previousPage(size, offset) {
38
+ return __awaiter(this, void 0, void 0, function* () {
39
+ return __classPrivateFieldGet(this, _Page_query, "f").getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
40
+ });
41
+ }
42
+ firstPage(size, offset) {
43
+ return __awaiter(this, void 0, void 0, function* () {
44
+ return __classPrivateFieldGet(this, _Page_query, "f").getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
45
+ });
46
+ }
47
+ lastPage(size, offset) {
48
+ return __awaiter(this, void 0, void 0, function* () {
49
+ return __classPrivateFieldGet(this, _Page_query, "f").getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
50
+ });
51
+ }
52
+ // TODO: We need to add something on the backend if we want a hasPreviousPage
53
+ hasNextPage() {
54
+ return this.meta.page.more;
55
+ }
56
+ }
57
+ exports.Page = Page;
58
+ _Page_query = new WeakMap();