MCPcopy Create free account
hub / github.com/dadi/api / updateFields

Method updateFields

dadi/lib/model/schemaStore.js:339–424  ·  view source on GitHub ↗
({fields, name, property})

Source from the content-addressed store, hash-verified

337 }
338
339 async updateFields({fields, name, property}) {
340 const database = await this.connection
341 const {results} = await this.find({
342 name,
343 property
344 })
345
346 if (results.length === 0) {
347 throw new Error('SCHEMA_NOT_FOUND')
348 }
349
350 const {fields: currentFields} = results[0]
351 const hasValidFields = Object.keys(fields).some(
352 fieldName => currentFields[fieldName]
353 )
354
355 if (!hasValidFields) {
356 throw new Error('FIELD_NOT_FOUND')
357 }
358
359 const updatedFields = Object.keys(currentFields).reduce(
360 (result, fieldName) => {
361 // If the field is not part of the update, it remains unchanged.
362 if (!fields[fieldName]) {
363 result[fieldName] = currentFields[fieldName]
364
365 return result
366 }
367
368 const newFieldName = fields[fieldName].name || fieldName
369 const newFieldSchema = Object.assign(
370 {},
371 currentFields[fieldName],
372 fields[fieldName]
373 )
374
375 // The update may contain a `name` property, which we need to remove
376 // before inserting.
377 delete newFieldSchema.name
378
379 result[newFieldName] = newFieldSchema
380
381 return result
382 },
383 {}
384 )
385
386 await this.validate({
387 fields: updatedFields
388 })
389
390 const update = {
391 fields: updatedFields,
392 timestamp: Date.now()
393 }
394 const {matchedCount} = await database.update({
395 collection: config.get('schemas.collection'),
396 query: {

Callers 1

collections.jsFile · 0.80

Calls 4

findMethod · 0.95
validateMethod · 0.95
getMethod · 0.95
ModelFunction · 0.85

Tested by

no test coverage detected