mirror of
https://github.com/directus/directus.git
synced 2026-01-26 17:58:01 -05:00
Shift hook emits to field service (#9351)
* Shift hook emits to field service * Update field hooks to use filter and action * Allow updating filter payload in hook Co-authored-by: rijkvanzanten <rijkvanzanten@me.com>
This commit is contained in:
@@ -246,23 +246,53 @@ export class FieldsService {
|
||||
schema: this.schema,
|
||||
});
|
||||
|
||||
if (field.type && ALIAS_TYPES.includes(field.type) === false) {
|
||||
const hookAdjustedField = await emitter.emitFilter(
|
||||
`fields.create`,
|
||||
field,
|
||||
{
|
||||
collection: collection,
|
||||
},
|
||||
{
|
||||
database: trx,
|
||||
schema: this.schema,
|
||||
accountability: this.accountability,
|
||||
}
|
||||
);
|
||||
|
||||
if (hookAdjustedField.type && ALIAS_TYPES.includes(hookAdjustedField.type) === false) {
|
||||
if (table) {
|
||||
this.addColumnToTable(table, field as Field);
|
||||
this.addColumnToTable(table, hookAdjustedField as Field);
|
||||
} else {
|
||||
await trx.schema.alterTable(collection, (table) => {
|
||||
this.addColumnToTable(table, field as Field);
|
||||
this.addColumnToTable(table, hookAdjustedField as Field);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
if (field.meta) {
|
||||
await itemsService.createOne({
|
||||
...field.meta,
|
||||
collection: collection,
|
||||
field: field.field,
|
||||
});
|
||||
if (hookAdjustedField.meta) {
|
||||
await itemsService.createOne(
|
||||
{
|
||||
...hookAdjustedField.meta,
|
||||
collection: collection,
|
||||
field: hookAdjustedField.field,
|
||||
},
|
||||
{ emitEvents: false }
|
||||
);
|
||||
}
|
||||
|
||||
emitter.emitAction(
|
||||
`fields.create`,
|
||||
{
|
||||
payload: hookAdjustedField,
|
||||
key: hookAdjustedField.field,
|
||||
collection: collection,
|
||||
},
|
||||
{
|
||||
database: getDatabase(),
|
||||
schema: this.schema,
|
||||
accountability: this.accountability,
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
if (this.cache && env.CACHE_AUTO_PURGE) {
|
||||
@@ -277,13 +307,31 @@ export class FieldsService {
|
||||
throw new ForbiddenException();
|
||||
}
|
||||
|
||||
if (field.schema) {
|
||||
const existingColumn = await this.schemaInspector.columnInfo(collection, field.field);
|
||||
const hookAdjustedField = await emitter.emitFilter(
|
||||
`fields.update`,
|
||||
field,
|
||||
{
|
||||
keys: [field.field],
|
||||
collection: collection,
|
||||
},
|
||||
{
|
||||
database: this.knex,
|
||||
schema: this.schema,
|
||||
accountability: this.accountability,
|
||||
}
|
||||
);
|
||||
|
||||
if (!isEqual(existingColumn, field.schema)) {
|
||||
const record = field.meta
|
||||
? await this.knex.select('id').from('directus_fields').where({ collection, field: field.field }).first()
|
||||
: null;
|
||||
|
||||
if (hookAdjustedField.schema) {
|
||||
const existingColumn = await this.schemaInspector.columnInfo(collection, hookAdjustedField.field);
|
||||
|
||||
if (!isEqual(existingColumn, hookAdjustedField.schema)) {
|
||||
try {
|
||||
await this.knex.schema.alterTable(collection, (table) => {
|
||||
if (!field.schema) return;
|
||||
if (!hookAdjustedField.schema) return;
|
||||
this.addColumnToTable(table, field, existingColumn);
|
||||
});
|
||||
} catch (err: any) {
|
||||
@@ -292,25 +340,26 @@ export class FieldsService {
|
||||
}
|
||||
}
|
||||
|
||||
if (field.meta) {
|
||||
const record = await this.knex
|
||||
.select('id')
|
||||
.from('directus_fields')
|
||||
.where({ collection, field: field.field })
|
||||
.first();
|
||||
|
||||
if (hookAdjustedField.meta) {
|
||||
if (record) {
|
||||
await this.itemsService.updateOne(record.id, {
|
||||
...field.meta,
|
||||
collection: collection,
|
||||
field: field.field,
|
||||
});
|
||||
await this.itemsService.updateOne(
|
||||
record.id,
|
||||
{
|
||||
...hookAdjustedField.meta,
|
||||
collection: collection,
|
||||
field: hookAdjustedField.field,
|
||||
},
|
||||
{ emitEvents: false }
|
||||
);
|
||||
} else {
|
||||
await this.itemsService.createOne({
|
||||
...field.meta,
|
||||
collection: collection,
|
||||
field: field.field,
|
||||
});
|
||||
await this.itemsService.createOne(
|
||||
{
|
||||
...hookAdjustedField.meta,
|
||||
collection: collection,
|
||||
field: hookAdjustedField.field,
|
||||
},
|
||||
{ emitEvents: false }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -320,6 +369,20 @@ export class FieldsService {
|
||||
|
||||
await this.systemCache.clear();
|
||||
|
||||
emitter.emitAction(
|
||||
`fields.update`,
|
||||
{
|
||||
payload: hookAdjustedField,
|
||||
keys: [hookAdjustedField.field],
|
||||
collection: collection,
|
||||
},
|
||||
{
|
||||
database: getDatabase(),
|
||||
schema: this.schema,
|
||||
accountability: this.accountability,
|
||||
}
|
||||
);
|
||||
|
||||
return field.field;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user