Kysely - WIP

* create database migration files
* kysely codegen
* kysely migrate
This commit is contained in:
Philipinho
2024-03-24 16:59:26 +00:00
parent 7d56920ad5
commit d855152dda
19 changed files with 2634 additions and 643 deletions

View File

@ -0,0 +1,63 @@
import { Kysely, sql } from 'kysely';
export async function up(db: Kysely<any>): Promise<void> {
await db.schema
.createTable('page_ordering')
.addColumn('id', 'uuid', (col) =>
col.primaryKey().defaultTo(sql`gen_random_uuid()`),
)
.addColumn('entityId', 'uuid', (col) => col.notNull())
.addColumn('entityType', 'varchar', (col) => col.notNull())
.addColumn('childrenIds', sql`uuid[]`, (col) => col.notNull())
.addColumn('spaceId', 'uuid', (col) => col.notNull())
.addColumn('workspaceId', 'uuid', (col) => col.notNull())
.addColumn('createdAt', 'timestamp', (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addColumn('updatedAt', 'timestamp', (col) =>
col.notNull().defaultTo(sql`now()`),
)
.addColumn('deletedAt', 'timestamp', (col) => col)
.addUniqueConstraint('UQ_page_ordering_entityId_entityType', [
'entityId',
'entityType',
])
.execute();
// foreign key relations
await db.schema
.alterTable('page_ordering')
.addForeignKeyConstraint(
'FK_page_ordering_spaces_spaceId',
['spaceId'],
'spaces',
['id'],
)
.onDelete('cascade')
.execute();
await db.schema
.alterTable('page_ordering')
.addForeignKeyConstraint(
'FK_page_ordering_workspaces_workspaceId',
['workspaceId'],
'workspaces',
['id'],
)
.onDelete('cascade')
.execute();
}
export async function down(db: Kysely<any>): Promise<void> {
await db.schema
.alterTable('page_ordering')
.dropConstraint('FK_page_ordering_spaces_spaceId')
.execute();
await db.schema
.alterTable('page_ordering')
.dropConstraint('FK_page_ordering_workspaces_workspaceId')
.execute();
await db.schema.dropTable('page_ordering').execute();
}