@contember/schema-migrations 2.2.0-alpha.10 → 2.2.0-alpha.12

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -41,21 +41,30 @@ const updateAclEveryEntity = (...updater) => (args) => ({
41
41
  )
42
42
  });
43
43
  const updateAclFieldPermissions = (updater) => ({ entityPermissions, entityName }) => {
44
- const operations = {};
45
- const handlers = {
46
- create: (value) => updater(value, entityName, schema.Acl.Operation.create),
47
- update: (value) => updater(value, entityName, schema.Acl.Operation.update),
48
- read: (value) => updater(value, entityName, schema.Acl.Operation.read),
49
- delete: (value) => value,
50
- customPrimary: (value) => value,
51
- refreshMaterializedView: (value) => value,
52
- noRoot: (value) => value
53
- };
54
- const types = ["create", "update", "read", "delete", "customPrimary", "noRoot"];
55
- for (const key of types) {
56
- if (key in entityPermissions.operations) {
57
- operations[key] = handlers[key](entityPermissions.operations[key]);
44
+ const source = entityPermissions.operations;
45
+ const updateFields = (value, operation) => updater(value, entityName, operation);
46
+ const operations = { ...source };
47
+ if (source.create !== void 0) {
48
+ operations.create = updateFields(source.create, schema.Acl.Operation.create);
49
+ }
50
+ if (source.update !== void 0) {
51
+ operations.update = updateFields(source.update, schema.Acl.Operation.update);
52
+ }
53
+ if (source.read !== void 0) {
54
+ operations.read = updateFields(source.read, schema.Acl.Operation.read);
55
+ }
56
+ if (source.through !== void 0) {
57
+ const through = { ...source.through };
58
+ if (source.through.create !== void 0) {
59
+ through.create = updateFields(source.through.create, schema.Acl.Operation.create);
60
+ }
61
+ if (source.through.update !== void 0) {
62
+ through.update = updateFields(source.through.update, schema.Acl.Operation.update);
63
+ }
64
+ if (source.through.read !== void 0) {
65
+ through.read = updateFields(source.through.read, schema.Acl.Operation.read);
58
66
  }
67
+ operations.through = through;
59
68
  }
60
69
  return {
61
70
  ...entityPermissions,
@@ -1 +1 @@
1
- {"version":3,"file":"schemaUpdateUtils.cjs","sources":["../../../../../../packages/schema-migrations/src/modifications/utils/schemaUpdateUtils.ts"],"sourcesContent":["import { Acl, Model, Schema, Writable } from '@contember/schema'\nimport { SchemaUpdateError } from '../exceptions.js'\nimport { isDefined } from '../../utils/isDefined.js'\nimport { isInverseRelation, isOwningRelation, isRelation, PredicateDefinitionProcessor } from '@contember/schema-utils'\nimport { VERSION_ACL_PATCH, VERSION_REMOVE_RELATION_INVERSE_SIDE } from '../ModificationVersions.js'\n\nexport type SchemaUpdater = (args: { schema: Schema }) => Schema\nexport type ModelUpdater = (args: { schema: Schema; model: Model.Schema }) => Model.Schema\nexport type EntityUpdater = (args: { schema: Schema; model: Model.Schema; entity: Model.Entity }) => Model.Entity\nexport type FieldUpdater<In extends Model.AnyField, Out extends Model.AnyField> = (args: {\n\tschema: Schema\n\tmodel: Model.Schema\n\tentity: Model.Entity\n\tfield: In\n}) => Out | undefined\nexport type AclUpdater = (args: { schema: Schema; acl: Acl.Schema }) => Acl.Schema\nexport type AclRoleUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n}) => Acl.RolePermissions\nexport type AclPermissionsUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n\tentities: Acl.Permissions\n}) => Acl.Permissions\n\nexport type AclEntityPermissionsUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n\tentities: Acl.Permissions\n\tentityName: string\n\tentityPermissions: Acl.EntityPermissions\n}) => Acl.EntityPermissions\n\nexport type EntityAclEveryPredicatesUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n\tentities: Acl.Permissions\n\tentityName: string\n\tentityPermissions: Acl.EntityPermissions\n\tpredicate: Acl.PredicateDefinition\n}) => Acl.PredicateDefinition\n\nexport const updateSchema = (...schemaUpdater: (SchemaUpdater | undefined)[]): SchemaUpdater => args =>\n\tschemaUpdater.filter(isDefined).reduce((schema, updater) => updater({ ...args, schema }), args.schema)\n\nexport const updateAcl = (updater: AclUpdater): SchemaUpdater => args => ({\n\t...args.schema,\n\tacl: updater({ schema: args.schema, acl: args.schema.acl }),\n})\n\nexport const updateAclEveryRole = (...updater: AclRoleUpdater[]): AclUpdater => args => ({\n\t...args.acl,\n\troles: Object.fromEntries(\n\t\tupdater.reduce(\n\t\t\t(acc, updater) => acc.map(([name, role]) => [name, updater({ ...args, role })]),\n\t\t\tObject.entries(args.acl.roles),\n\t\t),\n\t),\n})\n\nexport const updateAclEntities = (updater: AclPermissionsUpdater): AclRoleUpdater => args => ({\n\t...args.role,\n\tentities: updater({ ...args, entities: args.role.entities }),\n})\n\nexport const updateAclEveryEntity = (...updater: AclEntityPermissionsUpdater[]): AclRoleUpdater => args => ({\n\t...args.role,\n\tentities: Object.fromEntries(\n\t\tupdater.reduce(\n\t\t\t(acc, updater) =>\n\t\t\t\tacc.map(([entity, entityPermissions]) => [\n\t\t\t\t\tentity,\n\t\t\t\t\tupdater({\n\t\t\t\t\t\t...args,\n\t\t\t\t\t\tentities: args.role.entities,\n\t\t\t\t\t\tentityName: entity,\n\t\t\t\t\t\tentityPermissions,\n\t\t\t\t\t}),\n\t\t\t\t]),\n\t\t\tObject.entries(args.role.entities),\n\t\t),\n\t),\n})\n\ntype EntityAclFieldPermissionsUpdater = (\n\tfieldPermissions: Acl.FieldPermissions,\n\tentityName: string,\n\toperation: Acl.Operation,\n) => Acl.FieldPermissions\ntype EntityOperationHandler = {\n\t[K in keyof Required<Acl.EntityOperations>]: (\n\t\tvalue: Exclude<Acl.EntityOperations[K], undefined>,\n\t) => Acl.EntityOperations[K]\n}\n\nexport const updateAclFieldPermissions =\n\t(updater: EntityAclFieldPermissionsUpdater): AclEntityPermissionsUpdater => ({ entityPermissions, entityName }) => {\n\t\tconst operations: Writable<Acl.EntityOperations> = {}\n\t\tconst handlers: EntityOperationHandler = {\n\t\t\tcreate: value => updater(value, entityName, Acl.Operation.create),\n\t\t\tupdate: value => updater(value, entityName, Acl.Operation.update),\n\t\t\tread: value => updater(value, entityName, Acl.Operation.read),\n\t\t\tdelete: value => value,\n\t\t\tcustomPrimary: value => value,\n\t\t\trefreshMaterializedView: value => value,\n\t\t\tnoRoot: value => value,\n\t\t}\n\t\tconst types: (keyof Acl.EntityOperations)[] = ['create', 'update', 'read', 'delete', 'customPrimary', 'noRoot']\n\t\tfor (const key of types) {\n\t\t\tif (key in entityPermissions.operations) {\n\t\t\t\toperations[key] = handlers[key](entityPermissions.operations[key] as any) as any\n\t\t\t}\n\t\t}\n\n\t\treturn {\n\t\t\t...entityPermissions,\n\t\t\toperations,\n\t\t}\n\t}\n\nexport const updateAclEveryPredicate = (updater: EntityAclEveryPredicatesUpdater): AclEntityPermissionsUpdater => args => {\n\treturn {\n\t\t...args.entityPermissions,\n\t\tpredicates: Object.fromEntries(\n\t\t\tObject.entries(args.entityPermissions.predicates).map(([name, predicate]) => [\n\t\t\t\tname,\n\t\t\t\tupdater({\n\t\t\t\t\t...args,\n\t\t\t\t\tpredicate,\n\t\t\t\t}),\n\t\t\t]),\n\t\t),\n\t}\n}\n\nexport const updateModel = (...modelUpdate: (ModelUpdater | undefined)[]): SchemaUpdater => args => ({\n\t...args.schema,\n\tmodel: modelUpdate\n\t\t.filter((it): it is ModelUpdater => it !== undefined)\n\t\t.reduce((model, updater) => updater({ ...args, model }), args.schema.model),\n})\n\nexport const updateEntity = (name: string, entityUpdate: EntityUpdater): ModelUpdater => args => {\n\tif (!args.model.entities[name]) {\n\t\tthrow new SchemaUpdateError(`Entity ${name} not found`)\n\t}\n\treturn {\n\t\t...args.model,\n\t\tentities: {\n\t\t\t...args.model.entities,\n\t\t\t[name]: entityUpdate({ ...args, entity: args.model.entities[name] }),\n\t\t},\n\t}\n}\n\nexport const updateEveryEntity = (entityUpdate: EntityUpdater): ModelUpdater => args => ({\n\t...args.model,\n\tentities: Object.fromEntries(\n\t\tObject.entries(args.model.entities).map(([name, entity]) => [name, entityUpdate({ ...args, entity })]),\n\t),\n})\n\nexport const updateField =\n\t<In extends Model.AnyField = Model.AnyField, Out extends Model.AnyField = In>(name: string, fieldUpdater: FieldUpdater<In, Out>): EntityUpdater =>\n\targs => {\n\t\tconst { [name]: field, ...otherFields } = args.entity.fields\n\t\tif (!args.entity.fields[name]) {\n\t\t\tthrow new SchemaUpdateError(`Field ${args.entity.name}::${name} not found`)\n\t\t}\n\t\tconst updatedField = fieldUpdater({ ...args, field: args.entity.fields[name] as In })\n\n\t\treturn {\n\t\t\t...args.entity,\n\t\t\tfields: {\n\t\t\t\t...otherFields,\n\t\t\t\t...(updatedField ? { [name]: updatedField } : {}),\n\t\t\t},\n\t\t}\n\t}\n\nexport const updateEveryField = (fieldUpdater: FieldUpdater<Model.AnyField, Model.AnyField>): EntityUpdater => args => ({\n\t...args.entity,\n\tfields: Object.fromEntries(\n\t\tObject.entries(args.entity.fields)\n\t\t\t.map(([name, field]) => {\n\t\t\t\tconst updatedField = fieldUpdater({ ...args, field })\n\t\t\t\tif (!updatedField) {\n\t\t\t\t\treturn undefined\n\t\t\t\t}\n\t\t\t\treturn [name, updatedField]\n\t\t\t})\n\t\t\t.filter(isDefined),\n\t),\n})\n\nexport const addField = (field: Model.AnyField): EntityUpdater => args => {\n\treturn {\n\t\t...args.entity,\n\t\tfields: {\n\t\t\t...args.entity.fields,\n\t\t\t[field.name]: field,\n\t\t},\n\t}\n}\n\nexport const removeField = (entityName: string, fieldName: string, version: number): SchemaUpdater => {\n\treturn ({ schema }) => {\n\t\tconst entity = schema.model.entities[entityName]\n\t\tconst field = entity.fields[fieldName]\n\t\treturn updateSchema(\n\t\t\tversion >= VERSION_ACL_PATCH\n\t\t\t\t? updateAcl(\n\t\t\t\t\tupdateAclEveryRole(\n\t\t\t\t\t\tupdateAclEveryEntity(\n\t\t\t\t\t\t\tupdateAclFieldPermissions((permissions, entityName) => {\n\t\t\t\t\t\t\t\tif (entityName !== entity.name) {\n\t\t\t\t\t\t\t\t\treturn permissions\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tconst { [fieldName]: field, ...other } = permissions\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\t...other,\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\tupdateAclEveryPredicate(({ predicate, entityName }) => {\n\t\t\t\t\t\t\t\tconst processor = new PredicateDefinitionProcessor(schema.model)\n\t\t\t\t\t\t\t\treturn processor.process(schema.model.entities[entityName], predicate, {\n\t\t\t\t\t\t\t\t\thandleColumn: ctx => ctx.entity.name === entity.name && ctx.column.name === field.name ? undefined : ctx.value,\n\t\t\t\t\t\t\t\t\thandleRelation: ctx => ctx.entity.name === entity.name && ctx.relation.name === field.name ? undefined : ctx.value,\n\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t),\n\t\t\t\t\t),\n\t\t\t\t)\n\t\t\t\t: undefined,\n\t\t\tisRelation(field)\n\t\t\t\t&& isOwningRelation(field)\n\t\t\t\t&& field.inversedBy\n\t\t\t\t&& version >= VERSION_REMOVE_RELATION_INVERSE_SIDE\n\t\t\t\t? removeField(field.target, field.inversedBy, version)\n\t\t\t\t: undefined,\n\t\t\tupdateModel(\n\t\t\t\tupdateEntity(entity.name, ({ entity }) => {\n\t\t\t\t\tconst { [field.name]: removed, ...fields } = entity.fields\n\t\t\t\t\tconst indexes = entity.indexes.filter(index => !index.fields.includes(field.name))\n\t\t\t\t\tconst unique = entity.unique.filter(index => !index.fields.includes(field.name))\n\t\t\t\t\treturn {\n\t\t\t\t\t\t...entity,\n\t\t\t\t\t\tfields,\n\t\t\t\t\t\tindexes,\n\t\t\t\t\t\tunique,\n\t\t\t\t\t}\n\t\t\t\t}),\n\t\t\t\tisRelation(field) && isInverseRelation(field)\n\t\t\t\t\t? updateEntity(\n\t\t\t\t\t\tfield.target,\n\t\t\t\t\t\tupdateField<Model.AnyOwningRelation>(field.ownedBy, ({ field: { inversedBy, ...field } }) => field),\n\t\t\t\t\t)\n\t\t\t\t\t: undefined,\n\t\t\t),\n\t\t)({ schema })\n\t}\n}\n"],"names":["isDefined","schema","updater","Acl","SchemaUpdateError","VERSION_ACL_PATCH","entityName","field","PredicateDefinitionProcessor","isRelation","isOwningRelation","VERSION_REMOVE_RELATION_INVERSE_SIDE","entity","isInverseRelation"],"mappings":";;;;;;;AA+CO,MAAM,eAAe,IAAI,kBAAgE,CAAA,SAC/F,cAAc,OAAOA,UAAAA,SAAS,EAAE,OAAO,CAACC,SAAQ,YAAY,QAAQ,EAAE,GAAG,MAAM,QAAAA,SAAQ,GAAG,KAAK,MAAM;AAE/F,MAAM,YAAY,CAAC,YAAuC,CAAA,UAAS;AAAA,EACzE,GAAG,KAAK;AAAA,EACR,KAAK,QAAQ,EAAE,QAAQ,KAAK,QAAQ,KAAK,KAAK,OAAO,IAAA,CAAK;AAC3D;AAEO,MAAM,qBAAqB,IAAI,YAA0C,CAAA,UAAS;AAAA,EACxF,GAAG,KAAK;AAAA,EACR,OAAO,OAAO;AAAA,IACb,QAAQ;AAAA,MACP,CAAC,KAAKC,aAAY,IAAI,IAAI,CAAC,CAAC,MAAM,IAAI,MAAM,CAAC,MAAMA,SAAQ,EAAE,GAAG,MAAM,KAAA,CAAM,CAAC,CAAC;AAAA,MAC9E,OAAO,QAAQ,KAAK,IAAI,KAAK;AAAA,IAAA;AAAA,EAC9B;AAEF;AAEO,MAAM,oBAAoB,CAAC,YAAmD,CAAA,UAAS;AAAA,EAC7F,GAAG,KAAK;AAAA,EACR,UAAU,QAAQ,EAAE,GAAG,MAAM,UAAU,KAAK,KAAK,SAAA,CAAU;AAC5D;AAEO,MAAM,uBAAuB,IAAI,YAA2D,CAAA,UAAS;AAAA,EAC3G,GAAG,KAAK;AAAA,EACR,UAAU,OAAO;AAAA,IAChB,QAAQ;AAAA,MACP,CAAC,KAAKA,aACL,IAAI,IAAI,CAAC,CAAC,QAAQ,iBAAiB,MAAM;AAAA,QACxC;AAAA,QACAA,SAAQ;AAAA,UACP,GAAG;AAA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ations/src/modifications/utils/schemaUpdateUtils.ts"],"sourcesContent":["import { Acl, Model, Schema, Writable } from '@contember/schema'\nimport { SchemaUpdateError } from '../exceptions.js'\nimport { isDefined } from '../../utils/isDefined.js'\nimport { isInverseRelation, isOwningRelation, isRelation, PredicateDefinitionProcessor } from '@contember/schema-utils'\nimport { VERSION_ACL_PATCH, VERSION_REMOVE_RELATION_INVERSE_SIDE } from '../ModificationVersions.js'\n\nexport type SchemaUpdater = (args: { schema: Schema }) => Schema\nexport type ModelUpdater = (args: { schema: Schema; model: Model.Schema }) => Model.Schema\nexport type EntityUpdater = (args: { schema: Schema; model: Model.Schema; entity: Model.Entity }) => Model.Entity\nexport type FieldUpdater<In extends Model.AnyField, Out extends Model.AnyField> = (args: {\n\tschema: Schema\n\tmodel: Model.Schema\n\tentity: Model.Entity\n\tfield: In\n}) => Out | undefined\nexport type AclUpdater = (args: { schema: Schema; acl: Acl.Schema }) => Acl.Schema\nexport type AclRoleUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n}) => Acl.RolePermissions\nexport type AclPermissionsUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n\tentities: Acl.Permissions\n}) => Acl.Permissions\n\nexport type AclEntityPermissionsUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n\tentities: Acl.Permissions\n\tentityName: string\n\tentityPermissions: Acl.EntityPermissions\n}) => Acl.EntityPermissions\n\nexport type EntityAclEveryPredicatesUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n\tentities: Acl.Permissions\n\tentityName: string\n\tentityPermissions: Acl.EntityPermissions\n\tpredicate: Acl.PredicateDefinition\n}) => Acl.PredicateDefinition\n\nexport const updateSchema = (...schemaUpdater: (SchemaUpdater | undefined)[]): SchemaUpdater => args =>\n\tschemaUpdater.filter(isDefined).reduce((schema, updater) => updater({ ...args, schema }), args.schema)\n\nexport const updateAcl = (updater: AclUpdater): SchemaUpdater => args => ({\n\t...args.schema,\n\tacl: updater({ schema: args.schema, acl: args.schema.acl }),\n})\n\nexport const updateAclEveryRole = (...updater: AclRoleUpdater[]): AclUpdater => args => ({\n\t...args.acl,\n\troles: Object.fromEntries(\n\t\tupdater.reduce(\n\t\t\t(acc, updater) => acc.map(([name, role]) => [name, updater({ ...args, role })]),\n\t\t\tObject.entries(args.acl.roles),\n\t\t),\n\t),\n})\n\nexport const updateAclEntities = (updater: AclPermissionsUpdater): AclRoleUpdater => args => ({\n\t...args.role,\n\tentities: updater({ ...args, entities: args.role.entities }),\n})\n\nexport const updateAclEveryEntity = (...updater: AclEntityPermissionsUpdater[]): AclRoleUpdater => args => ({\n\t...args.role,\n\tentities: Object.fromEntries(\n\t\tupdater.reduce(\n\t\t\t(acc, updater) =>\n\t\t\t\tacc.map(([entity, entityPermissions]) => [\n\t\t\t\t\tentity,\n\t\t\t\t\tupdater({\n\t\t\t\t\t\t...args,\n\t\t\t\t\t\tentities: args.role.entities,\n\t\t\t\t\t\tentityName: entity,\n\t\t\t\t\t\tentityPermissions,\n\t\t\t\t\t}),\n\t\t\t\t]),\n\t\t\tObject.entries(args.role.entities),\n\t\t),\n\t),\n})\n\ntype EntityAclFieldPermissionsUpdater = (\n\tfieldPermissions: Acl.FieldPermissions,\n\tentityName: string,\n\toperation: Acl.Operation,\n) => Acl.FieldPermissions\nexport const updateAclFieldPermissions =\n\t(updater: EntityAclFieldPermissionsUpdater): AclEntityPermissionsUpdater => ({ entityPermissions, entityName }) => {\n\t\tconst source = entityPermissions.operations\n\t\tconst updateFields = (value: Acl.FieldPermissions, operation: Acl.Operation) => updater(value, entityName, operation)\n\t\t// Carry every key over untouched by default, then rewrite the ones holding field names. An\n\t\t// operation key that is not handled here must survive rather than disappear - the previous\n\t\t// allow-list shape silently dropped `refreshMaterializedView` on every field rename.\n\t\tconst operations: Writable<Acl.EntityOperations> = { ...source }\n\t\tif (source.create !== undefined) {\n\t\t\toperations.create = updateFields(source.create, Acl.Operation.create)\n\t\t}\n\t\tif (source.update !== undefined) {\n\t\t\toperations.update = updateFields(source.update, Acl.Operation.update)\n\t\t}\n\t\tif (source.read !== undefined) {\n\t\t\toperations.read = updateFields(source.read, Acl.Operation.read)\n\t\t}\n\t\tif (source.through !== undefined) {\n\t\t\tconst through: Writable<Acl.ThroughOperations> = { ...source.through }\n\t\t\tif (source.through.create !== undefined) {\n\t\t\t\tthrough.create = updateFields(source.through.create, Acl.Operation.create)\n\t\t\t}\n\t\t\tif (source.through.update !== undefined) {\n\t\t\t\tthrough.update = updateFields(source.through.update, Acl.Operation.update)\n\t\t\t}\n\t\t\tif (source.through.read !== undefined) {\n\t\t\t\tthrough.read = updateFields(source.through.read, Acl.Operation.read)\n\t\t\t}\n\t\t\toperations.through = through\n\t\t}\n\n\t\treturn {\n\t\t\t...entityPermissions,\n\t\t\toperations,\n\t\t}\n\t}\n\nexport const updateAclEveryPredicate = (updater: EntityAclEveryPredicatesUpdater): AclEntityPermissionsUpdater => args => {\n\treturn {\n\t\t...args.entityPermissions,\n\t\tpredicates: Object.fromEntries(\n\t\t\tObject.entries(args.entityPermissions.predicates).map(([name, predicate]) => [\n\t\t\t\tname,\n\t\t\t\tupdater({\n\t\t\t\t\t...args,\n\t\t\t\t\tpredicate,\n\t\t\t\t}),\n\t\t\t]),\n\t\t),\n\t}\n}\n\nexport const updateModel = (...modelUpdate: (ModelUpdater | undefined)[]): SchemaUpdater => args => ({\n\t...args.schema,\n\tmodel: modelUpdate\n\t\t.filter((it): it is ModelUpdater => it !== undefined)\n\t\t.reduce((model, updater) => updater({ ...args, model }), args.schema.model),\n})\n\nexport const updateEntity = (name: string, entityUpdate: EntityUpdater): ModelUpdater => args => {\n\tif (!args.model.entities[name]) {\n\t\tthrow new SchemaUpdateError(`Entity ${name} not found`)\n\t}\n\treturn {\n\t\t...args.model,\n\t\tentities: {\n\t\t\t...args.model.entities,\n\t\t\t[name]: entityUpdate({ ...args, entity: args.model.entities[name] }),\n\t\t},\n\t}\n}\n\nexport const updateEveryEntity = (entityUpdate: EntityUpdater): ModelUpdater => args => ({\n\t...args.model,\n\tentities: Object.fromEntries(\n\t\tObject.entries(args.model.entities).map(([name, entity]) => [name, entityUpdate({ ...args, entity })]),\n\t),\n})\n\nexport const updateField =\n\t<In extends Model.AnyField = Model.AnyField, Out extends Model.AnyField = In>(name: string, fieldUpdater: FieldUpdater<In, Out>): EntityUpdater =>\n\targs => {\n\t\tconst { [name]: field, ...otherFields } = args.entity.fields\n\t\tif (!args.entity.fields[name]) {\n\t\t\tthrow new SchemaUpdateError(`Field ${args.entity.name}::${name} not found`)\n\t\t}\n\t\tconst updatedField = fieldUpdater({ ...args, field: args.entity.fields[name] as In })\n\n\t\treturn {\n\t\t\t...args.entity,\n\t\t\tfields: {\n\t\t\t\t...otherFields,\n\t\t\t\t...(updatedField ? { [name]: updatedField } : {}),\n\t\t\t},\n\t\t}\n\t}\n\nexport const updateEveryField = (fieldUpdater: FieldUpdater<Model.AnyField, Model.AnyField>): EntityUpdater => args => ({\n\t...args.entity,\n\tfields: Object.fromEntries(\n\t\tObject.entries(args.entity.fields)\n\t\t\t.map(([name, field]) => {\n\t\t\t\tconst updatedField = fieldUpdater({ ...args, field })\n\t\t\t\tif (!updatedField) {\n\t\t\t\t\treturn undefined\n\t\t\t\t}\n\t\t\t\treturn [name, updatedField]\n\t\t\t})\n\t\t\t.filter(isDefined),\n\t),\n})\n\nexport const addField = (field: Model.AnyField): EntityUpdater => args => {\n\treturn {\n\t\t...args.entity,\n\t\tfields: {\n\t\t\t...args.entity.fields,\n\t\t\t[field.name]: field,\n\t\t},\n\t}\n}\n\nexport const removeField = (entityName: string, fieldName: string, version: number): SchemaUpdater => {\n\treturn ({ schema }) => {\n\t\tconst entity = schema.model.entities[entityName]\n\t\tconst field = entity.fields[fieldName]\n\t\treturn updateSchema(\n\t\t\tversion >= VERSION_ACL_PATCH\n\t\t\t\t? updateAcl(\n\t\t\t\t\tupdateAclEveryRole(\n\t\t\t\t\t\tupdateAclEveryEntity(\n\t\t\t\t\t\t\tupdateAclFieldPermissions((permissions, entityName) => {\n\t\t\t\t\t\t\t\tif (entityName !== entity.name) {\n\t\t\t\t\t\t\t\t\treturn permissions\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tconst { [fieldName]: field, ...other } = permissions\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\t...other,\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\tupdateAclEveryPredicate(({ predicate, entityName }) => {\n\t\t\t\t\t\t\t\tconst processor = new PredicateDefinitionProcessor(schema.model)\n\t\t\t\t\t\t\t\treturn processor.process(schema.model.entities[entityName], predicate, {\n\t\t\t\t\t\t\t\t\thandleColumn: ctx => ctx.entity.name === entity.name && ctx.column.name === field.name ? undefined : ctx.value,\n\t\t\t\t\t\t\t\t\thandleRelation: ctx => ctx.entity.name === entity.name && ctx.relation.name === field.name ? undefined : ctx.value,\n\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t),\n\t\t\t\t\t),\n\t\t\t\t)\n\t\t\t\t: undefined,\n\t\t\tisRelation(field)\n\t\t\t\t&& isOwningRelation(field)\n\t\t\t\t&& field.inversedBy\n\t\t\t\t&& version >= VERSION_REMOVE_RELATION_INVERSE_SIDE\n\t\t\t\t? removeField(field.target, field.inversedBy, version)\n\t\t\t\t: undefined,\n\t\t\tupdateModel(\n\t\t\t\tupdateEntity(entity.name, ({ entity }) => {\n\t\t\t\t\tconst { [field.name]: removed, ...fields } = entity.fields\n\t\t\t\t\tconst indexes = entity.indexes.filter(index => !index.fields.includes(field.name))\n\t\t\t\t\tconst unique = entity.unique.filter(index => !index.fields.includes(field.name))\n\t\t\t\t\treturn {\n\t\t\t\t\t\t...entity,\n\t\t\t\t\t\tfields,\n\t\t\t\t\t\tindexes,\n\t\t\t\t\t\tunique,\n\t\t\t\t\t}\n\t\t\t\t}),\n\t\t\t\tisRelation(field) && isInverseRelation(field)\n\t\t\t\t\t? updateEntity(\n\t\t\t\t\t\tfield.target,\n\t\t\t\t\t\tupdateField<Model.AnyOwningRelation>(field.ownedBy, ({ field: { inversedBy, ...field } }) => field),\n\t\t\t\t\t)\n\t\t\t\t\t: undefined,\n\t\t\t),\n\t\t)({ schema })\n\t}\n}\n"],"names":["isDefined","schema","updater","Acl","SchemaUpdateError","VERSION_ACL_PATCH","entityName","field","PredicateDefinitionProcessor","isRelation","isOwningRelation","VERSION_REMOVE_RELATION_INVERSE_SIDE","entity","isInverseRelation"],"mappings":";;;;;;;AA+CO,MAAM,eAAe,IAAI,kBAAgE,CAAA,SAC/F,cAAc,OAAOA,UAAAA,SAAS,EAAE,OAAO,CAACC,SAAQ,YAAY,QAAQ,EAAE,GAAG,MAAM,QAAAA,SAAQ,GAAG,KAAK,MAAM;AAE/F,MAAM,YAAY,CAAC,YAAuC,CAAA,UAAS;AAAA,EACzE,GAAG,KAAK;AAAA,EACR,KAAK,QAAQ,EAAE,QAAQ,KAAK,QAAQ,KAAK,KAAK,OAAO,IAAA,CAAK;AAC3D;AAEO,MAAM,qBAAqB,IAAI,YAA0C,CAAA,UAAS;AAAA,EACxF,GAAG,KAAK;AAAA,EACR,OAAO,OAAO;AAAA,IACb,QAAQ;AAAA,MACP,CAAC,KAAKC,aAAY,IAAI,IAAI,CAAC,CAAC,MAAM,IAAI,MAAM,CAAC,MAAMA,SAAQ,EAAE,GAAG,MAAM,KAAA,CAAM,CAAC,CAAC;AAAA,MAC9E,OAAO,QAAQ,KAAK,IAAI,KAAK;AAAA,IAAA;AAAA,EAC9B;AAEF;AAEO,MAAM,oBAAoB,CAAC,YAAmD,CAAA,UAAS;AAAA,EAC7F,GAAG,KAAK;AAAA,EACR,UAAU,QAAQ,EAAE,GAAG,MAAM,UAAU,KAAK,KAAK,SAAA,CAAU;AAC5D;AAEO,MAAM,uBAAuB,IAAI,YA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AAA,UAAS,CAAC,MAAM,OAAO,SAAS,MAAM,IAAI,CAAC;AAC/E,iBAAO;AAAA,YACN,GAAGA;AAAAA,YACH;AAAA,YACA;AAAA,YACA;AAAA,UAAA;AAAA,QAEF,CAAC;AAAA,QACDH,YAAAA,WAAW,KAAK,KAAKI,8BAAkB,KAAK,IACzC;AAAA,UACD,MAAM;AAAA,UACN,YAAqC,MAAM,SAAS,CAAC,EAAE,OAAO,EAAE,YAAY,GAAGN,OAAAA,EAAM,MAAQA,MAAK;AAAA,QAAA,IAEjG;AAAA,MAAA;AAAA,IACJ,EACC,EAAE,QAAAN,QAAA,CAAQ;AAAA,EACb;AACD;;;;;;;;;;;;;;;"}
@@ -39,21 +39,30 @@ const updateAclEveryEntity = (...updater) => (args) => ({
39
39
  )
40
40
  });
41
41
  const updateAclFieldPermissions = (updater) => ({ entityPermissions, entityName }) => {
42
- const operations = {};
43
- const handlers = {
44
- create: (value) => updater(value, entityName, Acl.Operation.create),
45
- update: (value) => updater(value, entityName, Acl.Operation.update),
46
- read: (value) => updater(value, entityName, Acl.Operation.read),
47
- delete: (value) => value,
48
- customPrimary: (value) => value,
49
- refreshMaterializedView: (value) => value,
50
- noRoot: (value) => value
51
- };
52
- const types = ["create", "update", "read", "delete", "customPrimary", "noRoot"];
53
- for (const key of types) {
54
- if (key in entityPermissions.operations) {
55
- operations[key] = handlers[key](entityPermissions.operations[key]);
42
+ const source = entityPermissions.operations;
43
+ const updateFields = (value, operation) => updater(value, entityName, operation);
44
+ const operations = { ...source };
45
+ if (source.create !== void 0) {
46
+ operations.create = updateFields(source.create, Acl.Operation.create);
47
+ }
48
+ if (source.update !== void 0) {
49
+ operations.update = updateFields(source.update, Acl.Operation.update);
50
+ }
51
+ if (source.read !== void 0) {
52
+ operations.read = updateFields(source.read, Acl.Operation.read);
53
+ }
54
+ if (source.through !== void 0) {
55
+ const through = { ...source.through };
56
+ if (source.through.create !== void 0) {
57
+ through.create = updateFields(source.through.create, Acl.Operation.create);
58
+ }
59
+ if (source.through.update !== void 0) {
60
+ through.update = updateFields(source.through.update, Acl.Operation.update);
61
+ }
62
+ if (source.through.read !== void 0) {
63
+ through.read = updateFields(source.through.read, Acl.Operation.read);
56
64
  }
65
+ operations.through = through;
57
66
  }
58
67
  return {
59
68
  ...entityPermissions,
@@ -1 +1 @@
1
- {"version":3,"file":"schemaUpdateUtils.js","sources":["../../../../../../packages/schema-migrations/src/modifications/utils/schemaUpdateUtils.ts"],"sourcesContent":["import { Acl, Model, Schema, Writable } from '@contember/schema'\nimport { SchemaUpdateError } from '../exceptions.js'\nimport { isDefined } from '../../utils/isDefined.js'\nimport { isInverseRelation, isOwningRelation, isRelation, PredicateDefinitionProcessor } from '@contember/schema-utils'\nimport { VERSION_ACL_PATCH, VERSION_REMOVE_RELATION_INVERSE_SIDE } from '../ModificationVersions.js'\n\nexport type SchemaUpdater = (args: { schema: Schema }) => Schema\nexport type ModelUpdater = (args: { schema: Schema; model: Model.Schema }) => Model.Schema\nexport type EntityUpdater = (args: { schema: Schema; model: Model.Schema; entity: Model.Entity }) => Model.Entity\nexport type FieldUpdater<In extends Model.AnyField, Out extends Model.AnyField> = (args: {\n\tschema: Schema\n\tmodel: Model.Schema\n\tentity: Model.Entity\n\tfield: In\n}) => Out | undefined\nexport type AclUpdater = (args: { schema: Schema; acl: Acl.Schema }) => Acl.Schema\nexport type AclRoleUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n}) => Acl.RolePermissions\nexport type AclPermissionsUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n\tentities: Acl.Permissions\n}) => Acl.Permissions\n\nexport type AclEntityPermissionsUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n\tentities: Acl.Permissions\n\tentityName: string\n\tentityPermissions: Acl.EntityPermissions\n}) => Acl.EntityPermissions\n\nexport type EntityAclEveryPredicatesUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n\tentities: Acl.Permissions\n\tentityName: string\n\tentityPermissions: Acl.EntityPermissions\n\tpredicate: Acl.PredicateDefinition\n}) => Acl.PredicateDefinition\n\nexport const updateSchema = (...schemaUpdater: (SchemaUpdater | undefined)[]): SchemaUpdater => args =>\n\tschemaUpdater.filter(isDefined).reduce((schema, updater) => updater({ ...args, schema }), args.schema)\n\nexport const updateAcl = (updater: AclUpdater): SchemaUpdater => args => ({\n\t...args.schema,\n\tacl: updater({ schema: args.schema, acl: args.schema.acl }),\n})\n\nexport const updateAclEveryRole = (...updater: AclRoleUpdater[]): AclUpdater => args => ({\n\t...args.acl,\n\troles: Object.fromEntries(\n\t\tupdater.reduce(\n\t\t\t(acc, updater) => acc.map(([name, role]) => [name, updater({ ...args, role })]),\n\t\t\tObject.entries(args.acl.roles),\n\t\t),\n\t),\n})\n\nexport const updateAclEntities = (updater: AclPermissionsUpdater): AclRoleUpdater => args => ({\n\t...args.role,\n\tentities: updater({ ...args, entities: args.role.entities }),\n})\n\nexport const updateAclEveryEntity = (...updater: AclEntityPermissionsUpdater[]): AclRoleUpdater => args => ({\n\t...args.role,\n\tentities: Object.fromEntries(\n\t\tupdater.reduce(\n\t\t\t(acc, updater) =>\n\t\t\t\tacc.map(([entity, entityPermissions]) => [\n\t\t\t\t\tentity,\n\t\t\t\t\tupdater({\n\t\t\t\t\t\t...args,\n\t\t\t\t\t\tentities: args.role.entities,\n\t\t\t\t\t\tentityName: entity,\n\t\t\t\t\t\tentityPermissions,\n\t\t\t\t\t}),\n\t\t\t\t]),\n\t\t\tObject.entries(args.role.entities),\n\t\t),\n\t),\n})\n\ntype EntityAclFieldPermissionsUpdater = (\n\tfieldPermissions: Acl.FieldPermissions,\n\tentityName: string,\n\toperation: Acl.Operation,\n) => Acl.FieldPermissions\ntype EntityOperationHandler = {\n\t[K in keyof Required<Acl.EntityOperations>]: (\n\t\tvalue: Exclude<Acl.EntityOperations[K], undefined>,\n\t) => Acl.EntityOperations[K]\n}\n\nexport const updateAclFieldPermissions =\n\t(updater: EntityAclFieldPermissionsUpdater): AclEntityPermissionsUpdater => ({ entityPermissions, entityName }) => {\n\t\tconst operations: Writable<Acl.EntityOperations> = {}\n\t\tconst handlers: EntityOperationHandler = {\n\t\t\tcreate: value => updater(value, entityName, Acl.Operation.create),\n\t\t\tupdate: value => updater(value, entityName, Acl.Operation.update),\n\t\t\tread: value => updater(value, entityName, Acl.Operation.read),\n\t\t\tdelete: value => value,\n\t\t\tcustomPrimary: value => value,\n\t\t\trefreshMaterializedView: value => value,\n\t\t\tnoRoot: value => value,\n\t\t}\n\t\tconst types: (keyof Acl.EntityOperations)[] = ['create', 'update', 'read', 'delete', 'customPrimary', 'noRoot']\n\t\tfor (const key of types) {\n\t\t\tif (key in entityPermissions.operations) {\n\t\t\t\toperations[key] = handlers[key](entityPermissions.operations[key] as any) as any\n\t\t\t}\n\t\t}\n\n\t\treturn {\n\t\t\t...entityPermissions,\n\t\t\toperations,\n\t\t}\n\t}\n\nexport const updateAclEveryPredicate = (updater: EntityAclEveryPredicatesUpdater): AclEntityPermissionsUpdater => args => {\n\treturn {\n\t\t...args.entityPermissions,\n\t\tpredicates: Object.fromEntries(\n\t\t\tObject.entries(args.entityPermissions.predicates).map(([name, predicate]) => [\n\t\t\t\tname,\n\t\t\t\tupdater({\n\t\t\t\t\t...args,\n\t\t\t\t\tpredicate,\n\t\t\t\t}),\n\t\t\t]),\n\t\t),\n\t}\n}\n\nexport const updateModel = (...modelUpdate: (ModelUpdater | undefined)[]): SchemaUpdater => args => ({\n\t...args.schema,\n\tmodel: modelUpdate\n\t\t.filter((it): it is ModelUpdater => it !== undefined)\n\t\t.reduce((model, updater) => updater({ ...args, model }), args.schema.model),\n})\n\nexport const updateEntity = (name: string, entityUpdate: EntityUpdater): ModelUpdater => args => {\n\tif (!args.model.entities[name]) {\n\t\tthrow new SchemaUpdateError(`Entity ${name} not found`)\n\t}\n\treturn {\n\t\t...args.model,\n\t\tentities: {\n\t\t\t...args.model.entities,\n\t\t\t[name]: entityUpdate({ ...args, entity: args.model.entities[name] }),\n\t\t},\n\t}\n}\n\nexport const updateEveryEntity = (entityUpdate: EntityUpdater): ModelUpdater => args => ({\n\t...args.model,\n\tentities: Object.fromEntries(\n\t\tObject.entries(args.model.entities).map(([name, entity]) => [name, entityUpdate({ ...args, entity })]),\n\t),\n})\n\nexport const updateField =\n\t<In extends Model.AnyField = Model.AnyField, Out extends Model.AnyField = In>(name: string, fieldUpdater: FieldUpdater<In, Out>): EntityUpdater =>\n\targs => {\n\t\tconst { [name]: field, ...otherFields } = args.entity.fields\n\t\tif (!args.entity.fields[name]) {\n\t\t\tthrow new SchemaUpdateError(`Field ${args.entity.name}::${name} not found`)\n\t\t}\n\t\tconst updatedField = fieldUpdater({ ...args, field: args.entity.fields[name] as In })\n\n\t\treturn {\n\t\t\t...args.entity,\n\t\t\tfields: {\n\t\t\t\t...otherFields,\n\t\t\t\t...(updatedField ? { [name]: updatedField } : {}),\n\t\t\t},\n\t\t}\n\t}\n\nexport const updateEveryField = (fieldUpdater: FieldUpdater<Model.AnyField, Model.AnyField>): EntityUpdater => args => ({\n\t...args.entity,\n\tfields: Object.fromEntries(\n\t\tObject.entries(args.entity.fields)\n\t\t\t.map(([name, field]) => {\n\t\t\t\tconst updatedField = fieldUpdater({ ...args, field })\n\t\t\t\tif (!updatedField) {\n\t\t\t\t\treturn undefined\n\t\t\t\t}\n\t\t\t\treturn [name, updatedField]\n\t\t\t})\n\t\t\t.filter(isDefined),\n\t),\n})\n\nexport const addField = (field: Model.AnyField): EntityUpdater => args => {\n\treturn {\n\t\t...args.entity,\n\t\tfields: {\n\t\t\t...args.entity.fields,\n\t\t\t[field.name]: field,\n\t\t},\n\t}\n}\n\nexport const removeField = (entityName: string, fieldName: string, version: number): SchemaUpdater => {\n\treturn ({ schema }) => {\n\t\tconst entity = schema.model.entities[entityName]\n\t\tconst field = entity.fields[fieldName]\n\t\treturn updateSchema(\n\t\t\tversion >= VERSION_ACL_PATCH\n\t\t\t\t? updateAcl(\n\t\t\t\t\tupdateAclEveryRole(\n\t\t\t\t\t\tupdateAclEveryEntity(\n\t\t\t\t\t\t\tupdateAclFieldPermissions((permissions, entityName) => {\n\t\t\t\t\t\t\t\tif (entityName !== entity.name) {\n\t\t\t\t\t\t\t\t\treturn permissions\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tconst { [fieldName]: field, ...other } = permissions\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\t...other,\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\tupdateAclEveryPredicate(({ predicate, entityName }) => {\n\t\t\t\t\t\t\t\tconst processor = new PredicateDefinitionProcessor(schema.model)\n\t\t\t\t\t\t\t\treturn processor.process(schema.model.entities[entityName], predicate, {\n\t\t\t\t\t\t\t\t\thandleColumn: ctx => ctx.entity.name === entity.name && ctx.column.name === field.name ? undefined : ctx.value,\n\t\t\t\t\t\t\t\t\thandleRelation: ctx => ctx.entity.name === entity.name && ctx.relation.name === field.name ? undefined : ctx.value,\n\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t),\n\t\t\t\t\t),\n\t\t\t\t)\n\t\t\t\t: undefined,\n\t\t\tisRelation(field)\n\t\t\t\t&& isOwningRelation(field)\n\t\t\t\t&& field.inversedBy\n\t\t\t\t&& version >= VERSION_REMOVE_RELATION_INVERSE_SIDE\n\t\t\t\t? removeField(field.target, field.inversedBy, version)\n\t\t\t\t: undefined,\n\t\t\tupdateModel(\n\t\t\t\tupdateEntity(entity.name, ({ entity }) => {\n\t\t\t\t\tconst { [field.name]: removed, ...fields } = entity.fields\n\t\t\t\t\tconst indexes = entity.indexes.filter(index => !index.fields.includes(field.name))\n\t\t\t\t\tconst unique = entity.unique.filter(index => !index.fields.includes(field.name))\n\t\t\t\t\treturn {\n\t\t\t\t\t\t...entity,\n\t\t\t\t\t\tfields,\n\t\t\t\t\t\tindexes,\n\t\t\t\t\t\tunique,\n\t\t\t\t\t}\n\t\t\t\t}),\n\t\t\t\tisRelation(field) && isInverseRelation(field)\n\t\t\t\t\t? updateEntity(\n\t\t\t\t\t\tfield.target,\n\t\t\t\t\t\tupdateField<Model.AnyOwningRelation>(field.ownedBy, ({ field: { inversedBy, ...field } }) => field),\n\t\t\t\t\t)\n\t\t\t\t\t: undefined,\n\t\t\t),\n\t\t)({ schema })\n\t}\n}\n"],"names":["updater","entityName","field","entity"],"mappings":";;;;;AA+CO,MAAM,eAAe,IAAI,kBAAgE,CAAA,SAC/F,cAAc,OAAO,SAAS,EAAE,OAAO,CAAC,QAAQ,YAAY,QAAQ,EAAE,GAAG,MAAM,QAAQ,GAAG,KAAK,MAAM;AAE/F,MAAM,YAAY,CAAC,YAAuC,CAAA,UAAS;AAAA,EACzE,GAAG,KAAK;AAAA,EACR,KAAK,QAAQ,EAAE,QAAQ,KAAK,QAAQ,KAAK,KAAK,OAAO,IAAA,CAAK;AAC3D;AAEO,MAAM,qBAAqB,IAAI,YAA0C,CAAA,UAAS;AAAA,EACxF,GAAG,KAAK;AAAA,EACR,OAAO,OAAO;AAAA,IACb,QAAQ;AAAA,MACP,CAAC,KAAKA,aAAY,IAAI,IAAI,CAAC,CAAC,MAAM,IAAI,MAAM,CAAC,MAAMA,SAAQ,EAAE,GAAG,MAAM,KAAA,CAAM,CAAC,CAAC;AAAA,MAC9E,OAAO,QAAQ,KAAK,IAAI,KAAK;AAAA,IAAA;AAAA,EAC9B;AAEF;AAEO,MAAM,oBAAoB,CAAC,YAAmD,CAAA,UAAS;AAAA,EAC7F,GAAG,KAAK;AAAA,EACR,UAAU,QAAQ,EAAE,GAAG,MAAM,UAAU,KAAK,KAAK,SAAA,CAAU;AAC5D;AAEO,MAAM,uBAAuB,IAAI,YAA2D,CAAA,UAAS;AAAA,EAC3G,GAAG,KAAK;AAAA,EACR,UAAU,OAAO;AAAA,IAChB,QA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/src/modifications/utils/schemaUpdateUtils.ts"],"sourcesContent":["import { Acl, Model, Schema, Writable } from '@contember/schema'\nimport { SchemaUpdateError } from '../exceptions.js'\nimport { isDefined } from '../../utils/isDefined.js'\nimport { isInverseRelation, isOwningRelation, isRelation, PredicateDefinitionProcessor } from '@contember/schema-utils'\nimport { VERSION_ACL_PATCH, VERSION_REMOVE_RELATION_INVERSE_SIDE } from '../ModificationVersions.js'\n\nexport type SchemaUpdater = (args: { schema: Schema }) => Schema\nexport type ModelUpdater = (args: { schema: Schema; model: Model.Schema }) => Model.Schema\nexport type EntityUpdater = (args: { schema: Schema; model: Model.Schema; entity: Model.Entity }) => Model.Entity\nexport type FieldUpdater<In extends Model.AnyField, Out extends Model.AnyField> = (args: {\n\tschema: Schema\n\tmodel: Model.Schema\n\tentity: Model.Entity\n\tfield: In\n}) => Out | undefined\nexport type AclUpdater = (args: { schema: Schema; acl: Acl.Schema }) => Acl.Schema\nexport type AclRoleUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n}) => Acl.RolePermissions\nexport type AclPermissionsUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n\tentities: Acl.Permissions\n}) => Acl.Permissions\n\nexport type AclEntityPermissionsUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n\tentities: Acl.Permissions\n\tentityName: string\n\tentityPermissions: Acl.EntityPermissions\n}) => Acl.EntityPermissions\n\nexport type EntityAclEveryPredicatesUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n\tentities: Acl.Permissions\n\tentityName: string\n\tentityPermissions: Acl.EntityPermissions\n\tpredicate: Acl.PredicateDefinition\n}) => Acl.PredicateDefinition\n\nexport const updateSchema = (...schemaUpdater: (SchemaUpdater | undefined)[]): SchemaUpdater => args =>\n\tschemaUpdater.filter(isDefined).reduce((schema, updater) => updater({ ...args, schema }), args.schema)\n\nexport const updateAcl = (updater: AclUpdater): SchemaUpdater => args => ({\n\t...args.schema,\n\tacl: updater({ schema: args.schema, acl: args.schema.acl }),\n})\n\nexport const updateAclEveryRole = (...updater: AclRoleUpdater[]): AclUpdater => args => ({\n\t...args.acl,\n\troles: Object.fromEntries(\n\t\tupdater.reduce(\n\t\t\t(acc, updater) => acc.map(([name, role]) => [name, updater({ ...args, role })]),\n\t\t\tObject.entries(args.acl.roles),\n\t\t),\n\t),\n})\n\nexport const updateAclEntities = (updater: AclPermissionsUpdater): AclRoleUpdater => args => ({\n\t...args.role,\n\tentities: updater({ ...args, entities: args.role.entities }),\n})\n\nexport const updateAclEveryEntity = (...updater: AclEntityPermissionsUpdater[]): AclRoleUpdater => args => ({\n\t...args.role,\n\tentities: Object.fromEntries(\n\t\tupdater.reduce(\n\t\t\t(acc, updater) =>\n\t\t\t\tacc.map(([entity, entityPermissions]) => [\n\t\t\t\t\tentity,\n\t\t\t\t\tupdater({\n\t\t\t\t\t\t...args,\n\t\t\t\t\t\tentities: args.role.entities,\n\t\t\t\t\t\tentityName: entity,\n\t\t\t\t\t\tentityPermissions,\n\t\t\t\t\t}),\n\t\t\t\t]),\n\t\t\tObject.entries(args.role.entities),\n\t\t),\n\t),\n})\n\ntype EntityAclFieldPermissionsUpdater = (\n\tfieldPermissions: Acl.FieldPermissions,\n\tentityName: string,\n\toperation: Acl.Operation,\n) => Acl.FieldPermissions\nexport const updateAclFieldPermissions =\n\t(updater: EntityAclFieldPermissionsUpdater): AclEntityPermissionsUpdater => ({ entityPermissions, entityName }) => {\n\t\tconst source = entityPermissions.operations\n\t\tconst updateFields = (value: Acl.FieldPermissions, operation: Acl.Operation) => updater(value, entityName, operation)\n\t\t// Carry every key over untouched by default, then rewrite the ones holding field names. An\n\t\t// operation key that is not handled here must survive rather than disappear - the previous\n\t\t// allow-list shape silently dropped `refreshMaterializedView` on every field rename.\n\t\tconst operations: Writable<Acl.EntityOperations> = { ...source }\n\t\tif (source.create !== undefined) {\n\t\t\toperations.create = updateFields(source.create, Acl.Operation.create)\n\t\t}\n\t\tif (source.update !== undefined) {\n\t\t\toperations.update = updateFields(source.update, Acl.Operation.update)\n\t\t}\n\t\tif (source.read !== undefined) {\n\t\t\toperations.read = updateFields(source.read, Acl.Operation.read)\n\t\t}\n\t\tif (source.through !== undefined) {\n\t\t\tconst through: Writable<Acl.ThroughOperations> = { ...source.through }\n\t\t\tif (source.through.create !== undefined) {\n\t\t\t\tthrough.create = updateFields(source.through.create, Acl.Operation.create)\n\t\t\t}\n\t\t\tif (source.through.update !== undefined) {\n\t\t\t\tthrough.update = updateFields(source.through.update, Acl.Operation.update)\n\t\t\t}\n\t\t\tif (source.through.read !== undefined) {\n\t\t\t\tthrough.read = updateFields(source.through.read, Acl.Operation.read)\n\t\t\t}\n\t\t\toperations.through = through\n\t\t}\n\n\t\treturn {\n\t\t\t...entityPermissions,\n\t\t\toperations,\n\t\t}\n\t}\n\nexport const updateAclEveryPredicate = (updater: EntityAclEveryPredicatesUpdater): AclEntityPermissionsUpdater => args => {\n\treturn {\n\t\t...args.entityPermissions,\n\t\tpredicates: Object.fromEntries(\n\t\t\tObject.entries(args.entityPermissions.predicates).map(([name, predicate]) => [\n\t\t\t\tname,\n\t\t\t\tupdater({\n\t\t\t\t\t...args,\n\t\t\t\t\tpredicate,\n\t\t\t\t}),\n\t\t\t]),\n\t\t),\n\t}\n}\n\nexport const updateModel = (...modelUpdate: (ModelUpdater | undefined)[]): SchemaUpdater => args => ({\n\t...args.schema,\n\tmodel: modelUpdate\n\t\t.filter((it): it is ModelUpdater => it !== undefined)\n\t\t.reduce((model, updater) => updater({ ...args, model }), args.schema.model),\n})\n\nexport const updateEntity = (name: string, entityUpdate: EntityUpdater): ModelUpdater => args => {\n\tif (!args.model.entities[name]) {\n\t\tthrow new SchemaUpdateError(`Entity ${name} not found`)\n\t}\n\treturn {\n\t\t...args.model,\n\t\tentities: {\n\t\t\t...args.model.entities,\n\t\t\t[name]: entityUpdate({ ...args, entity: args.model.entities[name] }),\n\t\t},\n\t}\n}\n\nexport const updateEveryEntity = (entityUpdate: EntityUpdater): ModelUpdater => args => ({\n\t...args.model,\n\tentities: Object.fromEntries(\n\t\tObject.entries(args.model.entities).map(([name, entity]) => [name, entityUpdate({ ...args, entity })]),\n\t),\n})\n\nexport const updateField =\n\t<In extends Model.AnyField = Model.AnyField, Out extends Model.AnyField = In>(name: string, fieldUpdater: FieldUpdater<In, Out>): EntityUpdater =>\n\targs => {\n\t\tconst { [name]: field, ...otherFields } = args.entity.fields\n\t\tif (!args.entity.fields[name]) {\n\t\t\tthrow new SchemaUpdateError(`Field ${args.entity.name}::${name} not found`)\n\t\t}\n\t\tconst updatedField = fieldUpdater({ ...args, field: args.entity.fields[name] as In })\n\n\t\treturn {\n\t\t\t...args.entity,\n\t\t\tfields: {\n\t\t\t\t...otherFields,\n\t\t\t\t...(updatedField ? { [name]: updatedField } : {}),\n\t\t\t},\n\t\t}\n\t}\n\nexport const updateEveryField = (fieldUpdater: FieldUpdater<Model.AnyField, Model.AnyField>): EntityUpdater => args => ({\n\t...args.entity,\n\tfields: Object.fromEntries(\n\t\tObject.entries(args.entity.fields)\n\t\t\t.map(([name, field]) => {\n\t\t\t\tconst updatedField = fieldUpdater({ ...args, field })\n\t\t\t\tif (!updatedField) {\n\t\t\t\t\treturn undefined\n\t\t\t\t}\n\t\t\t\treturn [name, updatedField]\n\t\t\t})\n\t\t\t.filter(isDefined),\n\t),\n})\n\nexport const addField = (field: Model.AnyField): EntityUpdater => args => {\n\treturn {\n\t\t...args.entity,\n\t\tfields: {\n\t\t\t...args.entity.fields,\n\t\t\t[field.name]: field,\n\t\t},\n\t}\n}\n\nexport const removeField = (entityName: string, fieldName: string, version: number): SchemaUpdater => {\n\treturn ({ schema }) => {\n\t\tconst entity = schema.model.entities[entityName]\n\t\tconst field = entity.fields[fieldName]\n\t\treturn updateSchema(\n\t\t\tversion >= VERSION_ACL_PATCH\n\t\t\t\t? updateAcl(\n\t\t\t\t\tupdateAclEveryRole(\n\t\t\t\t\t\tupdateAclEveryEntity(\n\t\t\t\t\t\t\tupdateAclFieldPermissions((permissions, entityName) => {\n\t\t\t\t\t\t\t\tif (entityName !== entity.name) {\n\t\t\t\t\t\t\t\t\treturn permissions\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tconst { [fieldName]: field, ...other } = permissions\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\t...other,\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\tupdateAclEveryPredicate(({ predicate, entityName }) => {\n\t\t\t\t\t\t\t\tconst processor = new PredicateDefinitionProcessor(schema.model)\n\t\t\t\t\t\t\t\treturn processor.process(schema.model.entities[entityName], predicate, {\n\t\t\t\t\t\t\t\t\thandleColumn: ctx => ctx.entity.name === entity.name && ctx.column.name === field.name ? undefined : ctx.value,\n\t\t\t\t\t\t\t\t\thandleRelation: ctx => ctx.entity.name === entity.name && ctx.relation.name === field.name ? undefined : ctx.value,\n\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t),\n\t\t\t\t\t),\n\t\t\t\t)\n\t\t\t\t: undefined,\n\t\t\tisRelation(field)\n\t\t\t\t&& isOwningRelation(field)\n\t\t\t\t&& field.inversedBy\n\t\t\t\t&& version >= VERSION_REMOVE_RELATION_INVERSE_SIDE\n\t\t\t\t? removeField(field.target, field.inversedBy, version)\n\t\t\t\t: undefined,\n\t\t\tupdateModel(\n\t\t\t\tupdateEntity(entity.name, ({ entity }) => {\n\t\t\t\t\tconst { [field.name]: removed, ...fields } = entity.fields\n\t\t\t\t\tconst indexes = entity.indexes.filter(index => !index.fields.includes(field.name))\n\t\t\t\t\tconst unique = entity.unique.filter(index => !index.fields.includes(field.name))\n\t\t\t\t\treturn {\n\t\t\t\t\t\t...entity,\n\t\t\t\t\t\tfields,\n\t\t\t\t\t\tindexes,\n\t\t\t\t\t\tunique,\n\t\t\t\t\t}\n\t\t\t\t}),\n\t\t\t\tisRelation(field) && isInverseRelation(field)\n\t\t\t\t\t? updateEntity(\n\t\t\t\t\t\tfield.target,\n\t\t\t\t\t\tupdateField<Model.AnyOwningRelation>(field.ownedBy, ({ field: { inversedBy, ...field } }) => field),\n\t\t\t\t\t)\n\t\t\t\t\t: undefined,\n\t\t\t),\n\t\t)({ schema })\n\t}\n}\n"],"names":["updater","entityName","field","entity"],"mappings":";;;;;AA+CO,MAAM,eAAe,IAAI,kBAAgE,CAAA,SAC/F,cAAc,OAAO,SAAS,EAAE,OAAO,CAAC,QAAQ,YAAY,QAAQ,EAAE,GAAG,MAAM,QAAQ,GAAG,KAAK,MAAM;AAE/F,MAAM,YAAY,CAAC,YAAuC,CAAA,UAAS;AAAA,EACzE,GAAG,KAAK;AAAA,EACR,KAAK,QAAQ,EAAE,QAAQ,KAAK,QAAQ,KAAK,KAAK,OAAO,IAAA,CAAK;AAC3D;AAEO,MAAM,qBAAqB,IAAI,YAA0C,CAAA,UAAS;AAAA,EACxF,GAAG,KAAK;AAAA,EACR,OAAO,OAAO;AAAA,IACb,QAAQ;AAAA,MACP,CAAC,KAAKA,aAAY,IAAI,IAAI,CAAC,CAAC,MAAM,IAAI,MAAM,CAAC,MAAMA,SAAQ,EAAE,GAAG,MAAM,KAAA,CAAM,CAAC,CAAC;AAAA,MAC9E,OAAO,QAAQ,KAAK,IAAI,KAAK;AAAA,IAAA;AAAA,EAC9B;AAEF;AAEO,MAAM,oBAAoB,CAAC,YAAmD,CAAA,UAAS;AAAA,EAC7F,GAAG,KAAK;AAAA,EACR,UAAU,QAAQ,EAAE,GAAG,MAAM,UAAU,KAAK,KAAK,SAAA,CAAU;AAC5D;AAEO,MAAM,uBAAuB,IAAI,YAA2D,CAAA,UAAS;AAAA,EAC3G,GAAG,KAAK;AAAA,EACR,UAAU,OAAO;AAAA,IAChB,QAAQ;AAAA,MACP,CAAC,KAAKA,aACL,IAAI,IAAI,CAAC,CAAC,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AE,OAAO,EAAE,YAAY,GAAGD,OAAAA,EAAM,MAAQA,MAAK;AAAA,QAAA,IAEjG;AAAA,MAAA;AAAA,IACJ,EACC,EAAE,OAAA,CAAQ;AAAA,EACb;AACD;"}
@@ -41,21 +41,30 @@ const updateAclEveryEntity = (...updater) => (args) => ({
41
41
  )
42
42
  });
43
43
  const updateAclFieldPermissions = (updater) => ({ entityPermissions, entityName }) => {
44
- const operations = {};
45
- const handlers = {
46
- create: (value) => updater(value, entityName, schema.Acl.Operation.create),
47
- update: (value) => updater(value, entityName, schema.Acl.Operation.update),
48
- read: (value) => updater(value, entityName, schema.Acl.Operation.read),
49
- delete: (value) => value,
50
- customPrimary: (value) => value,
51
- refreshMaterializedView: (value) => value,
52
- noRoot: (value) => value
53
- };
54
- const types = ["create", "update", "read", "delete", "customPrimary", "noRoot"];
55
- for (const key of types) {
56
- if (key in entityPermissions.operations) {
57
- operations[key] = handlers[key](entityPermissions.operations[key]);
44
+ const source = entityPermissions.operations;
45
+ const updateFields = (value, operation) => updater(value, entityName, operation);
46
+ const operations = { ...source };
47
+ if (source.create !== void 0) {
48
+ operations.create = updateFields(source.create, schema.Acl.Operation.create);
49
+ }
50
+ if (source.update !== void 0) {
51
+ operations.update = updateFields(source.update, schema.Acl.Operation.update);
52
+ }
53
+ if (source.read !== void 0) {
54
+ operations.read = updateFields(source.read, schema.Acl.Operation.read);
55
+ }
56
+ if (source.through !== void 0) {
57
+ const through = { ...source.through };
58
+ if (source.through.create !== void 0) {
59
+ through.create = updateFields(source.through.create, schema.Acl.Operation.create);
60
+ }
61
+ if (source.through.update !== void 0) {
62
+ through.update = updateFields(source.through.update, schema.Acl.Operation.update);
63
+ }
64
+ if (source.through.read !== void 0) {
65
+ through.read = updateFields(source.through.read, schema.Acl.Operation.read);
58
66
  }
67
+ operations.through = through;
59
68
  }
60
69
  return {
61
70
  ...entityPermissions,
@@ -1 +1 @@
1
- {"version":3,"file":"schemaUpdateUtils.cjs","sources":["../../../../../../packages/schema-migrations/src/modifications/utils/schemaUpdateUtils.ts"],"sourcesContent":["import { Acl, Model, Schema, Writable } from '@contember/schema'\nimport { SchemaUpdateError } from '../exceptions.js'\nimport { isDefined } from '../../utils/isDefined.js'\nimport { isInverseRelation, isOwningRelation, isRelation, PredicateDefinitionProcessor } from '@contember/schema-utils'\nimport { VERSION_ACL_PATCH, VERSION_REMOVE_RELATION_INVERSE_SIDE } from '../ModificationVersions.js'\n\nexport type SchemaUpdater = (args: { schema: Schema }) => Schema\nexport type ModelUpdater = (args: { schema: Schema; model: Model.Schema }) => Model.Schema\nexport type EntityUpdater = (args: { schema: Schema; model: Model.Schema; entity: Model.Entity }) => Model.Entity\nexport type FieldUpdater<In extends Model.AnyField, Out extends Model.AnyField> = (args: {\n\tschema: Schema\n\tmodel: Model.Schema\n\tentity: Model.Entity\n\tfield: In\n}) => Out | undefined\nexport type AclUpdater = (args: { schema: Schema; acl: Acl.Schema }) => Acl.Schema\nexport type AclRoleUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n}) => Acl.RolePermissions\nexport type AclPermissionsUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n\tentities: Acl.Permissions\n}) => Acl.Permissions\n\nexport type AclEntityPermissionsUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n\tentities: Acl.Permissions\n\tentityName: string\n\tentityPermissions: Acl.EntityPermissions\n}) => Acl.EntityPermissions\n\nexport type EntityAclEveryPredicatesUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n\tentities: Acl.Permissions\n\tentityName: string\n\tentityPermissions: Acl.EntityPermissions\n\tpredicate: Acl.PredicateDefinition\n}) => Acl.PredicateDefinition\n\nexport const updateSchema = (...schemaUpdater: (SchemaUpdater | undefined)[]): SchemaUpdater => args =>\n\tschemaUpdater.filter(isDefined).reduce((schema, updater) => updater({ ...args, schema }), args.schema)\n\nexport const updateAcl = (updater: AclUpdater): SchemaUpdater => args => ({\n\t...args.schema,\n\tacl: updater({ schema: args.schema, acl: args.schema.acl }),\n})\n\nexport const updateAclEveryRole = (...updater: AclRoleUpdater[]): AclUpdater => args => ({\n\t...args.acl,\n\troles: Object.fromEntries(\n\t\tupdater.reduce(\n\t\t\t(acc, updater) => acc.map(([name, role]) => [name, updater({ ...args, role })]),\n\t\t\tObject.entries(args.acl.roles),\n\t\t),\n\t),\n})\n\nexport const updateAclEntities = (updater: AclPermissionsUpdater): AclRoleUpdater => args => ({\n\t...args.role,\n\tentities: updater({ ...args, entities: args.role.entities }),\n})\n\nexport const updateAclEveryEntity = (...updater: AclEntityPermissionsUpdater[]): AclRoleUpdater => args => ({\n\t...args.role,\n\tentities: Object.fromEntries(\n\t\tupdater.reduce(\n\t\t\t(acc, updater) =>\n\t\t\t\tacc.map(([entity, entityPermissions]) => [\n\t\t\t\t\tentity,\n\t\t\t\t\tupdater({\n\t\t\t\t\t\t...args,\n\t\t\t\t\t\tentities: args.role.entities,\n\t\t\t\t\t\tentityName: entity,\n\t\t\t\t\t\tentityPermissions,\n\t\t\t\t\t}),\n\t\t\t\t]),\n\t\t\tObject.entries(args.role.entities),\n\t\t),\n\t),\n})\n\ntype EntityAclFieldPermissionsUpdater = (\n\tfieldPermissions: Acl.FieldPermissions,\n\tentityName: string,\n\toperation: Acl.Operation,\n) => Acl.FieldPermissions\ntype EntityOperationHandler = {\n\t[K in keyof Required<Acl.EntityOperations>]: (\n\t\tvalue: Exclude<Acl.EntityOperations[K], undefined>,\n\t) => Acl.EntityOperations[K]\n}\n\nexport const updateAclFieldPermissions =\n\t(updater: EntityAclFieldPermissionsUpdater): AclEntityPermissionsUpdater => ({ entityPermissions, entityName }) => {\n\t\tconst operations: Writable<Acl.EntityOperations> = {}\n\t\tconst handlers: EntityOperationHandler = {\n\t\t\tcreate: value => updater(value, entityName, Acl.Operation.create),\n\t\t\tupdate: value => updater(value, entityName, Acl.Operation.update),\n\t\t\tread: value => updater(value, entityName, Acl.Operation.read),\n\t\t\tdelete: value => value,\n\t\t\tcustomPrimary: value => value,\n\t\t\trefreshMaterializedView: value => value,\n\t\t\tnoRoot: value => value,\n\t\t}\n\t\tconst types: (keyof Acl.EntityOperations)[] = ['create', 'update', 'read', 'delete', 'customPrimary', 'noRoot']\n\t\tfor (const key of types) {\n\t\t\tif (key in entityPermissions.operations) {\n\t\t\t\toperations[key] = handlers[key](entityPermissions.operations[key] as any) as any\n\t\t\t}\n\t\t}\n\n\t\treturn {\n\t\t\t...entityPermissions,\n\t\t\toperations,\n\t\t}\n\t}\n\nexport const updateAclEveryPredicate = (updater: EntityAclEveryPredicatesUpdater): AclEntityPermissionsUpdater => args => {\n\treturn {\n\t\t...args.entityPermissions,\n\t\tpredicates: Object.fromEntries(\n\t\t\tObject.entries(args.entityPermissions.predicates).map(([name, predicate]) => [\n\t\t\t\tname,\n\t\t\t\tupdater({\n\t\t\t\t\t...args,\n\t\t\t\t\tpredicate,\n\t\t\t\t}),\n\t\t\t]),\n\t\t),\n\t}\n}\n\nexport const updateModel = (...modelUpdate: (ModelUpdater | undefined)[]): SchemaUpdater => args => ({\n\t...args.schema,\n\tmodel: modelUpdate\n\t\t.filter((it): it is ModelUpdater => it !== undefined)\n\t\t.reduce((model, updater) => updater({ ...args, model }), args.schema.model),\n})\n\nexport const updateEntity = (name: string, entityUpdate: EntityUpdater): ModelUpdater => args => {\n\tif (!args.model.entities[name]) {\n\t\tthrow new SchemaUpdateError(`Entity ${name} not found`)\n\t}\n\treturn {\n\t\t...args.model,\n\t\tentities: {\n\t\t\t...args.model.entities,\n\t\t\t[name]: entityUpdate({ ...args, entity: args.model.entities[name] }),\n\t\t},\n\t}\n}\n\nexport const updateEveryEntity = (entityUpdate: EntityUpdater): ModelUpdater => args => ({\n\t...args.model,\n\tentities: Object.fromEntries(\n\t\tObject.entries(args.model.entities).map(([name, entity]) => [name, entityUpdate({ ...args, entity })]),\n\t),\n})\n\nexport const updateField =\n\t<In extends Model.AnyField = Model.AnyField, Out extends Model.AnyField = In>(name: string, fieldUpdater: FieldUpdater<In, Out>): EntityUpdater =>\n\targs => {\n\t\tconst { [name]: field, ...otherFields } = args.entity.fields\n\t\tif (!args.entity.fields[name]) {\n\t\t\tthrow new SchemaUpdateError(`Field ${args.entity.name}::${name} not found`)\n\t\t}\n\t\tconst updatedField = fieldUpdater({ ...args, field: args.entity.fields[name] as In })\n\n\t\treturn {\n\t\t\t...args.entity,\n\t\t\tfields: {\n\t\t\t\t...otherFields,\n\t\t\t\t...(updatedField ? { [name]: updatedField } : {}),\n\t\t\t},\n\t\t}\n\t}\n\nexport const updateEveryField = (fieldUpdater: FieldUpdater<Model.AnyField, Model.AnyField>): EntityUpdater => args => ({\n\t...args.entity,\n\tfields: Object.fromEntries(\n\t\tObject.entries(args.entity.fields)\n\t\t\t.map(([name, field]) => {\n\t\t\t\tconst updatedField = fieldUpdater({ ...args, field })\n\t\t\t\tif (!updatedField) {\n\t\t\t\t\treturn undefined\n\t\t\t\t}\n\t\t\t\treturn [name, updatedField]\n\t\t\t})\n\t\t\t.filter(isDefined),\n\t),\n})\n\nexport const addField = (field: Model.AnyField): EntityUpdater => args => {\n\treturn {\n\t\t...args.entity,\n\t\tfields: {\n\t\t\t...args.entity.fields,\n\t\t\t[field.name]: field,\n\t\t},\n\t}\n}\n\nexport const removeField = (entityName: string, fieldName: string, version: number): SchemaUpdater => {\n\treturn ({ schema }) => {\n\t\tconst entity = schema.model.entities[entityName]\n\t\tconst field = entity.fields[fieldName]\n\t\treturn updateSchema(\n\t\t\tversion >= VERSION_ACL_PATCH\n\t\t\t\t? updateAcl(\n\t\t\t\t\tupdateAclEveryRole(\n\t\t\t\t\t\tupdateAclEveryEntity(\n\t\t\t\t\t\t\tupdateAclFieldPermissions((permissions, entityName) => {\n\t\t\t\t\t\t\t\tif (entityName !== entity.name) {\n\t\t\t\t\t\t\t\t\treturn permissions\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tconst { [fieldName]: field, ...other } = permissions\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\t...other,\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\tupdateAclEveryPredicate(({ predicate, entityName }) => {\n\t\t\t\t\t\t\t\tconst processor = new PredicateDefinitionProcessor(schema.model)\n\t\t\t\t\t\t\t\treturn processor.process(schema.model.entities[entityName], predicate, {\n\t\t\t\t\t\t\t\t\thandleColumn: ctx => ctx.entity.name === entity.name && ctx.column.name === field.name ? undefined : ctx.value,\n\t\t\t\t\t\t\t\t\thandleRelation: ctx => ctx.entity.name === entity.name && ctx.relation.name === field.name ? undefined : ctx.value,\n\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t),\n\t\t\t\t\t),\n\t\t\t\t)\n\t\t\t\t: undefined,\n\t\t\tisRelation(field)\n\t\t\t\t&& isOwningRelation(field)\n\t\t\t\t&& field.inversedBy\n\t\t\t\t&& version >= VERSION_REMOVE_RELATION_INVERSE_SIDE\n\t\t\t\t? removeField(field.target, field.inversedBy, version)\n\t\t\t\t: undefined,\n\t\t\tupdateModel(\n\t\t\t\tupdateEntity(entity.name, ({ entity }) => {\n\t\t\t\t\tconst { [field.name]: removed, ...fields } = entity.fields\n\t\t\t\t\tconst indexes = entity.indexes.filter(index => !index.fields.includes(field.name))\n\t\t\t\t\tconst unique = entity.unique.filter(index => !index.fields.includes(field.name))\n\t\t\t\t\treturn {\n\t\t\t\t\t\t...entity,\n\t\t\t\t\t\tfields,\n\t\t\t\t\t\tindexes,\n\t\t\t\t\t\tunique,\n\t\t\t\t\t}\n\t\t\t\t}),\n\t\t\t\tisRelation(field) && isInverseRelation(field)\n\t\t\t\t\t? updateEntity(\n\t\t\t\t\t\tfield.target,\n\t\t\t\t\t\tupdateField<Model.AnyOwningRelation>(field.ownedBy, ({ field: { inversedBy, ...field } }) => field),\n\t\t\t\t\t)\n\t\t\t\t\t: undefined,\n\t\t\t),\n\t\t)({ schema })\n\t}\n}\n"],"names":["isDefined","schema","updater","Acl","SchemaUpdateError","VERSION_ACL_PATCH","entityName","field","PredicateDefinitionProcessor","isRelation","isOwningRelation","VERSION_REMOVE_RELATION_INVERSE_SIDE","entity","isInverseRelation"],"mappings":";;;;;;;AA+CO,MAAM,eAAe,IAAI,kBAAgE,CAAA,SAC/F,cAAc,OAAOA,UAAAA,SAAS,EAAE,OAAO,CAACC,SAAQ,YAAY,QAAQ,EAAE,GAAG,MAAM,QAAAA,SAAQ,GAAG,KAAK,MAAM;AAE/F,MAAM,YAAY,CAAC,YAAuC,CAAA,UAAS;AAAA,EACzE,GAAG,KAAK;AAAA,EACR,KAAK,QAAQ,EAAE,QAAQ,KAAK,QAAQ,KAAK,KAAK,OAAO,IAAA,CAAK;AAC3D;AAEO,MAAM,qBAAqB,IAAI,YAA0C,CAAA,UAAS;AAAA,EACxF,GAAG,KAAK;AAAA,EACR,OAAO,OAAO;AAAA,IACb,QAAQ;AAAA,MACP,CAAC,KAAKC,aAAY,IAAI,IAAI,CAAC,CAAC,MAAM,IAAI,MAAM,CAAC,MAAMA,SAAQ,EAAE,GAAG,MAAM,KAAA,CAAM,CAAC,CAAC;AAAA,MAC9E,OAAO,QAAQ,KAAK,IAAI,KAAK;AAAA,IAAA;AAAA,EAC9B;AAEF;AAEO,MAAM,oBAAoB,CAAC,YAAmD,CAAA,UAAS;AAAA,EAC7F,GAAG,KAAK;AAAA,EACR,UAAU,QAAQ,EAAE,GAAG,MAAM,UAAU,KAAK,KAAK,SAAA,CAAU;AAC5D;AAEO,MAAM,uBAAuB,IAAI,YAA2D,CAAA,UAAS;AAAA,EAC3G,GAAG,KAAK;AAAA,EACR,UAAU,OAAO;AAAA,IAChB,QAAQ;AAAA,MACP,CAAC,KAAKA,aACL,IAAI,IAAI,CAAC,CAAC,QAAQ,iBAAiB,MAAM;AAAA,QACxC;AAAA,QACAA,SAAQ;AAAA,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../../../../packages/schema-migrations/src/modifications/utils/schemaUpdateUtils.ts"],"sourcesContent":["import { Acl, Model, Schema, Writable } from '@contember/schema'\nimport { SchemaUpdateError } from '../exceptions.js'\nimport { isDefined } from '../../utils/isDefined.js'\nimport { isInverseRelation, isOwningRelation, isRelation, PredicateDefinitionProcessor } from '@contember/schema-utils'\nimport { VERSION_ACL_PATCH, VERSION_REMOVE_RELATION_INVERSE_SIDE } from '../ModificationVersions.js'\n\nexport type SchemaUpdater = (args: { schema: Schema }) => Schema\nexport type ModelUpdater = (args: { schema: Schema; model: Model.Schema }) => Model.Schema\nexport type EntityUpdater = (args: { schema: Schema; model: Model.Schema; entity: Model.Entity }) => Model.Entity\nexport type FieldUpdater<In extends Model.AnyField, Out extends Model.AnyField> = (args: {\n\tschema: Schema\n\tmodel: Model.Schema\n\tentity: Model.Entity\n\tfield: In\n}) => Out | undefined\nexport type AclUpdater = (args: { schema: Schema; acl: Acl.Schema }) => Acl.Schema\nexport type AclRoleUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n}) => Acl.RolePermissions\nexport type AclPermissionsUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n\tentities: Acl.Permissions\n}) => Acl.Permissions\n\nexport type AclEntityPermissionsUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n\tentities: Acl.Permissions\n\tentityName: string\n\tentityPermissions: Acl.EntityPermissions\n}) => Acl.EntityPermissions\n\nexport type EntityAclEveryPredicatesUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n\tentities: Acl.Permissions\n\tentityName: string\n\tentityPermissions: Acl.EntityPermissions\n\tpredicate: Acl.PredicateDefinition\n}) => Acl.PredicateDefinition\n\nexport const updateSchema = (...schemaUpdater: (SchemaUpdater | undefined)[]): SchemaUpdater => args =>\n\tschemaUpdater.filter(isDefined).reduce((schema, updater) => updater({ ...args, schema }), args.schema)\n\nexport const updateAcl = (updater: AclUpdater): SchemaUpdater => args => ({\n\t...args.schema,\n\tacl: updater({ schema: args.schema, acl: args.schema.acl }),\n})\n\nexport const updateAclEveryRole = (...updater: AclRoleUpdater[]): AclUpdater => args => ({\n\t...args.acl,\n\troles: Object.fromEntries(\n\t\tupdater.reduce(\n\t\t\t(acc, updater) => acc.map(([name, role]) => [name, updater({ ...args, role })]),\n\t\t\tObject.entries(args.acl.roles),\n\t\t),\n\t),\n})\n\nexport const updateAclEntities = (updater: AclPermissionsUpdater): AclRoleUpdater => args => ({\n\t...args.role,\n\tentities: updater({ ...args, entities: args.role.entities }),\n})\n\nexport const updateAclEveryEntity = (...updater: AclEntityPermissionsUpdater[]): AclRoleUpdater => args => ({\n\t...args.role,\n\tentities: Object.fromEntries(\n\t\tupdater.reduce(\n\t\t\t(acc, updater) =>\n\t\t\t\tacc.map(([entity, entityPermissions]) => [\n\t\t\t\t\tentity,\n\t\t\t\t\tupdater({\n\t\t\t\t\t\t...args,\n\t\t\t\t\t\tentities: args.role.entities,\n\t\t\t\t\t\tentityName: entity,\n\t\t\t\t\t\tentityPermissions,\n\t\t\t\t\t}),\n\t\t\t\t]),\n\t\t\tObject.entries(args.role.entities),\n\t\t),\n\t),\n})\n\ntype EntityAclFieldPermissionsUpdater = (\n\tfieldPermissions: Acl.FieldPermissions,\n\tentityName: string,\n\toperation: Acl.Operation,\n) => Acl.FieldPermissions\nexport const updateAclFieldPermissions =\n\t(updater: EntityAclFieldPermissionsUpdater): AclEntityPermissionsUpdater => ({ entityPermissions, entityName }) => {\n\t\tconst source = entityPermissions.operations\n\t\tconst updateFields = (value: Acl.FieldPermissions, operation: Acl.Operation) => updater(value, entityName, operation)\n\t\t// Carry every key over untouched by default, then rewrite the ones holding field names. An\n\t\t// operation key that is not handled here must survive rather than disappear - the previous\n\t\t// allow-list shape silently dropped `refreshMaterializedView` on every field rename.\n\t\tconst operations: Writable<Acl.EntityOperations> = { ...source }\n\t\tif (source.create !== undefined) {\n\t\t\toperations.create = updateFields(source.create, Acl.Operation.create)\n\t\t}\n\t\tif (source.update !== undefined) {\n\t\t\toperations.update = updateFields(source.update, Acl.Operation.update)\n\t\t}\n\t\tif (source.read !== undefined) {\n\t\t\toperations.read = updateFields(source.read, Acl.Operation.read)\n\t\t}\n\t\tif (source.through !== undefined) {\n\t\t\tconst through: Writable<Acl.ThroughOperations> = { ...source.through }\n\t\t\tif (source.through.create !== undefined) {\n\t\t\t\tthrough.create = updateFields(source.through.create, Acl.Operation.create)\n\t\t\t}\n\t\t\tif (source.through.update !== undefined) {\n\t\t\t\tthrough.update = updateFields(source.through.update, Acl.Operation.update)\n\t\t\t}\n\t\t\tif (source.through.read !== undefined) {\n\t\t\t\tthrough.read = updateFields(source.through.read, Acl.Operation.read)\n\t\t\t}\n\t\t\toperations.through = through\n\t\t}\n\n\t\treturn {\n\t\t\t...entityPermissions,\n\t\t\toperations,\n\t\t}\n\t}\n\nexport const updateAclEveryPredicate = (updater: EntityAclEveryPredicatesUpdater): AclEntityPermissionsUpdater => args => {\n\treturn {\n\t\t...args.entityPermissions,\n\t\tpredicates: Object.fromEntries(\n\t\t\tObject.entries(args.entityPermissions.predicates).map(([name, predicate]) => [\n\t\t\t\tname,\n\t\t\t\tupdater({\n\t\t\t\t\t...args,\n\t\t\t\t\tpredicate,\n\t\t\t\t}),\n\t\t\t]),\n\t\t),\n\t}\n}\n\nexport const updateModel = (...modelUpdate: (ModelUpdater | undefined)[]): SchemaUpdater => args => ({\n\t...args.schema,\n\tmodel: modelUpdate\n\t\t.filter((it): it is ModelUpdater => it !== undefined)\n\t\t.reduce((model, updater) => updater({ ...args, model }), args.schema.model),\n})\n\nexport const updateEntity = (name: string, entityUpdate: EntityUpdater): ModelUpdater => args => {\n\tif (!args.model.entities[name]) {\n\t\tthrow new SchemaUpdateError(`Entity ${name} not found`)\n\t}\n\treturn {\n\t\t...args.model,\n\t\tentities: {\n\t\t\t...args.model.entities,\n\t\t\t[name]: entityUpdate({ ...args, entity: args.model.entities[name] }),\n\t\t},\n\t}\n}\n\nexport const updateEveryEntity = (entityUpdate: EntityUpdater): ModelUpdater => args => ({\n\t...args.model,\n\tentities: Object.fromEntries(\n\t\tObject.entries(args.model.entities).map(([name, entity]) => [name, entityUpdate({ ...args, entity })]),\n\t),\n})\n\nexport const updateField =\n\t<In extends Model.AnyField = Model.AnyField, Out extends Model.AnyField = In>(name: string, fieldUpdater: FieldUpdater<In, Out>): EntityUpdater =>\n\targs => {\n\t\tconst { [name]: field, ...otherFields } = args.entity.fields\n\t\tif (!args.entity.fields[name]) {\n\t\t\tthrow new SchemaUpdateError(`Field ${args.entity.name}::${name} not found`)\n\t\t}\n\t\tconst updatedField = fieldUpdater({ ...args, field: args.entity.fields[name] as In })\n\n\t\treturn {\n\t\t\t...args.entity,\n\t\t\tfields: {\n\t\t\t\t...otherFields,\n\t\t\t\t...(updatedField ? { [name]: updatedField } : {}),\n\t\t\t},\n\t\t}\n\t}\n\nexport const updateEveryField = (fieldUpdater: FieldUpdater<Model.AnyField, Model.AnyField>): EntityUpdater => args => ({\n\t...args.entity,\n\tfields: Object.fromEntries(\n\t\tObject.entries(args.entity.fields)\n\t\t\t.map(([name, field]) => {\n\t\t\t\tconst updatedField = fieldUpdater({ ...args, field })\n\t\t\t\tif (!updatedField) {\n\t\t\t\t\treturn undefined\n\t\t\t\t}\n\t\t\t\treturn [name, updatedField]\n\t\t\t})\n\t\t\t.filter(isDefined),\n\t),\n})\n\nexport const addField = (field: Model.AnyField): EntityUpdater => args => {\n\treturn {\n\t\t...args.entity,\n\t\tfields: {\n\t\t\t...args.entity.fields,\n\t\t\t[field.name]: field,\n\t\t},\n\t}\n}\n\nexport const removeField = (entityName: string, fieldName: string, version: number): SchemaUpdater => {\n\treturn ({ schema }) => {\n\t\tconst entity = schema.model.entities[entityName]\n\t\tconst field = entity.fields[fieldName]\n\t\treturn updateSchema(\n\t\t\tversion >= VERSION_ACL_PATCH\n\t\t\t\t? updateAcl(\n\t\t\t\t\tupdateAclEveryRole(\n\t\t\t\t\t\tupdateAclEveryEntity(\n\t\t\t\t\t\t\tupdateAclFieldPermissions((permissions, entityName) => {\n\t\t\t\t\t\t\t\tif (entityName !== entity.name) {\n\t\t\t\t\t\t\t\t\treturn permissions\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tconst { [fieldName]: field, ...other } = permissions\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\t...other,\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\tupdateAclEveryPredicate(({ predicate, entityName }) => {\n\t\t\t\t\t\t\t\tconst processor = new PredicateDefinitionProcessor(schema.model)\n\t\t\t\t\t\t\t\treturn processor.process(schema.model.entities[entityName], predicate, {\n\t\t\t\t\t\t\t\t\thandleColumn: ctx => ctx.entity.name === entity.name && ctx.column.name === field.name ? undefined : ctx.value,\n\t\t\t\t\t\t\t\t\thandleRelation: ctx => ctx.entity.name === entity.name && ctx.relation.name === field.name ? undefined : ctx.value,\n\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t),\n\t\t\t\t\t),\n\t\t\t\t)\n\t\t\t\t: undefined,\n\t\t\tisRelation(field)\n\t\t\t\t&& isOwningRelation(field)\n\t\t\t\t&& field.inversedBy\n\t\t\t\t&& version >= VERSION_REMOVE_RELATION_INVERSE_SIDE\n\t\t\t\t? removeField(field.target, field.inversedBy, version)\n\t\t\t\t: undefined,\n\t\t\tupdateModel(\n\t\t\t\tupdateEntity(entity.name, ({ entity }) => {\n\t\t\t\t\tconst { [field.name]: removed, ...fields } = entity.fields\n\t\t\t\t\tconst indexes = entity.indexes.filter(index => !index.fields.includes(field.name))\n\t\t\t\t\tconst unique = entity.unique.filter(index => !index.fields.includes(field.name))\n\t\t\t\t\treturn {\n\t\t\t\t\t\t...entity,\n\t\t\t\t\t\tfields,\n\t\t\t\t\t\tindexes,\n\t\t\t\t\t\tunique,\n\t\t\t\t\t}\n\t\t\t\t}),\n\t\t\t\tisRelation(field) && isInverseRelation(field)\n\t\t\t\t\t? updateEntity(\n\t\t\t\t\t\tfield.target,\n\t\t\t\t\t\tupdateField<Model.AnyOwningRelation>(field.ownedBy, ({ field: { inversedBy, ...field } }) => field),\n\t\t\t\t\t)\n\t\t\t\t\t: undefined,\n\t\t\t),\n\t\t)({ schema })\n\t}\n}\n"],"names":["isDefined","schema","updater","Acl","SchemaUpdateError","VERSION_ACL_PATCH","entityName","field","PredicateDefinitionProcessor","isRelation","isOwningRelation","VERSION_REMOVE_RELATION_INVERSE_SIDE","entity","isInverseRelation"],"mappings":";;;;;;;AA+CO,MAAM,eAAe,IAAI,kBAAgE,CAAA,SAC/F,cAAc,OAAOA,UAAAA,SAAS,EAAE,OAAO,CAACC,SAAQ,YAAY,QAAQ,EAAE,GAAG,MAAM,QAAAA,SAAQ,GAAG,KAAK,MAAM;AAE/F,MAAM,YAAY,CAAC,YAAuC,CAAA,UAAS;AAAA,EACzE,GAAG,KAAK;AAAA,EACR,KAAK,QAAQ,EAAE,QAAQ,KAAK,QAAQ,KAAK,KAAK,OAAO,IAAA,CAAK;AAC3D;AAEO,MAAM,qBAAqB,IAAI,YAA0C,CAAA,UAAS;AAAA,EACxF,GAAG,KAAK;AAAA,EACR,OAAO,OAAO;AAAA,IACb,QAAQ;AAAA,MACP,CAAC,KAAKC,aAAY,IAAI,IAAI,CAAC,CAAC,MAAM,IAAI,MAAM,CAAC,MAAMA,SAAQ,EAAE,GAAG,MAAM,KAAA,CAAM,CAAC,CAAC;AAAA,MAC9E,OAAO,QAAQ,KAAK,IAAI,KAAK;AAAA,IAAA;AAAA,EAC9B;AAEF;AAEO,MAAM,oBAAoB,CAAC,YAAmD,CAAA,UAAS;AAAA,EAC7F,GAAG,KAAK;AAAA,EACR,UAAU,QAAQ,EAAE,GAAG,MAAM,UAAU,KAAK,KAAK,SAAA,CAAU;AAC5D;AAEO,MAAM,uB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,OAAO,OAAO,CAAA,UAAS,CAAC,MAAM,OAAO,SAAS,MAAM,IAAI,CAAC;AAC/E,iBAAO;AAAA,YACN,GAAGA;AAAAA,YACH;AAAA,YACA;AAAA,YACA;AAAA,UAAA;AAAA,QAEF,CAAC;AAAA,QACDH,YAAAA,WAAW,KAAK,KAAKI,8BAAkB,KAAK,IACzC;AAAA,UACD,MAAM;AAAA,UACN,YAAqC,MAAM,SAAS,CAAC,EAAE,OAAO,EAAE,YAAY,GAAGN,OAAAA,EAAM,MAAQA,MAAK;AAAA,QAAA,IAEjG;AAAA,MAAA;AAAA,IACJ,EACC,EAAE,QAAAN,QAAA,CAAQ;AAAA,EACb;AACD;;;;;;;;;;;;;;;"}
@@ -39,21 +39,30 @@ const updateAclEveryEntity = (...updater) => (args) => ({
39
39
  )
40
40
  });
41
41
  const updateAclFieldPermissions = (updater) => ({ entityPermissions, entityName }) => {
42
- const operations = {};
43
- const handlers = {
44
- create: (value) => updater(value, entityName, Acl.Operation.create),
45
- update: (value) => updater(value, entityName, Acl.Operation.update),
46
- read: (value) => updater(value, entityName, Acl.Operation.read),
47
- delete: (value) => value,
48
- customPrimary: (value) => value,
49
- refreshMaterializedView: (value) => value,
50
- noRoot: (value) => value
51
- };
52
- const types = ["create", "update", "read", "delete", "customPrimary", "noRoot"];
53
- for (const key of types) {
54
- if (key in entityPermissions.operations) {
55
- operations[key] = handlers[key](entityPermissions.operations[key]);
42
+ const source = entityPermissions.operations;
43
+ const updateFields = (value, operation) => updater(value, entityName, operation);
44
+ const operations = { ...source };
45
+ if (source.create !== void 0) {
46
+ operations.create = updateFields(source.create, Acl.Operation.create);
47
+ }
48
+ if (source.update !== void 0) {
49
+ operations.update = updateFields(source.update, Acl.Operation.update);
50
+ }
51
+ if (source.read !== void 0) {
52
+ operations.read = updateFields(source.read, Acl.Operation.read);
53
+ }
54
+ if (source.through !== void 0) {
55
+ const through = { ...source.through };
56
+ if (source.through.create !== void 0) {
57
+ through.create = updateFields(source.through.create, Acl.Operation.create);
58
+ }
59
+ if (source.through.update !== void 0) {
60
+ through.update = updateFields(source.through.update, Acl.Operation.update);
61
+ }
62
+ if (source.through.read !== void 0) {
63
+ through.read = updateFields(source.through.read, Acl.Operation.read);
56
64
  }
65
+ operations.through = through;
57
66
  }
58
67
  return {
59
68
  ...entityPermissions,
@@ -1 +1 @@
1
- {"version":3,"file":"schemaUpdateUtils.js","sources":["../../../../../../packages/schema-migrations/src/modifications/utils/schemaUpdateUtils.ts"],"sourcesContent":["import { Acl, Model, Schema, Writable } from '@contember/schema'\nimport { SchemaUpdateError } from '../exceptions.js'\nimport { isDefined } from '../../utils/isDefined.js'\nimport { isInverseRelation, isOwningRelation, isRelation, PredicateDefinitionProcessor } from '@contember/schema-utils'\nimport { VERSION_ACL_PATCH, VERSION_REMOVE_RELATION_INVERSE_SIDE } from '../ModificationVersions.js'\n\nexport type SchemaUpdater = (args: { schema: Schema }) => Schema\nexport type ModelUpdater = (args: { schema: Schema; model: Model.Schema }) => Model.Schema\nexport type EntityUpdater = (args: { schema: Schema; model: Model.Schema; entity: Model.Entity }) => Model.Entity\nexport type FieldUpdater<In extends Model.AnyField, Out extends Model.AnyField> = (args: {\n\tschema: Schema\n\tmodel: Model.Schema\n\tentity: Model.Entity\n\tfield: In\n}) => Out | undefined\nexport type AclUpdater = (args: { schema: Schema; acl: Acl.Schema }) => Acl.Schema\nexport type AclRoleUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n}) => Acl.RolePermissions\nexport type AclPermissionsUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n\tentities: Acl.Permissions\n}) => Acl.Permissions\n\nexport type AclEntityPermissionsUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n\tentities: Acl.Permissions\n\tentityName: string\n\tentityPermissions: Acl.EntityPermissions\n}) => Acl.EntityPermissions\n\nexport type EntityAclEveryPredicatesUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n\tentities: Acl.Permissions\n\tentityName: string\n\tentityPermissions: Acl.EntityPermissions\n\tpredicate: Acl.PredicateDefinition\n}) => Acl.PredicateDefinition\n\nexport const updateSchema = (...schemaUpdater: (SchemaUpdater | undefined)[]): SchemaUpdater => args =>\n\tschemaUpdater.filter(isDefined).reduce((schema, updater) => updater({ ...args, schema }), args.schema)\n\nexport const updateAcl = (updater: AclUpdater): SchemaUpdater => args => ({\n\t...args.schema,\n\tacl: updater({ schema: args.schema, acl: args.schema.acl }),\n})\n\nexport const updateAclEveryRole = (...updater: AclRoleUpdater[]): AclUpdater => args => ({\n\t...args.acl,\n\troles: Object.fromEntries(\n\t\tupdater.reduce(\n\t\t\t(acc, updater) => acc.map(([name, role]) => [name, updater({ ...args, role })]),\n\t\t\tObject.entries(args.acl.roles),\n\t\t),\n\t),\n})\n\nexport const updateAclEntities = (updater: AclPermissionsUpdater): AclRoleUpdater => args => ({\n\t...args.role,\n\tentities: updater({ ...args, entities: args.role.entities }),\n})\n\nexport const updateAclEveryEntity = (...updater: AclEntityPermissionsUpdater[]): AclRoleUpdater => args => ({\n\t...args.role,\n\tentities: Object.fromEntries(\n\t\tupdater.reduce(\n\t\t\t(acc, updater) =>\n\t\t\t\tacc.map(([entity, entityPermissions]) => [\n\t\t\t\t\tentity,\n\t\t\t\t\tupdater({\n\t\t\t\t\t\t...args,\n\t\t\t\t\t\tentities: args.role.entities,\n\t\t\t\t\t\tentityName: entity,\n\t\t\t\t\t\tentityPermissions,\n\t\t\t\t\t}),\n\t\t\t\t]),\n\t\t\tObject.entries(args.role.entities),\n\t\t),\n\t),\n})\n\ntype EntityAclFieldPermissionsUpdater = (\n\tfieldPermissions: Acl.FieldPermissions,\n\tentityName: string,\n\toperation: Acl.Operation,\n) => Acl.FieldPermissions\ntype EntityOperationHandler = {\n\t[K in keyof Required<Acl.EntityOperations>]: (\n\t\tvalue: Exclude<Acl.EntityOperations[K], undefined>,\n\t) => Acl.EntityOperations[K]\n}\n\nexport const updateAclFieldPermissions =\n\t(updater: EntityAclFieldPermissionsUpdater): AclEntityPermissionsUpdater => ({ entityPermissions, entityName }) => {\n\t\tconst operations: Writable<Acl.EntityOperations> = {}\n\t\tconst handlers: EntityOperationHandler = {\n\t\t\tcreate: value => updater(value, entityName, Acl.Operation.create),\n\t\t\tupdate: value => updater(value, entityName, Acl.Operation.update),\n\t\t\tread: value => updater(value, entityName, Acl.Operation.read),\n\t\t\tdelete: value => value,\n\t\t\tcustomPrimary: value => value,\n\t\t\trefreshMaterializedView: value => value,\n\t\t\tnoRoot: value => value,\n\t\t}\n\t\tconst types: (keyof Acl.EntityOperations)[] = ['create', 'update', 'read', 'delete', 'customPrimary', 'noRoot']\n\t\tfor (const key of types) {\n\t\t\tif (key in entityPermissions.operations) {\n\t\t\t\toperations[key] = handlers[key](entityPermissions.operations[key] as any) as any\n\t\t\t}\n\t\t}\n\n\t\treturn {\n\t\t\t...entityPermissions,\n\t\t\toperations,\n\t\t}\n\t}\n\nexport const updateAclEveryPredicate = (updater: EntityAclEveryPredicatesUpdater): AclEntityPermissionsUpdater => args => {\n\treturn {\n\t\t...args.entityPermissions,\n\t\tpredicates: Object.fromEntries(\n\t\t\tObject.entries(args.entityPermissions.predicates).map(([name, predicate]) => [\n\t\t\t\tname,\n\t\t\t\tupdater({\n\t\t\t\t\t...args,\n\t\t\t\t\tpredicate,\n\t\t\t\t}),\n\t\t\t]),\n\t\t),\n\t}\n}\n\nexport const updateModel = (...modelUpdate: (ModelUpdater | undefined)[]): SchemaUpdater => args => ({\n\t...args.schema,\n\tmodel: modelUpdate\n\t\t.filter((it): it is ModelUpdater => it !== undefined)\n\t\t.reduce((model, updater) => updater({ ...args, model }), args.schema.model),\n})\n\nexport const updateEntity = (name: string, entityUpdate: EntityUpdater): ModelUpdater => args => {\n\tif (!args.model.entities[name]) {\n\t\tthrow new SchemaUpdateError(`Entity ${name} not found`)\n\t}\n\treturn {\n\t\t...args.model,\n\t\tentities: {\n\t\t\t...args.model.entities,\n\t\t\t[name]: entityUpdate({ ...args, entity: args.model.entities[name] }),\n\t\t},\n\t}\n}\n\nexport const updateEveryEntity = (entityUpdate: EntityUpdater): ModelUpdater => args => ({\n\t...args.model,\n\tentities: Object.fromEntries(\n\t\tObject.entries(args.model.entities).map(([name, entity]) => [name, entityUpdate({ ...args, entity })]),\n\t),\n})\n\nexport const updateField =\n\t<In extends Model.AnyField = Model.AnyField, Out extends Model.AnyField = In>(name: string, fieldUpdater: FieldUpdater<In, Out>): EntityUpdater =>\n\targs => {\n\t\tconst { [name]: field, ...otherFields } = args.entity.fields\n\t\tif (!args.entity.fields[name]) {\n\t\t\tthrow new SchemaUpdateError(`Field ${args.entity.name}::${name} not found`)\n\t\t}\n\t\tconst updatedField = fieldUpdater({ ...args, field: args.entity.fields[name] as In })\n\n\t\treturn {\n\t\t\t...args.entity,\n\t\t\tfields: {\n\t\t\t\t...otherFields,\n\t\t\t\t...(updatedField ? { [name]: updatedField } : {}),\n\t\t\t},\n\t\t}\n\t}\n\nexport const updateEveryField = (fieldUpdater: FieldUpdater<Model.AnyField, Model.AnyField>): EntityUpdater => args => ({\n\t...args.entity,\n\tfields: Object.fromEntries(\n\t\tObject.entries(args.entity.fields)\n\t\t\t.map(([name, field]) => {\n\t\t\t\tconst updatedField = fieldUpdater({ ...args, field })\n\t\t\t\tif (!updatedField) {\n\t\t\t\t\treturn undefined\n\t\t\t\t}\n\t\t\t\treturn [name, updatedField]\n\t\t\t})\n\t\t\t.filter(isDefined),\n\t),\n})\n\nexport const addField = (field: Model.AnyField): EntityUpdater => args => {\n\treturn {\n\t\t...args.entity,\n\t\tfields: {\n\t\t\t...args.entity.fields,\n\t\t\t[field.name]: field,\n\t\t},\n\t}\n}\n\nexport const removeField = (entityName: string, fieldName: string, version: number): SchemaUpdater => {\n\treturn ({ schema }) => {\n\t\tconst entity = schema.model.entities[entityName]\n\t\tconst field = entity.fields[fieldName]\n\t\treturn updateSchema(\n\t\t\tversion >= VERSION_ACL_PATCH\n\t\t\t\t? updateAcl(\n\t\t\t\t\tupdateAclEveryRole(\n\t\t\t\t\t\tupdateAclEveryEntity(\n\t\t\t\t\t\t\tupdateAclFieldPermissions((permissions, entityName) => {\n\t\t\t\t\t\t\t\tif (entityName !== entity.name) {\n\t\t\t\t\t\t\t\t\treturn permissions\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tconst { [fieldName]: field, ...other } = permissions\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\t...other,\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\tupdateAclEveryPredicate(({ predicate, entityName }) => {\n\t\t\t\t\t\t\t\tconst processor = new PredicateDefinitionProcessor(schema.model)\n\t\t\t\t\t\t\t\treturn processor.process(schema.model.entities[entityName], predicate, {\n\t\t\t\t\t\t\t\t\thandleColumn: ctx => ctx.entity.name === entity.name && ctx.column.name === field.name ? undefined : ctx.value,\n\t\t\t\t\t\t\t\t\thandleRelation: ctx => ctx.entity.name === entity.name && ctx.relation.name === field.name ? undefined : ctx.value,\n\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t),\n\t\t\t\t\t),\n\t\t\t\t)\n\t\t\t\t: undefined,\n\t\t\tisRelation(field)\n\t\t\t\t&& isOwningRelation(field)\n\t\t\t\t&& field.inversedBy\n\t\t\t\t&& version >= VERSION_REMOVE_RELATION_INVERSE_SIDE\n\t\t\t\t? removeField(field.target, field.inversedBy, version)\n\t\t\t\t: undefined,\n\t\t\tupdateModel(\n\t\t\t\tupdateEntity(entity.name, ({ entity }) => {\n\t\t\t\t\tconst { [field.name]: removed, ...fields } = entity.fields\n\t\t\t\t\tconst indexes = entity.indexes.filter(index => !index.fields.includes(field.name))\n\t\t\t\t\tconst unique = entity.unique.filter(index => !index.fields.includes(field.name))\n\t\t\t\t\treturn {\n\t\t\t\t\t\t...entity,\n\t\t\t\t\t\tfields,\n\t\t\t\t\t\tindexes,\n\t\t\t\t\t\tunique,\n\t\t\t\t\t}\n\t\t\t\t}),\n\t\t\t\tisRelation(field) && isInverseRelation(field)\n\t\t\t\t\t? updateEntity(\n\t\t\t\t\t\tfield.target,\n\t\t\t\t\t\tupdateField<Model.AnyOwningRelation>(field.ownedBy, ({ field: { inversedBy, ...field } }) => field),\n\t\t\t\t\t)\n\t\t\t\t\t: undefined,\n\t\t\t),\n\t\t)({ schema })\n\t}\n}\n"],"names":["updater","entityName","field","entity"],"mappings":";;;;;AA+CO,MAAM,eAAe,IAAI,kBAAgE,CAAA,SAC/F,cAAc,OAAO,SAAS,EAAE,OAAO,CAAC,QAAQ,YAAY,QAAQ,EAAE,GAAG,MAAM,QAAQ,GAAG,KAAK,MAAM;AAE/F,MAAM,YAAY,CAAC,YAAuC,CAAA,UAAS;AAAA,EACzE,GAAG,KAAK;AAAA,EACR,KAAK,QAAQ,EAAE,QAAQ,KAAK,QAAQ,KAAK,KAAK,OAAO,IAAA,CAAK;AAC3D;AAEO,MAAM,qBAAqB,IAAI,YAA0C,CAAA,UAAS;AAAA,EACxF,GAAG,KAAK;AAAA,EACR,OAAO,OAAO;AAAA,IACb,QAAQ;AAAA,MACP,CAAC,KAAKA,aAAY,IAAI,IAAI,CAAC,CAAC,MAAM,IAAI,MAAM,CAAC,MAAMA,SAAQ,EAAE,GAAG,MAAM,KAAA,CAAM,CAAC,CAAC;AAAA,MAC9E,OAAO,QAAQ,KAAK,IAAI,KAAK;AAAA,IAAA;AAAA,EAC9B;AAEF;AAEO,MAAM,oBAAoB,CAAC,YAAmD,CAAA,UAAS;AAAA,EAC7F,GAAG,KAAK;AAAA,EACR,UAAU,QAAQ,EAAE,GAAG,MAAM,UAAU,KAAK,KAAK,SAAA,CAAU;AAC5D;AAEO,MAAM,uBAAuB,IAAI,YAA2D,CAAA,UAAS;AAAA,EAC3G,GAAG,KAAK;AAAA,EACR,UAAU,OAAO;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../../../packages/schema-migrations/src/modifications/utils/schemaUpdateUtils.ts"],"sourcesContent":["import { Acl, Model, Schema, Writable } from '@contember/schema'\nimport { SchemaUpdateError } from '../exceptions.js'\nimport { isDefined } from '../../utils/isDefined.js'\nimport { isInverseRelation, isOwningRelation, isRelation, PredicateDefinitionProcessor } from '@contember/schema-utils'\nimport { VERSION_ACL_PATCH, VERSION_REMOVE_RELATION_INVERSE_SIDE } from '../ModificationVersions.js'\n\nexport type SchemaUpdater = (args: { schema: Schema }) => Schema\nexport type ModelUpdater = (args: { schema: Schema; model: Model.Schema }) => Model.Schema\nexport type EntityUpdater = (args: { schema: Schema; model: Model.Schema; entity: Model.Entity }) => Model.Entity\nexport type FieldUpdater<In extends Model.AnyField, Out extends Model.AnyField> = (args: {\n\tschema: Schema\n\tmodel: Model.Schema\n\tentity: Model.Entity\n\tfield: In\n}) => Out | undefined\nexport type AclUpdater = (args: { schema: Schema; acl: Acl.Schema }) => Acl.Schema\nexport type AclRoleUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n}) => Acl.RolePermissions\nexport type AclPermissionsUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n\tentities: Acl.Permissions\n}) => Acl.Permissions\n\nexport type AclEntityPermissionsUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n\tentities: Acl.Permissions\n\tentityName: string\n\tentityPermissions: Acl.EntityPermissions\n}) => Acl.EntityPermissions\n\nexport type EntityAclEveryPredicatesUpdater = (args: {\n\tschema: Schema\n\tacl: Acl.Schema\n\trole: Acl.RolePermissions\n\tentities: Acl.Permissions\n\tentityName: string\n\tentityPermissions: Acl.EntityPermissions\n\tpredicate: Acl.PredicateDefinition\n}) => Acl.PredicateDefinition\n\nexport const updateSchema = (...schemaUpdater: (SchemaUpdater | undefined)[]): SchemaUpdater => args =>\n\tschemaUpdater.filter(isDefined).reduce((schema, updater) => updater({ ...args, schema }), args.schema)\n\nexport const updateAcl = (updater: AclUpdater): SchemaUpdater => args => ({\n\t...args.schema,\n\tacl: updater({ schema: args.schema, acl: args.schema.acl }),\n})\n\nexport const updateAclEveryRole = (...updater: AclRoleUpdater[]): AclUpdater => args => ({\n\t...args.acl,\n\troles: Object.fromEntries(\n\t\tupdater.reduce(\n\t\t\t(acc, updater) => acc.map(([name, role]) => [name, updater({ ...args, role })]),\n\t\t\tObject.entries(args.acl.roles),\n\t\t),\n\t),\n})\n\nexport const updateAclEntities = (updater: AclPermissionsUpdater): AclRoleUpdater => args => ({\n\t...args.role,\n\tentities: updater({ ...args, entities: args.role.entities }),\n})\n\nexport const updateAclEveryEntity = (...updater: AclEntityPermissionsUpdater[]): AclRoleUpdater => args => ({\n\t...args.role,\n\tentities: Object.fromEntries(\n\t\tupdater.reduce(\n\t\t\t(acc, updater) =>\n\t\t\t\tacc.map(([entity, entityPermissions]) => [\n\t\t\t\t\tentity,\n\t\t\t\t\tupdater({\n\t\t\t\t\t\t...args,\n\t\t\t\t\t\tentities: args.role.entities,\n\t\t\t\t\t\tentityName: entity,\n\t\t\t\t\t\tentityPermissions,\n\t\t\t\t\t}),\n\t\t\t\t]),\n\t\t\tObject.entries(args.role.entities),\n\t\t),\n\t),\n})\n\ntype EntityAclFieldPermissionsUpdater = (\n\tfieldPermissions: Acl.FieldPermissions,\n\tentityName: string,\n\toperation: Acl.Operation,\n) => Acl.FieldPermissions\nexport const updateAclFieldPermissions =\n\t(updater: EntityAclFieldPermissionsUpdater): AclEntityPermissionsUpdater => ({ entityPermissions, entityName }) => {\n\t\tconst source = entityPermissions.operations\n\t\tconst updateFields = (value: Acl.FieldPermissions, operation: Acl.Operation) => updater(value, entityName, operation)\n\t\t// Carry every key over untouched by default, then rewrite the ones holding field names. An\n\t\t// operation key that is not handled here must survive rather than disappear - the previous\n\t\t// allow-list shape silently dropped `refreshMaterializedView` on every field rename.\n\t\tconst operations: Writable<Acl.EntityOperations> = { ...source }\n\t\tif (source.create !== undefined) {\n\t\t\toperations.create = updateFields(source.create, Acl.Operation.create)\n\t\t}\n\t\tif (source.update !== undefined) {\n\t\t\toperations.update = updateFields(source.update, Acl.Operation.update)\n\t\t}\n\t\tif (source.read !== undefined) {\n\t\t\toperations.read = updateFields(source.read, Acl.Operation.read)\n\t\t}\n\t\tif (source.through !== undefined) {\n\t\t\tconst through: Writable<Acl.ThroughOperations> = { ...source.through }\n\t\t\tif (source.through.create !== undefined) {\n\t\t\t\tthrough.create = updateFields(source.through.create, Acl.Operation.create)\n\t\t\t}\n\t\t\tif (source.through.update !== undefined) {\n\t\t\t\tthrough.update = updateFields(source.through.update, Acl.Operation.update)\n\t\t\t}\n\t\t\tif (source.through.read !== undefined) {\n\t\t\t\tthrough.read = updateFields(source.through.read, Acl.Operation.read)\n\t\t\t}\n\t\t\toperations.through = through\n\t\t}\n\n\t\treturn {\n\t\t\t...entityPermissions,\n\t\t\toperations,\n\t\t}\n\t}\n\nexport const updateAclEveryPredicate = (updater: EntityAclEveryPredicatesUpdater): AclEntityPermissionsUpdater => args => {\n\treturn {\n\t\t...args.entityPermissions,\n\t\tpredicates: Object.fromEntries(\n\t\t\tObject.entries(args.entityPermissions.predicates).map(([name, predicate]) => [\n\t\t\t\tname,\n\t\t\t\tupdater({\n\t\t\t\t\t...args,\n\t\t\t\t\tpredicate,\n\t\t\t\t}),\n\t\t\t]),\n\t\t),\n\t}\n}\n\nexport const updateModel = (...modelUpdate: (ModelUpdater | undefined)[]): SchemaUpdater => args => ({\n\t...args.schema,\n\tmodel: modelUpdate\n\t\t.filter((it): it is ModelUpdater => it !== undefined)\n\t\t.reduce((model, updater) => updater({ ...args, model }), args.schema.model),\n})\n\nexport const updateEntity = (name: string, entityUpdate: EntityUpdater): ModelUpdater => args => {\n\tif (!args.model.entities[name]) {\n\t\tthrow new SchemaUpdateError(`Entity ${name} not found`)\n\t}\n\treturn {\n\t\t...args.model,\n\t\tentities: {\n\t\t\t...args.model.entities,\n\t\t\t[name]: entityUpdate({ ...args, entity: args.model.entities[name] }),\n\t\t},\n\t}\n}\n\nexport const updateEveryEntity = (entityUpdate: EntityUpdater): ModelUpdater => args => ({\n\t...args.model,\n\tentities: Object.fromEntries(\n\t\tObject.entries(args.model.entities).map(([name, entity]) => [name, entityUpdate({ ...args, entity })]),\n\t),\n})\n\nexport const updateField =\n\t<In extends Model.AnyField = Model.AnyField, Out extends Model.AnyField = In>(name: string, fieldUpdater: FieldUpdater<In, Out>): EntityUpdater =>\n\targs => {\n\t\tconst { [name]: field, ...otherFields } = args.entity.fields\n\t\tif (!args.entity.fields[name]) {\n\t\t\tthrow new SchemaUpdateError(`Field ${args.entity.name}::${name} not found`)\n\t\t}\n\t\tconst updatedField = fieldUpdater({ ...args, field: args.entity.fields[name] as In })\n\n\t\treturn {\n\t\t\t...args.entity,\n\t\t\tfields: {\n\t\t\t\t...otherFields,\n\t\t\t\t...(updatedField ? { [name]: updatedField } : {}),\n\t\t\t},\n\t\t}\n\t}\n\nexport const updateEveryField = (fieldUpdater: FieldUpdater<Model.AnyField, Model.AnyField>): EntityUpdater => args => ({\n\t...args.entity,\n\tfields: Object.fromEntries(\n\t\tObject.entries(args.entity.fields)\n\t\t\t.map(([name, field]) => {\n\t\t\t\tconst updatedField = fieldUpdater({ ...args, field })\n\t\t\t\tif (!updatedField) {\n\t\t\t\t\treturn undefined\n\t\t\t\t}\n\t\t\t\treturn [name, updatedField]\n\t\t\t})\n\t\t\t.filter(isDefined),\n\t),\n})\n\nexport const addField = (field: Model.AnyField): EntityUpdater => args => {\n\treturn {\n\t\t...args.entity,\n\t\tfields: {\n\t\t\t...args.entity.fields,\n\t\t\t[field.name]: field,\n\t\t},\n\t}\n}\n\nexport const removeField = (entityName: string, fieldName: string, version: number): SchemaUpdater => {\n\treturn ({ schema }) => {\n\t\tconst entity = schema.model.entities[entityName]\n\t\tconst field = entity.fields[fieldName]\n\t\treturn updateSchema(\n\t\t\tversion >= VERSION_ACL_PATCH\n\t\t\t\t? updateAcl(\n\t\t\t\t\tupdateAclEveryRole(\n\t\t\t\t\t\tupdateAclEveryEntity(\n\t\t\t\t\t\t\tupdateAclFieldPermissions((permissions, entityName) => {\n\t\t\t\t\t\t\t\tif (entityName !== entity.name) {\n\t\t\t\t\t\t\t\t\treturn permissions\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tconst { [fieldName]: field, ...other } = permissions\n\t\t\t\t\t\t\t\treturn {\n\t\t\t\t\t\t\t\t\t...other,\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t\tupdateAclEveryPredicate(({ predicate, entityName }) => {\n\t\t\t\t\t\t\t\tconst processor = new PredicateDefinitionProcessor(schema.model)\n\t\t\t\t\t\t\t\treturn processor.process(schema.model.entities[entityName], predicate, {\n\t\t\t\t\t\t\t\t\thandleColumn: ctx => ctx.entity.name === entity.name && ctx.column.name === field.name ? undefined : ctx.value,\n\t\t\t\t\t\t\t\t\thandleRelation: ctx => ctx.entity.name === entity.name && ctx.relation.name === field.name ? undefined : ctx.value,\n\t\t\t\t\t\t\t\t})\n\t\t\t\t\t\t\t}),\n\t\t\t\t\t\t),\n\t\t\t\t\t),\n\t\t\t\t)\n\t\t\t\t: undefined,\n\t\t\tisRelation(field)\n\t\t\t\t&& isOwningRelation(field)\n\t\t\t\t&& field.inversedBy\n\t\t\t\t&& version >= VERSION_REMOVE_RELATION_INVERSE_SIDE\n\t\t\t\t? removeField(field.target, field.inversedBy, version)\n\t\t\t\t: undefined,\n\t\t\tupdateModel(\n\t\t\t\tupdateEntity(entity.name, ({ entity }) => {\n\t\t\t\t\tconst { [field.name]: removed, ...fields } = entity.fields\n\t\t\t\t\tconst indexes = entity.indexes.filter(index => !index.fields.includes(field.name))\n\t\t\t\t\tconst unique = entity.unique.filter(index => !index.fields.includes(field.name))\n\t\t\t\t\treturn {\n\t\t\t\t\t\t...entity,\n\t\t\t\t\t\tfields,\n\t\t\t\t\t\tindexes,\n\t\t\t\t\t\tunique,\n\t\t\t\t\t}\n\t\t\t\t}),\n\t\t\t\tisRelation(field) && isInverseRelation(field)\n\t\t\t\t\t? updateEntity(\n\t\t\t\t\t\tfield.target,\n\t\t\t\t\t\tupdateField<Model.AnyOwningRelation>(field.ownedBy, ({ field: { inversedBy, ...field } }) => field),\n\t\t\t\t\t)\n\t\t\t\t\t: undefined,\n\t\t\t),\n\t\t)({ schema })\n\t}\n}\n"],"names":["updater","entityName","field","entity"],"mappings":";;;;;AA+CO,MAAM,eAAe,IAAI,kBAAgE,CAAA,SAC/F,cAAc,OAAO,SAAS,EAAE,OAAO,CAAC,QAAQ,YAAY,QAAQ,EAAE,GAAG,MAAM,QAAQ,GAAG,KAAK,MAAM;AAE/F,MAAM,YAAY,CAAC,YAAuC,CAAA,UAAS;AAAA,EACzE,GAAG,KAAK;AAAA,EACR,KAAK,QAAQ,EAAE,QAAQ,KAAK,QAAQ,KAAK,KAAK,OAAO,IAAA,CAAK;AAC3D;AAEO,MAAM,qBAAqB,IAAI,YAA0C,CAAA,UAAS;AAAA,EACxF,GAAG,KAAK;AAAA,EACR,OAAO,OAAO;AAAA,IACb,QAAQ;AAAA,MACP,CAAC,KAAKA,aAAY,IAAI,IAAI,CAAC,CAAC,MAAM,IAAI,MAAM,CAAC,MAAMA,SAAQ,EAAE,GAAG,MAAM,KAAA,CAAM,CAAC,CAAC;AAAA,MAC9E,OAAO,QAAQ,KAAK,IAAI,KAAK;AAAA,IAAA;AAAA,EAC9B;AAEF;AAEO,MAAM,oBAAoB,CAAC,YAAmD,CAAA,UAAS;AAAA,EAC7F,GAAG,KAAK;AAAA,EACR,UAAU,QAAQ,EAAE,GAAG,MAAM,UAAU,KAAK,KAAK,SAAA,CAAU;AAC5D;AAEO,MAAM,uBAAuB,IAAI,YAA2D,CAAA,UAAS;AAAA,EAC3G,GAAG,KAAK;AAAA,EACR,UAAU,OAAO;AAAA,IAChB,QAAQ;AAAA,MACP,CAAC,KAAKA,aACL,IAAI,IAA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