1- import { HttpMethod } from "./drivers/default/enums/httpMethod" ;
2- import { Model } from "./model" ;
3- import { Orion } from "./orion" ;
4-
5- export class Batch
6- {
7- private static $httpClient ( ) {
8- const httpClient = Orion . makeHttpClient ( Orion . getBaseUrl ( ) ) ;
9-
10- return httpClient ;
11- }
12-
13- public static async store < M extends Model > ( items : M [ ] )
14- {
15- if ( ! items . length )
16- return ;
17-
18- const client = this . $httpClient ( ) ;
19- const url = items [ 0 ] . $resource ( ) ;
20-
21- const data = {
22- resources : items . map ( x => x . $attributes )
23- } ;
24-
25- return client . request (
26- `${ url } /batch` ,
27- HttpMethod . POST ,
28- null ,
29- data
30- )
31- }
32-
33- public static async update < M extends Model > ( items : M [ ] )
34- {
35- if ( ! items . length )
36- return ;
37-
38- const client = this . $httpClient ( ) ;
39- const url = items [ 0 ] . $resource ( ) ;
40-
41- const data = {
42- resources : { }
43- } ;
44- items . forEach ( ( v , i ) => data . resources [ v . $getKey ( ) ] = v . $attributes ) ;
45-
46- return client . request (
47- `${ url } /batch` ,
48- HttpMethod . PATCH ,
49- null ,
50- data
51- )
52- }
53-
54- public static async delete < M extends Model > ( items : number [ ] | M [ ] , baseUrl ?: string )
55- {
56- const { ids, url} = this . getBatchIds ( items ) ;
57- const data = {
58- resources : ids
59- } ;
60- const finalUrl = url || baseUrl ;
61- const client = this . $httpClient ( ) ;
62-
63-
64- if ( ! finalUrl )
65- throw "BuilderError: url not found"
66- return client . request (
67- `${ finalUrl } /batch` ,
68- HttpMethod . DELETE ,
69- data
70- )
71- }
72-
73-
74- public static async restore < M extends Model > ( items : number [ ] | M [ ] , baseUrl ?: string )
75- {
76- const { ids, url} = this . getBatchIds ( items ) ;
77- const data = {
78- resources : ids
79- } ;
80- const finalUrl = url || baseUrl ;
81- const client = this . $httpClient ( ) ;
82-
83-
84- if ( ! finalUrl )
85- throw "BuilderError: url not found"
86- return client . request (
87- `${ finalUrl } /batch/restore` ,
88- HttpMethod . POST ,
89- data
90- )
91- }
92-
93- private static getBatchIds < M extends Model > ( items : number [ ] | M [ ] ) : { ids : unknown [ ] , url : string | undefined } {
94- let foundUrl : string | undefined = undefined ;
95-
96- let ids = items . map ( ( x : number | M ) => {
97- // also find the url while we're at it
98- if ( typeof ( x ) == 'object' && x . $resource ( ) && foundUrl == undefined ) {
99- foundUrl = x . $resource ( ) ;
100- }
101-
102- return typeof ( x ) == 'number' ? x : x . $attributes [ x . $getKeyName ( ) ]
103- } )
104-
105- return {
106- ids,
107- url : foundUrl ,
108- }
109- }
1+ import { HttpMethod } from "./drivers/default/enums/httpMethod" ;
2+ import { Model } from "./model" ;
3+ import { Orion } from "./orion" ;
4+ import { ExtractModelAttributesType } from "./types/extractModelAttributesType" ;
5+ import { ExtractModelAllAttributesType } from "./types/extractModelAllAttributesType" ;
6+ import { ExtractModelRelationsType } from "./types/extractModelRelationsType" ;
7+ import { ModelConstructor } from "./contracts/modelConstructor" ;
8+ import { QueryBuilder } from "./drivers/default/builders/queryBuilder" ;
9+ import { ExtractModelPersistedAttributesType } from "./types/extractModelPersistedAttributesType" ;
10+ import { ExtractModelKeyType } from "./types/extractModelKeyType" ;
11+ import { UrlBuilder } from "./builders/urlBuilder" ;
12+
13+ type HydrateAttributes < M extends Model > = ExtractModelAttributesType < M > & ExtractModelPersistedAttributesType < M > & ExtractModelRelationsType < M > ;
14+
15+
16+ export class Batch
17+ {
18+ private static $httpClient < M extends Model > (
19+ modelConstructor : ModelConstructor < M >
20+ ) {
21+ let baseUrl = ''
22+ if ( Orion . getBaseUrl ( ) ) {
23+ baseUrl = Orion . getBaseUrl ( ) ;
24+ } else {
25+ baseUrl = UrlBuilder . getResourceBaseUrl ( modelConstructor ) ;
26+ }
27+ const httpClient = Orion . makeHttpClient ( baseUrl ) ;
28+
29+ return httpClient ;
30+ }
31+
32+ public static async store <
33+ M extends Model ,
34+ Attributes = ExtractModelAttributesType < M > ,
35+ PersistedAttributes = ExtractModelPersistedAttributesType < M > ,
36+ Relations = ExtractModelRelationsType < M > ,
37+ AllAttributes = Attributes & PersistedAttributes
38+ > ( items : M [ ] ) : Promise < M [ ] >
39+ {
40+ if ( ! items . length )
41+ return [ ] ;
42+
43+ const client = this . $httpClient ( items [ 0 ] . constructor as ModelConstructor < M > ) ;
44+ const url = items [ 0 ] . $resource ( ) ;
45+
46+ const data = {
47+ resources : items . map ( x => x . $attributes )
48+ } ;
49+
50+ const response = await client . request < { data : Array < AllAttributes & Relations > } > (
51+ `${ url } /batch` ,
52+ HttpMethod . POST ,
53+ null ,
54+ data
55+ ) ;
56+
57+ return response . data . data . map ( ( attributes : AllAttributes & Relations ) => {
58+ const model : M = new ( items [ 0 ] . constructor as ModelConstructor < M > ) ( ) ;
59+ return ( model . $query ( ) as QueryBuilder < M > )
60+ . hydrate ( attributes as HydrateAttributes < M > , response )
61+ } ) ;
62+ }
63+
64+ public static async update <
65+ M extends Model ,
66+ Attributes = ExtractModelAttributesType < M > ,
67+ PersistedAttributes = ExtractModelPersistedAttributesType < M > ,
68+ Relations = ExtractModelRelationsType < M > ,
69+ AllAttributes = Attributes & PersistedAttributes
70+ > ( items : M [ ] ) : Promise < M [ ] >
71+ {
72+ if ( ! items . length )
73+ return [ ] ;
74+
75+ const client = this . $httpClient ( items [ 0 ] . constructor as ModelConstructor < M > ) ;
76+ const url = items [ 0 ] . $resource ( ) ;
77+
78+ const data = {
79+ resources : { }
80+ } ;
81+ items . forEach ( ( v , i ) => data . resources [ v . $getKey ( ) ] = v . $attributes ) ;
82+
83+ const response = await client . request < { data : Array < AllAttributes & Relations > } > (
84+ `${ url } /batch` ,
85+ HttpMethod . PATCH ,
86+ null ,
87+ data
88+ )
89+
90+ return response . data . data . map ( ( attributes : AllAttributes & Relations ) => {
91+ const model : M = new ( items [ 0 ] . constructor as ModelConstructor < M > ) ( ) ;
92+ return ( model . $query ( ) as QueryBuilder < M > )
93+ . hydrate ( attributes as HydrateAttributes < M > , response )
94+ } ) ;
95+ }
96+
97+
98+
99+ public static async delete < M extends Model > ( items : M [ ] ) : Promise < M [ ] > ;
100+ public static async delete < M extends Model > ( items : number [ ] , target : M ) : Promise < M [ ] > ;
101+ public static async delete <
102+ M extends Model ,
103+ Attributes = ExtractModelAttributesType < M > ,
104+ PersistedAttributes = ExtractModelPersistedAttributesType < M > ,
105+ Relations = ExtractModelRelationsType < M > ,
106+ AllAttributes = Attributes & PersistedAttributes ,
107+ > ( items : number [ ] | M [ ] , target ?: M ) : Promise < M [ ] >
108+ {
109+ const { ids, url, isModel} = this . getBatchIds ( items ) ;
110+ const finalUrl = url || target ?. $resource ( ) ;
111+ if ( ! finalUrl )
112+ throw "BuilderError: url not found"
113+
114+ const data = {
115+ resources : ids
116+ } ;
117+
118+ const client = this . $httpClient ( items [ 0 ] . constructor as ModelConstructor < M > ) ;
119+ const response = await client . request < { data : Array < AllAttributes & Relations > } > (
120+ `${ finalUrl } /batch` ,
121+ HttpMethod . DELETE ,
122+ null ,
123+ data
124+ ) ;
125+
126+ const modelConstructor = ( isModel ? items [ 0 ] . constructor : target ?. constructor ) as ModelConstructor < M >
127+ if ( ! modelConstructor )
128+ return [ ] ;
129+
130+ return response . data . data . map ( ( attributes : AllAttributes & Relations ) => {
131+ const model : M = new modelConstructor ( ) ;
132+ return ( model . $query ( ) as QueryBuilder < M > )
133+ . hydrate ( attributes as HydrateAttributes < M > , response )
134+ } ) ;
135+ }
136+
137+
138+ public static async restore < M extends Model > ( items : M [ ] ) : Promise < M [ ] > ;
139+ public static async restore < M extends Model > ( items : number [ ] , target : M ) : Promise < M [ ] > ;
140+ public static async restore <
141+ M extends Model ,
142+ Attributes = ExtractModelAttributesType < M > ,
143+ PersistedAttributes = ExtractModelPersistedAttributesType < M > ,
144+ Relations = ExtractModelRelationsType < M > ,
145+ AllAttributes = Attributes & PersistedAttributes ,
146+ > ( items : number [ ] | M [ ] , target ?: M ) : Promise < M [ ] >
147+ {
148+ const { ids, url, isModel} = this . getBatchIds ( items ) ;
149+ const data = {
150+ resources : ids
151+ } ;
152+ const finalUrl = url || target ?. $resource ( ) ;
153+ const client = this . $httpClient ( items [ 0 ] . constructor as ModelConstructor < M > ) ;
154+
155+
156+ if ( ! finalUrl )
157+ throw "BuilderError: url not found"
158+ const response = await client . request < { data : Array < AllAttributes & Relations > } > (
159+ `${ finalUrl } /batch/restore` ,
160+ HttpMethod . POST ,
161+ null ,
162+ data
163+ )
164+
165+ const modelConstructor = ( isModel ? items [ 0 ] . constructor : target ?. constructor ) as ModelConstructor < M >
166+ if ( ! modelConstructor )
167+ return [ ] ;
168+
169+ return response . data . data . map ( ( attributes : AllAttributes & Relations ) => {
170+ const model : M = new modelConstructor ( ) ;
171+ return ( model . $query ( ) as QueryBuilder < M > )
172+ . hydrate ( attributes as HydrateAttributes < M > , response )
173+ } ) ;
174+ }
175+
176+ private static getBatchIds < M extends Model > ( items : number [ ] | M [ ] ) : { ids : unknown [ ] , url : string | undefined , isModel : boolean } {
177+ let foundUrl : string | undefined = undefined ;
178+ let isModel = false ;
179+
180+ let ids = items . map ( ( x : number | M ) => {
181+ // also find the url while we're at it
182+ if ( typeof ( x ) == 'object' && x . $resource ( ) && foundUrl == undefined ) {
183+ foundUrl = x . $resource ( ) ;
184+ isModel = true ;
185+ }
186+
187+ return typeof ( x ) == 'number' ? x : x . $attributes [ x . $getKeyName ( ) ]
188+ } )
189+
190+ return {
191+ ids,
192+ url : foundUrl ,
193+ isModel
194+ }
195+ }
110196}
0 commit comments