• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mia-platform / crud-service / 5288232769

pending completion
5288232769

Pull #92

github

web-flow
Merge a47d150c2 into 8334e4887
Pull Request #92: Feat/writable views

962 of 1075 branches covered (89.49%)

Branch coverage included in aggregate %.

168 of 168 new or added lines in 5 files covered. (100.0%)

1947 of 2021 relevant lines covered (96.34%)

21691.14 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

96.3
/lib/generatePathFieldsForRawSchema.js
1
/* eslint-disable no-underscore-dangle */
2
/*
3
 * Copyright 2023 Mia s.r.l.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *     http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17

18
'use strict'
19

20
const {
21
  ARRAY,
22
  RAWOBJECTTYPE,
23
  JSON_SCHEMA_ARRAY_TYPE,
24
  JSON_SCHEMA_OBJECT_TYPE,
25
} = require('./consts')
112✔
26
const generatePathFromJsonSchema = require('./generatePathFromJsonSchema')
112✔
27

28
function getFieldJsonSchemaByTypeCompatibility(field) {
29
  if (field.type === ARRAY) {
2,179✔
30
    return {
948✔
31
      type: JSON_SCHEMA_ARRAY_TYPE,
32
      items: {
33
        type: JSON_SCHEMA_OBJECT_TYPE,
34
        properties: field.items.schema.properties,
35
        additionalProperties: field.items.schema.additionalProperties,
36
      },
37
    }
38
  }
39
  return {
1,231✔
40
    type: JSON_SCHEMA_OBJECT_TYPE,
41
    properties: field.schema.properties,
42
    additionalProperties: field.schema.additionalProperties,
43
  }
44
}
45

46
function getFieldJsonSchemaByType(jsonSchema) {
47
  if (jsonSchema.type === JSON_SCHEMA_ARRAY_TYPE) {
104✔
48
    return {
24✔
49
      type: JSON_SCHEMA_ARRAY_TYPE,
50
      items: {
51
        type: JSON_SCHEMA_OBJECT_TYPE,
52
        properties: jsonSchema.items.properties,
53
        additionalProperties: jsonSchema.items.additionalProperties,
54
      },
55
    }
56
  }
57
  return {
80✔
58
    type: JSON_SCHEMA_OBJECT_TYPE,
59
    properties: jsonSchema.properties,
60
    additionalProperties: jsonSchema.additionalProperties,
61
  }
62
}
63

64
const generatePathFieldsForRawSchemaCompatibility = (logger, collectionDefinition) => collectionDefinition
4,106✔
65
  .fields
66
  .filter(field => {
67
    const isObjectWithRawSchema = field.type === RAWOBJECTTYPE && field.schema
44,682✔
68
    const isArrayOfRawObjectWithRawSchema = field.type === ARRAY
44,682✔
69
        && field.items.type === RAWOBJECTTYPE
70
        && field.items.schema
71

72
    return isObjectWithRawSchema || isArrayOfRawObjectWithRawSchema
44,682✔
73
  })
74
  .reduce((acc, field) => {
75
    const jsonSchema = {
2,179✔
76
      type: JSON_SCHEMA_OBJECT_TYPE,
77
      properties: {
78
        [field.name]: getFieldJsonSchemaByTypeCompatibility(field),
79
      },
80
    }
81
    let generatedForFields = {}
2,179✔
82
    try {
2,179✔
83
      generatedForFields = generatePathFromJsonSchema(jsonSchema)
2,179✔
84
    } catch (error) {
85
      logger.error(
4✔
86
        { collectionName: collectionDefinition.name, field: field.name },
87
        error.message
88
      )
89
      throw error
4✔
90
    }
91
    return {
2,175✔
92
      paths: {
93
        ...acc.paths,
94
        ...generatedForFields.paths,
95
      },
96
      patternProperties: {
97
        ...acc.patternProperties,
98
        ...generatedForFields.patternProperties,
99
      },
100
      pathsOperators: {
101
        ...acc.pathsOperators,
102
        ...generatedForFields.pathsOperators,
103
      },
104
      patternPropertiesOperators: {
105
        ...acc.patternPropertiesOperators,
106
        ...generatedForFields.patternPropertiesOperators,
107
      },
108
    }
109
  }, {
110
    paths: {},
111
    patternProperties: {},
112
    pathsOperators: {},
113
    patternPropertiesOperators: {},
114
  })
115

116
module.exports = function generatePathFieldsForRawSchema(logger, collectionDefinition) {
112✔
117
  if (!collectionDefinition.schema) {
4,162✔
118
    return generatePathFieldsForRawSchemaCompatibility(logger, collectionDefinition)
4,106✔
119
  }
120

121
  return Object
56✔
122
    .entries(collectionDefinition.schema.properties)
123
    .filter(([, jsonSchema]) => {
124
      if (jsonSchema.__mia_configuration?.type) { return false }
772✔
125
      const isObject = jsonSchema.type === JSON_SCHEMA_OBJECT_TYPE
676✔
126
      const isArrayOfObject = jsonSchema.type === JSON_SCHEMA_ARRAY_TYPE
676✔
127
        && jsonSchema.items.type === JSON_SCHEMA_OBJECT_TYPE
128
        && Boolean(jsonSchema.items.properties)
129

130
      return isObject || isArrayOfObject
676✔
131
    })
132
    .reduce((acc, [propertyName, propertyJsonSchema]) => {
133
      const jsonSchema = {
104✔
134
        type: JSON_SCHEMA_OBJECT_TYPE,
135
        properties: {
136
          [propertyName]: getFieldJsonSchemaByType(propertyJsonSchema),
137
        },
138
      }
139
      let generatedForFields = {}
104✔
140
      try {
104✔
141
        generatedForFields = generatePathFromJsonSchema(jsonSchema)
104✔
142
      } catch (error) {
143
        logger.error(
×
144
          { collectionName: collectionDefinition.name, field: propertyName },
145
          error.message
146
        )
147
        throw error
×
148
      }
149
      return {
104✔
150
        paths: {
151
          ...acc.paths,
152
          ...generatedForFields.paths,
153
        },
154
        patternProperties: {
155
          ...acc.patternProperties,
156
          ...generatedForFields.patternProperties,
157
        },
158
        pathsOperators: {
159
          ...acc.pathsOperators,
160
          ...generatedForFields.pathsOperators,
161
        },
162
        patternPropertiesOperators: {
163
          ...acc.patternPropertiesOperators,
164
          ...generatedForFields.patternPropertiesOperators,
165
        },
166
      }
167
    }, {
168
      paths: {},
169
      patternProperties: {},
170
      pathsOperators: {},
171
      patternPropertiesOperators: {},
172
    })
173
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc