1
1
// schemas.js
2
2
3
3
var express = require ( 'express' ) ,
4
- Parse = require ( 'parse/node' ) . Parse ,
5
- Schema = require ( '../Schema' ) ;
4
+ Parse = require ( 'parse/node' ) . Parse ,
5
+ Schema = require ( '../Schema' ) ;
6
6
7
7
import PromiseRouter from '../PromiseRouter' ;
8
8
@@ -49,26 +49,26 @@ function getAllSchemas(req) {
49
49
return masterKeyRequiredResponse ( ) ;
50
50
}
51
51
return req . config . database . collection ( '_SCHEMA' )
52
- . then ( coll => coll . find ( { } ) . toArray ( ) )
53
- . then ( schemas => ( { response : {
54
- results : schemas . map ( mongoSchemaToSchemaAPIResponse )
55
- } } ) ) ;
52
+ . then ( coll => coll . find ( { } ) . toArray ( ) )
53
+ . then ( schemas => ( { response : {
54
+ results : schemas . map ( mongoSchemaToSchemaAPIResponse )
55
+ } } ) ) ;
56
56
}
57
57
58
58
function getOneSchema ( req ) {
59
59
if ( ! req . auth . isMaster ) {
60
60
return masterKeyRequiredResponse ( ) ;
61
61
}
62
62
return req . config . database . collection ( '_SCHEMA' )
63
- . then ( coll => coll . findOne ( { '_id' : req . params . className } ) )
64
- . then ( schema => ( { response : mongoSchemaToSchemaAPIResponse ( schema ) } ) )
65
- . catch ( ( ) => ( {
66
- status : 400 ,
67
- response : {
68
- code : 103 ,
69
- error : 'class ' + req . params . className + ' does not exist' ,
70
- }
71
- } ) ) ;
63
+ . then ( coll => coll . findOne ( { '_id' : req . params . className } ) )
64
+ . then ( schema => ( { response : mongoSchemaToSchemaAPIResponse ( schema ) } ) )
65
+ . catch ( ( ) => ( {
66
+ status : 400 ,
67
+ response : {
68
+ code : 103 ,
69
+ error : 'class ' + req . params . className + ' does not exist' ,
70
+ }
71
+ } ) ) ;
72
72
}
73
73
74
74
function createSchema ( req ) {
@@ -91,12 +91,12 @@ function createSchema(req) {
91
91
} ) ;
92
92
}
93
93
return req . config . database . loadSchema ( )
94
- . then ( schema => schema . addClassIfNotExists ( className , req . body . fields ) )
95
- . then ( result => ( { response : mongoSchemaToSchemaAPIResponse ( result ) } ) )
96
- . catch ( error => ( {
97
- status : 400 ,
98
- response : error ,
99
- } ) ) ;
94
+ . then ( schema => schema . addClassIfNotExists ( className , req . body . fields ) )
95
+ . then ( result => ( { response : mongoSchemaToSchemaAPIResponse ( result ) } ) )
96
+ . catch ( error => ( {
97
+ status : 400 ,
98
+ response : error ,
99
+ } ) ) ;
100
100
}
101
101
102
102
function modifySchema ( req ) {
@@ -112,75 +112,48 @@ function modifySchema(req) {
112
112
var className = req . params . className ;
113
113
114
114
return req . config . database . loadSchema ( )
115
- . then ( schema => {
116
- if ( ! schema . data [ className ] ) {
117
- return Promise . resolve ( {
118
- status : 400 ,
119
- response : {
120
- code : Parse . Error . INVALID_CLASS_NAME ,
121
- error : 'class ' + req . params . className + ' does not exist' ,
115
+ . then ( schema => {
116
+ if ( ! schema . data [ className ] ) {
117
+ throw new Parse . Error ( Parse . Error . INVALID_CLASS_NAME , `Class ${ req . params . className } does not exist.` ) ;
118
+ }
119
+
120
+ let existingFields = schema . data [ className ] ;
121
+ Object . keys ( submittedFields ) . forEach ( name => {
122
+ let field = submittedFields [ name ] ;
123
+ if ( existingFields [ name ] && field . __op !== 'Delete' ) {
124
+ throw new Parse . Error ( 255 , `Field ${ name } exists, cannot update.` ) ;
125
+ }
126
+ if ( ! existingFields [ name ] && field . __op === 'Delete' ) {
127
+ throw new Parse . Error ( 255 , `Field ${ name } does not exist, cannot delete.` ) ;
122
128
}
123
129
} ) ;
124
- }
125
- var existingFields = schema . data [ className ] ;
126
-
127
- for ( var submittedFieldName in submittedFields ) {
128
- if ( existingFields [ submittedFieldName ] && submittedFields [ submittedFieldName ] . __op !== 'Delete' ) {
129
- return Promise . resolve ( {
130
- status : 400 ,
131
- response : {
132
- code : 255 ,
133
- error : 'field ' + submittedFieldName + ' exists, cannot update' ,
134
- }
135
- } ) ;
136
- }
137
130
138
- if ( ! existingFields [ submittedFieldName ] && submittedFields [ submittedFieldName ] . __op === 'Delete' ) {
139
- return Promise . resolve ( {
140
- status : 400 ,
141
- response : {
142
- code : 255 ,
143
- error : 'field ' + submittedFieldName + ' does not exist, cannot delete' ,
144
- }
145
- } ) ;
131
+ let newSchema = Schema . buildMergedSchemaObject ( existingFields , submittedFields ) ;
132
+ let mongoObject = Schema . mongoSchemaFromFieldsAndClassName ( newSchema , className ) ;
133
+ if ( ! mongoObject . result ) {
134
+ throw new Parse . Error ( mongoObject . code , mongoObject . error ) ;
146
135
}
147
- }
148
136
149
- var newSchema = Schema . buildMergedSchemaObject ( existingFields , submittedFields ) ;
150
- var mongoObject = Schema . mongoSchemaFromFieldsAndClassName ( newSchema , className ) ;
151
- if ( ! mongoObject . result ) {
152
- return Promise . resolve ( {
153
- status : 400 ,
154
- response : mongoObject ,
137
+ // Finally we have checked to make sure the request is valid and we can start deleting fields.
138
+ // Do all deletions first, then a single save to _SCHEMA collection to handle all additions.
139
+ let deletionPromises = [ ] ;
140
+ Object . keys ( submittedFields ) . forEach ( submittedFieldName => {
141
+ if ( submittedFields [ submittedFieldName ] . __op === 'Delete' ) {
142
+ let promise = schema . deleteField ( submittedFieldName , className , req . config . database ) ;
143
+ deletionPromises . push ( promise ) ;
144
+ }
155
145
} ) ;
156
- }
157
146
158
- // Finally we have checked to make sure the request is valid and we can start deleting fields.
159
- // Do all deletions first, then a single save to _SCHEMA collection to handle all additions.
160
- var deletionPromises = [ ]
161
- Object . keys ( submittedFields ) . forEach ( submittedFieldName => {
162
- if ( submittedFields [ submittedFieldName ] . __op === 'Delete' ) {
163
- var promise = req . config . database . connect ( )
164
- . then ( ( ) => schema . deleteField (
165
- submittedFieldName ,
166
- className ,
167
- req . config . database . adapter . database ,
168
- req . config . database . collectionPrefix
169
- ) ) ;
170
- deletionPromises . push ( promise ) ;
171
- }
147
+ return Promise . all ( deletionPromises )
148
+ . then ( ( ) => new Promise ( ( resolve , reject ) => {
149
+ schema . collection . update ( { _id : className } , mongoObject . result , { w : 1 } , ( err , docs ) => {
150
+ if ( err ) {
151
+ reject ( err ) ;
152
+ }
153
+ resolve ( { response : mongoSchemaToSchemaAPIResponse ( mongoObject . result ) } ) ;
154
+ } )
155
+ } ) ) ;
172
156
} ) ;
173
-
174
- return Promise . all ( deletionPromises )
175
- . then ( ( ) => new Promise ( ( resolve , reject ) => {
176
- schema . collection . update ( { _id : className } , mongoObject . result , { w : 1 } , ( err , docs ) => {
177
- if ( err ) {
178
- reject ( err ) ;
179
- }
180
- resolve ( { response : mongoSchemaToSchemaAPIResponse ( mongoObject . result ) } ) ;
181
- } )
182
- } ) ) ;
183
- } ) ;
184
157
}
185
158
186
159
// A helper function that removes all join tables for a schema. Returns a promise.
0 commit comments