Skip to content

Commit a57428b

Browse files
committed
Integrated into queryBuilder
1 parent ff5f481 commit a57428b

File tree

3 files changed

+83
-206
lines changed

3 files changed

+83
-206
lines changed

src/batch.ts

Lines changed: 0 additions & 194 deletions
This file was deleted.

src/drivers/default/builders/queryBuilder.ts

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,23 @@ export class QueryBuilder<
104104
return this.hydrate(response.data.data, response);
105105
}
106106

107+
public async batchStore(items: M[]): Promise<M[]> {
108+
const data = {
109+
resources: items.map(x => x.$attributes)
110+
};
111+
112+
const response = await this.httpClient.request<{data: Array<AllAttributes & Relations> }>(
113+
`/batch`,
114+
HttpMethod.POST,
115+
null,
116+
data
117+
);
118+
119+
return response.data.data.map((attributes) => {
120+
return this.hydrate(attributes, response);
121+
})
122+
}
123+
107124
public async update(key: Key, attributes: Attributes): Promise<M> {
108125
const response = await this.httpClient.request<{ data: AllAttributes & Relations }>(
109126
`/${key}`,
@@ -115,6 +132,24 @@ export class QueryBuilder<
115132
return this.hydrate(response.data.data, response);
116133
}
117134

135+
public async batchUpdate(items: M[]): Promise<M[]> {
136+
const data = {
137+
resources: {}
138+
};
139+
items.forEach((v) => data.resources[v.$getKey()] = v.$attributes);
140+
141+
const response = await this.httpClient.request<{ data: Array< AllAttributes & Relations > }>(
142+
`batch`,
143+
HttpMethod.PATCH,
144+
null,
145+
data
146+
)
147+
148+
return response.data.data.map((attributes: AllAttributes & Relations) => {
149+
return this.hydrate(attributes, response);
150+
});
151+
}
152+
118153
public async destroy(key: Key, force: boolean = false): Promise<M> {
119154
const response = await this.httpClient.request<{ data: AllAttributes & Relations }>(
120155
`/${key}`,
@@ -125,6 +160,27 @@ export class QueryBuilder<
125160
return this.hydrate(response.data.data, response);
126161
}
127162

163+
public async batchDelete(items: Key[]): Promise<M[]>
164+
{
165+
if (!items.length)
166+
return [];
167+
168+
const data = {
169+
resources: items
170+
};
171+
172+
const response = await this.httpClient.request<{ data: Array< AllAttributes & Relations > }>(
173+
`/batch`,
174+
HttpMethod.DELETE,
175+
null,
176+
data
177+
);
178+
179+
return response.data.data.map((attributes: AllAttributes & Relations) => {
180+
return this.hydrate(attributes, response);
181+
});
182+
}
183+
128184
public async restore(key: Key): Promise<M> {
129185
const response = await this.httpClient.request<{ data: AllAttributes & Relations }>(
130186
`/${key}/restore`,
@@ -135,6 +191,24 @@ export class QueryBuilder<
135191
return this.hydrate(response.data.data, response);
136192
}
137193

194+
public async batchRestore(items: Key[]): Promise<M[]> {
195+
const data = {
196+
resources: items
197+
};
198+
199+
const response = await this.httpClient.request<{ data: Array< AllAttributes & Relations > }>(
200+
`/batch/restore`,
201+
HttpMethod.POST,
202+
null,
203+
data
204+
);
205+
206+
return response.data.data.map((attributes: AllAttributes & Relations) => {
207+
return this.hydrate(attributes, response);
208+
});
209+
}
210+
211+
138212
public with(relations: string[]): this {
139213
this.includes = relations;
140214

tests/integration/batch.test.ts

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import makeServer from './drivers/default/server';
22
import Post from '../stubs/models/post';
3-
import { Batch } from '../../src/batch';
43
import { Orion } from '../../src/orion';
54

65
let server: any;
@@ -22,7 +21,7 @@ describe('Batch tests', () => {
2221
posts[0].$attributes.title = "First";
2322
posts[1].$attributes.title = "Second";
2423

25-
const res = await Batch.store(posts);
24+
const res = await Post.$query().batchStore(posts);
2625

2726
expect(server.schema.posts.all()).toHaveLength(2);
2827
expect(server.schema.posts.find('1').attrs.title).toBe("First")
@@ -43,12 +42,12 @@ describe('Batch tests', () => {
4342
posts[1].$attributes.title = "Second";
4443
posts[2].$attributes.title = "Third";
4544

46-
let res = await Batch.store(posts);
45+
let res = await Post.$query().batchStore(posts);
4746

4847
res[0].$attributes.title = "NewFirst";
4948
res[1].$attributes.title = "NewSecond";
5049

51-
res = await Batch.update([res[0],res[1]]);
50+
res = await Post.$query().batchUpdate([res[0],res[1]]);
5251

5352
expect(res).toHaveLength(2);
5453
expect(server.schema.posts.find('1').attrs.title).toBe("NewFirst")
@@ -69,16 +68,15 @@ describe('Batch tests', () => {
6968
posts[1].$attributes.title = "Second";
7069
posts[2].$attributes.title = "Third";
7170

72-
let res = await Batch.store(posts);
71+
let res = await Post.$query().batchStore(posts);
7372

74-
let ModelDelete = await Batch.delete([res[1]]);
75-
let idDelete = await Batch.delete([3], new Post);
73+
let ModelDelete = await Post.$query().batchDelete([res[1].$getKey(), res[2].$getKey()]);
7674

7775
expect(server.schema.posts.find('1').attrs.deleted_at).toBeUndefined();
7876
expect(server.schema.posts.find('2').attrs.deleted_at).toBeDefined();
7977
expect(server.schema.posts.find('3').attrs.deleted_at).toBeDefined();
8078
expect(server.schema.posts.find('2').attrs.title).toEqual(ModelDelete[0].$attributes.title)
81-
expect(server.schema.posts.find('3').attrs.title).toEqual(idDelete[0].$attributes.title)
79+
expect(server.schema.posts.find('3').attrs.title).toEqual(ModelDelete[1].$attributes.title)
8280

8381

8482
});
@@ -93,13 +91,12 @@ describe('Batch tests', () => {
9391
posts[1].$attributes.title = "Second";
9492
posts[2].$attributes.title = "Third";
9593

96-
let res = await Batch.store(posts);
94+
let res = await Post.$query().batchStore(posts);
9795

9896
// delete ID 2 & 3
99-
let ModelDelete = await Batch.delete([res[1]]);
100-
let idDelete = await Batch.delete([3], new Post);
97+
let ModelDelete = await Post.$query().batchDelete([res[1].$getKey(), res[2].$getKey()]);
10198

102-
res = await Batch.restore([...ModelDelete, ...idDelete]);
99+
res = await Post.$query().batchRestore(ModelDelete.map(x => x.$getKey()));
103100

104101
expect(server.schema.posts.find('1').attrs.deleted_at).toBeFalsy();
105102
expect(server.schema.posts.find('2').attrs.deleted_at).toBeFalsy();

0 commit comments

Comments
 (0)