feat: implement authentication and database modules with relations and group management
Added new authentication strategies (JWT and GitHub OAuth), guards, and controllers. Implemented database module, schema with relations, and group management features, including CRD operations and person-to-group associations. Integrated validation and CORS configuration.
This commit is contained in:
55
backend/src/database/migrations/generate-migrations.ts
Normal file
55
backend/src/database/migrations/generate-migrations.ts
Normal file
@@ -0,0 +1,55 @@
|
||||
import { exec } from 'child_process';
|
||||
import * as path from 'path';
|
||||
import * as fs from 'fs';
|
||||
|
||||
/**
|
||||
* Script to generate migrations using drizzle-kit
|
||||
*
|
||||
* This script:
|
||||
* 1. Runs drizzle-kit generate to create migration files
|
||||
* 2. Ensures the migrations directory exists
|
||||
* 3. Handles errors and provides feedback
|
||||
*/
|
||||
const main = async () => {
|
||||
console.log('Generating migrations...');
|
||||
|
||||
// Ensure migrations directory exists
|
||||
const migrationsDir = path.join(__dirname);
|
||||
if (!fs.existsSync(migrationsDir)) {
|
||||
fs.mkdirSync(migrationsDir, { recursive: true });
|
||||
}
|
||||
|
||||
// Run drizzle-kit generate command
|
||||
const command = 'npx drizzle-kit generate:pg';
|
||||
|
||||
exec(command, (error, stdout, stderr) => {
|
||||
if (error) {
|
||||
console.error(`Error generating migrations: ${error.message}`);
|
||||
return;
|
||||
}
|
||||
|
||||
if (stderr) {
|
||||
console.error(`Migration generation stderr: ${stderr}`);
|
||||
}
|
||||
|
||||
console.log(stdout);
|
||||
console.log('Migrations generated successfully');
|
||||
|
||||
// List generated migration files
|
||||
const files = fs.readdirSync(migrationsDir)
|
||||
.filter(file => file.endsWith('.sql'));
|
||||
|
||||
if (files.length === 0) {
|
||||
console.log('No migration files were generated. Your schema might be up to date.');
|
||||
} else {
|
||||
console.log('Generated migration files:');
|
||||
files.forEach(file => console.log(`- ${file}`));
|
||||
}
|
||||
});
|
||||
};
|
||||
|
||||
main().catch(err => {
|
||||
console.error('Migration generation failed');
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
91
backend/src/database/migrations/migrate.ts
Normal file
91
backend/src/database/migrations/migrate.ts
Normal file
@@ -0,0 +1,91 @@
|
||||
import { drizzle } from 'drizzle-orm/node-postgres';
|
||||
import { migrate } from 'drizzle-orm/node-postgres/migrator';
|
||||
import { Pool } from 'pg';
|
||||
import * as dotenv from 'dotenv';
|
||||
import * as path from 'path';
|
||||
import * as schema from '../schema';
|
||||
|
||||
/**
|
||||
* Script to run database migrations
|
||||
*
|
||||
* This script:
|
||||
* 1. Establishes a connection to the PostgreSQL database
|
||||
* 2. Creates a Drizzle ORM instance
|
||||
* 3. Runs all pending migrations
|
||||
*
|
||||
* It can be used:
|
||||
* - As a standalone script: `node dist/database/migrations/migrate.js`
|
||||
* - Integrated with NestJS application lifecycle in database.service.ts
|
||||
*/
|
||||
|
||||
// Load environment variables
|
||||
dotenv.config();
|
||||
|
||||
export const runMigrations = async (options?: { migrationsFolder?: string }) => {
|
||||
// First try to get the full DATABASE_URL
|
||||
let connectionString = process.env.DATABASE_URL;
|
||||
|
||||
// If DATABASE_URL is not provided, construct it from individual variables
|
||||
if (!connectionString) {
|
||||
const password = process.env.POSTGRES_PASSWORD || 'postgres';
|
||||
const username = process.env.POSTGRES_USER || 'postgres';
|
||||
const host = process.env.POSTGRES_HOST || 'localhost';
|
||||
const port = Number(process.env.POSTGRES_PORT || 5432);
|
||||
const database = process.env.POSTGRES_DB || 'groupmaker';
|
||||
|
||||
connectionString = `postgres://${username}:${password}@${host}:${port}/${database}`;
|
||||
}
|
||||
|
||||
// Create the PostgreSQL pool
|
||||
const pool = new Pool({
|
||||
connectionString,
|
||||
});
|
||||
|
||||
// Create the Drizzle ORM instance
|
||||
const db = drizzle(pool, { schema });
|
||||
|
||||
console.log('Running migrations...');
|
||||
|
||||
try {
|
||||
// Test the connection
|
||||
const client = await pool.connect();
|
||||
try {
|
||||
await client.query('SELECT NOW()');
|
||||
console.log('Database connection established successfully');
|
||||
} finally {
|
||||
client.release();
|
||||
}
|
||||
|
||||
// Determine migrations folder path
|
||||
const migrationsFolder = options?.migrationsFolder || path.join(__dirname);
|
||||
console.log(`Using migrations folder: ${migrationsFolder}`);
|
||||
|
||||
// Run migrations
|
||||
await migrate(db, { migrationsFolder });
|
||||
|
||||
console.log('Migrations completed successfully');
|
||||
return { success: true };
|
||||
} catch (error) {
|
||||
console.error('Migration failed');
|
||||
console.error(error);
|
||||
return { success: false, error };
|
||||
} finally {
|
||||
await pool.end();
|
||||
console.log('Database connection closed');
|
||||
}
|
||||
};
|
||||
|
||||
// Run migrations if this script is executed directly
|
||||
if (require.main === module) {
|
||||
runMigrations()
|
||||
.then(result => {
|
||||
if (!result.success) {
|
||||
process.exit(1);
|
||||
}
|
||||
})
|
||||
.catch(err => {
|
||||
console.error('Migration failed');
|
||||
console.error(err);
|
||||
process.exit(1);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user