@contember/schema-utils
Advanced tools
Comparing version 0.8.3 to 0.8.5
import { Model } from '@contember/schema'; | ||
export declare enum ModelErrorCode { | ||
ENTITY_NOT_FOUND = "entityNotFound", | ||
FIELD_NOT_FOUND = "fieldNotFound", | ||
NOT_RELATION = "notRelation", | ||
NOT_OWNING_SIDE = "notOwningSide" | ||
} | ||
export declare class ModelError extends Error { | ||
readonly code: ModelErrorCode; | ||
constructor(code: ModelErrorCode, message: string); | ||
} | ||
export declare const getEntity: (schema: Model.Schema, entityName: string) => Model.Entity; | ||
@@ -12,2 +22,3 @@ export declare const getColumnName: (schema: Model.Schema, entity: Model.Entity, fieldName: string) => string; | ||
export declare const acceptRelationTypeVisitor: <T>(schema: Model.Schema, entity: string | Model.Entity, relation: string | Model.AnyRelation, visitor: Model.RelationByTypeVisitor<T>) => T; | ||
export declare const isRelation: (field: Model.AnyField) => field is Model.AnyRelation; | ||
export declare const isInversedRelation: (relation: Model.Relation) => relation is Model.InversedRelation; | ||
@@ -14,0 +25,0 @@ export declare const isOwnerRelation: (relation: Model.Relation) => relation is Model.OwnerRelation; |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.emptyModelSchema = exports.isOwnerRelation = exports.isInversedRelation = exports.acceptRelationTypeVisitor = exports.acceptFieldVisitor = exports.acceptEveryFieldVisitor = exports.getUniqueConstraints = exports.getTargetEntity = exports.getColumnType = exports.getColumnName = exports.getEntity = void 0; | ||
exports.emptyModelSchema = exports.isOwnerRelation = exports.isInversedRelation = exports.isRelation = exports.acceptRelationTypeVisitor = exports.acceptFieldVisitor = exports.acceptEveryFieldVisitor = exports.getUniqueConstraints = exports.getTargetEntity = exports.getColumnType = exports.getColumnName = exports.getEntity = exports.ModelError = exports.ModelErrorCode = void 0; | ||
const utils_1 = require("../utils"); | ||
const schema_1 = require("@contember/schema"); | ||
const NamingHelper_1 = require("./NamingHelper"); | ||
var ModelErrorCode; | ||
(function (ModelErrorCode) { | ||
ModelErrorCode["ENTITY_NOT_FOUND"] = "entityNotFound"; | ||
ModelErrorCode["FIELD_NOT_FOUND"] = "fieldNotFound"; | ||
ModelErrorCode["NOT_RELATION"] = "notRelation"; | ||
ModelErrorCode["NOT_OWNING_SIDE"] = "notOwningSide"; | ||
})(ModelErrorCode = exports.ModelErrorCode || (exports.ModelErrorCode = {})); | ||
class ModelError extends Error { | ||
constructor(code, message) { | ||
super(message); | ||
this.code = code; | ||
} | ||
} | ||
exports.ModelError = ModelError; | ||
const createEntityNotFoundError = (entityName) => new ModelError(ModelErrorCode.ENTITY_NOT_FOUND, `Entity ${entityName} not found`); | ||
const createFieldNotFoundError = (entityName, fieldName) => new ModelError(ModelErrorCode.FIELD_NOT_FOUND, `Field ${fieldName} of entity ${entityName} not found`); | ||
exports.getEntity = (schema, entityName) => { | ||
const entity = schema.entities[entityName]; | ||
if (!entity) { | ||
throw new Error(`Entity ${entityName} not found`); | ||
throw createEntityNotFoundError(entityName); | ||
} | ||
@@ -21,3 +37,3 @@ return entity; | ||
} | ||
throw new Error('Not an owning side'); | ||
throw new ModelError(ModelErrorCode.NOT_OWNING_SIDE, `Field ${relation.name} of entity ${entity.name} is not an owning side`); | ||
}, | ||
@@ -34,3 +50,3 @@ }); | ||
} | ||
throw new Error('Not an owning side'); | ||
throw new ModelError(ModelErrorCode.NOT_OWNING_SIDE, `Field ${relation.name} of entity ${entity.name} is not an owning side`); | ||
}, | ||
@@ -66,3 +82,3 @@ }); | ||
if (!entityObj) { | ||
throw new Error(`entity ${entity} not found`); | ||
throw createEntityNotFoundError(typeof entity === 'string' ? entity : 'unknown'); | ||
} | ||
@@ -78,7 +94,7 @@ const result = {}; | ||
if (!entityObj) { | ||
throw new Error(`entity ${entity} not found`); | ||
throw createEntityNotFoundError(typeof entity === 'string' ? entity : 'unknown'); | ||
} | ||
const fieldObj = typeof field === 'string' ? entityObj.fields[field] : field; | ||
if (!fieldObj) { | ||
throw new Error(`field ${field} of entity ${entityObj.name} not found`); | ||
throw createFieldNotFoundError(entityObj.name, typeof field === 'string' ? field : 'unknown'); | ||
} | ||
@@ -89,3 +105,3 @@ if (utils_1.isIt(fieldObj, 'columnType')) { | ||
if (!utils_1.isIt(fieldObj, 'target')) { | ||
throw new Error(); | ||
throw new Error(`Invalid field type`); | ||
} | ||
@@ -102,6 +118,6 @@ const targetEntity = exports.getEntity(schema, fieldObj.target); | ||
else { | ||
throw new Error(); | ||
throw new Error('Invalid relaton type'); | ||
} | ||
if (targetRelation && !utils_1.isIt(targetRelation, 'target')) { | ||
throw new Error(); | ||
throw new Error('Invalid target'); | ||
} | ||
@@ -123,3 +139,3 @@ return visitor.visitRelation(entityObj, fieldObj, targetEntity, targetRelation); | ||
} | ||
throw new Error(); | ||
throw new Error('Invalid field type'); | ||
}; | ||
@@ -129,10 +145,10 @@ exports.acceptRelationTypeVisitor = (schema, entity, relation, visitor) => { | ||
if (!entityObj) { | ||
throw new Error(`entity ${entity} not found`); | ||
throw createEntityNotFoundError(typeof entity === 'string' ? entity : 'unknown'); | ||
} | ||
const relationObj = typeof relation === 'string' ? entityObj.fields[relation] : relation; | ||
if (!relationObj) { | ||
throw new Error(`relation ${relation} of entity ${entityObj.name} not found`); | ||
throw createFieldNotFoundError(entityObj.name, typeof relation === 'string' ? relation : 'unknown'); | ||
} | ||
if (!utils_1.isIt(relationObj, 'target')) { | ||
throw new Error(`field ${relation} is not a relation`); | ||
if (!exports.isRelation(relationObj)) { | ||
throw new ModelError(ModelErrorCode.NOT_RELATION, `Field ${relationObj.name} of entity ${entityObj.name} is not a relation`); | ||
} | ||
@@ -142,4 +158,4 @@ const targetEntity = exports.getEntity(schema, relationObj.target); | ||
const targetRelation = targetEntity.fields[relationObj.ownedBy]; | ||
if (!utils_1.isIt(targetRelation, 'target')) { | ||
throw new Error(); | ||
if (!exports.isRelation(targetRelation)) { | ||
throw new Error('Invalid target relation'); | ||
} | ||
@@ -153,4 +169,5 @@ switch (relationObj.type) { | ||
return visitor.visitOneHasMany(entityObj, relationObj, targetEntity, targetRelation); | ||
default: | ||
return utils_1.assertNever(relationObj); | ||
} | ||
throw new Error(); | ||
} | ||
@@ -166,7 +183,11 @@ else if (exports.isOwnerRelation(relationObj)) { | ||
return visitor.visitManyHasOne(entityObj, relationObj, targetEntity, targetRelation); | ||
default: | ||
return utils_1.assertNever(relationObj); | ||
} | ||
throw new Error(); | ||
} | ||
throw new Error(); | ||
throw new Error('Invalid relation type'); | ||
}; | ||
exports.isRelation = (field) => { | ||
return utils_1.isIt(field, 'target'); | ||
}; | ||
exports.isInversedRelation = (relation) => { | ||
@@ -173,0 +194,0 @@ return relation.ownedBy !== undefined; |
@@ -0,3 +1,4 @@ | ||
export * from './assertNever'; | ||
export * from './isIt'; | ||
export * from './deepCompare'; | ||
//# sourceMappingURL=index.d.ts.map |
@@ -13,4 +13,5 @@ "use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
__exportStar(require("./assertNever"), exports); | ||
__exportStar(require("./isIt"), exports); | ||
__exportStar(require("./deepCompare"), exports); | ||
//# sourceMappingURL=index.js.map |
{ | ||
"name": "@contember/schema-utils", | ||
"version": "0.8.3", | ||
"version": "0.8.5", | ||
"license": "Apache-2.0", | ||
@@ -9,3 +9,3 @@ "main": "dist/src/index.js", | ||
"dependencies": { | ||
"@contember/schema": "^0.8.3" | ||
"@contember/schema": "^0.8.5" | ||
}, | ||
@@ -16,3 +16,3 @@ "devDependencies": { | ||
}, | ||
"gitHead": "0a911e120c64bb1a66104ed638bb04f82359dc0f" | ||
"gitHead": "88a20ce90066758caabf124d9f3aec149d3f8d72" | ||
} |
@@ -1,9 +0,28 @@ | ||
import { isIt } from '../utils' | ||
import { assertNever, isIt } from '../utils' | ||
import { Model } from '@contember/schema' | ||
import { NamingHelper } from './NamingHelper' | ||
export enum ModelErrorCode { | ||
ENTITY_NOT_FOUND = 'entityNotFound', | ||
FIELD_NOT_FOUND = 'fieldNotFound', | ||
NOT_RELATION = 'notRelation', | ||
NOT_OWNING_SIDE = 'notOwningSide', | ||
} | ||
export class ModelError extends Error { | ||
constructor(public readonly code: ModelErrorCode, message: string) { | ||
super(message) | ||
} | ||
} | ||
const createEntityNotFoundError = (entityName: string) => | ||
new ModelError(ModelErrorCode.ENTITY_NOT_FOUND, `Entity ${entityName} not found`) | ||
const createFieldNotFoundError = (entityName: string, fieldName: string) => | ||
new ModelError(ModelErrorCode.FIELD_NOT_FOUND, `Field ${fieldName} of entity ${entityName} not found`) | ||
export const getEntity = (schema: Model.Schema, entityName: string): Model.Entity => { | ||
const entity = schema.entities[entityName] | ||
if (!entity) { | ||
throw new Error(`Entity ${entityName} not found`) | ||
throw createEntityNotFoundError(entityName) | ||
} | ||
@@ -20,3 +39,6 @@ return entity | ||
} | ||
throw new Error('Not an owning side') | ||
throw new ModelError( | ||
ModelErrorCode.NOT_OWNING_SIDE, | ||
`Field ${relation.name} of entity ${entity.name} is not an owning side`, | ||
) | ||
}, | ||
@@ -34,3 +56,6 @@ }) | ||
} | ||
throw new Error('Not an owning side') | ||
throw new ModelError( | ||
ModelErrorCode.NOT_OWNING_SIDE, | ||
`Field ${relation.name} of entity ${entity.name} is not an owning side`, | ||
) | ||
}, | ||
@@ -80,3 +105,3 @@ }) | ||
if (!entityObj) { | ||
throw new Error(`entity ${entity} not found`) | ||
throw createEntityNotFoundError(typeof entity === 'string' ? entity : 'unknown') | ||
} | ||
@@ -98,7 +123,7 @@ const result: { [fieldName: string]: T } = {} | ||
if (!entityObj) { | ||
throw new Error(`entity ${entity} not found`) | ||
throw createEntityNotFoundError(typeof entity === 'string' ? entity : 'unknown') | ||
} | ||
const fieldObj: Model.AnyField = typeof field === 'string' ? entityObj.fields[field] : field | ||
if (!fieldObj) { | ||
throw new Error(`field ${field} of entity ${entityObj.name} not found`) | ||
throw createFieldNotFoundError(entityObj.name, typeof field === 'string' ? field : 'unknown') | ||
} | ||
@@ -109,3 +134,3 @@ if (isIt<Model.AnyColumn>(fieldObj, 'columnType')) { | ||
if (!isIt<Model.AnyRelation>(fieldObj, 'target')) { | ||
throw new Error() | ||
throw new Error(`Invalid field type`) | ||
} | ||
@@ -121,6 +146,6 @@ const targetEntity = getEntity(schema, fieldObj.target) | ||
} else { | ||
throw new Error() | ||
throw new Error('Invalid relaton type') | ||
} | ||
if (targetRelation && !isIt<Model.Relation>(targetRelation, 'target')) { | ||
throw new Error() | ||
throw new Error('Invalid target') | ||
} | ||
@@ -144,3 +169,3 @@ return visitor.visitRelation(entityObj, fieldObj, targetEntity, targetRelation) | ||
} | ||
throw new Error() | ||
throw new Error('Invalid field type') | ||
} | ||
@@ -156,11 +181,13 @@ | ||
if (!entityObj) { | ||
throw new Error(`entity ${entity} not found`) | ||
throw createEntityNotFoundError(typeof entity === 'string' ? entity : 'unknown') | ||
} | ||
const relationObj: Model.AnyRelation = | ||
typeof relation === 'string' ? (entityObj.fields[relation] as Model.AnyRelation) : relation | ||
const relationObj: Model.AnyField = typeof relation === 'string' ? entityObj.fields[relation] : relation | ||
if (!relationObj) { | ||
throw new Error(`relation ${relation} of entity ${entityObj.name} not found`) | ||
throw createFieldNotFoundError(entityObj.name, typeof relation === 'string' ? relation : 'unknown') | ||
} | ||
if (!isIt<Model.Relation>(relationObj, 'target')) { | ||
throw new Error(`field ${relation} is not a relation`) | ||
if (!isRelation(relationObj)) { | ||
throw new ModelError( | ||
ModelErrorCode.NOT_RELATION, | ||
`Field ${relationObj.name} of entity ${entityObj.name} is not a relation`, | ||
) | ||
} | ||
@@ -171,4 +198,4 @@ const targetEntity = getEntity(schema, relationObj.target) | ||
const targetRelation = targetEntity.fields[relationObj.ownedBy] | ||
if (!isIt<Model.Relation>(targetRelation, 'target')) { | ||
throw new Error() | ||
if (!isRelation(targetRelation)) { | ||
throw new Error('Invalid target relation') | ||
} | ||
@@ -197,4 +224,5 @@ switch (relationObj.type) { | ||
) | ||
default: | ||
return assertNever(relationObj) | ||
} | ||
throw new Error() | ||
} else if (isOwnerRelation(relationObj)) { | ||
@@ -225,9 +253,14 @@ const targetRelation = relationObj.inversedBy ? targetEntity.fields[relationObj.inversedBy] : null | ||
) | ||
default: | ||
return assertNever(relationObj) | ||
} | ||
throw new Error() | ||
} | ||
throw new Error() | ||
throw new Error('Invalid relation type') | ||
} | ||
export const isRelation = (field: Model.AnyField): field is Model.AnyRelation => { | ||
return isIt<Model.Relation>(field, 'target') | ||
} | ||
export const isInversedRelation = (relation: Model.Relation): relation is Model.InversedRelation => { | ||
@@ -234,0 +267,0 @@ return (relation as Model.InversedRelation).ownedBy !== undefined |
@@ -0,2 +1,3 @@ | ||
export * from './assertNever' | ||
export * from './isIt' | ||
export * from './deepCompare' |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Unpopular package
QualityThis package is not very popular.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
330110
112
3504
270
1
6
Updated@contember/schema@^0.8.5