Skip to content

Commit

Permalink
feat: add knex database integration
Browse files Browse the repository at this point in the history
Co-authored-by: Rafael Belchior <[email protected]>
Signed-off-by: Yogesh01000100 <[email protected]>
  • Loading branch information
Yogesh01000100 and RafaelAPB committed Nov 21, 2024
1 parent c625bbb commit 9ed98cc
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 32 deletions.
15 changes: 15 additions & 0 deletions packages/cactus-plugin-satp-hermes/docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
11 changes: 10 additions & 1 deletion packages/cactus-plugin-satp-hermes/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down
18 changes: 17 additions & 1 deletion packages/cactus-plugin-satp-hermes/src/knex/knexfile-remote.ts
Original file line number Diff line number Diff line change
@@ -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",
Expand All @@ -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"),
},
},
};
23 changes: 21 additions & 2 deletions packages/cactus-plugin-satp-hermes/src/knex/knexfile.ts
Original file line number Diff line number Diff line change
@@ -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"),
},
},
};

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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");
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { Knex } from "knex";

export async function up(knex: Knex): Promise<void> {
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<void> {
return knex.schema.dropTable("remote-logs");
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// 20240821000000_seed_dev_logs.ts

import { Knex } from "knex";

export async function seed(knex: Knex): Promise<void> {
// 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" }),
});
}

0 comments on commit 9ed98cc

Please sign in to comment.