@xata.io/client 0.0.0-beta.123dd7a → 0.0.0-beta.44b2150

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.d.ts CHANGED
@@ -1,3 +1,6 @@
1
+ import { Page } from './schema/pagination';
2
+ import { Query, QueryOptions } from './schema/query';
3
+ import { Selectable, SelectableColumn, Select } from './schema/selection';
1
4
  export interface XataRecord {
2
5
  id: string;
3
6
  xata: {
@@ -7,107 +10,30 @@ export interface XataRecord {
7
10
  update(data: Selectable<this>): Promise<this>;
8
11
  delete(): Promise<void>;
9
12
  }
10
- export declare type Queries<T> = {
11
- [key in keyof T as T[key] extends Query<infer A, infer B> ? key : never]: T[key];
12
- };
13
- export declare type OmitQueries<T> = {
14
- [key in keyof T as T[key] extends Query<infer A, infer B> ? never : key]: T[key];
15
- };
16
- export declare type OmitLinks<T> = {
17
- [key in keyof T as T[key] extends XataRecord ? never : key]: T[key];
18
- };
19
- export declare type OmitMethods<T> = {
20
- [key in keyof T as T[key] extends Function ? never : key]: T[key];
21
- };
22
- export declare type Selectable<T> = Omit<OmitQueries<OmitMethods<T>>, 'id' | 'xata'>;
23
- export declare type Select<T, K extends keyof T> = Pick<T, K> & Queries<T> & XataRecord;
24
- export declare type Include<T> = {
25
- [key in keyof T as T[key] extends XataRecord ? key : never]?: boolean | Array<keyof Selectable<T[key]>>;
26
- };
27
- declare type SortDirection = 'asc' | 'desc';
28
- declare type Operator = '$gt' | '$lt' | '$ge' | '$le' | '$exists' | '$notExists' | '$endsWith' | '$startsWith' | '$pattern' | '$is' | '$isNot' | '$contains' | '$includes' | '$includesSubstring' | '$includesPattern' | '$includesAll';
29
- declare type Constraint<T> = {
30
- [key in Operator]?: T;
31
- };
32
- declare type DeepConstraint<T> = T extends Record<string, any> ? {
33
- [key in keyof T]?: T[key] | DeepConstraint<T[key]>;
34
- } : Constraint<T>;
35
- declare type ComparableType = number | Date;
36
- export declare const gt: <T extends ComparableType>(value: T) => Constraint<T>;
37
- export declare const ge: <T extends ComparableType>(value: T) => Constraint<T>;
38
- export declare const gte: <T extends ComparableType>(value: T) => Constraint<T>;
39
- export declare const lt: <T extends ComparableType>(value: T) => Constraint<T>;
40
- export declare const lte: <T extends ComparableType>(value: T) => Constraint<T>;
41
- export declare const le: <T extends ComparableType>(value: T) => Constraint<T>;
42
- export declare const exists: (column: string) => Constraint<string>;
43
- export declare const notExists: (column: string) => Constraint<string>;
44
- export declare const startsWith: (value: string) => Constraint<string>;
45
- export declare const endsWith: (value: string) => Constraint<string>;
46
- export declare const pattern: (value: string) => Constraint<string>;
47
- export declare const is: <T>(value: T) => Constraint<T>;
48
- export declare const isNot: <T>(value: T) => Constraint<T>;
49
- export declare const contains: <T>(value: T) => Constraint<T>;
50
- export declare const includes: (value: string) => Constraint<string>;
51
- export declare const includesSubstring: (value: string) => Constraint<string>;
52
- export declare const includesPattern: (value: string) => Constraint<string>;
53
- export declare const includesAll: (value: string) => Constraint<string>;
54
- declare type FilterConstraints<T> = {
55
- [key in keyof T]?: T[key] extends Record<string, any> ? FilterConstraints<T[key]> : T[key] | DeepConstraint<T[key]>;
56
- };
57
- declare type BulkQueryOptions<T> = {
58
- filter?: FilterConstraints<T>;
59
- sort?: {
60
- column: keyof T;
61
- direction?: SortDirection;
62
- } | keyof T;
63
- };
64
- declare type QueryOrConstraint<T, R> = Query<T, R> | Constraint<T>;
65
- export declare class Query<T, R = T> {
66
- table: string;
67
- repository: Repository<T>;
68
- readonly $any?: QueryOrConstraint<T, R>[];
69
- readonly $all?: QueryOrConstraint<T, R>[];
70
- readonly $not?: QueryOrConstraint<T, R>[];
71
- readonly $none?: QueryOrConstraint<T, R>[];
72
- readonly $sort?: Record<string, SortDirection>;
73
- constructor(repository: Repository<T> | null, table: string, data: Partial<Query<T, R>>, parent?: Query<T, R>);
74
- any(...queries: Query<T, R>[]): Query<T, R>;
75
- all(...queries: Query<T, R>[]): Query<T, R>;
76
- not(...queries: Query<T, R>[]): Query<T, R>;
77
- none(...queries: Query<T, R>[]): Query<T, R>;
78
- filter(constraints: FilterConstraints<T>): Query<T, R>;
79
- filter<F extends keyof T>(column: F, value: FilterConstraints<T[F]> | DeepConstraint<T[F]>): Query<T, R>;
80
- sort<F extends keyof T>(column: F, direction: SortDirection): Query<T, R>;
81
- getMany(options?: BulkQueryOptions<T>): Promise<R[]>;
82
- getOne(options?: BulkQueryOptions<T>): Promise<R | null>;
83
- deleteAll(): Promise<number>;
84
- include(columns: Include<T>): this;
85
- }
86
- export declare abstract class Repository<T> extends Query<T, Selectable<T>> {
87
- select<K extends keyof Selectable<T>>(...columns: K[]): Query<T, Select<T, K>>;
13
+ export declare abstract class Repository<T extends XataRecord> extends Query<T> {
88
14
  abstract create(object: Selectable<T>): Promise<T>;
15
+ abstract createMany(objects: Selectable<T>[]): Promise<T[]>;
89
16
  abstract read(id: string): Promise<T | null>;
90
17
  abstract update(id: string, object: Partial<T>): Promise<T>;
91
18
  abstract delete(id: string): void;
92
- abstract query<R>(query: Query<T, R>): Promise<R[]>;
19
+ abstract query<R extends XataRecord, Options extends QueryOptions<T>>(query: Query<T, R>, options: Options): Promise<Page<T, typeof options['columns'] extends SelectableColumn<T>[] ? Select<T, typeof options['columns'][number]> : R>>;
93
20
  }
94
- export declare class RestRepository<T> extends Repository<T> {
95
- client: BaseClient<any>;
96
- fetch: any;
21
+ export declare class RestRepository<T extends XataRecord> extends Repository<T> {
22
+ #private;
97
23
  constructor(client: BaseClient<any>, table: string);
98
- request(method: string, path: string, body?: unknown): Promise<any>;
99
- select<K extends keyof T>(...columns: K[]): Query<T, Select<T, K>>;
24
+ request<T>(method: string, path: string, body?: unknown): Promise<T | undefined>;
100
25
  create(object: T): Promise<T>;
26
+ createMany(objects: T[]): Promise<T[]>;
101
27
  read(id: string): Promise<T | null>;
102
28
  update(id: string, object: Partial<T>): Promise<T>;
103
29
  delete(id: string): Promise<void>;
104
- query<R>(query: Query<T, R>): Promise<R[]>;
30
+ query<R extends XataRecord, Options extends QueryOptions<T>>(query: Query<T, R>, options: Options): Promise<Page<T, typeof options['columns'] extends SelectableColumn<T>[] ? Select<T, typeof options['columns'][number]> : R>>;
105
31
  }
106
32
  interface RepositoryFactory {
107
- createRepository<T>(client: BaseClient<any>, table: string): Repository<T>;
33
+ createRepository<T extends XataRecord>(client: BaseClient<any>, table: string): Repository<T>;
108
34
  }
109
35
  export declare class RestRespositoryFactory implements RepositoryFactory {
110
- createRepository<T>(client: BaseClient<any>, table: string): Repository<T>;
36
+ createRepository<T extends XataRecord>(client: BaseClient<any>, table: string): Repository<T>;
111
37
  }
112
38
  declare type BranchStrategyValue = string | undefined | null;
113
39
  declare type BranchStrategyBuilder = () => BranchStrategyValue | Promise<BranchStrategyValue>;
@@ -115,7 +41,7 @@ declare type BranchStrategy = BranchStrategyValue | BranchStrategyBuilder;
115
41
  declare type BranchStrategyOption = NonNullable<BranchStrategy | BranchStrategy[]>;
116
42
  export declare type XataClientOptions = {
117
43
  fetch?: unknown;
118
- databaseURL: string;
44
+ databaseURL?: string;
119
45
  branch: BranchStrategyOption;
120
46
  apiKey: string;
121
47
  repositoryFactory?: RepositoryFactory;
@@ -134,4 +60,4 @@ export declare class XataError extends Error {
134
60
  constructor(message: string, status: number);
135
61
  }
136
62
  export declare type Links = Record<string, Array<string[]>>;
137
- export {};
63
+ export * from './schema';
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,42 @@ 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
+ const errors_1 = require("./util/errors");
46
+ class Repository extends query_1.Query {
162
47
  }
163
48
  exports.Repository = Repository;
164
49
  class RestRepository extends Repository {
165
50
  constructor(client, table) {
166
51
  super(null, table, {});
167
- this.client = client;
168
- const { fetch } = client.options;
169
- if (fetch) {
170
- this.fetch = fetch;
52
+ _RestRepository_client.set(this, void 0);
53
+ _RestRepository_fetch.set(this, void 0);
54
+ _RestRepository_table.set(this, void 0);
55
+ __classPrivateFieldSet(this, _RestRepository_client, client, "f");
56
+ __classPrivateFieldSet(this, _RestRepository_table, table, "f");
57
+ const doWeHaveFetch = typeof fetch !== 'undefined';
58
+ const isInjectedFetchProblematic = !__classPrivateFieldGet(this, _RestRepository_client, "f").options.fetch;
59
+ if (doWeHaveFetch) {
60
+ __classPrivateFieldSet(this, _RestRepository_fetch, fetch, "f");
171
61
  }
172
- else if (typeof window === 'object') {
173
- this.fetch = window.fetch;
62
+ else if (isInjectedFetchProblematic) {
63
+ throw new Error(errors_1.errors.falsyFetchImplementation);
174
64
  }
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
- }
65
+ else {
66
+ __classPrivateFieldSet(this, _RestRepository_fetch, __classPrivateFieldGet(this, _RestRepository_client, "f").options.fetch, "f");
187
67
  }
188
- Object.defineProperty(this, 'client', { enumerable: false });
189
- Object.defineProperty(this, 'fetch', { enumerable: false });
190
- Object.defineProperty(this, 'hostname', { enumerable: false });
191
68
  }
192
69
  request(method, path, body) {
193
70
  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}`, {
71
+ const { databaseURL, apiKey } = __classPrivateFieldGet(this, _RestRepository_client, "f").options;
72
+ const branch = yield __classPrivateFieldGet(this, _RestRepository_client, "f").getBranch();
73
+ const fetchImpl = __classPrivateFieldGet(this, _RestRepository_fetch, "f");
74
+ const resp = yield fetchImpl(`${databaseURL}:${branch}${path}`, {
197
75
  method,
198
76
  headers: {
199
77
  Accept: '*/*',
@@ -219,31 +97,46 @@ class RestRepository extends Repository {
219
97
  throw new XataError(resp.statusText, resp.status);
220
98
  }
221
99
  if (resp.status === 204)
222
- return;
100
+ return undefined;
223
101
  return resp.json();
224
102
  });
225
103
  }
226
- select(...columns) {
227
- return new Query(this.repository, this.table, {});
228
- }
229
104
  create(object) {
230
105
  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
- }
106
+ const record = transformObjectLinks(object);
107
+ const response = yield this.request('POST', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/data`, record);
108
+ if (!response) {
109
+ throw new Error("The server didn't return any data for the query");
110
+ }
111
+ const finalObject = yield this.read(response.id);
112
+ if (!finalObject) {
113
+ throw new Error('The server failed to save the record');
114
+ }
115
+ return finalObject;
116
+ });
117
+ }
118
+ createMany(objects) {
119
+ return __awaiter(this, void 0, void 0, function* () {
120
+ const records = objects.map((object) => transformObjectLinks(object));
121
+ const response = yield this.request('POST', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/bulk`, { records });
122
+ if (!response) {
123
+ throw new Error("The server didn't return any data for the query");
124
+ }
125
+ // TODO: Use filer.$any() to get all the records
126
+ const finalObjects = yield Promise.all(response.recordIDs.map((id) => this.read(id)));
127
+ if (finalObjects.some((object) => !object)) {
128
+ throw new Error('The server failed to save the record');
237
129
  }
238
- const obj = yield this.request('POST', `/tables/${this.table}/data`, body);
239
- return this.client.initObject(this.table, obj);
130
+ return finalObjects;
240
131
  });
241
132
  }
242
133
  read(id) {
243
134
  return __awaiter(this, void 0, void 0, function* () {
244
135
  try {
245
- const obj = yield this.request('GET', `/tables/${this.table}/data/${id}`);
246
- return this.client.initObject(this.table, obj);
136
+ const response = yield this.request('GET', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/data/${id}`);
137
+ if (!response)
138
+ return null;
139
+ return __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), response);
247
140
  }
248
141
  catch (err) {
249
142
  if (err.status === 404)
@@ -254,33 +147,42 @@ class RestRepository extends Repository {
254
147
  }
255
148
  update(id, object) {
256
149
  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);
150
+ const response = yield this.request('PUT', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/data/${id}`, object);
151
+ if (!response) {
152
+ throw new Error("The server didn't return any data for the query");
153
+ }
154
+ // TODO: Review this, not sure we are properly initializing the object
155
+ return __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), response);
259
156
  });
260
157
  }
261
158
  delete(id) {
262
159
  return __awaiter(this, void 0, void 0, function* () {
263
- yield this.request('DELETE', `/tables/${this.table}/data/${id}`);
160
+ yield this.request('DELETE', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/data/${id}`);
264
161
  });
265
162
  }
266
- query(query) {
163
+ query(query, options) {
164
+ var _a, _b, _c;
267
165
  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
- };
166
+ const data = query.getQueryOptions();
274
167
  const body = {
275
- filter: Object.values(filter).some(Boolean) ? filter : undefined,
276
- sort: query.$sort
168
+ filter: Object.values(data.filter).some(Boolean) ? data.filter : undefined,
169
+ sort: (_a = (0, filters_1.buildSortFilter)(options === null || options === void 0 ? void 0 : options.sort)) !== null && _a !== void 0 ? _a : data.sort,
170
+ page: (_b = options === null || options === void 0 ? void 0 : options.page) !== null && _b !== void 0 ? _b : data.page,
171
+ columns: (_c = options === null || options === void 0 ? void 0 : options.columns) !== null && _c !== void 0 ? _c : data.columns
277
172
  };
278
- const result = yield this.request('POST', `/tables/${this.table}/query`, body);
279
- return result.records.map((record) => this.client.initObject(this.table, record));
173
+ const response = yield this.request('POST', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/query`, body);
174
+ if (!response) {
175
+ throw new Error("The server didn't return any data for the query");
176
+ }
177
+ const { meta, records: objects } = response;
178
+ const records = objects.map((record) => __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), record));
179
+ // TODO: We should properly type this any
180
+ return new pagination_1.Page(query, meta, records);
280
181
  });
281
182
  }
282
183
  }
283
184
  exports.RestRepository = RestRepository;
185
+ _RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _RestRepository_table = new WeakMap();
284
186
  class RestRespositoryFactory {
285
187
  createRepository(client, table) {
286
188
  return new RestRepository(client, table);
@@ -376,3 +278,13 @@ exports.XataError = XataError;
376
278
  const isBranchStrategyBuilder = (strategy) => {
377
279
  return typeof strategy === 'function';
378
280
  };
281
+ // TODO: We can find a better implementation for links
282
+ const transformObjectLinks = (object) => {
283
+ return Object.entries(object).reduce((acc, [key, value]) => {
284
+ if (value && typeof value === 'object' && typeof value.id === 'string') {
285
+ return Object.assign(Object.assign({}, acc), { [key]: value.id });
286
+ }
287
+ return Object.assign(Object.assign({}, acc), { [key]: value });
288
+ }, {});
289
+ };
290
+ __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;