|
1 | | -module.exports = (_, clean) => { |
| 1 | +/** |
| 2 | + * @typedef {import('../types').ColumnDefinition} ColumnDefinition |
| 3 | + * @typedef {import('../types').JsonSchema} JsonSchema |
| 4 | + * @typedef {import('../types').ConstraintDto} ConstraintDto |
| 5 | + */ |
| 6 | + |
| 7 | +const _ = require('lodash'); |
| 8 | + |
| 9 | +module.exports = clean => { |
2 | 10 | const mapProperties = (jsonSchema, iteratee) => { |
3 | 11 | return Object.entries(jsonSchema.properties).map(iteratee); |
4 | 12 | }; |
@@ -166,11 +174,67 @@ module.exports = (_, clean) => { |
166 | 174 | ]; |
167 | 175 | }; |
168 | 176 |
|
| 177 | + /** |
| 178 | + * @param {{ jsonSchema: JsonSchema }} |
| 179 | + * @returns {ConstraintDto[]} |
| 180 | + */ |
| 181 | + const getCompositeKeyConstraints = ({ jsonSchema }) => { |
| 182 | + const compositePrimaryKeys = getCompositePrimaryKeys(jsonSchema); |
| 183 | + const compositeUniqueKeys = getCompositeUniqueKeys(jsonSchema); |
| 184 | + |
| 185 | + return [...compositePrimaryKeys, ...compositeUniqueKeys]; |
| 186 | + }; |
| 187 | + |
| 188 | + /** |
| 189 | + * @param {{ columnDefinition: ColumnDefinition }} |
| 190 | + * @returns {ConstraintDto | undefined} |
| 191 | + */ |
| 192 | + const getPrimaryKeyConstraint = ({ columnDefinition }) => { |
| 193 | + if (!isPrimaryKey(columnDefinition)) { |
| 194 | + return; |
| 195 | + } |
| 196 | + |
| 197 | + return hydratePrimaryKeyOptions(columnDefinition.primaryKeyOptions ?? {}, '', columnDefinition.isActivated); |
| 198 | + }; |
| 199 | + |
| 200 | + /** |
| 201 | + * @param {{ columnDefinition: ColumnDefinition }} |
| 202 | + * @returns {ConstraintDto[]} |
| 203 | + */ |
| 204 | + const getUniqueKeyConstraints = ({ columnDefinition }) => { |
| 205 | + if (!isUniqueKey(columnDefinition)) { |
| 206 | + return []; |
| 207 | + } |
| 208 | + |
| 209 | + if (isInlineUnique(columnDefinition)) { |
| 210 | + const constraint = hydrateUniqueOptions({}, '', columnDefinition.isActivated); |
| 211 | + |
| 212 | + return [constraint]; |
| 213 | + } |
| 214 | + |
| 215 | + return columnDefinition.uniqueKeyOptions.map(uniqueKeyOption => { |
| 216 | + return hydrateUniqueOptions(uniqueKeyOption, '', columnDefinition.isActivated); |
| 217 | + }); |
| 218 | + }; |
| 219 | + |
| 220 | + /** |
| 221 | + * @param {{ columnDefinition: ColumnDefinition }} |
| 222 | + * @returns {ConstraintDto[]} |
| 223 | + */ |
| 224 | + const getColumnConstraints = ({ columnDefinition }) => { |
| 225 | + const primaryKeyConstraint = getPrimaryKeyConstraint({ columnDefinition }); |
| 226 | + const uniqueKeyConstraints = getUniqueKeyConstraints({ columnDefinition }); |
| 227 | + |
| 228 | + return [primaryKeyConstraint, ...uniqueKeyConstraints].filter(Boolean); |
| 229 | + }; |
| 230 | + |
169 | 231 | return { |
170 | 232 | getTableKeyConstraints, |
171 | 233 | isInlineUnique, |
172 | 234 | isInlinePrimaryKey, |
173 | 235 | hydratePrimaryKeyOptions, |
174 | 236 | hydrateUniqueOptions, |
| 237 | + getColumnConstraints, |
| 238 | + getCompositeKeyConstraints, |
175 | 239 | }; |
176 | 240 | }; |
0 commit comments