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

feat: add db setup script #67

Merged
merged 4 commits into from
Jan 14, 2025
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
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
"format": "dprint fmt --staged",
"typecheck": "tsc --noEmit",
"lint:js": "eslint --fix",
"lint": "npm run typecheck && npm run lint:js"
"lint": "npm run typecheck && npm run lint:js",
"db:setup": "src/db/wrangler-development-script.sh"
},
"dependencies": {
"hono": "^4.6.12"
Expand Down
18 changes: 18 additions & 0 deletions src/controllers/deleteProfile.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import type { Context } from 'hono';
import { StatusCodes } from '../constants/status-codes.ts';

export const deleteProfileById = async (
context: Context<EnvironmentBindings>
) => {
const userId = context.req.param('id');

try {
const { results } = await context.env.database
.prepare('DELETE FROM profile WHERE id = ?')
.bind(userId)
.run();
return context.json(results);
} catch (error) {
return context.json({ err: error.message }, StatusCodes.NOT_FOUND);
}
};
30 changes: 20 additions & 10 deletions src/controllers/getProfiles.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
import type { Context } from 'hono';
import profiles from '../data/profiles.json';
import { StatusCodes } from '../constants/status-codes.ts';

export const getAllProfiles = (context: Context) => context.json(profiles, StatusCodes.OKAY);

export const getProfileById = (context: Context) => {
const id = context.req.param('id');
const profile = profiles.find((currentProfile) => currentProfile.id === id);

if (!profile) {
return context.json({ error: 'Profile not found' }, StatusCodes.NOT_FOUND);
export const getProfileById = async (context: Context<EnvironmentBindings>) => {
const userId = context.req.param('id');
try {
const { results } = await context.env.database
.prepare('SELECT * FROM profile WHERE id = ?')
.bind(userId)
.run();
return context.json(results);
} catch (e) {
return context.json({ err: e.message }, StatusCodes.NOT_FOUND);
}
};

return context.json(profile, StatusCodes.OKAY);
export const getAllProfiles = async (context: Context) => {
try {
const { results } = await context.env.database
.prepare('SELECT * FROM profile')
.all();
return context.json(results);
} catch (e) {
return context.json({ err: e.message }, StatusCodes.NOT_FOUND);
}
};
29 changes: 0 additions & 29 deletions src/controllers/sqlControllers/getProfiles.ts

This file was deleted.

4 changes: 2 additions & 2 deletions src/db/wrangler-development-script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ wrangler d1 execute vms-local-db --local --command "CREATE TABLE IF NOT EXISTS p

wrangler d1 execute vms-local-db --local --command "CREATE TABLE IF NOT EXISTS team (id TEXT PRIMARY KEY NOT NULL UNIQUE COLLATE BINARY, schemaVersion INTEGER NOT NULL, name TEXT NOT NULL, description TEXT, happenedAt TEXT NOT NULL, insertedAt TEXT NOT NULL);"

wrangler d1 execute vms-local-db --local --command "CREATE TABLE IF NOT EXISTS role (id TEXT PRIMARY KEY NOT NULL UNIQUE COLLATE BINARY, schemaVersion INTEGER NOT NULL, role TEXT NOT NULL, description TEXT, teamId TEXT NOT NULL UNIQUE COLLATE BINARY, profileId TEXT NOT NULL UNIQUE COLLATE BINARY, happenedAt TEXT NOT NULL, insertedAt TEXT NOT NULL, FOREIGN KEY (teamId) REFERENCES team(id), FOREIGN KEY (profileId) REFERENCES profile(id));"
wrangler d1 execute vms-local-db --local --command "CREATE TABLE IF NOT EXISTS role (id TEXT PRIMARY KEY NOT NULL UNIQUE COLLATE BINARY, schemaVersion INTEGER NOT NULL, role TEXT NOT NULL, description TEXT, teamId TEXT NOT NULL UNIQUE COLLATE BINARY, profileId TEXT NOT NULL UNIQUE COLLATE BINARY, happenedAt TEXT NOT NULL, insertedAt TEXT NOT NULL, FOREIGN KEY (teamId) REFERENCES team(id), FOREIGN KEY (profileId) REFERENCES profile(id) ON DELETE CASCADE);"

wrangler d1 execute vms-local-db --local --command "CREATE TABLE IF NOT EXISTS event_log (id TEXT PRIMARY KEY NOT NULL UNIQUE COLLATE BINARY, schemaVersion INTEGER NOT NULL, subject TEXT NOT NULL UNIQUE COLLATE BINARY, subjectSource TEXT NOT NULL DEFAULT 'profile' CHECK("subjectSource" IN ('profile', 'team', 'role', 'special')), verb TEXT NOT NULL, object TEXT NOT NULL UNIQUE COLLATE BINARY,objectSource TEXT NOT NULL DEFAULT 'special' CHECK("objectSource" IN ('profile', 'team', 'role', 'special')), happenedAt TEXT NOT NULL, insertedAt TEXT NOT NULL);"

Expand All @@ -24,4 +24,4 @@ wrangler d1 execute vms-local-db --local --command "INSERT INTO role (id, schema

# Insert dummy data into event_log table
wrangler d1 execute vms-local-db --local --command "INSERT INTO event_log (id, schemaVersion, subject, subjectSource, verb, object, objectSource, happenedAt, insertedAt) VALUES ('1', 1, 'John Doe', 'profile', 'created', 'profile', 'profile', '2024-12-17T00:00:00Z', '2024-12-17T00:00:00Z');"
wrangler d1 execute vms-local-db --local --command "INSERT INTO event_log (id, schemaVersion, subject, subjectSource, verb, object, objectSource, happenedAt, insertedAt) VALUES ('2', 1, 'Team Alpha', 'team', 'created', 'team', 'team', '2024-12-17T00:00:00Z', '2024-12-17T00:00:00Z');"
wrangler d1 execute vms-local-db --local --command "INSERT INTO event_log (id, schemaVersion, subject, subjectSource, verb, object, objectSource, happenedAt, insertedAt) VALUES ('2', 1, 'Team Alpha', 'team', 'created', 'team', 'team', '2024-12-17T00:00:00Z', '2024-12-17T00:00:00Z');"
9 changes: 2 additions & 7 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,8 @@ import { Hono } from 'hono';
import { cors } from 'hono/cors';
import profilesRoute from './routes/profiles/profiles';
import teamsRoutes from './routes/teams/teams';
import profilesSqlRoute from './routes/profiles/profilesSql';
import teamsSqlRoute from './routes/teams/teamsSql';



const app = new Hono();

app.get('/', (context) => context.text('Welome to volunteer management system!'));
Expand All @@ -23,12 +20,10 @@ app.use(
})
);

// Existing routes
app.route('/profiles', profilesRoute);
app.route('/teams', teamsRoutes);

app.route('/sql/profiles', profilesSqlRoute);
// TODO: should be changed to interact with real DB
app.route('/sql/teams', teamsSqlRoute);

app.route('/teams', teamsRoutes);

export default app;
10 changes: 6 additions & 4 deletions src/routes/profiles/profiles.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { Hono } from 'hono';
import { getAllProfiles, getProfileById } from '../../controllers/getProfiles';
import { deleteProfileById } from '../../controllers/deleteProfile';
import {
getAllProfiles,
getProfileById
} from '../../controllers/getProfiles';

const profilesRoutes = new Hono();

// Route to get all profiles
profilesRoutes.get('/', getAllProfiles);

// Route to get a profile by ID
profilesRoutes.get('/:id', getProfileById);


profilesRoutes.get('/:id', getProfileById).delete('/:id', deleteProfileById);

export default profilesRoutes;
17 changes: 0 additions & 17 deletions src/routes/profiles/profilesSql.ts

This file was deleted.