feat: add topic hierarchy APIs
This commit is contained in:
@@ -3,6 +3,7 @@ import type { Kysely } from 'kysely';
|
||||
import {
|
||||
FACTS_TABLE,
|
||||
FACT_TOPICS_TABLE,
|
||||
TOPIC_RELATIONS_TABLE,
|
||||
TOPICS_TABLE,
|
||||
} from './schema';
|
||||
import type { IdentityDatabaseSchema } from '../types/database';
|
||||
@@ -52,6 +53,20 @@ export async function initializeSchema(
|
||||
.addPrimaryKeyConstraint('fact_topics_pk', ['fact_id', 'topic_id', 'position'])
|
||||
.execute();
|
||||
|
||||
await db.schema
|
||||
.createTable(TOPIC_RELATIONS_TABLE)
|
||||
.ifNotExists()
|
||||
.addColumn('parent_topic_id', 'text', (column) =>
|
||||
column.notNull().references(`${TOPICS_TABLE}.id`).onDelete('cascade'),
|
||||
)
|
||||
.addColumn('child_topic_id', 'text', (column) =>
|
||||
column.notNull().references(`${TOPICS_TABLE}.id`).onDelete('cascade'),
|
||||
)
|
||||
.addColumn('relation', 'text', (column) => column.notNull())
|
||||
.addColumn('created_at', 'text', (column) => column.notNull())
|
||||
.addPrimaryKeyConstraint('topic_relations_pk', ['parent_topic_id', 'child_topic_id', 'relation'])
|
||||
.execute();
|
||||
|
||||
await db.schema
|
||||
.createIndex('fact_topics_topic_id_idx')
|
||||
.ifNotExists()
|
||||
@@ -65,4 +80,18 @@ export async function initializeSchema(
|
||||
.on(FACT_TOPICS_TABLE)
|
||||
.column('fact_id')
|
||||
.execute();
|
||||
|
||||
await db.schema
|
||||
.createIndex('topic_relations_parent_topic_id_idx')
|
||||
.ifNotExists()
|
||||
.on(TOPIC_RELATIONS_TABLE)
|
||||
.column('parent_topic_id')
|
||||
.execute();
|
||||
|
||||
await db.schema
|
||||
.createIndex('topic_relations_child_topic_id_idx')
|
||||
.ifNotExists()
|
||||
.on(TOPIC_RELATIONS_TABLE)
|
||||
.column('child_topic_id')
|
||||
.execute();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user