@@ -93,6 +93,33 @@ function parseFieldTypeToMongoFieldType({ type, targetClass }) {
93
93
}
94
94
}
95
95
96
+ // Returns { code, error } if invalid, or { result }, an object
97
+ // suitable for inserting into _SCHEMA collection, otherwise.
98
+ function mongoSchemaFromFieldsAndClassNameAndCLP ( fields , className , classLevelPermissions ) {
99
+
100
+ let mongoObject = {
101
+ _id : className ,
102
+ objectId : 'string' ,
103
+ updatedAt : 'string' ,
104
+ createdAt : 'string'
105
+ } ;
106
+
107
+ for ( let fieldName in fields ) {
108
+ mongoObject [ fieldName ] = parseFieldTypeToMongoFieldType ( fields [ fieldName ] ) ;
109
+ }
110
+
111
+ if ( typeof classLevelPermissions !== 'undefined' ) {
112
+ mongoObject . _metadata = mongoObject . _metadata || { } ;
113
+ if ( ! classLevelPermissions ) {
114
+ delete mongoObject . _metadata . class_permissions ;
115
+ } else {
116
+ mongoObject . _metadata . class_permissions = classLevelPermissions ;
117
+ }
118
+ }
119
+
120
+ return { result : mongoObject } ;
121
+ }
122
+
96
123
class MongoSchemaCollection {
97
124
_collection : MongoCollection ;
98
125
@@ -136,8 +163,12 @@ class MongoSchemaCollection {
136
163
// later PR. Returns a promise that is expected to resolve with the newly created schema, in Parse format.
137
164
// If the class already exists, returns a promise that rejects with undefined as the reason. If the collection
138
165
// can't be added for a reason other than it already existing, requirements for rejection reason are TBD.
139
- addSchema ( name : string , fields ) {
140
- let mongoObject = _mongoSchemaObjectFromNameFields ( name , fields ) ;
166
+ addSchema ( name : string , fields , classLevelPermissions ) {
167
+ let mongoSchema = mongoSchemaFromFieldsAndClassNameAndCLP ( fields , name , classLevelPermissions ) ;
168
+ if ( ! mongoSchema . result ) {
169
+ throw new Parse . Error ( mongoSchema . code , mongoSchema . error ) ;
170
+ }
171
+ let mongoObject = _mongoSchemaObjectFromNameFields ( name , mongoSchema . result ) ;
141
172
return this . _collection . insertOne ( mongoObject )
142
173
. then ( result => mongoSchemaToParseSchema ( result . ops [ 0 ] ) )
143
174
. catch ( error => {
@@ -155,18 +186,27 @@ class MongoSchemaCollection {
155
186
upsertSchema ( name : string , query : string , update ) {
156
187
return this . _collection . upsertOne ( _mongoSchemaQueryFromNameQuery ( name , query ) , update ) ;
157
188
}
189
+
190
+ updateField ( className : string , fieldName : string , type : string ) {
191
+ // We don't have this field. Update the schema.
192
+ // Note that we use the $exists guard and $set to avoid race
193
+ // conditions in the database. This is important!
194
+ let query = { } ;
195
+ query [ fieldName ] = { '$exists' : false } ;
196
+ let update = { } ;
197
+ if ( typeof type === 'string' ) {
198
+ type = {
199
+ type : type
200
+ }
201
+ }
202
+ update [ fieldName ] = parseFieldTypeToMongoFieldType ( type ) ;
203
+ update = { '$set' : update } ;
204
+ return this . upsertSchema ( className , query , update ) ;
205
+ }
158
206
}
159
207
160
208
// Exported for testing reasons and because we haven't moved all mongo schema format
161
209
// related logic into the database adapter yet.
162
210
MongoSchemaCollection . _TESTmongoSchemaToParseSchema = mongoSchemaToParseSchema
163
211
164
- // Exported because we haven't moved all mongo schema format related logic
165
- // into the database adapter yet. We will remove this before too long.
166
- MongoSchemaCollection . _DONOTUSEmongoFieldToParseSchemaField = mongoFieldToParseSchemaField
167
-
168
- // Exported because we haven't moved all mongo schema format related logic
169
- // into the database adapter yet. We will remove this before too long.
170
- MongoSchemaCollection . _DONOTUSEparseFieldTypeToMongoFieldType = parseFieldTypeToMongoFieldType ;
171
-
172
212
export default MongoSchemaCollection
0 commit comments