@xata.io/client 0.0.0-beta.58bd26f → 0.0.0-beta.64a31a3

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.
@@ -1,304 +0,0 @@
1
- "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- Object.defineProperty(exports, "__esModule", { value: true });
12
- const _1 = require("./");
13
- const buildClient = (options = {}) => {
14
- const { apiKey = '1234', databaseURL = 'https://my-workspace-5df34do.staging.xatabase.co/db/xata', branch = 'main' } = options;
15
- const fetch = jest.fn();
16
- const client = new _1.BaseClient({ fetch, apiKey, databaseURL, branch }, {});
17
- const users = new _1.RestRepository(client, 'users');
18
- return { fetch, client, users };
19
- };
20
- describe('client options', () => {
21
- test('option parameters are set', () => {
22
- const { client } = buildClient({ apiKey: 'apiKey', databaseURL: 'url' });
23
- expect(client.options.apiKey).toBe('apiKey');
24
- expect(client.options.databaseURL).toBe('url');
25
- });
26
- test('throws if mandatory options are missing', () => {
27
- // @ts-expect-error Options are mandatory in TypeScript
28
- expect(() => buildClient({ apiKey: null }, {})).toThrow('Options databaseURL, apiKey and branch are required');
29
- // @ts-expect-error Options are mandatory in TypeScript
30
- expect(() => buildClient({ databaseURL: null }, {})).toThrow('Options databaseURL, apiKey and branch are required');
31
- // @ts-expect-error Options are mandatory in TypeScript
32
- expect(() => buildClient({ branch: null }, {})).toThrow('Options databaseURL, apiKey and branch are required');
33
- });
34
- test('throws if branch cannot be resolved', () => {
35
- const { users } = buildClient({ branch: () => null });
36
- expect(users.request('GET', '/foo')).rejects.toThrow('Unable to resolve branch value');
37
- });
38
- test('provide branch as a string', () => __awaiter(void 0, void 0, void 0, function* () {
39
- const { fetch, users } = buildClient({ branch: 'branch' });
40
- fetch.mockReset().mockImplementation(() => {
41
- return {
42
- ok: true,
43
- json: () => __awaiter(void 0, void 0, void 0, function* () { return ({}); })
44
- };
45
- });
46
- yield users.request('GET', '/foo');
47
- expect(fetch).toHaveBeenCalledTimes(1);
48
- expect(fetch.mock.calls[0]).toMatchInlineSnapshot(`
49
- Array [
50
- "https://my-workspace-5df34do.staging.xatabase.co/db/xata:branch/foo",
51
- Object {
52
- "body": undefined,
53
- "headers": Object {
54
- "Accept": "*/*",
55
- "Authorization": "Bearer 1234",
56
- "Content-Type": "application/json",
57
- },
58
- "method": "GET",
59
- },
60
- ]
61
- `);
62
- }));
63
- test('provide branch as an array', () => __awaiter(void 0, void 0, void 0, function* () {
64
- const { fetch, users } = buildClient({
65
- branch: [process.env.NOT_DEFINED_VARIABLE, () => __awaiter(void 0, void 0, void 0, function* () { return null; }), 'branch', 'main']
66
- });
67
- fetch.mockReset().mockImplementation(() => {
68
- return {
69
- ok: true,
70
- json: () => __awaiter(void 0, void 0, void 0, function* () { return ({}); })
71
- };
72
- });
73
- yield users.request('GET', '/foo');
74
- expect(fetch).toHaveBeenCalledTimes(1);
75
- expect(fetch.mock.calls[0]).toMatchInlineSnapshot(`
76
- Array [
77
- "https://my-workspace-5df34do.staging.xatabase.co/db/xata:branch/foo",
78
- Object {
79
- "body": undefined,
80
- "headers": Object {
81
- "Accept": "*/*",
82
- "Authorization": "Bearer 1234",
83
- "Content-Type": "application/json",
84
- },
85
- "method": "GET",
86
- },
87
- ]
88
- `);
89
- }));
90
- test('provide branch as a function', () => __awaiter(void 0, void 0, void 0, function* () {
91
- const { fetch, users } = buildClient({ branch: () => 'branch' });
92
- fetch.mockReset().mockImplementation(() => {
93
- return {
94
- ok: true,
95
- json: () => __awaiter(void 0, void 0, void 0, function* () { return ({}); })
96
- };
97
- });
98
- yield users.request('GET', '/foo');
99
- expect(fetch).toHaveBeenCalledTimes(1);
100
- expect(fetch.mock.calls[0]).toMatchInlineSnapshot(`
101
- Array [
102
- "https://my-workspace-5df34do.staging.xatabase.co/db/xata:branch/foo",
103
- Object {
104
- "body": undefined,
105
- "headers": Object {
106
- "Accept": "*/*",
107
- "Authorization": "Bearer 1234",
108
- "Content-Type": "application/json",
109
- },
110
- "method": "GET",
111
- },
112
- ]
113
- `);
114
- }));
115
- test('ensure branch resolution is memoized', () => __awaiter(void 0, void 0, void 0, function* () {
116
- const branchGetter = jest.fn(() => 'branch');
117
- const { fetch, users } = buildClient({ branch: branchGetter });
118
- fetch.mockReset().mockImplementation(() => {
119
- return {
120
- ok: true,
121
- json: () => __awaiter(void 0, void 0, void 0, function* () { return ({}); })
122
- };
123
- });
124
- yield users.request('GET', '/foo');
125
- yield users.request('GET', '/foo');
126
- expect(branchGetter).toHaveBeenCalledTimes(1);
127
- }));
128
- });
129
- describe('request', () => {
130
- test('builds the right arguments for a GET request', () => __awaiter(void 0, void 0, void 0, function* () {
131
- const { fetch, users } = buildClient();
132
- fetch.mockReset().mockImplementation(() => {
133
- return {
134
- ok: true,
135
- json: () => __awaiter(void 0, void 0, void 0, function* () { return ({}); })
136
- };
137
- });
138
- yield users.request('GET', '/foo');
139
- expect(fetch).toHaveBeenCalledTimes(1);
140
- expect(fetch.mock.calls[0]).toMatchInlineSnapshot(`
141
- Array [
142
- "https://my-workspace-5df34do.staging.xatabase.co/db/xata:main/foo",
143
- Object {
144
- "body": undefined,
145
- "headers": Object {
146
- "Accept": "*/*",
147
- "Authorization": "Bearer 1234",
148
- "Content-Type": "application/json",
149
- },
150
- "method": "GET",
151
- },
152
- ]
153
- `);
154
- }));
155
- test('builds the right arguments for a POST request', () => __awaiter(void 0, void 0, void 0, function* () {
156
- const { fetch, users } = buildClient();
157
- fetch.mockReset().mockImplementation(() => {
158
- return {
159
- ok: true,
160
- json: () => __awaiter(void 0, void 0, void 0, function* () { return ({}); })
161
- };
162
- });
163
- yield users.request('POST', '/foo', { a: 1 });
164
- expect(fetch).toHaveBeenCalledTimes(1);
165
- expect(fetch.mock.calls[0]).toMatchInlineSnapshot(`
166
- Array [
167
- "https://my-workspace-5df34do.staging.xatabase.co/db/xata:main/foo",
168
- Object {
169
- "body": "{\\"a\\":1}",
170
- "headers": Object {
171
- "Accept": "*/*",
172
- "Authorization": "Bearer 1234",
173
- "Content-Type": "application/json",
174
- },
175
- "method": "POST",
176
- },
177
- ]
178
- `);
179
- }));
180
- test('throws if the response is not ok', () => __awaiter(void 0, void 0, void 0, function* () {
181
- const { fetch, users } = buildClient();
182
- fetch.mockImplementation(() => {
183
- return {
184
- ok: false,
185
- status: 404,
186
- statusText: 'Not Found'
187
- };
188
- });
189
- expect(users.request('GET', '/foo')).rejects.toThrow(new _1.XataError('Not Found', 404));
190
- }));
191
- test('throws with the error from the server if the response is not ok', () => __awaiter(void 0, void 0, void 0, function* () {
192
- const { fetch, users } = buildClient();
193
- fetch.mockImplementation(() => {
194
- return {
195
- ok: false,
196
- status: 404,
197
- statusText: 'Not Found',
198
- json: () => __awaiter(void 0, void 0, void 0, function* () { return ({ message: 'Resource not found' }); })
199
- };
200
- });
201
- expect(users.request('GET', '/foo')).rejects.toThrow(new _1.XataError('Resource not found', 404));
202
- }));
203
- test('returns the json body if the response is ok', () => __awaiter(void 0, void 0, void 0, function* () {
204
- const { fetch, users } = buildClient();
205
- const json = { a: 1 };
206
- fetch.mockImplementation(() => {
207
- return {
208
- ok: true,
209
- json: () => __awaiter(void 0, void 0, void 0, function* () { return json; })
210
- };
211
- });
212
- const result = yield users.request('GET', '/foo');
213
- expect(result).toEqual(json);
214
- }));
215
- });
216
- function expectRequest(users, expectedRequest, callback, response) {
217
- return __awaiter(this, void 0, void 0, function* () {
218
- const request = jest.fn(() => __awaiter(this, void 0, void 0, function* () { return response; }));
219
- users.request = request;
220
- yield callback();
221
- const { calls } = request.mock;
222
- expect(calls.length).toBe(1);
223
- const [method, path, body] = calls[0];
224
- expect(method).toBe(expectedRequest.method);
225
- expect(path).toBe(expectedRequest.path);
226
- expect(JSON.stringify(body)).toBe(JSON.stringify(expectedRequest.body));
227
- });
228
- }
229
- describe('query', () => {
230
- describe('getMany', () => {
231
- test('simple query', () => __awaiter(void 0, void 0, void 0, function* () {
232
- const { users } = buildClient();
233
- const expected = { method: 'POST', path: '/tables/users/query', body: {} };
234
- expectRequest(users, expected, () => users.getMany(), { records: [] });
235
- }));
236
- test('query with one filter', () => __awaiter(void 0, void 0, void 0, function* () {
237
- const { users } = buildClient();
238
- const expected = { method: 'POST', path: '/tables/users/query', body: { filter: { $all: [{ name: 'foo' }] } } };
239
- expectRequest(users, expected, () => users.filter('name', 'foo').getMany(), { records: [] });
240
- }));
241
- });
242
- describe('getOne', () => {
243
- test('returns a single object', () => __awaiter(void 0, void 0, void 0, function* () {
244
- const { users } = buildClient();
245
- const result = { records: [{ id: '1234' }] };
246
- const expected = { method: 'POST', path: '/tables/users/query', body: {} };
247
- expectRequest(users, expected, () => __awaiter(void 0, void 0, void 0, function* () {
248
- const first = yield users.select().getOne();
249
- expect(first === null || first === void 0 ? void 0 : first.id).toBe(result.records[0].id);
250
- }), result);
251
- }));
252
- test('returns null if no objects are returned', () => __awaiter(void 0, void 0, void 0, function* () {
253
- const { users } = buildClient();
254
- const result = { records: [] };
255
- const expected = { method: 'POST', path: '/tables/users/query', body: {} };
256
- expectRequest(users, expected, () => __awaiter(void 0, void 0, void 0, function* () {
257
- const first = yield users.getOne();
258
- expect(first).toBeNull();
259
- }), result);
260
- }));
261
- });
262
- });
263
- describe('read', () => {
264
- test('reads an object by id successfully', () => __awaiter(void 0, void 0, void 0, function* () {
265
- const { users } = buildClient();
266
- const id = 'rec_1234';
267
- const expected = { method: 'GET', path: `/tables/users/data/${id}`, body: undefined };
268
- expectRequest(users, expected, () => users.read(id));
269
- }));
270
- });
271
- describe('Repository.update', () => {
272
- test('updates and object successfully', () => __awaiter(void 0, void 0, void 0, function* () {
273
- const { users } = buildClient();
274
- const object = { id: 'rec_1234', xata: { version: 1 }, name: 'Ada' };
275
- const expected = { method: 'PUT', path: `/tables/users/data/${object.id}`, body: object };
276
- expectRequest(users, expected, () => __awaiter(void 0, void 0, void 0, function* () {
277
- const result = yield users.update(object.id, object);
278
- expect(result.id).toBe(object.id);
279
- }), { id: object.id });
280
- }));
281
- });
282
- describe('Repository.delete', () => {
283
- test('deletes a record by id successfully', () => __awaiter(void 0, void 0, void 0, function* () {
284
- const { users } = buildClient();
285
- const id = 'rec_1234';
286
- const expected = { method: 'DELETE', path: `/tables/users/data/${id}`, body: undefined };
287
- expectRequest(users, expected, () => __awaiter(void 0, void 0, void 0, function* () {
288
- const result = yield users.delete(id);
289
- expect(result).toBe(undefined);
290
- }));
291
- }));
292
- });
293
- describe('create', () => {
294
- test('successful', () => __awaiter(void 0, void 0, void 0, function* () {
295
- const { users } = buildClient();
296
- const created = { id: 'rec_1234', _version: 0 };
297
- const object = { name: 'Ada' };
298
- const expected = { method: 'POST', path: '/tables/users/data', body: object };
299
- expectRequest(users, expected, () => __awaiter(void 0, void 0, void 0, function* () {
300
- const result = yield users.create(object);
301
- expect(result.id).toBe(created.id);
302
- }), created);
303
- }));
304
- });
package/src/index.test.ts DELETED
@@ -1,392 +0,0 @@
1
- import { BaseClient, RestRepository, XataClientOptions, XataError, XataRecord } from './';
2
-
3
- interface User extends XataRecord {
4
- name: string;
5
- }
6
-
7
- const buildClient = (options: Partial<XataClientOptions> = {}) => {
8
- const {
9
- apiKey = '1234',
10
- databaseURL = 'https://my-workspace-5df34do.staging.xatabase.co/db/xata',
11
- branch = 'main'
12
- } = options;
13
-
14
- const fetch = jest.fn();
15
- const client = new BaseClient({ fetch, apiKey, databaseURL, branch }, {});
16
- const users = new RestRepository<User>(client, 'users');
17
-
18
- return { fetch, client, users };
19
- };
20
-
21
- describe('client options', () => {
22
- test('option parameters are set', () => {
23
- const { client } = buildClient({ apiKey: 'apiKey', databaseURL: 'url' });
24
- expect(client.options.apiKey).toBe('apiKey');
25
- expect(client.options.databaseURL).toBe('url');
26
- });
27
-
28
- test('throws if mandatory options are missing', () => {
29
- // @ts-expect-error Options are mandatory in TypeScript
30
- expect(() => buildClient({ apiKey: null }, {})).toThrow('Options databaseURL, apiKey and branch are required');
31
-
32
- // @ts-expect-error Options are mandatory in TypeScript
33
- expect(() => buildClient({ databaseURL: null }, {})).toThrow('Options databaseURL, apiKey and branch are required');
34
-
35
- // @ts-expect-error Options are mandatory in TypeScript
36
- expect(() => buildClient({ branch: null }, {})).toThrow('Options databaseURL, apiKey and branch are required');
37
- });
38
-
39
- test('throws if branch cannot be resolved', () => {
40
- const { users } = buildClient({ branch: () => null });
41
-
42
- expect(users.request('GET', '/foo')).rejects.toThrow('Unable to resolve branch value');
43
- });
44
-
45
- test('provide branch as a string', async () => {
46
- const { fetch, users } = buildClient({ branch: 'branch' });
47
-
48
- fetch.mockReset().mockImplementation(() => {
49
- return {
50
- ok: true,
51
- json: async () => ({})
52
- };
53
- });
54
-
55
- await users.request('GET', '/foo');
56
-
57
- expect(fetch).toHaveBeenCalledTimes(1);
58
- expect(fetch.mock.calls[0]).toMatchInlineSnapshot(`
59
- Array [
60
- "https://my-workspace-5df34do.staging.xatabase.co/db/xata:branch/foo",
61
- Object {
62
- "body": undefined,
63
- "headers": Object {
64
- "Accept": "*/*",
65
- "Authorization": "Bearer 1234",
66
- "Content-Type": "application/json",
67
- },
68
- "method": "GET",
69
- },
70
- ]
71
- `);
72
- });
73
-
74
- test('provide branch as an array', async () => {
75
- const { fetch, users } = buildClient({
76
- branch: [process.env.NOT_DEFINED_VARIABLE, async () => null, 'branch', 'main']
77
- });
78
-
79
- fetch.mockReset().mockImplementation(() => {
80
- return {
81
- ok: true,
82
- json: async () => ({})
83
- };
84
- });
85
-
86
- await users.request('GET', '/foo');
87
-
88
- expect(fetch).toHaveBeenCalledTimes(1);
89
- expect(fetch.mock.calls[0]).toMatchInlineSnapshot(`
90
- Array [
91
- "https://my-workspace-5df34do.staging.xatabase.co/db/xata:branch/foo",
92
- Object {
93
- "body": undefined,
94
- "headers": Object {
95
- "Accept": "*/*",
96
- "Authorization": "Bearer 1234",
97
- "Content-Type": "application/json",
98
- },
99
- "method": "GET",
100
- },
101
- ]
102
- `);
103
- });
104
-
105
- test('provide branch as a function', async () => {
106
- const { fetch, users } = buildClient({ branch: () => 'branch' });
107
-
108
- fetch.mockReset().mockImplementation(() => {
109
- return {
110
- ok: true,
111
- json: async () => ({})
112
- };
113
- });
114
-
115
- await users.request('GET', '/foo');
116
-
117
- expect(fetch).toHaveBeenCalledTimes(1);
118
- expect(fetch.mock.calls[0]).toMatchInlineSnapshot(`
119
- Array [
120
- "https://my-workspace-5df34do.staging.xatabase.co/db/xata:branch/foo",
121
- Object {
122
- "body": undefined,
123
- "headers": Object {
124
- "Accept": "*/*",
125
- "Authorization": "Bearer 1234",
126
- "Content-Type": "application/json",
127
- },
128
- "method": "GET",
129
- },
130
- ]
131
- `);
132
- });
133
-
134
- test('ensure branch resolution is memoized', async () => {
135
- const branchGetter = jest.fn(() => 'branch');
136
-
137
- const { fetch, users } = buildClient({ branch: branchGetter });
138
-
139
- fetch.mockReset().mockImplementation(() => {
140
- return {
141
- ok: true,
142
- json: async () => ({})
143
- };
144
- });
145
-
146
- await users.request('GET', '/foo');
147
- await users.request('GET', '/foo');
148
-
149
- expect(branchGetter).toHaveBeenCalledTimes(1);
150
- });
151
- });
152
-
153
- describe('request', () => {
154
- test('builds the right arguments for a GET request', async () => {
155
- const { fetch, users } = buildClient();
156
-
157
- fetch.mockReset().mockImplementation(() => {
158
- return {
159
- ok: true,
160
- json: async () => ({})
161
- };
162
- });
163
-
164
- await users.request('GET', '/foo');
165
-
166
- expect(fetch).toHaveBeenCalledTimes(1);
167
- expect(fetch.mock.calls[0]).toMatchInlineSnapshot(`
168
- Array [
169
- "https://my-workspace-5df34do.staging.xatabase.co/db/xata:main/foo",
170
- Object {
171
- "body": undefined,
172
- "headers": Object {
173
- "Accept": "*/*",
174
- "Authorization": "Bearer 1234",
175
- "Content-Type": "application/json",
176
- },
177
- "method": "GET",
178
- },
179
- ]
180
- `);
181
- });
182
-
183
- test('builds the right arguments for a POST request', async () => {
184
- const { fetch, users } = buildClient();
185
-
186
- fetch.mockReset().mockImplementation(() => {
187
- return {
188
- ok: true,
189
- json: async () => ({})
190
- };
191
- });
192
-
193
- await users.request('POST', '/foo', { a: 1 });
194
-
195
- expect(fetch).toHaveBeenCalledTimes(1);
196
- expect(fetch.mock.calls[0]).toMatchInlineSnapshot(`
197
- Array [
198
- "https://my-workspace-5df34do.staging.xatabase.co/db/xata:main/foo",
199
- Object {
200
- "body": "{\\"a\\":1}",
201
- "headers": Object {
202
- "Accept": "*/*",
203
- "Authorization": "Bearer 1234",
204
- "Content-Type": "application/json",
205
- },
206
- "method": "POST",
207
- },
208
- ]
209
- `);
210
- });
211
-
212
- test('throws if the response is not ok', async () => {
213
- const { fetch, users } = buildClient();
214
-
215
- fetch.mockImplementation(() => {
216
- return {
217
- ok: false,
218
- status: 404,
219
- statusText: 'Not Found'
220
- };
221
- });
222
-
223
- expect(users.request('GET', '/foo')).rejects.toThrow(new XataError('Not Found', 404));
224
- });
225
-
226
- test('throws with the error from the server if the response is not ok', async () => {
227
- const { fetch, users } = buildClient();
228
-
229
- fetch.mockImplementation(() => {
230
- return {
231
- ok: false,
232
- status: 404,
233
- statusText: 'Not Found',
234
- json: async () => ({ message: 'Resource not found' })
235
- };
236
- });
237
-
238
- expect(users.request('GET', '/foo')).rejects.toThrow(new XataError('Resource not found', 404));
239
- });
240
-
241
- test('returns the json body if the response is ok', async () => {
242
- const { fetch, users } = buildClient();
243
-
244
- const json = { a: 1 };
245
- fetch.mockImplementation(() => {
246
- return {
247
- ok: true,
248
- json: async () => json
249
- };
250
- });
251
-
252
- const result = await users.request('GET', '/foo');
253
- expect(result).toEqual(json);
254
- });
255
- });
256
-
257
- type ExpectedRequest = {
258
- method: string;
259
- path: string;
260
- body: unknown;
261
- };
262
-
263
- async function expectRequest(
264
- users: RestRepository<User>,
265
- expectedRequest: ExpectedRequest,
266
- callback: () => void,
267
- response?: unknown
268
- ) {
269
- const request = jest.fn(async () => response);
270
- users.request = request;
271
-
272
- await callback();
273
-
274
- const { calls } = request.mock;
275
- expect(calls.length).toBe(1);
276
- const [method, path, body] = calls[0] as any;
277
- expect(method).toBe(expectedRequest.method);
278
- expect(path).toBe(expectedRequest.path);
279
- expect(JSON.stringify(body)).toBe(JSON.stringify(expectedRequest.body));
280
- }
281
-
282
- describe('query', () => {
283
- describe('getMany', () => {
284
- test('simple query', async () => {
285
- const { users } = buildClient();
286
-
287
- const expected = { method: 'POST', path: '/tables/users/query', body: {} };
288
- expectRequest(users, expected, () => users.getMany(), { records: [] });
289
- });
290
-
291
- test('query with one filter', async () => {
292
- const { users } = buildClient();
293
-
294
- const expected = { method: 'POST', path: '/tables/users/query', body: { filter: { $all: [{ name: 'foo' }] } } };
295
- expectRequest(users, expected, () => users.filter('name', 'foo').getMany(), { records: [] });
296
- });
297
- });
298
-
299
- describe('getOne', () => {
300
- test('returns a single object', async () => {
301
- const { users } = buildClient();
302
-
303
- const result = { records: [{ id: '1234' }] };
304
- const expected = { method: 'POST', path: '/tables/users/query', body: {} };
305
- expectRequest(
306
- users,
307
- expected,
308
- async () => {
309
- const first = await users.select().getOne();
310
- expect(first?.id).toBe(result.records[0].id);
311
- },
312
- result
313
- );
314
- });
315
-
316
- test('returns null if no objects are returned', async () => {
317
- const { users } = buildClient();
318
-
319
- const result = { records: [] };
320
- const expected = { method: 'POST', path: '/tables/users/query', body: {} };
321
- expectRequest(
322
- users,
323
- expected,
324
- async () => {
325
- const first = await users.getOne();
326
- expect(first).toBeNull();
327
- },
328
- result
329
- );
330
- });
331
- });
332
- });
333
-
334
- describe('read', () => {
335
- test('reads an object by id successfully', async () => {
336
- const { users } = buildClient();
337
-
338
- const id = 'rec_1234';
339
- const expected = { method: 'GET', path: `/tables/users/data/${id}`, body: undefined };
340
- expectRequest(users, expected, () => users.read(id));
341
- });
342
- });
343
-
344
- describe('Repository.update', () => {
345
- test('updates and object successfully', async () => {
346
- const { users } = buildClient();
347
-
348
- const object = { id: 'rec_1234', xata: { version: 1 }, name: 'Ada' } as User;
349
- const expected = { method: 'PUT', path: `/tables/users/data/${object.id}`, body: object };
350
- expectRequest(
351
- users,
352
- expected,
353
- async () => {
354
- const result = await users.update(object.id, object);
355
- expect(result.id).toBe(object.id);
356
- },
357
- { id: object.id }
358
- );
359
- });
360
- });
361
-
362
- describe('Repository.delete', () => {
363
- test('deletes a record by id successfully', async () => {
364
- const { users } = buildClient();
365
-
366
- const id = 'rec_1234';
367
- const expected = { method: 'DELETE', path: `/tables/users/data/${id}`, body: undefined };
368
- expectRequest(users, expected, async () => {
369
- const result = await users.delete(id);
370
- expect(result).toBe(undefined);
371
- });
372
- });
373
- });
374
-
375
- describe('create', () => {
376
- test('successful', async () => {
377
- const { users } = buildClient();
378
-
379
- const created = { id: 'rec_1234', _version: 0 };
380
- const object = { name: 'Ada' } as User;
381
- const expected = { method: 'POST', path: '/tables/users/data', body: object };
382
- expectRequest(
383
- users,
384
- expected,
385
- async () => {
386
- const result = await users.create(object);
387
- expect(result.id).toBe(created.id);
388
- },
389
- created
390
- );
391
- });
392
- });