diff --git a/packages/backend/src/db/migrations/20230810124730_create_config.ts b/packages/backend/src/db/migrations/20230810124730_create_config.ts new file mode 100644 index 00000000..76bc914e --- /dev/null +++ b/packages/backend/src/db/migrations/20230810124730_create_config.ts @@ -0,0 +1,15 @@ +import { Knex } from 'knex'; + +export async function up(knex: Knex): Promise { + return knex.schema.createTable('config', (table) => { + table.uuid('id').primary().defaultTo(knex.raw('gen_random_uuid()')); + table.string('key').unique().notNullable(); + table.jsonb('value').notNullable().defaultTo({}); + + table.timestamps(true, true); + }); +} + +export async function down(knex: Knex): Promise { + return knex.schema.dropTable('config'); +} diff --git a/packages/backend/src/db/migrations/20230810134714_seed_update_config_permissions_to_admin.ts b/packages/backend/src/db/migrations/20230810134714_seed_update_config_permissions_to_admin.ts new file mode 100644 index 00000000..e953a085 --- /dev/null +++ b/packages/backend/src/db/migrations/20230810134714_seed_update_config_permissions_to_admin.ts @@ -0,0 +1,30 @@ +import { Knex } from 'knex'; + +const getPermissionForRole = ( + roleId: string, + subject: string, + actions: string[] +) => + actions.map((action) => ({ + role_id: roleId, + subject, + action, + conditions: [], + })); + +export async function up(knex: Knex): Promise { + const role = (await knex('roles') + .first(['id', 'key']) + .where({ key: 'admin' }) + .limit(1)) as { id: string; key: string }; + + await knex('permissions').insert( + getPermissionForRole(role.id, 'Config', [ + 'update', + ]) + ); +} + +export async function down(knex: Knex): Promise { + await knex('permissions').where({ subject: 'Config' }).delete(); +} diff --git a/packages/backend/src/models/config.ts b/packages/backend/src/models/config.ts new file mode 100644 index 00000000..a42b8f9d --- /dev/null +++ b/packages/backend/src/models/config.ts @@ -0,0 +1,23 @@ +import { IJSONValue } from '@automatisch/types'; +import Base from './base'; + +class Config extends Base { + id!: string; + key!: string; + value!: { data: IJSONValue }; + + static tableName = 'config'; + + static jsonSchema = { + type: 'object', + required: ['key', 'value'], + + properties: { + id: { type: 'string', format: 'uuid' }, + key: { type: 'string', minLength: 1 }, + value: { type: 'object' }, + }, + }; +} + +export default Config;