@xata.io/client 0.0.0-beta.249d55a → 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,156 +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 CursorNavigationOptions = {
58
- first?: string;
59
- } | {
60
- last?: string;
61
- } | {
62
- after?: string;
63
- before?: string;
64
- };
65
- declare type OffsetNavigationOptions = {
66
- size?: number;
67
- offset?: number;
68
- };
69
- declare type PaginationOptions = CursorNavigationOptions & OffsetNavigationOptions;
70
- declare type BulkQueryOptions<T> = {
71
- page?: PaginationOptions;
72
- };
73
- declare type QueryOrConstraint<T, R> = Query<T, R> | Constraint<T>;
74
- declare type QueryMeta = {
75
- page: {
76
- cursor: string;
77
- more: boolean;
78
- };
79
- };
80
- interface BasePage<T, R> {
81
- query: Query<T, R>;
82
- meta: QueryMeta;
83
- records: R[];
84
- nextPage(size?: number, offset?: number): Promise<Page<T, R>>;
85
- previousPage(size?: number, offset?: number): Promise<Page<T, R>>;
86
- firstPage(size?: number, offset?: number): Promise<Page<T, R>>;
87
- lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
88
- hasNextPage(): boolean;
89
- }
90
- declare class Page<T, R> implements BasePage<T, R> {
91
- readonly query: Query<T, R>;
92
- readonly meta: QueryMeta;
93
- readonly records: R[];
94
- constructor(query: Query<T, R>, meta: QueryMeta, records?: R[]);
95
- nextPage(size?: number, offset?: number): Promise<Page<T, R>>;
96
- previousPage(size?: number, offset?: number): Promise<Page<T, R>>;
97
- firstPage(size?: number, offset?: number): Promise<Page<T, R>>;
98
- lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
99
- hasNextPage(): boolean;
100
- }
101
- export declare class Query<T, R = T> implements BasePage<T, R> {
102
- table: string;
103
- repository: Repository<T>;
104
- readonly $any?: QueryOrConstraint<T, R>[];
105
- readonly $all?: QueryOrConstraint<T, R>[];
106
- readonly $not?: QueryOrConstraint<T, R>[];
107
- readonly $none?: QueryOrConstraint<T, R>[];
108
- readonly $sort?: Record<string, SortDirection>;
109
- readonly query: Query<T, R>;
110
- readonly meta: QueryMeta;
111
- readonly records: R[];
112
- constructor(repository: Repository<T> | null, table: string, data: Partial<Query<T, R>>, parent?: Query<T, R>);
113
- any(...queries: Query<T, R>[]): Query<T, R>;
114
- all(...queries: Query<T, R>[]): Query<T, R>;
115
- not(...queries: Query<T, R>[]): Query<T, R>;
116
- none(...queries: Query<T, R>[]): Query<T, R>;
117
- filter(constraints: FilterConstraints<T>): Query<T, R>;
118
- filter<F extends keyof T>(column: F, value: FilterConstraints<T[F]> | DeepConstraint<T[F]>): Query<T, R>;
119
- sort<F extends keyof T>(column: F, direction: SortDirection): Query<T, R>;
120
- getPaginated(options?: BulkQueryOptions<T>): Promise<Page<T, R>>;
121
- [Symbol.asyncIterator](): AsyncIterableIterator<R>;
122
- getIterator(chunk: number, options?: Omit<BulkQueryOptions<T>, 'page'>): AsyncGenerator<R[]>;
123
- getMany(options?: BulkQueryOptions<T>): Promise<R[]>;
124
- getOne(options?: Omit<BulkQueryOptions<T>, 'page'>): Promise<R | null>;
125
- deleteAll(): Promise<number>;
126
- include(columns: Include<T>): this;
127
- nextPage(size?: number, offset?: number): Promise<Page<T, R>>;
128
- previousPage(size?: number, offset?: number): Promise<Page<T, R>>;
129
- firstPage(size?: number, offset?: number): Promise<Page<T, R>>;
130
- lastPage(size?: number, offset?: number): Promise<Page<T, R>>;
131
- hasNextPage(): boolean;
132
- }
133
- export declare abstract class Repository<T> extends Query<T, Selectable<T>> {
134
- 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> {
135
14
  abstract create(object: Selectable<T>): Promise<T>;
136
15
  abstract createMany(objects: Selectable<T>[]): Promise<T[]>;
137
16
  abstract read(id: string): Promise<T | null>;
138
17
  abstract update(id: string, object: Partial<T>): Promise<T>;
139
18
  abstract delete(id: string): void;
140
- abstract _runQuery<R>(query: Query<T, R>, options?: BulkQueryOptions<T>): Promise<Page<T, 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>>;
141
20
  }
142
- export declare class RestRepository<T> extends Repository<T> {
143
- client: BaseClient<any>;
144
- fetch: any;
21
+ export declare class RestRepository<T extends XataRecord> extends Repository<T> {
22
+ #private;
145
23
  constructor(client: BaseClient<any>, table: string);
146
24
  request<T>(method: string, path: string, body?: unknown): Promise<T | undefined>;
147
- select<K extends keyof T>(...columns: K[]): Query<T, Select<T, K>>;
148
25
  create(object: T): Promise<T>;
149
26
  createMany(objects: T[]): Promise<T[]>;
150
27
  read(id: string): Promise<T | null>;
151
28
  update(id: string, object: Partial<T>): Promise<T>;
152
29
  delete(id: string): Promise<void>;
153
- _runQuery<R>(query: Query<T, R>, options?: BulkQueryOptions<T>): Promise<Page<T, 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>>;
154
31
  }
155
32
  interface RepositoryFactory {
156
- createRepository<T>(client: BaseClient<any>, table: string): Repository<T>;
33
+ createRepository<T extends XataRecord>(client: BaseClient<any>, table: string): Repository<T>;
157
34
  }
158
35
  export declare class RestRespositoryFactory implements RepositoryFactory {
159
- createRepository<T>(client: BaseClient<any>, table: string): Repository<T>;
36
+ createRepository<T extends XataRecord>(client: BaseClient<any>, table: string): Repository<T>;
160
37
  }
161
38
  declare type BranchStrategyValue = string | undefined | null;
162
39
  declare type BranchStrategyBuilder = () => BranchStrategyValue | Promise<BranchStrategyValue>;
@@ -183,4 +60,4 @@ export declare class XataError extends Error {
183
60
  constructor(message: string, status: number);
184
61
  }
185
62
  export declare type Links = Record<string, Array<string[]>>;
186
- 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,279 +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
  };
18
- var __await = (this && this.__await) || function (v) { return this instanceof __await ? (this.v = v, this) : new __await(v); }
19
- var __asyncGenerator = (this && this.__asyncGenerator) || function (thisArg, _arguments, generator) {
20
- if (!Symbol.asyncIterator) throw new TypeError("Symbol.asyncIterator is not defined.");
21
- var g = generator.apply(thisArg, _arguments || []), i, q = [];
22
- return i = {}, verb("next"), verb("throw"), verb("return"), i[Symbol.asyncIterator] = function () { return this; }, i;
23
- function verb(n) { if (g[n]) i[n] = function (v) { return new Promise(function (a, b) { q.push([n, v, a, b]) > 1 || resume(n, v); }); }; }
24
- function resume(n, v) { try { step(g[n](v)); } catch (e) { settle(q[0][3], e); } }
25
- function step(r) { r.value instanceof __await ? Promise.resolve(r.value.v).then(fulfill, reject) : settle(q[0][2], r); }
26
- function fulfill(value) { resume("next", value); }
27
- function reject(value) { resume("throw", value); }
28
- function settle(f, v) { if (f(v), q.shift(), q.length) resume(q[0][0], q[0][1]); }
29
- };
39
+ var _RestRepository_client, _RestRepository_fetch, _RestRepository_table;
30
40
  Object.defineProperty(exports, "__esModule", { value: true });
31
- 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;
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");
32
45
  const errors_1 = require("./util/errors");
33
- const gt = (value) => ({ $gt: value });
34
- exports.gt = gt;
35
- const ge = (value) => ({ $ge: value });
36
- exports.ge = ge;
37
- const gte = (value) => ({ $ge: value });
38
- exports.gte = gte;
39
- const lt = (value) => ({ $lt: value });
40
- exports.lt = lt;
41
- const lte = (value) => ({ $le: value });
42
- exports.lte = lte;
43
- const le = (value) => ({ $le: value });
44
- exports.le = le;
45
- const exists = (column) => ({ $exists: column });
46
- exports.exists = exists;
47
- const notExists = (column) => ({ $notExists: column });
48
- exports.notExists = notExists;
49
- const startsWith = (value) => ({ $startsWith: value });
50
- exports.startsWith = startsWith;
51
- const endsWith = (value) => ({ $endsWith: value });
52
- exports.endsWith = endsWith;
53
- const pattern = (value) => ({ $pattern: value });
54
- exports.pattern = pattern;
55
- const is = (value) => ({ $is: value });
56
- exports.is = is;
57
- const isNot = (value) => ({ $isNot: value });
58
- exports.isNot = isNot;
59
- const contains = (value) => ({ $contains: value });
60
- exports.contains = contains;
61
- // TODO: these can only be applied to columns of type "multiple"
62
- const includes = (value) => ({ $includes: value });
63
- exports.includes = includes;
64
- const includesSubstring = (value) => ({ $includesSubstring: value });
65
- exports.includesSubstring = includesSubstring;
66
- const includesPattern = (value) => ({ $includesPattern: value });
67
- exports.includesPattern = includesPattern;
68
- const includesAll = (value) => ({ $includesAll: value });
69
- exports.includesAll = includesAll;
70
- class Page {
71
- constructor(query, meta, records = []) {
72
- this.query = query;
73
- this.meta = meta;
74
- this.records = records;
75
- }
76
- nextPage(size, offset) {
77
- return __awaiter(this, void 0, void 0, function* () {
78
- return this.query.getPaginated({ page: { size, offset, after: this.meta.page.cursor } });
79
- });
80
- }
81
- previousPage(size, offset) {
82
- return __awaiter(this, void 0, void 0, function* () {
83
- return this.query.getPaginated({ page: { size, offset, before: this.meta.page.cursor } });
84
- });
85
- }
86
- firstPage(size, offset) {
87
- return __awaiter(this, void 0, void 0, function* () {
88
- return this.query.getPaginated({ page: { size, offset, first: this.meta.page.cursor } });
89
- });
90
- }
91
- lastPage(size, offset) {
92
- return __awaiter(this, void 0, void 0, function* () {
93
- return this.query.getPaginated({ page: { size, offset, last: this.meta.page.cursor } });
94
- });
95
- }
96
- // TODO: We need to add something on the backend if we want a hasPreviousPage
97
- hasNextPage() {
98
- return this.meta.page.more;
99
- }
100
- }
101
- class Query {
102
- constructor(repository, table, data, parent) {
103
- // Cursor pagination
104
- this.query = this;
105
- this.meta = { page: { cursor: 'start', more: true } };
106
- this.records = [];
107
- if (repository) {
108
- this.repository = repository;
109
- }
110
- else {
111
- this.repository = this;
112
- }
113
- this.table = table;
114
- // For some reason Object.assign(this, parent) didn't work in this case
115
- // so doing all this manually:
116
- this.$any = parent === null || parent === void 0 ? void 0 : parent.$any;
117
- this.$all = parent === null || parent === void 0 ? void 0 : parent.$all;
118
- this.$not = parent === null || parent === void 0 ? void 0 : parent.$not;
119
- this.$none = parent === null || parent === void 0 ? void 0 : parent.$none;
120
- this.$sort = parent === null || parent === void 0 ? void 0 : parent.$sort;
121
- Object.assign(this, data);
122
- // These bindings are used to support deconstructing
123
- // const { any, not, filter, sort } = xata.users.query()
124
- this.any = this.any.bind(this);
125
- this.all = this.all.bind(this);
126
- this.not = this.not.bind(this);
127
- this.filter = this.filter.bind(this);
128
- this.sort = this.sort.bind(this);
129
- this.none = this.none.bind(this);
130
- Object.defineProperty(this, 'table', { enumerable: false });
131
- Object.defineProperty(this, 'repository', { enumerable: false });
132
- }
133
- any(...queries) {
134
- return new Query(this.repository, this.table, {
135
- $any: (this.$any || []).concat(queries)
136
- }, this);
137
- }
138
- all(...queries) {
139
- return new Query(this.repository, this.table, {
140
- $all: (this.$all || []).concat(queries)
141
- }, this);
142
- }
143
- not(...queries) {
144
- return new Query(this.repository, this.table, {
145
- $not: (this.$not || []).concat(queries)
146
- }, this);
147
- }
148
- none(...queries) {
149
- return new Query(this.repository, this.table, {
150
- $none: (this.$none || []).concat(queries)
151
- }, this);
152
- }
153
- filter(a, b) {
154
- if (arguments.length === 1) {
155
- const constraints = a;
156
- const queries = [];
157
- for (const [column, constraint] of Object.entries(constraints)) {
158
- queries.push({ [column]: constraint });
159
- }
160
- return new Query(this.repository, this.table, {
161
- $all: (this.$all || []).concat(queries)
162
- }, this);
163
- }
164
- else {
165
- const column = a;
166
- const value = b;
167
- return new Query(this.repository, this.table, {
168
- $all: (this.$all || []).concat({ [column]: value })
169
- }, this);
170
- }
171
- }
172
- sort(column, direction) {
173
- const sort = Object.assign(Object.assign({}, this.$sort), { [column]: direction });
174
- const q = new Query(this.repository, this.table, {
175
- $sort: sort
176
- }, this);
177
- return q;
178
- }
179
- getPaginated(options) {
180
- return __awaiter(this, void 0, void 0, function* () {
181
- return this.repository._runQuery(this, options);
182
- });
183
- }
184
- [Symbol.asyncIterator]() {
185
- return __asyncGenerator(this, arguments, function* _a() {
186
- var e_1, _b;
187
- try {
188
- for (var _c = __asyncValues(this.getIterator(1)), _d; _d = yield __await(_c.next()), !_d.done;) {
189
- const [record] = _d.value;
190
- yield yield __await(record);
191
- }
192
- }
193
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
194
- finally {
195
- try {
196
- if (_d && !_d.done && (_b = _c.return)) yield __await(_b.call(_c));
197
- }
198
- finally { if (e_1) throw e_1.error; }
199
- }
200
- });
201
- }
202
- getIterator(chunk, options = {}) {
203
- return __asyncGenerator(this, arguments, function* getIterator_1() {
204
- let offset = 0;
205
- let end = false;
206
- while (!end) {
207
- const { records, meta } = yield __await(this.getPaginated(Object.assign(Object.assign({}, options), { page: { size: chunk, offset } })));
208
- yield yield __await(records);
209
- offset += chunk;
210
- end = !meta.page.more;
211
- }
212
- });
213
- }
214
- getMany(options) {
215
- return __awaiter(this, void 0, void 0, function* () {
216
- const { records } = yield this.getPaginated(options);
217
- return records;
218
- });
219
- }
220
- getOne(options = {}) {
221
- return __awaiter(this, void 0, void 0, function* () {
222
- const records = yield this.getMany(Object.assign(Object.assign({}, options), { page: { size: 1 } }));
223
- return records[0] || null;
224
- });
225
- }
226
- deleteAll() {
227
- return __awaiter(this, void 0, void 0, function* () {
228
- // TODO: Return number of affected rows
229
- return 0;
230
- });
231
- }
232
- include(columns) {
233
- // TODO
234
- return this;
235
- }
236
- nextPage(size, offset) {
237
- return __awaiter(this, void 0, void 0, function* () {
238
- return this.firstPage(size, offset);
239
- });
240
- }
241
- previousPage(size, offset) {
242
- return __awaiter(this, void 0, void 0, function* () {
243
- return this.firstPage(size, offset);
244
- });
245
- }
246
- firstPage(size, offset) {
247
- return __awaiter(this, void 0, void 0, function* () {
248
- return this.getPaginated({ page: { size, offset } });
249
- });
250
- }
251
- lastPage(size, offset) {
252
- return __awaiter(this, void 0, void 0, function* () {
253
- return this.getPaginated({ page: { size, offset, before: 'end' } });
254
- });
255
- }
256
- hasNextPage() {
257
- return this.meta.page.more;
258
- }
259
- }
260
- exports.Query = Query;
261
- class Repository extends Query {
262
- select(...columns) {
263
- return new Query(this.repository, this.table, {});
264
- }
46
+ class Repository extends query_1.Query {
265
47
  }
266
48
  exports.Repository = Repository;
267
49
  class RestRepository extends Repository {
268
50
  constructor(client, table) {
269
51
  super(null, table, {});
270
- this.client = client;
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");
271
57
  const doWeHaveFetch = typeof fetch !== 'undefined';
272
- const isInjectedFetchProblematic = !this.client.options.fetch;
58
+ const isInjectedFetchProblematic = !__classPrivateFieldGet(this, _RestRepository_client, "f").options.fetch;
273
59
  if (doWeHaveFetch) {
274
- this.fetch = fetch;
60
+ __classPrivateFieldSet(this, _RestRepository_fetch, fetch, "f");
275
61
  }
276
62
  else if (isInjectedFetchProblematic) {
277
63
  throw new Error(errors_1.errors.falsyFetchImplementation);
278
64
  }
279
65
  else {
280
- this.fetch = this.client.options.fetch;
66
+ __classPrivateFieldSet(this, _RestRepository_fetch, __classPrivateFieldGet(this, _RestRepository_client, "f").options.fetch, "f");
281
67
  }
282
- Object.defineProperty(this, 'client', { enumerable: false });
283
- Object.defineProperty(this, 'fetch', { enumerable: false });
284
- Object.defineProperty(this, 'hostname', { enumerable: false });
285
68
  }
286
69
  request(method, path, body) {
287
70
  return __awaiter(this, void 0, void 0, function* () {
288
- const { databaseURL, apiKey } = this.client.options;
289
- const branch = yield this.client.getBranch();
290
- 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}`, {
291
75
  method,
292
76
  headers: {
293
77
  Accept: '*/*',
@@ -317,13 +101,10 @@ class RestRepository extends Repository {
317
101
  return resp.json();
318
102
  });
319
103
  }
320
- select(...columns) {
321
- return new Query(this.repository, this.table, {});
322
- }
323
104
  create(object) {
324
105
  return __awaiter(this, void 0, void 0, function* () {
325
106
  const record = transformObjectLinks(object);
326
- const response = yield this.request('POST', `/tables/${this.table}/data`, record);
107
+ const response = yield this.request('POST', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/data`, record);
327
108
  if (!response) {
328
109
  throw new Error("The server didn't return any data for the query");
329
110
  }
@@ -337,7 +118,7 @@ class RestRepository extends Repository {
337
118
  createMany(objects) {
338
119
  return __awaiter(this, void 0, void 0, function* () {
339
120
  const records = objects.map((object) => transformObjectLinks(object));
340
- const response = yield this.request('POST', `/tables/${this.table}/bulk`, { records });
121
+ const response = yield this.request('POST', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/bulk`, { records });
341
122
  if (!response) {
342
123
  throw new Error("The server didn't return any data for the query");
343
124
  }
@@ -352,10 +133,10 @@ class RestRepository extends Repository {
352
133
  read(id) {
353
134
  return __awaiter(this, void 0, void 0, function* () {
354
135
  try {
355
- const response = yield this.request('GET', `/tables/${this.table}/data/${id}`);
136
+ const response = yield this.request('GET', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/data/${id}`);
356
137
  if (!response)
357
138
  return null;
358
- return this.client.initObject(this.table, response);
139
+ return __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), response);
359
140
  }
360
141
  catch (err) {
361
142
  if (err.status === 404)
@@ -366,43 +147,42 @@ class RestRepository extends Repository {
366
147
  }
367
148
  update(id, object) {
368
149
  return __awaiter(this, void 0, void 0, function* () {
369
- const response = yield this.request('PUT', `/tables/${this.table}/data/${id}`, object);
150
+ const response = yield this.request('PUT', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/data/${id}`, object);
370
151
  if (!response) {
371
152
  throw new Error("The server didn't return any data for the query");
372
153
  }
373
154
  // TODO: Review this, not sure we are properly initializing the object
374
- return this.client.initObject(this.table, response);
155
+ return __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), response);
375
156
  });
376
157
  }
377
158
  delete(id) {
378
159
  return __awaiter(this, void 0, void 0, function* () {
379
- yield this.request('DELETE', `/tables/${this.table}/data/${id}`);
160
+ yield this.request('DELETE', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/data/${id}`);
380
161
  });
381
162
  }
382
- _runQuery(query, options) {
163
+ query(query, options) {
164
+ var _a, _b, _c;
383
165
  return __awaiter(this, void 0, void 0, function* () {
384
- const filter = {
385
- $any: query.$any,
386
- $all: query.$all,
387
- $not: query.$not,
388
- $none: query.$none
389
- };
166
+ const data = query.getQueryOptions();
390
167
  const body = {
391
- filter: Object.values(filter).some(Boolean) ? filter : undefined,
392
- sort: query.$sort,
393
- page: options === null || options === void 0 ? void 0 : options.page
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
394
172
  };
395
- const response = yield this.request('POST', `/tables/${this.table}/query`, body);
173
+ const response = yield this.request('POST', `/tables/${__classPrivateFieldGet(this, _RestRepository_table, "f")}/query`, body);
396
174
  if (!response) {
397
175
  throw new Error("The server didn't return any data for the query");
398
176
  }
399
177
  const { meta, records: objects } = response;
400
- const records = objects.map((record) => this.client.initObject(this.table, record));
401
- return new Page(query, meta, records);
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);
402
181
  });
403
182
  }
404
183
  }
405
184
  exports.RestRepository = RestRepository;
185
+ _RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _RestRepository_table = new WeakMap();
406
186
  class RestRespositoryFactory {
407
187
  createRepository(client, table) {
408
188
  return new RestRepository(client, table);
@@ -457,7 +237,7 @@ class BaseClient {
457
237
  return o;
458
238
  }
459
239
  getBranch() {
460
- var e_2, _a;
240
+ var e_1, _a;
461
241
  return __awaiter(this, void 0, void 0, function* () {
462
242
  if (this.branch)
463
243
  return this.branch;
@@ -476,12 +256,12 @@ class BaseClient {
476
256
  }
477
257
  }
478
258
  }
479
- catch (e_2_1) { e_2 = { error: e_2_1 }; }
259
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
480
260
  finally {
481
261
  try {
482
262
  if (strategies_1_1 && !strategies_1_1.done && (_a = strategies_1.return)) yield _a.call(strategies_1);
483
263
  }
484
- finally { if (e_2) throw e_2.error; }
264
+ finally { if (e_1) throw e_1.error; }
485
265
  }
486
266
  throw new Error('Unable to resolve branch value');
487
267
  });
@@ -507,3 +287,4 @@ const transformObjectLinks = (object) => {
507
287
  return Object.assign(Object.assign({}, acc), { [key]: value });
508
288
  }, {});
509
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
+ };