@xata.io/client 0.1.4 → 0.1.5
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 +17 -13
- package/dist/index.js +10 -10
- package/dist/index.test.js +9 -9
- package/package.json +1 -1
- package/src/index.test.ts +9 -9
- package/src/index.ts +16 -14
package/dist/index.d.ts
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
export interface XataRecord {
|
2
|
-
|
3
|
-
|
2
|
+
id: string;
|
3
|
+
xata: {
|
4
|
+
version: number;
|
5
|
+
};
|
4
6
|
read(): Promise<this>;
|
5
7
|
update(data: Selectable<this>): Promise<this>;
|
6
8
|
delete(): Promise<void>;
|
@@ -17,32 +19,34 @@ export declare type OmitLinks<T> = {
|
|
17
19
|
export declare type OmitMethods<T> = {
|
18
20
|
[key in keyof T as T[key] extends Function ? never : key]: T[key];
|
19
21
|
};
|
20
|
-
export declare type Selectable<T> = Omit<OmitQueries<OmitMethods<T>>, '
|
22
|
+
export declare type Selectable<T> = Omit<OmitQueries<OmitMethods<T>>, 'id' | 'xata'>;
|
21
23
|
export declare type Select<T, K extends keyof T> = Pick<T, K> & Queries<T> & XataRecord;
|
22
24
|
export declare type Include<T> = {
|
23
25
|
[key in keyof T as T[key] extends XataRecord ? key : never]?: boolean | Array<keyof Selectable<T[key]>>;
|
24
26
|
};
|
25
27
|
declare type SortDirection = 'asc' | 'desc';
|
26
28
|
declare type Operator = '$gt' | '$lt' | '$ge' | '$le' | '$exists' | '$notExists' | '$endsWith' | '$startsWith' | '$pattern' | '$is' | '$isNot' | '$contains' | '$includes' | '$includesSubstring' | '$includesPattern' | '$includesAll';
|
27
|
-
declare type Constraint<T> =
|
29
|
+
declare type Constraint<T> = {
|
30
|
+
[key in Operator]?: T;
|
31
|
+
};
|
28
32
|
declare type DeepConstraint<T> = T extends Record<string, any> ? {
|
29
33
|
[key in keyof T]?: T[key] | DeepConstraint<T[key]>;
|
30
34
|
} : Constraint<T>;
|
31
35
|
declare type ComparableType = number | Date;
|
32
|
-
export declare const gt: <T extends ComparableType>(value: T) =>
|
33
|
-
export declare const ge: <T extends ComparableType>(value: T) =>
|
34
|
-
export declare const gte: <T extends ComparableType>(value: T) =>
|
35
|
-
export declare const lt: <T extends ComparableType>(value: T) =>
|
36
|
-
export declare const lte: <T extends ComparableType>(value: T) =>
|
37
|
-
export declare const le: <T extends ComparableType>(value: T) =>
|
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>;
|
38
42
|
export declare const exists: (column: string) => Constraint<string>;
|
39
43
|
export declare const notExists: (column: string) => Constraint<string>;
|
40
44
|
export declare const startsWith: (value: string) => Constraint<string>;
|
41
45
|
export declare const endsWith: (value: string) => Constraint<string>;
|
42
46
|
export declare const pattern: (value: string) => Constraint<string>;
|
43
|
-
export declare const is: <T>(value: T) =>
|
44
|
-
export declare const isNot: <T>(value: T) =>
|
45
|
-
export declare const contains: <T>(value: T) =>
|
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>;
|
46
50
|
export declare const includes: (value: string) => Constraint<string>;
|
47
51
|
export declare const includesSubstring: (value: string) => Constraint<string>;
|
48
52
|
export declare const includesPattern: (value: string) => Constraint<string>;
|
package/dist/index.js
CHANGED
@@ -223,8 +223,8 @@ class RestRepository extends Repository {
|
|
223
223
|
const body = Object.assign({}, object);
|
224
224
|
for (const key of Object.keys(body)) {
|
225
225
|
const value = body[key];
|
226
|
-
if (value && typeof value === 'object' && typeof value.
|
227
|
-
body[key] = value.
|
226
|
+
if (value && typeof value === 'object' && typeof value.id === 'string') {
|
227
|
+
body[key] = value.id;
|
228
228
|
}
|
229
229
|
}
|
230
230
|
const obj = yield this.request('POST', `/tables/${this.table}/data`, body);
|
@@ -292,15 +292,15 @@ class BaseClient {
|
|
292
292
|
const [field, linkTable] = link;
|
293
293
|
const value = o[field];
|
294
294
|
if (value && typeof value === 'object') {
|
295
|
-
const {
|
296
|
-
if (Object.keys(value).find((col) =>
|
295
|
+
const { id } = value;
|
296
|
+
if (Object.keys(value).find((col) => col === 'id')) {
|
297
297
|
o[field] = this.initObject(linkTable, value);
|
298
298
|
}
|
299
|
-
else if (
|
299
|
+
else if (id) {
|
300
300
|
o[field] = {
|
301
|
-
|
301
|
+
id,
|
302
302
|
get: () => {
|
303
|
-
this.db[linkTable].read(
|
303
|
+
this.db[linkTable].read(id);
|
304
304
|
}
|
305
305
|
};
|
306
306
|
}
|
@@ -308,13 +308,13 @@ class BaseClient {
|
|
308
308
|
}
|
309
309
|
const db = this.db;
|
310
310
|
o.read = function () {
|
311
|
-
return db[table].read(o['
|
311
|
+
return db[table].read(o['id']);
|
312
312
|
};
|
313
313
|
o.update = function (data) {
|
314
|
-
return db[table].update(o['
|
314
|
+
return db[table].update(o['id'], data);
|
315
315
|
};
|
316
316
|
o.delete = function () {
|
317
|
-
return db[table].delete(o['
|
317
|
+
return db[table].delete(o['id']);
|
318
318
|
};
|
319
319
|
for (const prop of ['read', 'update', 'delete']) {
|
320
320
|
Object.defineProperty(o, prop, { enumerable: false });
|
package/dist/index.test.js
CHANGED
@@ -125,11 +125,11 @@ describe('query', () => {
|
|
125
125
|
});
|
126
126
|
describe('getOne', () => {
|
127
127
|
test('returns a single object', () => __awaiter(void 0, void 0, void 0, function* () {
|
128
|
-
const result = { records: [{
|
128
|
+
const result = { records: [{ id: '1234' }] };
|
129
129
|
const expected = { method: 'POST', path: '/tables/users/query', body: {} };
|
130
130
|
expectRequest(expected, () => __awaiter(void 0, void 0, void 0, function* () {
|
131
131
|
const first = yield users.select().getOne();
|
132
|
-
expect(first === null || first === void 0 ? void 0 : first.
|
132
|
+
expect(first === null || first === void 0 ? void 0 : first.id).toBe(result.records[0].id);
|
133
133
|
}), result);
|
134
134
|
}));
|
135
135
|
test('returns null if no objects are returned', () => __awaiter(void 0, void 0, void 0, function* () {
|
@@ -151,12 +151,12 @@ describe('read', () => {
|
|
151
151
|
});
|
152
152
|
describe('Repository.update', () => {
|
153
153
|
test('updates and object successfully', () => __awaiter(void 0, void 0, void 0, function* () {
|
154
|
-
const object = {
|
155
|
-
const expected = { method: 'PUT', path: `/tables/users/data/${object.
|
154
|
+
const object = { id: 'rec_1234', xata: { version: 1 }, name: 'Ada' };
|
155
|
+
const expected = { method: 'PUT', path: `/tables/users/data/${object.id}`, body: object };
|
156
156
|
expectRequest(expected, () => __awaiter(void 0, void 0, void 0, function* () {
|
157
|
-
const result = yield users.update(object.
|
158
|
-
expect(result.
|
159
|
-
}), {
|
157
|
+
const result = yield users.update(object.id, object);
|
158
|
+
expect(result.id).toBe(object.id);
|
159
|
+
}), { id: object.id });
|
160
160
|
}));
|
161
161
|
});
|
162
162
|
describe('Repository.delete', () => {
|
@@ -171,12 +171,12 @@ describe('Repository.delete', () => {
|
|
171
171
|
});
|
172
172
|
describe('create', () => {
|
173
173
|
test('successful', () => __awaiter(void 0, void 0, void 0, function* () {
|
174
|
-
const created = {
|
174
|
+
const created = { id: 'rec_1234', _version: 0 };
|
175
175
|
const object = { name: 'Ada' };
|
176
176
|
const expected = { method: 'POST', path: '/tables/users/data', body: object };
|
177
177
|
expectRequest(expected, () => __awaiter(void 0, void 0, void 0, function* () {
|
178
178
|
const result = yield users.create(object);
|
179
|
-
expect(result.
|
179
|
+
expect(result.id).toBe(created.id);
|
180
180
|
}), created);
|
181
181
|
}));
|
182
182
|
});
|
package/package.json
CHANGED
package/src/index.test.ts
CHANGED
@@ -145,13 +145,13 @@ describe('query', () => {
|
|
145
145
|
|
146
146
|
describe('getOne', () => {
|
147
147
|
test('returns a single object', async () => {
|
148
|
-
const result = { records: [{
|
148
|
+
const result = { records: [{ id: '1234' }] };
|
149
149
|
const expected = { method: 'POST', path: '/tables/users/query', body: {} };
|
150
150
|
expectRequest(
|
151
151
|
expected,
|
152
152
|
async () => {
|
153
153
|
const first = await users.select().getOne();
|
154
|
-
expect(first?.
|
154
|
+
expect(first?.id).toBe(result.records[0].id);
|
155
155
|
},
|
156
156
|
result
|
157
157
|
);
|
@@ -182,15 +182,15 @@ describe('read', () => {
|
|
182
182
|
|
183
183
|
describe('Repository.update', () => {
|
184
184
|
test('updates and object successfully', async () => {
|
185
|
-
const object = {
|
186
|
-
const expected = { method: 'PUT', path: `/tables/users/data/${object.
|
185
|
+
const object = { id: 'rec_1234', xata: { version: 1 }, name: 'Ada' } as User;
|
186
|
+
const expected = { method: 'PUT', path: `/tables/users/data/${object.id}`, body: object };
|
187
187
|
expectRequest(
|
188
188
|
expected,
|
189
189
|
async () => {
|
190
|
-
const result = await users.update(object.
|
191
|
-
expect(result.
|
190
|
+
const result = await users.update(object.id, object);
|
191
|
+
expect(result.id).toBe(object.id);
|
192
192
|
},
|
193
|
-
{
|
193
|
+
{ id: object.id }
|
194
194
|
);
|
195
195
|
});
|
196
196
|
});
|
@@ -207,14 +207,14 @@ describe('Repository.delete', () => {
|
|
207
207
|
|
208
208
|
describe('create', () => {
|
209
209
|
test('successful', async () => {
|
210
|
-
const created = {
|
210
|
+
const created = { id: 'rec_1234', _version: 0 };
|
211
211
|
const object = { name: 'Ada' } as User;
|
212
212
|
const expected = { method: 'POST', path: '/tables/users/data', body: object };
|
213
213
|
expectRequest(
|
214
214
|
expected,
|
215
215
|
async () => {
|
216
216
|
const result = await users.create(object);
|
217
|
-
expect(result.
|
217
|
+
expect(result.id).toBe(created.id);
|
218
218
|
},
|
219
219
|
created
|
220
220
|
);
|
package/src/index.ts
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
export interface XataRecord {
|
2
|
-
|
3
|
-
|
2
|
+
id: string;
|
3
|
+
xata: {
|
4
|
+
version: number;
|
5
|
+
};
|
4
6
|
read(): Promise<this>;
|
5
7
|
update(data: Selectable<this>): Promise<this>;
|
6
8
|
delete(): Promise<void>;
|
@@ -23,7 +25,7 @@ export type OmitMethods<T> = {
|
|
23
25
|
[key in keyof T as T[key] extends Function ? never : key]: T[key];
|
24
26
|
};
|
25
27
|
|
26
|
-
export type Selectable<T> = Omit<OmitQueries<OmitMethods<T>>, '
|
28
|
+
export type Selectable<T> = Omit<OmitQueries<OmitMethods<T>>, 'id' | 'xata'>;
|
27
29
|
|
28
30
|
export type Select<T, K extends keyof T> = Pick<T, K> & Queries<T> & XataRecord;
|
29
31
|
|
@@ -53,7 +55,7 @@ type Operator =
|
|
53
55
|
|
54
56
|
// TODO: restrict constraints depending on type?
|
55
57
|
// E.g. startsWith cannot be used with numbers
|
56
|
-
type Constraint<T> =
|
58
|
+
type Constraint<T> = { [key in Operator]?: T };
|
57
59
|
|
58
60
|
type DeepConstraint<T> = T extends Record<string, any>
|
59
61
|
? {
|
@@ -338,8 +340,8 @@ export class RestRepository<T> extends Repository<T> {
|
|
338
340
|
const body = { ...object } as Record<string, unknown>;
|
339
341
|
for (const key of Object.keys(body)) {
|
340
342
|
const value = body[key];
|
341
|
-
if (value && typeof value === 'object' && typeof (value as Record<string, unknown>).
|
342
|
-
body[key] = (value as XataRecord).
|
343
|
+
if (value && typeof value === 'object' && typeof (value as Record<string, unknown>).id === 'string') {
|
344
|
+
body[key] = (value as XataRecord).id;
|
343
345
|
}
|
344
346
|
}
|
345
347
|
const obj = await this.request('POST', `/tables/${this.table}/data`, body);
|
@@ -418,14 +420,14 @@ export class BaseClient<D extends Record<string, Repository<any>>> {
|
|
418
420
|
const value = o[field];
|
419
421
|
|
420
422
|
if (value && typeof value === 'object') {
|
421
|
-
const {
|
422
|
-
if (Object.keys(value).find((col) =>
|
423
|
+
const { id } = value as any;
|
424
|
+
if (Object.keys(value).find((col) => col === 'id')) {
|
423
425
|
o[field] = this.initObject(linkTable, value);
|
424
|
-
} else if (
|
426
|
+
} else if (id) {
|
425
427
|
o[field] = {
|
426
|
-
|
428
|
+
id,
|
427
429
|
get: () => {
|
428
|
-
this.db[linkTable].read(
|
430
|
+
this.db[linkTable].read(id);
|
429
431
|
}
|
430
432
|
};
|
431
433
|
}
|
@@ -434,13 +436,13 @@ export class BaseClient<D extends Record<string, Repository<any>>> {
|
|
434
436
|
|
435
437
|
const db = this.db;
|
436
438
|
o.read = function () {
|
437
|
-
return db[table].read(o['
|
439
|
+
return db[table].read(o['id'] as string);
|
438
440
|
};
|
439
441
|
o.update = function (data: any) {
|
440
|
-
return db[table].update(o['
|
442
|
+
return db[table].update(o['id'] as string, data);
|
441
443
|
};
|
442
444
|
o.delete = function () {
|
443
|
-
return db[table].delete(o['
|
445
|
+
return db[table].delete(o['id'] as string);
|
444
446
|
};
|
445
447
|
|
446
448
|
for (const prop of ['read', 'update', 'delete']) {
|