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

Method addFields

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

Source from the content-addressed store, hash-verified

23 }
24
25 async addFields({fields, name, property}) {
26 const database = await this.connection
27 const {results} = await this.find({
28 name,
29 property
30 })
31
32 if (results.length === 0) {
33 throw new Error('SCHEMA_NOT_FOUND')
34 }
35
36 const {fields: currentFields} = results[0]
37 const newFields = Object.keys(fields).reduce((result, fieldName) => {
38 if (currentFields[fieldName]) {
39 const error = new Error('FIELD_ALREADY_EXISTS')
40
41 error.field = fieldName
42
43 throw error
44 }
45
46 result[fieldName] = fields[fieldName]
47
48 return result
49 }, {})
50 const updatedFields = Object.assign({}, currentFields, newFields)
51
52 await this.validate({
53 fields: updatedFields
54 })
55
56 const update = {
57 fields: updatedFields,
58 timestamp: Date.now()
59 }
60 const {matchedCount} = await database.update({
61 collection: config.get('schemas.collection'),
62 query: {
63 name,
64 property
65 },
66 update: {
67 $set: update
68 }
69 })
70
71 if (matchedCount === 0) {
72 throw new Error('SCHEMA_NOT_FOUND')
73 }
74
75 const updatedSchema = await this.get({
76 name,
77 property
78 })
79
80 // Reloading the model.
81 Model({
82 isListable: true,

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