@xata.io/client 0.0.0-beta.21c5d41 → 0.0.0-beta.2781f49

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.
@@ -26,20 +26,22 @@ var __asyncValues = (this && this.__asyncValues) || function (o) {
26
26
  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); }); }; }
27
27
  function settle(resolve, reject, d, v) { Promise.resolve(v).then(function(v) { resolve({ value: v, done: d }); }, reject); }
28
28
  };
29
- var _RestRepository_instances, _RestRepository_client, _RestRepository_fetch, _RestRepository_table, _RestRepository_getFetchProps, _BaseClient_links, _BaseClient_branch;
29
+ var _RestRepository_instances, _RestRepository_client, _RestRepository_fetch, _RestRepository_table, _RestRepository_getFetchProps, _RestRepository_insertRecordWithoutId, _RestRepository_insertRecordWithId, _RestRepository_bulkInsertTableRecords, _RestRepository_updateRecordWithID, _RestRepository_upsertRecordWithID, _RestRepository_deleteRecord, _BaseClient_links, _BaseClient_branch;
30
30
  Object.defineProperty(exports, "__esModule", { value: true });
31
31
  exports.BaseClient = exports.RestRespositoryFactory = exports.RestRepository = exports.Repository = void 0;
32
32
  const api_1 = require("../api");
33
- const filters_1 = require("./filters");
33
+ const lang_1 = require("../util/lang");
34
34
  const pagination_1 = require("./pagination");
35
35
  const query_1 = require("./query");
36
+ const record_1 = require("./record");
37
+ const sorting_1 = require("./sorting");
36
38
  /**
37
39
  * Common interface for performing operations on a table.
38
40
  */
39
41
  class Repository extends query_1.Query {
40
42
  }
41
43
  exports.Repository = Repository;
42
- class RestRepository extends Repository {
44
+ class RestRepository extends query_1.Query {
43
45
  constructor(client, table) {
44
46
  var _a;
45
47
  super(null, table, {});
@@ -58,98 +60,126 @@ class RestRepository extends Repository {
58
60
  }
59
61
  __classPrivateFieldSet(this, _RestRepository_fetch, fetchImpl, "f");
60
62
  }
61
- create(object) {
63
+ create(a, b) {
62
64
  return __awaiter(this, void 0, void 0, function* () {
63
- const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
64
- const record = transformObjectLinks(object);
65
- const response = yield (0, api_1.insertRecord)(Object.assign({ pathParams: {
66
- workspace: '{workspaceId}',
67
- dbBranchName: '{dbBranch}',
68
- tableName: __classPrivateFieldGet(this, _RestRepository_table, "f")
69
- }, body: record }, fetchProps));
70
- const finalObject = yield this.read(response.id);
71
- if (!finalObject) {
72
- throw new Error('The server failed to save the record');
65
+ // Create many records
66
+ if (Array.isArray(a)) {
67
+ return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_bulkInsertTableRecords).call(this, a);
73
68
  }
74
- return finalObject;
75
- });
76
- }
77
- createMany(objects) {
78
- return __awaiter(this, void 0, void 0, function* () {
79
- const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
80
- const records = objects.map((object) => transformObjectLinks(object));
81
- const response = yield (0, api_1.bulkInsertTableRecords)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f") }, body: { records } }, fetchProps));
82
- const finalObjects = yield this.any(...response.recordIDs.map((id) => this.filter('id', id))).getAll();
83
- if (finalObjects.length !== objects.length) {
84
- throw new Error('The server failed to save some records');
69
+ // Create one record with id as param
70
+ if ((0, lang_1.isString)(a) && (0, lang_1.isObject)(b)) {
71
+ if (a === '')
72
+ throw new Error("The id can't be empty");
73
+ return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_insertRecordWithId).call(this, a, b);
74
+ }
75
+ // Create one record with id as property
76
+ if ((0, lang_1.isObject)(a) && (0, lang_1.isString)(a.id)) {
77
+ if (a.id === '')
78
+ throw new Error("The id can't be empty");
79
+ return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_insertRecordWithId).call(this, a.id, Object.assign(Object.assign({}, a), { id: undefined }));
85
80
  }
86
- return finalObjects;
81
+ // Create one record without id
82
+ if ((0, lang_1.isObject)(a)) {
83
+ return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_insertRecordWithoutId).call(this, a);
84
+ }
85
+ throw new Error('Invalid arguments for create method');
87
86
  });
88
87
  }
88
+ // TODO: Add column support: https://github.com/xataio/openapi/issues/139
89
89
  read(recordId) {
90
90
  return __awaiter(this, void 0, void 0, function* () {
91
91
  const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
92
- const response = yield (0, api_1.getRecord)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId } }, fetchProps));
93
- return __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), response);
94
- });
95
- }
96
- update(recordId, object) {
97
- return __awaiter(this, void 0, void 0, function* () {
98
- const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
99
- const response = yield (0, api_1.updateRecordWithID)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId }, body: object }, fetchProps));
100
- const item = yield this.read(response.id);
101
- if (!item)
102
- throw new Error('The server failed to save the record');
103
- return item;
92
+ try {
93
+ const response = yield (0, api_1.getRecord)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId } }, fetchProps));
94
+ return __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), response);
95
+ }
96
+ catch (e) {
97
+ if ((0, lang_1.isObject)(e) && e.status === 404) {
98
+ return null;
99
+ }
100
+ throw e;
101
+ }
104
102
  });
105
103
  }
106
- insert(recordId, object) {
104
+ update(a, b) {
107
105
  return __awaiter(this, void 0, void 0, function* () {
108
- const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
109
- const record = transformObjectLinks(object);
110
- const response = yield (0, api_1.insertRecordWithID)(Object.assign({ pathParams: {
111
- workspace: '{workspaceId}',
112
- dbBranchName: '{dbBranch}',
113
- tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"),
114
- recordId
115
- }, body: record }, fetchProps));
116
- const finalObject = yield this.read(response.id);
117
- if (!finalObject) {
118
- throw new Error('The server failed to save the record');
106
+ // Update many records
107
+ if (Array.isArray(a)) {
108
+ if (a.length > 100) {
109
+ // TODO: Implement bulk update when API has support for it
110
+ console.warn('Bulk update operation is not optimized in the Xata API yet, this request might be slow');
111
+ }
112
+ return Promise.all(a.map((object) => this.update(object)));
113
+ }
114
+ // Update one record with id as param
115
+ if ((0, lang_1.isString)(a) && (0, lang_1.isObject)(b)) {
116
+ return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_updateRecordWithID).call(this, a, b);
119
117
  }
120
- return finalObject;
118
+ // Update one record with id as property
119
+ if ((0, lang_1.isObject)(a) && (0, lang_1.isString)(a.id)) {
120
+ return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_updateRecordWithID).call(this, a.id, Object.assign(Object.assign({}, a), { id: undefined }));
121
+ }
122
+ throw new Error('Invalid arguments for update method');
121
123
  });
122
124
  }
123
- updateOrInsert(recordId, object) {
125
+ createOrUpdate(a, b) {
124
126
  return __awaiter(this, void 0, void 0, function* () {
125
- const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
126
- const response = yield (0, api_1.upsertRecordWithID)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId }, body: object }, fetchProps));
127
- const item = yield this.read(response.id);
128
- if (!item)
129
- throw new Error('The server failed to save the record');
130
- return item;
127
+ // Create or update many records
128
+ if (Array.isArray(a)) {
129
+ if (a.length > 100) {
130
+ // TODO: Implement bulk update when API has support for it
131
+ console.warn('Bulk update operation is not optimized in the Xata API yet, this request might be slow');
132
+ }
133
+ return Promise.all(a.map((object) => this.createOrUpdate(object)));
134
+ }
135
+ // Create or update one record with id as param
136
+ if ((0, lang_1.isString)(a) && (0, lang_1.isObject)(b)) {
137
+ return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_upsertRecordWithID).call(this, a, b);
138
+ }
139
+ // Create or update one record with id as property
140
+ if ((0, lang_1.isObject)(a) && (0, lang_1.isString)(a.id)) {
141
+ return __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_upsertRecordWithID).call(this, a.id, Object.assign(Object.assign({}, a), { id: undefined }));
142
+ }
143
+ throw new Error('Invalid arguments for createOrUpdate method');
131
144
  });
132
145
  }
133
146
  delete(recordId) {
134
147
  return __awaiter(this, void 0, void 0, function* () {
135
- const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
136
- yield (0, api_1.deleteRecord)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId } }, fetchProps));
148
+ // Delete many records
149
+ if (Array.isArray(recordId)) {
150
+ if (recordId.length > 100) {
151
+ // TODO: Implement bulk delete when API has support for it
152
+ console.warn('Bulk delete operation is not optimized in the Xata API yet, this request might be slow');
153
+ }
154
+ yield Promise.all(recordId.map((id) => this.delete(id)));
155
+ return;
156
+ }
157
+ // Delete one record with id as param
158
+ if ((0, lang_1.isString)(recordId)) {
159
+ yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_deleteRecord).call(this, recordId);
160
+ return;
161
+ }
162
+ // Delete one record with id as property
163
+ if ((0, lang_1.isObject)(recordId) && (0, lang_1.isString)(recordId.id)) {
164
+ yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_deleteRecord).call(this, recordId.id);
165
+ return;
166
+ }
167
+ throw new Error('Invalid arguments for delete method');
137
168
  });
138
169
  }
139
- query(query, options = {}) {
140
- var _a, _b, _c;
170
+ query(query) {
171
+ var _a;
141
172
  return __awaiter(this, void 0, void 0, function* () {
142
173
  const data = query.getQueryOptions();
143
174
  const body = {
144
- filter: Object.values(data.filter).some(Boolean) ? data.filter : undefined,
145
- sort: (_a = (0, filters_1.buildSortFilter)(options === null || options === void 0 ? void 0 : options.sort)) !== null && _a !== void 0 ? _a : data.sort,
146
- page: (_b = options === null || options === void 0 ? void 0 : options.page) !== null && _b !== void 0 ? _b : data.page,
147
- columns: (_c = options === null || options === void 0 ? void 0 : options.columns) !== null && _c !== void 0 ? _c : data.columns
175
+ filter: Object.values((_a = data.filter) !== null && _a !== void 0 ? _a : {}).some(Boolean) ? data.filter : undefined,
176
+ sort: data.sort ? (0, sorting_1.buildSortFilter)(data.sort) : undefined,
177
+ page: data.page,
178
+ columns: data.columns
148
179
  };
149
180
  const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
150
181
  const { meta, records: objects } = yield (0, api_1.queryTable)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f") }, body }, fetchProps));
151
182
  const records = objects.map((record) => __classPrivateFieldGet(this, _RestRepository_client, "f").initObject(__classPrivateFieldGet(this, _RestRepository_table, "f"), record));
152
- // TODO: We should properly type this any
153
183
  return new pagination_1.Page(query, meta, records);
154
184
  });
155
185
  }
@@ -172,6 +202,72 @@ _RestRepository_client = new WeakMap(), _RestRepository_fetch = new WeakMap(), _
172
202
  }
173
203
  };
174
204
  });
205
+ }, _RestRepository_insertRecordWithoutId = function _RestRepository_insertRecordWithoutId(object) {
206
+ return __awaiter(this, void 0, void 0, function* () {
207
+ const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
208
+ const record = transformObjectLinks(object);
209
+ const response = yield (0, api_1.insertRecord)(Object.assign({ pathParams: {
210
+ workspace: '{workspaceId}',
211
+ dbBranchName: '{dbBranch}',
212
+ tableName: __classPrivateFieldGet(this, _RestRepository_table, "f")
213
+ }, body: record }, fetchProps));
214
+ const finalObject = yield this.read(response.id);
215
+ if (!finalObject) {
216
+ throw new Error('The server failed to save the record');
217
+ }
218
+ return finalObject;
219
+ });
220
+ }, _RestRepository_insertRecordWithId = function _RestRepository_insertRecordWithId(recordId, object) {
221
+ return __awaiter(this, void 0, void 0, function* () {
222
+ const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
223
+ const record = transformObjectLinks(object);
224
+ const response = yield (0, api_1.insertRecordWithID)(Object.assign({ pathParams: {
225
+ workspace: '{workspaceId}',
226
+ dbBranchName: '{dbBranch}',
227
+ tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"),
228
+ recordId
229
+ }, body: record, queryParams: { createOnly: true } }, fetchProps));
230
+ const finalObject = yield this.read(response.id);
231
+ if (!finalObject) {
232
+ throw new Error('The server failed to save the record');
233
+ }
234
+ return finalObject;
235
+ });
236
+ }, _RestRepository_bulkInsertTableRecords = function _RestRepository_bulkInsertTableRecords(objects) {
237
+ return __awaiter(this, void 0, void 0, function* () {
238
+ const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
239
+ const records = objects.map((object) => transformObjectLinks(object));
240
+ const response = yield (0, api_1.bulkInsertTableRecords)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f") }, body: { records } }, fetchProps));
241
+ const finalObjects = yield this.any(...response.recordIDs.map((id) => this.filter('id', id))).getAll();
242
+ if (finalObjects.length !== objects.length) {
243
+ throw new Error('The server failed to save some records');
244
+ }
245
+ return finalObjects;
246
+ });
247
+ }, _RestRepository_updateRecordWithID = function _RestRepository_updateRecordWithID(recordId, object) {
248
+ return __awaiter(this, void 0, void 0, function* () {
249
+ const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
250
+ const record = transformObjectLinks(object);
251
+ const response = yield (0, api_1.updateRecordWithID)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId }, body: record }, fetchProps));
252
+ const item = yield this.read(response.id);
253
+ if (!item)
254
+ throw new Error('The server failed to save the record');
255
+ return item;
256
+ });
257
+ }, _RestRepository_upsertRecordWithID = function _RestRepository_upsertRecordWithID(recordId, object) {
258
+ return __awaiter(this, void 0, void 0, function* () {
259
+ const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
260
+ const response = yield (0, api_1.upsertRecordWithID)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId }, body: object }, fetchProps));
261
+ const item = yield this.read(response.id);
262
+ if (!item)
263
+ throw new Error('The server failed to save the record');
264
+ return item;
265
+ });
266
+ }, _RestRepository_deleteRecord = function _RestRepository_deleteRecord(recordId) {
267
+ return __awaiter(this, void 0, void 0, function* () {
268
+ const fetchProps = yield __classPrivateFieldGet(this, _RestRepository_instances, "m", _RestRepository_getFetchProps).call(this);
269
+ yield (0, api_1.deleteRecord)(Object.assign({ pathParams: { workspace: '{workspaceId}', dbBranchName: '{dbBranch}', tableName: __classPrivateFieldGet(this, _RestRepository_table, "f"), recordId } }, fetchProps));
270
+ });
175
271
  };
176
272
  class RestRespositoryFactory {
177
273
  createRepository(client, table) {
@@ -198,43 +294,31 @@ class BaseClient {
198
294
  });
199
295
  }
200
296
  initObject(table, object) {
201
- const o = {};
202
- Object.assign(o, object);
297
+ const result = {};
298
+ Object.assign(result, object);
203
299
  const tableLinks = __classPrivateFieldGet(this, _BaseClient_links, "f")[table] || [];
204
300
  for (const link of tableLinks) {
205
301
  const [field, linkTable] = link;
206
- const value = o[field];
207
- if (value && typeof value === 'object') {
208
- const { id } = value;
209
- if (Object.keys(value).find((col) => col === 'id')) {
210
- o[field] = this.initObject(linkTable, value);
211
- }
212
- else if (id) {
213
- o[field] = {
214
- id,
215
- get: () => {
216
- return this.db[linkTable].read(id);
217
- }
218
- };
219
- }
302
+ const value = result[field];
303
+ if (value && (0, lang_1.isObject)(value)) {
304
+ result[field] = this.initObject(linkTable, value);
220
305
  }
221
306
  }
222
307
  const db = this.db;
223
- o.read = function () {
224
- return db[table].read(o['id']);
308
+ result.read = function () {
309
+ return db[table].read(result['id']);
225
310
  };
226
- o.update = function (data) {
227
- return db[table].update(o['id'], data);
311
+ result.update = function (data) {
312
+ return db[table].update(result['id'], data);
228
313
  };
229
- o.delete = function () {
230
- return db[table].delete(o['id']);
314
+ result.delete = function () {
315
+ return db[table].delete(result['id']);
231
316
  };
232
317
  for (const prop of ['read', 'update', 'delete']) {
233
- Object.defineProperty(o, prop, { enumerable: false });
318
+ Object.defineProperty(result, prop, { enumerable: false });
234
319
  }
235
- // TODO: links and rev links
236
- Object.freeze(o);
237
- return o;
320
+ Object.freeze(result);
321
+ return result;
238
322
  }
239
323
  getBranch() {
240
324
  var e_1, _a;
@@ -272,12 +356,8 @@ _BaseClient_links = new WeakMap(), _BaseClient_branch = new WeakMap();
272
356
  const isBranchStrategyBuilder = (strategy) => {
273
357
  return typeof strategy === 'function';
274
358
  };
275
- // TODO: We can find a better implementation for links
276
359
  const transformObjectLinks = (object) => {
277
360
  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 });
361
+ return Object.assign(Object.assign({}, acc), { [key]: (0, record_1.isIdentifiable)(value) ? value.id : value });
282
362
  }, {});
283
363
  };
@@ -1,12 +1,25 @@
1
- import { StringKeys, UnionToIntersection, Values } from '../util/types';
2
- import { Query } from './query';
3
- import { BaseData, Identifiable, XataRecord } from './record';
4
- declare type Queries<T> = {
5
- [key in keyof T as T[key] extends Query<any, any> ? key : never]: T[key];
6
- };
7
- export declare type Selectable<T extends BaseData> = T & Partial<Identifiable>;
8
- export declare type SelectableColumn<O> = '*' | (O extends Array<unknown> ? never : O extends Record<string, any> ? '*' | Values<{
9
- [K in StringKeys<O>]: O[K] extends Record<string, any> ? `${K}.${SelectableColumn<O[K]>}` : K;
10
- }> : '');
11
- export declare type Select<T, K extends SelectableColumn<T>> = UnionToIntersection<K extends keyof T ? Pick<T, K> : T> & Queries<T> & XataRecord;
1
+ import { If, IsArray, IsObject, StringKeys, UnionToIntersection, Values } from '../util/types';
2
+ import { Link, XataRecord } from './record';
3
+ export declare type SelectableColumn<O, RecursivePath extends any[] = []> = '*' | 'id' | DataProps<O> | NestedColumns<O, RecursivePath>;
4
+ export declare type SelectedPick<O extends XataRecord, Key extends SelectableColumn<O>[]> = XataRecord & UnionToIntersection<Values<{
5
+ [K in Key[number]]: NestedValueAtColumn<O, K> & XataRecord;
6
+ }>>;
7
+ export declare type ValueAtColumn<O, P extends SelectableColumn<O>> = P extends '*' ? Values<O> : P extends 'id' ? string : P extends keyof O ? O[P] : P extends `${infer K}.${infer V}` ? K extends keyof O ? Values<O[K] extends XataRecord ? (V extends SelectableColumn<O[K]> ? {
8
+ V: ValueAtColumn<O[K], V>;
9
+ } : never) : O[K]> : never : never;
10
+ declare type MAX_RECURSION = 5;
11
+ declare type NestedColumns<O, RecursivePath extends any[]> = RecursivePath['length'] extends MAX_RECURSION ? never : If<IsObject<O>, Values<{
12
+ [K in DataProps<O>]: If<IsArray<NonNullable<O[K]>>, K, // If the property is an array, we stop recursion. We don't support object arrays yet
13
+ If<IsObject<NonNullable<O[K]>>, NonNullable<O[K]> extends XataRecord ? SelectableColumn<NonNullable<O[K]>, [...RecursivePath, O[K]]> extends string ? K | `${K}.${SelectableColumn<NonNullable<O[K]>, [...RecursivePath, O[K]]>}` : never : `${K}.${StringKeys<NonNullable<O[K]>> | '*'}`, // This allows usage of objects that are not links
14
+ K>>;
15
+ }>, never>;
16
+ declare type DataProps<O> = Exclude<StringKeys<O>, StringKeys<XataRecord>>;
17
+ declare type NestedValueAtColumn<O, Key extends SelectableColumn<O>> = Key extends `${infer N}.${infer M}` ? N extends DataProps<O> ? {
18
+ [K in N]: M extends SelectableColumn<NonNullable<O[K]>> ? NonNullable<O[K]> extends XataRecord ? ForwardNullable<O[K], NestedValueAtColumn<NonNullable<O[K]>, M> & XataRecord> : ForwardNullable<O[K], NestedValueAtColumn<NonNullable<O[K]>, M>> : unknown;
19
+ } : unknown : Key extends DataProps<O> ? {
20
+ [K in Key]: NonNullable<O[K]> extends XataRecord ? ForwardNullable<O[K], SelectedPick<NonNullable<O[K]>, ['*']>> : O[K];
21
+ } : Key extends '*' ? {
22
+ [K in StringKeys<O>]: NonNullable<O[K]> extends XataRecord ? ForwardNullable<O[K], Link<NonNullable<O[K]>>> : O[K];
23
+ } : unknown;
24
+ declare type ForwardNullable<T, R> = T extends NonNullable<T> ? R : R | null;
12
25
  export {};
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,203 @@
1
+ "use strict";
2
+ /* eslint-disable @typescript-eslint/no-unused-vars, @typescript-eslint/no-floating-promises */
3
+ Object.defineProperty(exports, "__esModule", { value: true });
4
+ // SelectableColumn<O> //
5
+ // --------------------------------------------------------------------------- //
6
+ const validTeamColumns = [
7
+ '*',
8
+ 'id',
9
+ 'name',
10
+ 'owner.*',
11
+ 'owner.address.*',
12
+ 'owner.address',
13
+ 'owner.address.street'
14
+ ];
15
+ // @ts-expect-error
16
+ const invalidFullNameTeamColumn = 'full_name';
17
+ // @ts-expect-error
18
+ const invalidPartialTeamColumn = 'owner.address.';
19
+ // @ts-expect-error
20
+ const invalidDeleteTeamColumn = 'owner.delete';
21
+ // @ts-expect-error
22
+ const invalidReadTeamColumn = 'owner.read.*';
23
+ // ValueAtColumn<O, P> //
24
+ // --------------------------------------------------------------------------- //
25
+ const labelsValue = ['foo'];
26
+ // @ts-expect-error
27
+ const invalidLabelsValue = [1];
28
+ // SelectedRecordPick<O, Key> //
29
+ // ---------------------------------------------------------------------------- //
30
+ function test1(user) {
31
+ var _a, _b, _c, _d;
32
+ user.id;
33
+ user.read();
34
+ user.full_name;
35
+ (_a = user.address) === null || _a === void 0 ? void 0 : _a.street;
36
+ // @ts-expect-error
37
+ user.team.id;
38
+ (_b = user.team) === null || _b === void 0 ? void 0 : _b.id;
39
+ (_c = user.team) === null || _c === void 0 ? void 0 : _c.read();
40
+ // @ts-expect-error
41
+ (_d = user.team) === null || _d === void 0 ? void 0 : _d.name;
42
+ user.partner.id;
43
+ user.partner.read();
44
+ // @ts-expect-error
45
+ user.partner.full_name;
46
+ user.team = null;
47
+ // @ts-expect-error
48
+ user.partner = null;
49
+ }
50
+ function test2(user) {
51
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
52
+ user.id;
53
+ user.read();
54
+ user.full_name;
55
+ (_a = user.team) === null || _a === void 0 ? void 0 : _a.id;
56
+ (_b = user.team) === null || _b === void 0 ? void 0 : _b.read();
57
+ (_c = user.team) === null || _c === void 0 ? void 0 : _c.name;
58
+ (_d = user.team) === null || _d === void 0 ? void 0 : _d.owner;
59
+ (_f = (_e = user.team) === null || _e === void 0 ? void 0 : _e.owner) === null || _f === void 0 ? void 0 : _f.id;
60
+ (_h = (_g = user.team) === null || _g === void 0 ? void 0 : _g.owner) === null || _h === void 0 ? void 0 : _h.read();
61
+ // @ts-expect-error
62
+ (_k = (_j = user.team) === null || _j === void 0 ? void 0 : _j.owner) === null || _k === void 0 ? void 0 : _k.full_name;
63
+ // @ts-expect-error
64
+ user.full_name = null;
65
+ user.email = null;
66
+ user.email = '';
67
+ // @ts-expect-error
68
+ user.email = 2;
69
+ if (user.team) {
70
+ // @ts-expect-error
71
+ user.team.name = null;
72
+ user.team.labels = null;
73
+ user.team.labels = ['foo'];
74
+ // @ts-expect-error
75
+ user.team.labels = [1];
76
+ }
77
+ }
78
+ function test3(user) {
79
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
80
+ user.id;
81
+ user.read();
82
+ // @ts-expect-error
83
+ user.full_name;
84
+ (_a = user.team) === null || _a === void 0 ? void 0 : _a.id;
85
+ (_b = user.team) === null || _b === void 0 ? void 0 : _b.read();
86
+ // @ts-expect-error
87
+ (_c = user.team) === null || _c === void 0 ? void 0 : _c.name;
88
+ (_e = (_d = user.team) === null || _d === void 0 ? void 0 : _d.owner) === null || _e === void 0 ? void 0 : _e.id;
89
+ (_g = (_f = user.team) === null || _f === void 0 ? void 0 : _f.owner) === null || _g === void 0 ? void 0 : _g.read();
90
+ (_j = (_h = user.team) === null || _h === void 0 ? void 0 : _h.owner) === null || _j === void 0 ? void 0 : _j.full_name;
91
+ }
92
+ function test4(user) {
93
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s;
94
+ user.id;
95
+ user.read();
96
+ // @ts-expect-error
97
+ user.full_name;
98
+ (_a = user.team) === null || _a === void 0 ? void 0 : _a.id;
99
+ (_b = user.team) === null || _b === void 0 ? void 0 : _b.read();
100
+ // @ts-expect-error
101
+ (_c = user.team) === null || _c === void 0 ? void 0 : _c.name;
102
+ (_e = (_d = user.team) === null || _d === void 0 ? void 0 : _d.owner) === null || _e === void 0 ? void 0 : _e.id;
103
+ (_g = (_f = user.team) === null || _f === void 0 ? void 0 : _f.owner) === null || _g === void 0 ? void 0 : _g.read();
104
+ // @ts-expect-error
105
+ (_j = (_h = user.team) === null || _h === void 0 ? void 0 : _h.owner) === null || _j === void 0 ? void 0 : _j.full_name;
106
+ (_l = (_k = user.team) === null || _k === void 0 ? void 0 : _k.owner) === null || _l === void 0 ? void 0 : _l.address;
107
+ (_p = (_o = (_m = user.team) === null || _m === void 0 ? void 0 : _m.owner) === null || _o === void 0 ? void 0 : _o.address) === null || _p === void 0 ? void 0 : _p.street;
108
+ (_s = (_r = (_q = user.team) === null || _q === void 0 ? void 0 : _q.owner) === null || _r === void 0 ? void 0 : _r.address) === null || _s === void 0 ? void 0 : _s.zipcode;
109
+ }
110
+ function test5(user) {
111
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
112
+ user.id;
113
+ user.read();
114
+ // @ts-expect-error
115
+ user.full_name;
116
+ user.settings;
117
+ (_a = user.settings) === null || _a === void 0 ? void 0 : _a.theme;
118
+ (_b = user.settings) === null || _b === void 0 ? void 0 : _b.language;
119
+ (_c = user.settings) === null || _c === void 0 ? void 0 : _c.signin;
120
+ (_e = (_d = user.settings) === null || _d === void 0 ? void 0 : _d.signin) === null || _e === void 0 ? void 0 : _e.email;
121
+ (_g = (_f = user.settings) === null || _f === void 0 ? void 0 : _f.signin) === null || _g === void 0 ? void 0 : _g.github;
122
+ // @ts-expect-error
123
+ (_h = user.settings) === null || _h === void 0 ? void 0 : _h.id;
124
+ // @ts-expect-error
125
+ (_j = user.settings) === null || _j === void 0 ? void 0 : _j.read();
126
+ // @ts-expect-error
127
+ (_l = (_k = user.settings) === null || _k === void 0 ? void 0 : _k.signin) === null || _l === void 0 ? void 0 : _l.id;
128
+ user.settings = null;
129
+ user.nonNullable.nested.value = 2;
130
+ // @ts-expect-error
131
+ user.nonNullable = null;
132
+ // @ts-expect-error
133
+ user.nonNullable.nested.value = null;
134
+ }
135
+ function test6(user) {
136
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l;
137
+ user.id;
138
+ user.read();
139
+ // @ts-expect-error
140
+ user.full_name;
141
+ user.settings;
142
+ (_a = user.settings) === null || _a === void 0 ? void 0 : _a.theme;
143
+ (_b = user.settings) === null || _b === void 0 ? void 0 : _b.language;
144
+ (_c = user.settings) === null || _c === void 0 ? void 0 : _c.signin;
145
+ (_e = (_d = user.settings) === null || _d === void 0 ? void 0 : _d.signin) === null || _e === void 0 ? void 0 : _e.email;
146
+ (_g = (_f = user.settings) === null || _f === void 0 ? void 0 : _f.signin) === null || _g === void 0 ? void 0 : _g.github;
147
+ // @ts-expect-error
148
+ (_h = user.settings) === null || _h === void 0 ? void 0 : _h.id;
149
+ // @ts-expect-error
150
+ (_j = user.settings) === null || _j === void 0 ? void 0 : _j.read();
151
+ // @ts-expect-error
152
+ (_l = (_k = user.settings) === null || _k === void 0 ? void 0 : _k.signin) === null || _l === void 0 ? void 0 : _l.id;
153
+ user.nonNullable.nested.value = 2;
154
+ user.nonNullable.nested.deep.depths.of.numbers = [1, 2, 3];
155
+ // @ts-expect-error
156
+ user.nonNullable.nested.deep.depths.of.numbers = 'invalid-string';
157
+ user.settings = null;
158
+ // @ts-expect-error
159
+ user.nonNullable = null;
160
+ // @ts-expect-error
161
+ user.nonNullable.nested.value = null;
162
+ }
163
+ function test7(user) {
164
+ var _a, _b;
165
+ user.partner;
166
+ user.partner.id;
167
+ user.partner.read();
168
+ user.partner.full_name;
169
+ // @ts-expect-error
170
+ user.partner.full_name = null;
171
+ user.partner.address;
172
+ // @ts-expect-error
173
+ user.team.id;
174
+ (_a = user.team) === null || _a === void 0 ? void 0 : _a.id;
175
+ // @ts-expect-error
176
+ user.team.read();
177
+ (_b = user.team) === null || _b === void 0 ? void 0 : _b.read();
178
+ // @ts-expect-error
179
+ user.partner = null;
180
+ user.team = null;
181
+ }
182
+ function test8(user) {
183
+ var _a, _b;
184
+ user.partner;
185
+ user.partner.id;
186
+ user.partner.read();
187
+ user.partner.full_name;
188
+ // @ts-expect-error
189
+ user.partner.full_name = null;
190
+ user.partner.address;
191
+ // @ts-expect-error
192
+ user.team.id;
193
+ (_a = user.team) === null || _a === void 0 ? void 0 : _a.id;
194
+ // @ts-expect-error
195
+ user.team.read();
196
+ (_b = user.team) === null || _b === void 0 ? void 0 : _b.read();
197
+ // @ts-expect-error
198
+ user.partner = null;
199
+ user.team = null;
200
+ }
201
+ test('fake test', () => {
202
+ // This is a fake test to make sure that the type definitions in this file are working
203
+ });
@@ -0,0 +1,17 @@
1
+ import { SingleOrArray, Values } from '../util/types';
2
+ import { XataRecord } from './record';
3
+ import { SelectableColumn } from './selection';
4
+ export declare type SortDirection = 'asc' | 'desc';
5
+ export declare type SortFilterExtended<T extends XataRecord> = {
6
+ column: SelectableColumn<T>;
7
+ direction?: SortDirection;
8
+ };
9
+ export declare type SortFilter<T extends XataRecord> = SelectableColumn<T> | SortFilterExtended<T>;
10
+ export declare type ApiSortFilter<T extends XataRecord> = SingleOrArray<Values<{
11
+ [key in SelectableColumn<T>]: {
12
+ [K in key]: SortDirection;
13
+ };
14
+ }>>;
15
+ export declare function isSortFilterString<T extends XataRecord>(value: any): value is SelectableColumn<T>;
16
+ export declare function isSortFilterObject<T extends XataRecord>(filter: SortFilter<T>): filter is SortFilterExtended<T>;
17
+ export declare function buildSortFilter<T extends XataRecord>(filter: SingleOrArray<SortFilter<T>>): ApiSortFilter<T>;