Upgrade database schema to version 2 and remove unused meta_* columns

- Increment `SCHEMA_VERSION` to 2 in `schema.ts`.
- Remove all `meta_*` columns from database tables.
- Add migration logic to handle schema upgrades and clean up unused columns.
- Modify database models and repository methods to exclude `meta_*` fields for stricter typings and improved structure.
- Refactor and optimize query statements across repositories to align with new schema changes.
This commit is contained in:
natreex
2025-11-26 19:17:40 -05:00
parent 736b9a3609
commit 9648d9e9be
13 changed files with 178 additions and 131 deletions

View File

@@ -15,7 +15,6 @@ export interface BookQuery extends Record<string, SQLiteValue> {
desired_word_count: number | null;
words_count: number | null;
cover_image: string | null;
book_meta: string | null;
}
export interface GuideLineQuery extends Record<string, SQLiteValue> {
@@ -29,7 +28,6 @@ export interface GuideLineQuery extends Record<string, SQLiteValue> {
pacing: string;
intended_audience: string;
key_messages: string;
meta_guide_line: string;
}
export interface PlotPointQuery extends Record<string, SQLiteValue> {
@@ -37,43 +35,32 @@ export interface PlotPointQuery extends Record<string, SQLiteValue> {
title: string;
summary: string;
linked_incident_id: string | null;
meta_plot: string;
}
export interface IncidentQuery extends Record<string, SQLiteValue> {
incident_id: string;
title: string;
summary: string;
meta_incident: string;
}
export interface IssueQuery extends Record<string, SQLiteValue> {
issue_id: string;
name: string;
meta_issue: string;
}
export interface ActQuery extends Record<string, SQLiteValue> {
act_index: number;
summary: string;
meta_acts: string;
}
export interface MetaBookQuery extends Record<string, SQLiteValue> {
book_meta: string;
}
export interface BookCoverQuery extends Record<string, SQLiteValue> {
cover_image: string;
book_meta: string;
}
export interface ChapterBookResult extends Record<string, SQLiteValue> {
title: string;
chapter_order: number;
meta_chapter: string;
content: string | null;
meta_chapter_content: string | null;
}
export interface WorldQuery extends Record<string, SQLiteValue> {
@@ -84,12 +71,10 @@ export interface WorldQuery extends Record<string, SQLiteValue> {
economy: string | null;
religion: string | null;
languages: string | null;
meta_world: string;
element_id: string | null;
element_name: string | null;
element_description: string | null;
element_type: number | null;
meta_element: string | null;
}
export interface WorldElementValue {
@@ -118,7 +103,7 @@ export default class BookRepo {
public static fetchBooks(userId: string, lang: 'fr' | 'en'): BookQuery[] {
try {
const db: Database = System.getDb();
return db.all('SELECT book_id,type,author_id,`title`,`sub_title`,summary,serie_id,desired_release_date,desired_word_count,words_count,cover_image,`book_meta` FROM erit_books WHERE author_id = ? ORDER BY book_id DESC', [userId]) as BookQuery[];
return db.all('SELECT book_id, type, author_id, title, sub_title, summary, serie_id, desired_release_date, desired_word_count, words_count, cover_image FROM erit_books WHERE author_id = ? ORDER BY book_id DESC', [userId]) as BookQuery[];
} catch (error: unknown) {
if (error instanceof Error) {
console.error(error.message);
@@ -150,7 +135,7 @@ export default class BookRepo {
let result: BookQuery;
try {
const db: Database = System.getDb();
result = db.get('SELECT book_id, author_id, `title`, `summary`, `sub_title`, `cover_image`,`desired_release_date`, desired_word_count, `words_count`, book_meta FROM `erit_books` WHERE `book_id`=? AND author_id=?', [bookId, userId]) as BookQuery;
result = db.get('SELECT book_id, author_id, title, summary, sub_title, cover_image, desired_release_date, desired_word_count, words_count FROM erit_books WHERE book_id=? AND author_id=?', [bookId, userId]) as BookQuery;
} catch (error: unknown) {
if (error instanceof Error) {
console.error(`DB Error: ${error.message}`);
@@ -201,7 +186,7 @@ export default class BookRepo {
public static fetchAllActs(userId: string, bookId: string, lang: 'fr' | 'en'): ActQuery[] {
try {
const db: Database = System.getDb();
return db.all('SELECT `act_index`,`summary`,`meta_acts` FROM `book_act_summaries` WHERE `book_id`=? AND `user_id`=?', [bookId, userId]) as ActQuery[];
return db.all('SELECT act_index, summary FROM book_act_summaries WHERE book_id=? AND user_id=?', [bookId, userId]) as ActQuery[];
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.message}`);
@@ -215,7 +200,7 @@ export default class BookRepo {
public static fetchAllIncitentIncidents(userId:string,bookId:string, lang: 'fr' | 'en'):IncidentQuery[]{
try {
const db: Database = System.getDb();
return db.all('SELECT incident_id,`title`,`summary`,`meta_incident` FROM `book_incidents` WHERE `author_id`=? AND `book_id`=?', [userId, bookId]) as IncidentQuery[];
return db.all('SELECT incident_id, title, summary FROM book_incidents WHERE author_id=? AND book_id=?', [userId, bookId]) as IncidentQuery[];
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.message}`);
@@ -229,7 +214,7 @@ export default class BookRepo {
public static fetchAllPlotPoints(userId:string,bookId:string, lang: 'fr' | 'en'):PlotPointQuery[]{
try {
const db: Database = System.getDb();
return db.all('SELECT plot_point_id,`title`,`summary`,`linked_incident_id`,`meta_plot` FROM `book_plot_points` WHERE `author_id`=? AND `book_id`=?', [userId, bookId]) as PlotPointQuery[];
return db.all('SELECT plot_point_id, title, summary, linked_incident_id FROM book_plot_points WHERE author_id=? AND book_id=?', [userId, bookId]) as PlotPointQuery[];
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.message}`);
@@ -243,7 +228,7 @@ export default class BookRepo {
public static fetchIssuesFromBook(userId:string,bookId:string, lang: 'fr' | 'en'):IssueQuery[]{
try {
const db: Database = System.getDb();
return db.all('SELECT issue_id,`name`,`meta_issue` FROM `book_issues` WHERE `author_id`=? AND `book_id`=?', [userId, bookId]) as IssueQuery[];
return db.all('SELECT issue_id, name FROM book_issues WHERE author_id=? AND book_id=?', [userId, bookId]) as IssueQuery[];
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.message}`);
@@ -278,7 +263,7 @@ export default class BookRepo {
public static fetchBookCover(userId:string,bookId:string, lang: 'fr' | 'en'):BookCoverQuery{
try {
const db: Database = System.getDb();
return db.get('SELECT cover_image, book_meta FROM erit_books WHERE author_id=? AND book_id=?', [userId, bookId]) as BookCoverQuery;
return db.get('SELECT cover_image FROM erit_books WHERE author_id=? AND book_id=?', [userId, bookId]) as BookCoverQuery;
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.message}`);
@@ -529,7 +514,7 @@ export default class BookRepo {
public static fetchWorlds(userId: string, bookId: string, lang: 'fr' | 'en'):WorldQuery[] {
try {
const db: Database = System.getDb();
return db.all('SELECT world.world_id AS world_id, world.name AS world_name,world.history, world.politics, world.economy, world.religion, world.languages, world.meta_world, element.element_id AS element_id,element.name AS element_name, element.description AS element_description, element.element_type, element.meta_element FROM `book_world` AS world LEFT JOIN book_world_elements AS element ON world.world_id=element.world_id WHERE world.author_id=? AND world.book_id=?', [userId, bookId]) as WorldQuery[];
return db.all('SELECT world.world_id AS world_id, world.name AS world_name, world.history, world.politics, world.economy, world.religion, world.languages, element.element_id AS element_id, element.name AS element_name, element.description AS element_description, element.element_type FROM book_world AS world LEFT JOIN book_world_elements AS element ON world.world_id=element.world_id WHERE world.author_id=? AND world.book_id=?', [userId, bookId]) as WorldQuery[];
} catch (e: unknown) {
if (e instanceof Error) {
console.error(`DB Error: ${e.message}`);
@@ -709,7 +694,7 @@ export default class BookRepo {
static fetchCompleteBookChapters(id: string, lang: 'fr' | 'en'): ChapterBookResult[] {
try {
const db: Database = System.getDb();
const result = db.all('SELECT title, chapter_order, meta_chapter, content.content, content.meta_chapter_content FROM `book_chapters` AS chapter LEFT JOIN book_chapter_content AS content ON chapter.chapter_id = content.chapter_id AND content.version = (SELECT MAX(version) FROM book_chapter_content WHERE chapter_id = chapter.chapter_id AND version > 1) WHERE chapter.book_id = ? ORDER BY chapter.chapter_order', [id]) as ChapterBookResult[];
const result = db.all('SELECT title, chapter_order, content.content FROM book_chapters AS chapter LEFT JOIN book_chapter_content AS content ON chapter.chapter_id = content.chapter_id AND content.version = (SELECT MAX(version) FROM book_chapter_content WHERE chapter_id = chapter.chapter_id AND version > 1) WHERE chapter.book_id = ? ORDER BY chapter.chapter_order', [id]) as ChapterBookResult[];
if (result.length === 0) {
throw new Error(lang === 'fr' ? `Aucun chapitre trouvé.` : `No chapters found.`);
}