From 9ed98cc56d05afff75e2d8b9b1dc22c0c434acc8 Mon Sep 17 00:00:00 2001 From: Yogesh01000100 Date: Tue, 19 Nov 2024 14:36:19 +0000 Subject: [PATCH] feat: add knex database integration Co-authored-by: Rafael Belchior Signed-off-by: Yogesh01000100 --- .../docker-compose.yml | 15 ++++++++ .../cactus-plugin-satp-hermes/package.json | 11 +++++- .../src/knex/knexfile-remote.ts | 18 +++++++++- .../src/knex/knexfile.ts | 23 ++++++++++-- .../20220331132128_create_logs_table.js | 15 -------- .../20220331132128_create_logs_table.ts | 16 +++++++++ ...20240130234303_create_remote_logs_table.js | 13 ------- ...20240130234303_create_remote_logs_table.ts | 14 ++++++++ .../seeds/1724235145_create_dummy_entries.ts | 35 +++++++++++++++++++ 9 files changed, 128 insertions(+), 32 deletions(-) delete mode 100644 packages/cactus-plugin-satp-hermes/src/knex/migrations/20220331132128_create_logs_table.js create mode 100644 packages/cactus-plugin-satp-hermes/src/knex/migrations/20220331132128_create_logs_table.ts delete mode 100644 packages/cactus-plugin-satp-hermes/src/knex/migrations/20240130234303_create_remote_logs_table.js create mode 100644 packages/cactus-plugin-satp-hermes/src/knex/migrations/20240130234303_create_remote_logs_table.ts create mode 100644 packages/cactus-plugin-satp-hermes/src/knex/seeds/1724235145_create_dummy_entries.ts diff --git a/packages/cactus-plugin-satp-hermes/docker-compose.yml b/packages/cactus-plugin-satp-hermes/docker-compose.yml index 75cc6307e4..f3d25c4350 100644 --- a/packages/cactus-plugin-satp-hermes/docker-compose.yml +++ b/packages/cactus-plugin-satp-hermes/docker-compose.yml @@ -9,3 +9,18 @@ services: - 3010:3010/tcp # SERVER_PORT - 3011:3011/tcp # CLIENT_PORT - 4010:4010/tcp # API_PORT + db: + image: postgres:13 + environment: + POSTGRES_DB: ${DB_NAME} + POSTGRES_USER: ${DB_USER} + POSTGRES_PASSWORD: ${DB_PASSWORD} + POSTGRES_HOST: ${DB_HOST} + PGPORT: ${DB_PORT} + ports: + - "${DB_PORT}:5432" + volumes: + - pgdata:/var/lib/postgresql/data + +volumes: + pgdata: \ No newline at end of file diff --git a/packages/cactus-plugin-satp-hermes/package.json b/packages/cactus-plugin-satp-hermes/package.json index 5c2cbef961..7f5b108cf3 100644 --- a/packages/cactus-plugin-satp-hermes/package.json +++ b/packages/cactus-plugin-satp-hermes/package.json @@ -80,7 +80,16 @@ "pretsc": "npm run generate-sdk", "start-gateway": "node ./dist/lib/main/typescript/plugin-satp-hermes-gateway-cli.js", "tsc": "tsc --project ./tsconfig.json", - "watch": "tsc --build --watch" + "watch": "tsc --build --watch", + "db:setup": "bash -c 'npm run db:destroy || true && run-s db:start db:migrate db:seed'", + "db:destroy": "docker-compose down -v && npm run db:cleanup", + "db:start": "docker-compose up -d", + "db:stop": "docker-compose down", + "db:reset": "run-s db:destroy db:start db:migrate db:seed", + "db:migrate": "knex migrate:latest --knexfile src/knex/knexfile.js", + "db:migrate:production": "knex migrate:latest --env production --knexfile src/knex/knexfile.ts", + "db:seed": "knex seed:run --knexfile src/knex/knexfile.ts", + "db:cleanup": "find src/knex/data -name '.dev-*.sqlite3' -delete" }, "jest": { "moduleNameMapper": { diff --git a/packages/cactus-plugin-satp-hermes/src/knex/knexfile-remote.ts b/packages/cactus-plugin-satp-hermes/src/knex/knexfile-remote.ts index d2a2e41600..98a8c4332c 100644 --- a/packages/cactus-plugin-satp-hermes/src/knex/knexfile-remote.ts +++ b/packages/cactus-plugin-satp-hermes/src/knex/knexfile-remote.ts @@ -1,7 +1,10 @@ import path from "path"; import { v4 as uuidv4 } from "uuid"; +import dotenv from "dotenv"; + +const envPath = process.env.ENV_PATH; +dotenv.config({ path: envPath }); -// default configuration for knex module.exports = { development: { client: "sqlite3", @@ -13,4 +16,17 @@ module.exports = { }, useNullAsDefault: true, }, + production: { + client: "pg", + connection: { + host: process.env.DB_HOST, + port: process.env.DB_PORT, + user: process.env.DB_USER, + password: process.env.DB_PASSWORD, + database: process.env.DB_NAME, + }, + migrations: { + directory: path.resolve(__dirname, "migrations"), + }, + }, }; diff --git a/packages/cactus-plugin-satp-hermes/src/knex/knexfile.ts b/packages/cactus-plugin-satp-hermes/src/knex/knexfile.ts index 47c34e0875..9bf0da1e63 100644 --- a/packages/cactus-plugin-satp-hermes/src/knex/knexfile.ts +++ b/packages/cactus-plugin-satp-hermes/src/knex/knexfile.ts @@ -1,16 +1,35 @@ import path from "path"; import { v4 as uuidv4 } from "uuid"; +import dotenv from "dotenv"; + +const envPath = process.env.ENV_PATH; +dotenv.config({ path: envPath }); -// default configuration for knex module.exports = { development: { client: "sqlite3", connection: { - filename: path.resolve(__dirname, ".dev-" + uuidv4() + ".sqlite3"), + filename: path.join(__dirname, "data", "/.dev-" + uuidv4() + ".sqlite3"), }, migrations: { directory: path.resolve(__dirname, "migrations"), }, + seeds: { + directory: path.resolve(__dirname, "seeds"), + }, useNullAsDefault: true, }, + production: { + client: "pg", + connection: { + host: process.env.DB_HOST, + port: process.env.DB_PORT, + user: process.env.DB_USER, + password: process.env.DB_PASSWORD, + database: process.env.DB_NAME, + }, + migrations: { + directory: path.resolve(__dirname, "migrations"), + }, + }, }; diff --git a/packages/cactus-plugin-satp-hermes/src/knex/migrations/20220331132128_create_logs_table.js b/packages/cactus-plugin-satp-hermes/src/knex/migrations/20220331132128_create_logs_table.js deleted file mode 100644 index 94c6d8712f..0000000000 --- a/packages/cactus-plugin-satp-hermes/src/knex/migrations/20220331132128_create_logs_table.js +++ /dev/null @@ -1,15 +0,0 @@ -exports.up = async (knex) => { - return await knex.schema.createTable("logs", function (table) { - table.string("sessionID").notNullable(); - table.string("type").notNullable(); - table.string("key").notNullable(); - table.string("operation").notNullable(); - table.string("timestamp").notNullable(); - table.string("data").notNullable(); - table.primary("key"); - }); -}; - -exports.down = async (knex) => { - return await knex.schema.dropTable("logs"); -}; diff --git a/packages/cactus-plugin-satp-hermes/src/knex/migrations/20220331132128_create_logs_table.ts b/packages/cactus-plugin-satp-hermes/src/knex/migrations/20220331132128_create_logs_table.ts new file mode 100644 index 0000000000..cbccd81720 --- /dev/null +++ b/packages/cactus-plugin-satp-hermes/src/knex/migrations/20220331132128_create_logs_table.ts @@ -0,0 +1,16 @@ +import { Knex } from "knex"; + +export function up(knex: Knex): Knex.SchemaBuilder { + return knex.schema.createTable("logs", (table) => { + table.string("sessionID").notNullable(); + table.string("type").notNullable(); + table.string("key").notNullable().primary(); + table.string("operation").notNullable(); + table.string("timestamp").notNullable(); + table.string("data").notNullable(); + }); +} + +export function down(knex: Knex): Knex.SchemaBuilder { + return knex.schema.dropTable("logs"); +} diff --git a/packages/cactus-plugin-satp-hermes/src/knex/migrations/20240130234303_create_remote_logs_table.js b/packages/cactus-plugin-satp-hermes/src/knex/migrations/20240130234303_create_remote_logs_table.js deleted file mode 100644 index 50625d7833..0000000000 --- a/packages/cactus-plugin-satp-hermes/src/knex/migrations/20240130234303_create_remote_logs_table.js +++ /dev/null @@ -1,13 +0,0 @@ -exports.up = async (knex) => { - return await knex.schema.createTable("remote-logs", function (table) { - table.string("key").notNullable(); - table.string("hash").notNullable(); - table.string("signature").notNullable(); - table.string("signerPubKey").notNullable(); - table.primary("key"); - }); -}; - -exports.down = async (knex) => { - return await knex.schema.dropTable("remote-logs"); -}; diff --git a/packages/cactus-plugin-satp-hermes/src/knex/migrations/20240130234303_create_remote_logs_table.ts b/packages/cactus-plugin-satp-hermes/src/knex/migrations/20240130234303_create_remote_logs_table.ts new file mode 100644 index 0000000000..7b51ef32c1 --- /dev/null +++ b/packages/cactus-plugin-satp-hermes/src/knex/migrations/20240130234303_create_remote_logs_table.ts @@ -0,0 +1,14 @@ +import { Knex } from "knex"; + +export async function up(knex: Knex): Promise { + return knex.schema.createTable("remote-logs", (table) => { + table.string("hash").notNullable(); + table.string("signature").notNullable(); + table.string("signerPubKey").notNullable(); + table.string("key").notNullable().primary(); + }); +} + +export async function down(knex: Knex): Promise { + return knex.schema.dropTable("remote-logs"); +} diff --git a/packages/cactus-plugin-satp-hermes/src/knex/seeds/1724235145_create_dummy_entries.ts b/packages/cactus-plugin-satp-hermes/src/knex/seeds/1724235145_create_dummy_entries.ts new file mode 100644 index 0000000000..6958544a30 --- /dev/null +++ b/packages/cactus-plugin-satp-hermes/src/knex/seeds/1724235145_create_dummy_entries.ts @@ -0,0 +1,35 @@ +// 20240821000000_seed_dev_logs.ts + +import { Knex } from "knex"; + +export async function seed(knex: Knex): Promise { + // Check if we're in the development environment + if (process.env.NODE_ENV !== "development") { + console.log("Skipping seed: Not in development environment"); + return; + } + + // Function to clear table if it exists + async function clearTableIfExists(tableName: string) { + if (await knex.schema.hasTable(tableName)) { + await knex(tableName).del(); + console.log(`Cleared existing entries from ${tableName}`); + } else { + console.log(`Table ${tableName} does not exist, skipping clear`); + } + } + + // Clear existing entries if tables exist + await clearTableIfExists("logs"); + await clearTableIfExists("remote-logs"); + + // Insert a single deterministic log entry + await knex("logs").insert({ + sessionID: "test-session-001", + type: "info", + key: "test-log-001", + operation: "create", + timestamp: "2024-08-21T12:00:00Z", + data: JSON.stringify({ message: "This is a test log entry" }), + }); +}