Added database creation upon start of API, only does this when the Database doesn't exist.
This commit is contained in:
@@ -12,6 +12,12 @@ import {
|
|||||||
Router,
|
Router,
|
||||||
} from "https://deno.land/x/oak@v17.1.2/mod.ts";
|
} from "https://deno.land/x/oak@v17.1.2/mod.ts";
|
||||||
import { oakCors } from "https://deno.land/x/cors@v1.2.2/mod.ts";
|
import { oakCors } from "https://deno.land/x/cors@v1.2.2/mod.ts";
|
||||||
|
import {
|
||||||
|
dirname,
|
||||||
|
fromFileUrl,
|
||||||
|
join,
|
||||||
|
} from "https://deno.land/std@0.224.0/path/mod.ts";
|
||||||
|
|
||||||
import * as db_utils from "../database/utils.ts";
|
import * as db_utils from "../database/utils.ts";
|
||||||
import * as helper_utils from "./helpers.ts";
|
import * as helper_utils from "./helpers.ts";
|
||||||
|
|
||||||
@@ -48,6 +54,9 @@ type ApiResponse = {
|
|||||||
body: unknown;
|
body: unknown;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// database creation if missing, runs here because this is the main file executed by the API.
|
||||||
|
db_utils.ensureDatabaseExists();
|
||||||
|
|
||||||
// +++ ROUTER ------------------------------------------------------- //
|
// +++ ROUTER ------------------------------------------------------- //
|
||||||
// Creates the routes for the API server.
|
// Creates the routes for the API server.
|
||||||
// Example: localhost:8000/api will show "testAPIPoint"
|
// Example: localhost:8000/api will show "testAPIPoint"
|
||||||
|
|||||||
@@ -55,7 +55,7 @@ export function createDatabase(): void {
|
|||||||
date_created_at TEXT,
|
date_created_at TEXT,
|
||||||
text TEXT,
|
text TEXT,
|
||||||
likes INTEGER
|
likes INTEGER
|
||||||
); -- Added semicolon here
|
);
|
||||||
|
|
||||||
CREATE TABLE IF NOT EXISTS messages (
|
CREATE TABLE IF NOT EXISTS messages (
|
||||||
message_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
message_id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
@@ -105,6 +105,3 @@ export function insertSampleData(): void {
|
|||||||
`,
|
`,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
createDatabase();
|
|
||||||
insertSampleData();
|
|
||||||
|
|||||||
@@ -16,41 +16,37 @@ import * as db_create from "./create_db.ts";
|
|||||||
|
|
||||||
// Import all internal utilities with renamed imports to avoid naming conflicts
|
// Import all internal utilities with renamed imports to avoid naming conflicts
|
||||||
import {
|
import {
|
||||||
// --- Account Functions --- //
|
addMessageToChat as addMessageToChatInternal,
|
||||||
getAllUsersFromDB as getAllUsersFromDBInternal,
|
createChat as createChatInternal,
|
||||||
getUserByUsername as getUserByUsernameInternal,
|
|
||||||
// getAllUserInfoByID as getAllUserInfoByIDInternal,
|
|
||||||
// Accidentally deleted function...
|
|
||||||
registerUser as registerUserInternal,
|
|
||||||
|
|
||||||
// --- Post Functions --- //
|
|
||||||
getPostsFromDB as getPostsFromDBInternal,
|
|
||||||
getPostById as getPostByIdInternal,
|
|
||||||
createPost as createPostInternal,
|
|
||||||
updatePost as updatePostInternal,
|
|
||||||
deletePost as deletePostInternal,
|
|
||||||
likePost as likePostInternal,
|
|
||||||
filterPosts,
|
|
||||||
|
|
||||||
// --- Comment Functions --- //
|
|
||||||
getCommentsFromDB as getCommentsFromDBInternal,
|
|
||||||
// getCommentsForComments as getCommentsForCommentsInternal,
|
// getCommentsForComments as getCommentsForCommentsInternal,
|
||||||
// Accidentally deleted function...
|
// Accidentally deleted function...
|
||||||
createComment as createCommentInternal,
|
createComment as createCommentInternal,
|
||||||
updateComment as updateCommentInternal,
|
createPost as createPostInternal,
|
||||||
|
deleteChat as deleteChatInternal,
|
||||||
deleteComment as deleteCommentInternal,
|
deleteComment as deleteCommentInternal,
|
||||||
likeComment as likeCommentInternal,
|
deletePost as deletePostInternal,
|
||||||
|
filterPosts,
|
||||||
// --- Chat Functions --- //
|
// --- Account Functions --- //
|
||||||
getUserChats as getUserChatsInternal,
|
getAllUsersFromDB as getAllUsersFromDBInternal,
|
||||||
getChatById as getChatByIdInternal,
|
getChatById as getChatByIdInternal,
|
||||||
getChatMessages as getChatMessagesInternal,
|
getChatMessages as getChatMessagesInternal,
|
||||||
createChat as createChatInternal,
|
// --- Comment Functions --- //
|
||||||
addMessageToChat as addMessageToChatInternal,
|
getCommentsFromDB as getCommentsFromDBInternal,
|
||||||
deleteChat as deleteChatInternal,
|
getPostById as getPostByIdInternal,
|
||||||
|
// --- Post Functions --- //
|
||||||
|
getPostsFromDB as getPostsFromDBInternal,
|
||||||
|
getUserByUsername as getUserByUsernameInternal,
|
||||||
|
// --- Chat Functions --- //
|
||||||
|
getUserChats as getUserChatsInternal,
|
||||||
|
likeComment as likeCommentInternal,
|
||||||
|
likePost as likePostInternal,
|
||||||
// --- Mapper Functions --- //
|
// --- Mapper Functions --- //
|
||||||
queryDatabase as queryDatabaseInternal,
|
queryDatabase as queryDatabaseInternal,
|
||||||
|
// getAllUserInfoByID as getAllUserInfoByIDInternal,
|
||||||
|
// Accidentally deleted function...
|
||||||
|
registerUser as registerUserInternal,
|
||||||
|
updateComment as updateCommentInternal,
|
||||||
|
updatePost as updatePostInternal,
|
||||||
} from "./helpers/utils/mod.ts";
|
} from "./helpers/utils/mod.ts";
|
||||||
|
|
||||||
// +++ VARIABLES ---------------------------------------------------- //
|
// +++ VARIABLES ---------------------------------------------------- //
|
||||||
@@ -64,11 +60,11 @@ const db = new DB(dbPath);
|
|||||||
// +++ INTERFACES --------------------------------------------------- //
|
// +++ INTERFACES --------------------------------------------------- //
|
||||||
// Only re-export interfaces that are needed by external code
|
// Only re-export interfaces that are needed by external code
|
||||||
export type {
|
export type {
|
||||||
Post,
|
|
||||||
Accounts,
|
Accounts,
|
||||||
Comments,
|
|
||||||
Chat,
|
Chat,
|
||||||
|
Comments,
|
||||||
Message,
|
Message,
|
||||||
|
Post,
|
||||||
} from "./helpers/interfaces.ts";
|
} from "./helpers/interfaces.ts";
|
||||||
// +++ HELPER FUNCTIONS --------------------------------------------- //
|
// +++ HELPER FUNCTIONS --------------------------------------------- //
|
||||||
export function queryDatabase<T>(
|
export function queryDatabase<T>(
|
||||||
@@ -80,6 +76,42 @@ export function queryDatabase<T>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
// +++ DATABASE INITIALIZATION -------------------------------------- //
|
// +++ DATABASE INITIALIZATION -------------------------------------- //
|
||||||
|
export async function ensureDatabaseExists(): Promise<void> {
|
||||||
|
try {
|
||||||
|
// Check if the database directory exists, create it if not
|
||||||
|
const dbDir = dirname(dbPath);
|
||||||
|
try {
|
||||||
|
await Deno.stat(dbDir);
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof Deno.errors.NotFound) {
|
||||||
|
// Create the database directory
|
||||||
|
await Deno.mkdir(dbDir, { recursive: true });
|
||||||
|
console.log(`Created database directory: ${dbDir}`);
|
||||||
|
} else {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the database file exists
|
||||||
|
try {
|
||||||
|
await Deno.stat(dbPath);
|
||||||
|
console.log("Database file already exists");
|
||||||
|
} catch (error) {
|
||||||
|
if (error instanceof Deno.errors.NotFound) {
|
||||||
|
// Nothing, file will be created below
|
||||||
|
} else {
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
createDatabaseIfNotExist();
|
||||||
|
insertSamples();
|
||||||
|
} catch (error) {
|
||||||
|
console.error("Error ensuring database exists:", error);
|
||||||
|
throw error;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
export function createDatabaseIfNotExist(): void {
|
export function createDatabaseIfNotExist(): void {
|
||||||
db_create.createDatabase();
|
db_create.createDatabase();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user