Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Team Member Support + Developer Env Enhancements #74

Merged
merged 4 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
"plugins": ["@typescript-eslint", "prettier"],
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
"rules": {
"@typescript-eslint/no-unused-vars": "error"
"@typescript-eslint/no-unused-vars": "warn",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/explicit-module-boundary-types": "off"
},
"ignorePatterns": [
"node_modules/",
Expand Down
5 changes: 3 additions & 2 deletions config/database.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ export interface IDatabaseConfig {
}

export class DATABASE_CONFIG {
public static readonly DB_HOST: string | undefined = process.env.DATABASE_HOST;
public static readonly DB_HOST: string | undefined =
process.env.DATABASE_HOST;
public static readonly DB_USERNAME: string | undefined =
process.env.DATABASE_USERNAME;
public static readonly DB_PASSWORD: string | undefined =
Expand All @@ -18,7 +19,7 @@ export class DATABASE_CONFIG {
if (
!DATABASE_CONFIG.DB_HOST ||
!DATABASE_CONFIG.DB_USERNAME ||
!DATABASE_CONFIG.DB_PASSWORD
!DATABASE_CONFIG.DB_PASSWORD
) {
throw new Error('Database configuration not found');
}
Expand Down
48 changes: 38 additions & 10 deletions lib/team.stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export class TeamStack extends cdk.Stack {
const baseLambdaDir = 'dist/';
const teamsLambdaDir = `${baseLambdaDir}/teams`;
const postsLambdaDir = `${baseLambdaDir}/posts`;
const membersLambdaDir = `${baseLambdaDir}/teams/members`;

const apiResources: IApiResources = {
subresources: {
Expand All @@ -51,7 +52,6 @@ export class TeamStack extends cdk.Stack {
subresources: {
'{aid}': {
endpoints: {

DELETE: {
id: 'deletePost',
path: `${postsLambdaDir}/deletePost`,
Expand All @@ -72,24 +72,52 @@ export class TeamStack extends cdk.Stack {
},
},
subresources: {
'{id}': {
'{teamid}': {
endpoints: {
GET: {
id: 'getTeam',
path: `${teamsLambdaDir}/getTeam`,
},
DELETE: {
id: 'deleteTeam',
path: `${teamsLambdaDir}/deleteTeam`,
},
},
subresources: {
members: {
endpoints: {
GET: {
id: 'getMembers',
path: `${membersLambdaDir}/getMembers`,
},
},
subresources: {
'{userid}': {
endpoints: {
DELETE: {
id: 'removeMember',
path: `${membersLambdaDir}/removeMember`,
},
POST: {
id: 'addMember',
path: `${membersLambdaDir}/addMember`,
},
},
},
},
},
},
},
},
},
},
}
}
};

this.apiService = new ApiService(
this,
apiResources,
`${Team_STACK_INFO.NAME}-API`,
lambdaConfigs
);
this.apiService = new ApiService(
this,
apiResources,
`${Team_STACK_INFO.NAME}-API`,
lambdaConfigs
);
}
}
2 changes: 1 addition & 1 deletion lib/templates/apigateway.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as apigateway from 'aws-cdk-lib/aws-apigateway';
import { Construct, IDependable } from 'constructs';
import { Role } from 'aws-cdk-lib/aws-iam';
import {Duration} from "aws-cdk-lib";
import { Duration } from 'aws-cdk-lib';

export type Method = 'GET' | 'POST' | 'PUT' | 'OPTIONS' | 'PATCH' | 'DELETE';

Expand Down
51 changes: 23 additions & 28 deletions lib/user.stack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ export class UserStack extends LPStack {
},
},
subresources: {
'me': {
me: {
endpoints: {
GET: {
id: 'getMe',
path: `${usersLambdaDir}/getMe`,
},
}
},
},
'{id}': {
endpoints: {
Expand All @@ -79,10 +79,10 @@ export class UserStack extends LPStack {
GET: {
id: 'getUserRoles',
path: `${baseLambdaDir}/roles/getUserRoles`,
}
},
},
subresources: {
"{roleId}": {
'{roleId}': {
endpoints: {
POST: {
id: 'addUserRole',
Expand All @@ -92,9 +92,9 @@ export class UserStack extends LPStack {
id: 'deleteUserRole',
path: `${baseLambdaDir}/roles/deleteUserRole`,
},
}
}
}
},
},
},
},
},
},
Expand All @@ -112,7 +112,7 @@ export class UserStack extends LPStack {
},
},
subresources: {
"{id}": {
'{id}': {
endpoints: {
DELETE: {
id: 'deleteFaculty',
Expand All @@ -122,9 +122,9 @@ export class UserStack extends LPStack {
id: 'updateFaculty',
path: `${facultiesLambdaDir}/updateFaculty`,
},
}
}
}
},
},
},
},
roles: {
endpoints: {
Expand All @@ -136,11 +136,9 @@ export class UserStack extends LPStack {
id: 'createRole',
path: `${rolesLambdaDir}/createRole`,
},


},
subresources: {
"{id}": {
'{id}': {
endpoints: {
PATCH: {
id: 'updateRole',
Expand All @@ -150,10 +148,9 @@ export class UserStack extends LPStack {
id: 'deleteRole',
path: `${rolesLambdaDir}/deleteRole`,
},
}

}
}
},
},
},
},
specializations: {
endpoints: {
Expand All @@ -165,10 +162,9 @@ export class UserStack extends LPStack {
id: 'createSpecialization',
path: `${specializationsLambdaDir}/createSpecialization`,
},

},
subresources: {
"{id}": {
'{id}': {
endpoints: {
DELETE: {
id: 'deleteSpecialization',
Expand All @@ -178,9 +174,9 @@ export class UserStack extends LPStack {
id: 'updateSpecialization',
path: `${specializationsLambdaDir}/updateSpecialization`,
},
}
}
}
},
},
},
},
standings: {
endpoints: {
Expand All @@ -192,10 +188,9 @@ export class UserStack extends LPStack {
id: 'createStanding',
path: `${standingsLambdaDir}/createStanding`,
},

},
subresources: {
"{id}": {
'{id}': {
endpoints: {
DELETE: {
id: 'deleteStanding',
Expand All @@ -205,9 +200,9 @@ export class UserStack extends LPStack {
id: 'updateStanding',
path: `${standingsLambdaDir}/updateStanding`,
},
}
}
}
},
},
},
},
},
};
Expand Down
70 changes: 40 additions & 30 deletions resources/database/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import * as path from 'path';
import * as fs from 'fs';
import dotenv from 'dotenv';
import mysql, { Connection } from 'mysql2';
console.log('Connected to PlanetScale!')
console.log('Connected to PlanetScale!');
dotenv.config();

const DATABASE_NAME = 'cosmic-dev';
Expand All @@ -14,11 +14,10 @@ const setUpDatabase = async (
withReset = false
): Promise<void> => {
console.log(chalk.blue('INFO: ') + 'Setting up database');
const tables = await query(
connection,
`SHOW TABLES IN ?? LIKE ?`,
[DATABASE_NAME, 'migrations']
);
const tables = await query(connection, `SHOW TABLES IN ?? LIKE ?`, [
DATABASE_NAME,
'migrations',
]);

if (withReset || tables.length === 0) {
await initializeDatabase(connection);
Expand All @@ -33,17 +32,18 @@ const setUpDatabase = async (

const initializeDatabase = async (connection: Connection): Promise<void> => {
await resetDatabase(connection);
await query(connection, `CREATE DATABASE IF NOT EXISTS ??`, [
DATABASE_NAME,
]);
await query(connection, `USE ??`, [DATABASE_NAME]);
await query(
connection,
`CREATE DATABASE IF NOT EXISTS ??`,
[DATABASE_NAME]
);
await query(connection, `USE ??`, [DATABASE_NAME]);
await query(connection, `CREATE TABLE IF NOT EXISTS migrations (
`CREATE TABLE IF NOT EXISTS migrations (
id INT AUTO_INCREMENT PRIMARY KEY,
timestamp TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
status VARCHAR(255) NOT NULL DEFAULT 'pending'
)`);
)`
);
};

const resetDatabase = async (connection: Connection): Promise<void> => {
Expand All @@ -56,10 +56,13 @@ const resetDatabase = async (connection: Connection): Promise<void> => {
if (database.Database === DATABASE_NAME) {
console.log(
chalk.bold(
'Emptying database ' + chalk.bold.underline(database.Database)
'Emptying database ' +
chalk.bold.underline(database.Database)
)
);
const tables = await query(connection, `SHOW TABLES IN ??`, [database.Database]);
const tables = await query(connection, `SHOW TABLES IN ??`, [
database.Database,
]);
console.log(tables);
for (const table of tables) {
const tableName = Object.values(table)[0];
Expand All @@ -69,9 +72,7 @@ const resetDatabase = async (connection: Connection): Promise<void> => {
}
};

async function runMigrations(
connection: Connection,
): Promise<void> {
async function runMigrations(connection: Connection): Promise<void> {
let files = fs.readdirSync(MIGRATION_PATH);

console.log(chalk.blue('INFO: ') + 'Running migrations');
Expand Down Expand Up @@ -143,7 +144,11 @@ async function executeStatements(
}
}

function query(connection: Connection, query: string, params?: any[]): Promise<any[]> {
function query(
connection: Connection,
query: string,
params?: any[]
): Promise<any[]> {
return new Promise<any[] | any>((resolve, reject) => {
connection.query(query, params, (error, results) => {
if (error) {
Expand All @@ -156,18 +161,23 @@ function query(connection: Connection, query: string, params?: any[]): Promise<a
}

const run = (): void => {
const connection = mysql.createConnection(process.env.DATABASE_URL!)
const connectionUrl = process.env.DATABASE_URL;
if (!connectionUrl) {
console.error(chalk.bgRed('No database connection URL provided'));
return;
}
const connection = mysql.createConnection(connectionUrl);
setUpDatabase(connection, true)
.then(() => {
console.log(chalk.bgGreen('Database setup completed'));
})
.catch((err) => {
console.error(chalk.bgRed(err));
})
.finally(() => {
console.log('Closing connection');
connection.end();
});
.then(() => {
console.log(chalk.bgGreen('Database setup completed'));
})
.catch((err) => {
console.error(chalk.bgRed(err));
})
.finally(() => {
console.log('Closing connection');
connection.end();
});
};

run();
run();
4 changes: 2 additions & 2 deletions scripts/build
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ while getopts ":e:" opt; do
done

# compile TS to JS with hot reload
esbuild ./src/**/*.ts \
find ./src -name "*.ts" | xargs esbuild \
--entry-names=[dir]/[name]/index \
--bundle \
--platform=node \
--target=node18 \
--outdir=./dist/ \
--sourcemap=inline \
--sourcemap=inline

Loading
Loading