@@ -5,55 +5,80 @@ const { ErrorHTTP422 } = require('./errors');
5
5
const ResourceGetter = require ( './resource-getter' ) ;
6
6
const CompositeKeysManager = require ( './composite-keys-manager' ) ;
7
7
8
- function ResourceCreator ( model , params ) {
9
- const schema = Interface . Schemas . schemas [ model . name ] ;
10
-
11
- this . perform = function perform ( ) {
12
- const promises = [ ] ;
13
- const recordCreated = model . build ( params ) ;
14
-
15
- if ( model . associations ) {
16
- _ . forOwn ( model . associations , ( association , name ) => {
17
- if ( association . associationType === 'BelongsTo' ) {
18
- promises . push ( recordCreated [ `set${ _ . upperFirst ( name ) } ` ] ( params [ name ] , { save : false } ) ) ;
19
- }
20
- } ) ;
8
+ class ResourceCreator {
9
+ constructor ( model , params ) {
10
+ this . model = model ;
11
+ this . params = params ;
12
+ this . schema = Interface . Schemas . schemas [ model . name ] ;
13
+ }
14
+
15
+ _makePromisesBeforeSave ( record ) {
16
+ return ( promises , [ name , association ] ) => {
17
+ if ( association . associationType === 'BelongsTo' ) {
18
+ const setterName = `set${ _ . upperFirst ( name ) } ` ;
19
+ const promise = record [ setterName ] ( this . params [ name ] , { save : false } ) ;
20
+ promises . push ( promise ) ;
21
+ }
22
+ return promises ;
23
+ } ;
24
+ }
25
+
26
+ _makePromisesAfterSave ( record ) {
27
+ return ( promises , [ name , association ] ) => {
28
+ let setterName ;
29
+ if ( association . associationType === 'HasOne' ) {
30
+ setterName = `set${ _ . upperFirst ( name ) } ` ;
31
+ } else if ( [ 'BelongsToMany' , 'HasMany' ] . includes ( association . associationType ) ) {
32
+ setterName = `add${ _ . upperFirst ( name ) } ` ;
33
+ }
34
+ if ( setterName ) {
35
+ const promise = record [ setterName ] ( this . params [ name ] ) ;
36
+ promises . push ( promise ) ;
37
+ }
38
+ return promises ;
39
+ } ;
40
+ }
41
+
42
+ async _handleSave ( record , callback ) {
43
+ const { associations } = this . model ;
44
+ if ( associations ) {
45
+ callback = callback . bind ( this ) ;
46
+ const promisesBeforeSave = Object . entries ( associations ) . reduce ( callback ( record ) , [ ] ) ;
47
+ await P . all ( promisesBeforeSave ) ;
48
+ }
49
+ }
50
+
51
+ async perform ( ) {
52
+ // buildInstance
53
+ const recordCreated = this . model . build ( this . params ) ;
54
+
55
+ // handleAssociationsBeforeSave
56
+ await this . _handleSave ( recordCreated , this . _makePromisesBeforeSave ) ;
57
+
58
+ // saveInstance (validate then save)
59
+ try {
60
+ await recordCreated . validate ( ) ;
61
+ } catch ( error ) {
62
+ throw new ErrorHTTP422 ( error . message ) ;
63
+ }
64
+ const record = await recordCreated . save ( ) ;
65
+
66
+ // handleAssociationsAfterSave
67
+ // NOTICE: Many to many associations have to be set after the record creation in order to
68
+ // have an id.
69
+ await this . _handleSave ( record , this . _makePromisesAfterSave ) ;
70
+
71
+ // appendCompositePrimary
72
+ if ( this . schema . isCompositePrimary ) {
73
+ record . forestCompositePrimary = new CompositeKeysManager ( this . model , this . schema , record )
74
+ . createCompositePrimary ( ) ;
21
75
}
22
76
23
- return P . all ( promises )
24
- . then ( ( ) => recordCreated . validate ( )
25
- . catch ( ( error ) => {
26
- throw new ErrorHTTP422 ( error . message ) ;
27
- } ) )
28
- . then ( ( ) => recordCreated . save ( ) )
29
- . then ( ( record ) => {
30
- const promisesAfterSave = [ ] ;
31
-
32
- // NOTICE: Many to many associations have to be set after the record creation in order to
33
- // have an id.
34
- if ( model . associations ) {
35
- _ . forOwn ( model . associations , ( association , name ) => {
36
- if ( association . associationType === 'HasOne' ) {
37
- promisesAfterSave . push ( record [ `set${ _ . upperFirst ( name ) } ` ] ( params [ name ] ) ) ;
38
- } else if ( [ 'BelongsToMany' , 'HasMany' ] . indexOf ( association . associationType ) > - 1 ) {
39
- promisesAfterSave . push ( record [ `add${ _ . upperFirst ( name ) } ` ] ( params [ name ] ) ) ;
40
- }
41
- } ) ;
42
- }
43
-
44
- return P . all ( promisesAfterSave )
45
- . thenReturn ( record ) ;
46
- } )
47
- . then ( ( record ) => {
48
- if ( schema . isCompositePrimary ) {
49
- record . forestCompositePrimary = new CompositeKeysManager ( model , schema , record )
50
- . createCompositePrimary ( ) ;
51
- }
52
- return new ResourceGetter ( model , {
53
- recordId : record [ schema . idField ] ,
54
- } ) . perform ( ) ;
55
- } ) ;
56
- } ;
77
+ // return makeResourceGetter()
78
+ return new ResourceGetter ( this . model , {
79
+ recordId : record [ this . schema . idField ] ,
80
+ } ) . perform ( ) ;
81
+ }
57
82
}
58
83
59
84
module . exports = ResourceCreator ;
0 commit comments