feat: create Config model

This commit is contained in:
Ali BARIN
2023-08-10 14:31:26 +00:00
parent 84b701747f
commit bafb8b86db
3 changed files with 68 additions and 0 deletions

View File

@@ -0,0 +1,15 @@
import { Knex } from 'knex';
export async function up(knex: Knex): Promise<void> {
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<void> {
return knex.schema.dropTable('config');
}

View File

@@ -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<void> {
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<void> {
await knex('permissions').where({ subject: 'Config' }).delete();
}

View File

@@ -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;