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

Method deleteFields

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

Source from the content-addressed store, hash-verified

239 }
240
241 async deleteFields({fields, name, property}) {
242 const database = await this.connection
243 const {results} = await this.find({
244 name,
245 property
246 })
247
248 if (results.length === 0) {
249 throw new Error('SCHEMA_NOT_FOUND')
250 }
251
252 const {fields: currentFields} = results[0]
253 const hasValidFields = fields.some(fieldName => currentFields[fieldName])
254
255 if (!hasValidFields) {
256 throw new Error('FIELD_NOT_FOUND')
257 }
258
259 const updatedFields = Object.keys(currentFields).reduce(
260 (result, fieldName) => {
261 if (!fields.includes(fieldName)) {
262 result[fieldName] = currentFields[fieldName]
263 }
264
265 return result
266 },
267 {}
268 )
269
270 await this.validate({
271 fields: updatedFields
272 })
273
274 const update = {
275 fields: updatedFields,
276 timestamp: Date.now()
277 }
278 const {matchedCount} = await database.update({
279 collection: config.get('schemas.collection'),
280 query: {
281 name,
282 property
283 },
284 update: {
285 $set: update
286 }
287 })
288
289 if (matchedCount === 0) {
290 throw new Error('SCHEMA_NOT_FOUND')
291 }
292
293 const updatedSchema = await this.get({
294 name,
295 property
296 })
297
298 // Reloading the model.

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