refactor: migrate monorepo from pnpm to Bun runtime and workspaces
- root: bun workspaces via package.json, packageManager bun@1.4.0, scripts use bun --filter - backend: Bun.serve, bun test (vitest removed), bun --watch dev script, tsconfig types bun - tests: migrate 6 test files from vitest to bun:test (mock.module) - validate: replace @hono/zod-validator with hono validator + zod safeParse (fixes TS2589) - lockfile: bunfig.toml saveTextLockfile, regenerated bun.lock (text) for turbo - docs: AGENTS.md, README.md, stack.md updated to Bun stack
This commit is contained in:
@@ -4,12 +4,12 @@
|
||||
"private": true,
|
||||
"type": "module",
|
||||
"scripts": {
|
||||
"dev": "tsx watch src/server.ts",
|
||||
"start": "tsx src/server.ts",
|
||||
"dev": "bun --watch src/server.ts",
|
||||
"start": "bun src/server.ts",
|
||||
"build": "prisma generate && tsc",
|
||||
"typecheck": "tsc --noEmit",
|
||||
"test": "vitest run",
|
||||
"test:watch": "vitest",
|
||||
"test": "bun test",
|
||||
"test:watch": "bun test --watch",
|
||||
"lint": "biome check .",
|
||||
"lint:fix": "biome check . --write",
|
||||
"db:generate": "prisma generate",
|
||||
@@ -20,26 +20,23 @@
|
||||
"dependencies": {
|
||||
"@better-auth/passkey": "^1.7.2",
|
||||
"@gruperly/shared": "workspace:*",
|
||||
"@hono/node-server": "^2.1.1",
|
||||
"@hono/zod-validator": "^0.8.0",
|
||||
"@prisma/adapter-pg": "^7.10.0",
|
||||
"@prisma/client": "^7.10.0",
|
||||
"better-auth": "1.7.2",
|
||||
"dotenv": "^16.4.5",
|
||||
"hono": "^4.13.3",
|
||||
"nodemailer": "^9.0.6",
|
||||
"pino": "^9.5.0",
|
||||
"pino-pretty": "^13.0.0",
|
||||
"tsx": "^4.19.2",
|
||||
"zod": "3.24.2"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@biomejs/biome": "^2.4.15",
|
||||
"@gruperly/config": "workspace:*",
|
||||
"@types/bun": "^1.4.0",
|
||||
"@types/node": "^20.17.0",
|
||||
"@types/nodemailer": "^8.0.1",
|
||||
"dotenv": "^16.4.5",
|
||||
"prisma": "^7.10.0",
|
||||
"typescript": "^5.7.0",
|
||||
"vitest": "^2.1.8"
|
||||
"typescript": "^5.7.0"
|
||||
}
|
||||
}
|
||||
@@ -1,57 +1,62 @@
|
||||
import { zValidator } from '@hono/zod-validator';
|
||||
import type { Context } from 'hono';
|
||||
import type { ZodIssue, ZodSchema } from 'zod';
|
||||
import type { Context, MiddlewareHandler } from 'hono';
|
||||
import { validator } from 'hono/validator';
|
||||
import type { ZodTypeAny, z } from 'zod';
|
||||
import { validationProblem } from './problem-builders';
|
||||
import { zodIssuesToRecord } from './zod-issues';
|
||||
|
||||
type ValidationTarget = 'json' | 'query' | 'param' | 'header' | 'form';
|
||||
|
||||
function makeValidator(target: ValidationTarget, schema: ZodSchema) {
|
||||
return zValidator(
|
||||
target,
|
||||
schema,
|
||||
(result: { success: boolean; error?: { issues: ZodIssue[] } }, c: Context) => {
|
||||
if (result.success) {
|
||||
return;
|
||||
}
|
||||
type ValidationInput<S extends ZodTypeAny, T extends ValidationTarget> = {
|
||||
out: { [K in T]: z.output<S> };
|
||||
};
|
||||
|
||||
const requestId = c.get('requestId');
|
||||
const instance = requestId ? `/requests/${requestId}` : undefined;
|
||||
const issues = result.error?.issues ?? [];
|
||||
function makeValidator<S extends ZodTypeAny, T extends ValidationTarget>(
|
||||
target: T,
|
||||
schema: S,
|
||||
): MiddlewareHandler<{}, string, ValidationInput<S, T>> {
|
||||
const middleware = validator(target, (value: unknown, c: Context) => {
|
||||
const result = schema.safeParse(value);
|
||||
|
||||
return c.json(
|
||||
validationProblem({
|
||||
detail: `Invalid ${target} data`,
|
||||
instance,
|
||||
errors: zodIssuesToRecord(issues),
|
||||
}),
|
||||
400,
|
||||
{
|
||||
'Content-Type': 'application/problem+json',
|
||||
},
|
||||
);
|
||||
},
|
||||
);
|
||||
if (result.success) {
|
||||
return result.data;
|
||||
}
|
||||
|
||||
const requestId = c.get('requestId');
|
||||
const instance = requestId ? `/requests/${requestId}` : undefined;
|
||||
|
||||
return c.json(
|
||||
validationProblem({
|
||||
detail: `Invalid ${target} data`,
|
||||
instance,
|
||||
errors: zodIssuesToRecord(result.error.issues),
|
||||
}),
|
||||
400,
|
||||
{
|
||||
'Content-Type': 'application/problem+json',
|
||||
},
|
||||
);
|
||||
});
|
||||
|
||||
return middleware as unknown as MiddlewareHandler<{}, string, ValidationInput<S, T>>;
|
||||
}
|
||||
|
||||
export const validate = {
|
||||
json(schema: ZodSchema) {
|
||||
json<S extends ZodTypeAny>(schema: S) {
|
||||
return makeValidator('json', schema);
|
||||
},
|
||||
|
||||
query(schema: ZodSchema) {
|
||||
query<S extends ZodTypeAny>(schema: S) {
|
||||
return makeValidator('query', schema);
|
||||
},
|
||||
|
||||
param(schema: ZodSchema) {
|
||||
param<S extends ZodTypeAny>(schema: S) {
|
||||
return makeValidator('param', schema);
|
||||
},
|
||||
|
||||
header(schema: ZodSchema) {
|
||||
header<S extends ZodTypeAny>(schema: S) {
|
||||
return makeValidator('header', schema);
|
||||
},
|
||||
|
||||
form(schema: ZodSchema) {
|
||||
form<S extends ZodTypeAny>(schema: S) {
|
||||
return makeValidator('form', schema);
|
||||
},
|
||||
};
|
||||
};
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
import 'dotenv/config';
|
||||
import { type Prisma, PrismaClient } from '@generated/prisma/client';
|
||||
import type { Result } from '@gruperly/shared';
|
||||
import { PrismaPg } from '@prisma/adapter-pg';
|
||||
|
||||
@@ -1,5 +1,3 @@
|
||||
import 'dotenv/config';
|
||||
import { serve } from '@hono/node-server';
|
||||
import app from '@/app';
|
||||
import { getPrismaClient } from '@/lib/prisma';
|
||||
import { logger } from '@/logger';
|
||||
@@ -8,22 +6,13 @@ const port = Number(process.env.PORT ?? 4000);
|
||||
|
||||
logger.info(`Server running on http://localhost:${port}`);
|
||||
|
||||
const server = serve({
|
||||
fetch: app.fetch,
|
||||
const server = Bun.serve({
|
||||
port,
|
||||
fetch: app.fetch,
|
||||
});
|
||||
|
||||
async function shutdown(): Promise<void> {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
server.close((error) => {
|
||||
if (error) {
|
||||
reject(error);
|
||||
return;
|
||||
}
|
||||
|
||||
resolve();
|
||||
});
|
||||
});
|
||||
server.stop(true);
|
||||
await getPrismaClient().$disconnect();
|
||||
}
|
||||
|
||||
|
||||
@@ -1,44 +1,27 @@
|
||||
import { beforeEach, describe, expect, it, mock, vi } from 'bun:test';
|
||||
import { Hono } from 'hono';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const { db } = vi.hoisted(() => {
|
||||
return {
|
||||
db: {
|
||||
group: {
|
||||
findMany: vi.fn(),
|
||||
findFirst: vi.fn(),
|
||||
count: vi.fn(),
|
||||
create: vi.fn(),
|
||||
},
|
||||
groupMember: {
|
||||
create: vi.fn(),
|
||||
},
|
||||
organization: {
|
||||
findUnique: vi.fn(),
|
||||
},
|
||||
} as {
|
||||
group: {
|
||||
findMany: ReturnType<typeof vi.fn>;
|
||||
findFirst: ReturnType<typeof vi.fn>;
|
||||
count: ReturnType<typeof vi.fn>;
|
||||
create: ReturnType<typeof vi.fn>;
|
||||
};
|
||||
groupMember: { create: ReturnType<typeof vi.fn> };
|
||||
organization: { findUnique: ReturnType<typeof vi.fn> };
|
||||
},
|
||||
};
|
||||
});
|
||||
const db = {
|
||||
group: {
|
||||
findMany: mock(),
|
||||
findFirst: mock(),
|
||||
count: mock(),
|
||||
create: mock(),
|
||||
},
|
||||
groupMember: {
|
||||
create: mock(),
|
||||
},
|
||||
organization: {
|
||||
findUnique: mock(),
|
||||
},
|
||||
};
|
||||
|
||||
vi.mock('@/lib/prisma', () => ({
|
||||
mock.module('@/lib/prisma', () => ({
|
||||
default: db,
|
||||
getPrismaClient: vi.fn(),
|
||||
getPrismaClient: mock(),
|
||||
UnitOfWork: class {
|
||||
executeResult = vi.fn(
|
||||
async (cb: (tx: unknown) => Promise<unknown>) => cb(db),
|
||||
);
|
||||
execute = vi.fn(
|
||||
async (cb: (tx: unknown) => Promise<unknown>) => cb(db),
|
||||
);
|
||||
executeResult = mock(async (cb: (tx: unknown) => Promise<unknown>) => cb(db));
|
||||
execute = mock(async (cb: (tx: unknown) => Promise<unknown>) => cb(db));
|
||||
},
|
||||
}));
|
||||
|
||||
@@ -71,8 +54,8 @@ describe('groups routes', () => {
|
||||
});
|
||||
|
||||
it('lists groups scoped to the current user', async () => {
|
||||
vi.mocked(prisma.group.findMany).mockResolvedValue([group]);
|
||||
vi.mocked(prisma.group.count).mockResolvedValue(1);
|
||||
prisma.group.findMany.mockResolvedValue([group]);
|
||||
prisma.group.count.mockResolvedValue(1);
|
||||
|
||||
const res = await makeApp({ id: userId }).request('/groups');
|
||||
|
||||
@@ -118,9 +101,9 @@ describe('groups routes', () => {
|
||||
name: 'Escuela Alfa',
|
||||
members: [{ userId, role: 'owner' }],
|
||||
};
|
||||
vi.mocked(prisma.organization.findUnique).mockResolvedValue(organization as never);
|
||||
vi.mocked(prisma.group.findFirst).mockResolvedValue(null);
|
||||
vi.mocked(prisma.group.create).mockResolvedValue(group);
|
||||
prisma.organization.findUnique.mockResolvedValue(organization as never);
|
||||
prisma.group.findFirst.mockResolvedValue(null);
|
||||
prisma.group.create.mockResolvedValue(group);
|
||||
|
||||
const res = await makeApp({ id: userId }).request('/groups/from-organization', {
|
||||
method: 'POST',
|
||||
@@ -148,8 +131,8 @@ describe('groups routes', () => {
|
||||
name: 'Escuela Alfa',
|
||||
members: [{ userId, role: 'owner' }],
|
||||
};
|
||||
vi.mocked(prisma.organization.findUnique).mockResolvedValue(organization as never);
|
||||
vi.mocked(prisma.group.findFirst).mockResolvedValue(group);
|
||||
prisma.organization.findUnique.mockResolvedValue(organization as never);
|
||||
prisma.group.findFirst.mockResolvedValue(group);
|
||||
|
||||
const res = await makeApp({ id: userId }).request('/groups/from-organization', {
|
||||
method: 'POST',
|
||||
@@ -164,7 +147,7 @@ describe('groups routes', () => {
|
||||
});
|
||||
|
||||
it('rejects creating a group for an unknown organization', async () => {
|
||||
vi.mocked(prisma.organization.findUnique).mockResolvedValue(null);
|
||||
prisma.organization.findUnique.mockResolvedValue(null);
|
||||
|
||||
const res = await makeApp({ id: userId }).request('/groups/from-organization', {
|
||||
method: 'POST',
|
||||
@@ -183,7 +166,7 @@ describe('groups routes', () => {
|
||||
name: 'Escuela Alfa',
|
||||
members: [{ userId: 'other-user', role: 'owner' }],
|
||||
};
|
||||
vi.mocked(prisma.organization.findUnique).mockResolvedValue(organization as never);
|
||||
prisma.organization.findUnique.mockResolvedValue(organization as never);
|
||||
|
||||
const res = await makeApp({ id: userId }).request('/groups/from-organization', {
|
||||
method: 'POST',
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
import { beforeEach, describe, expect, it, mock, vi } from 'bun:test';
|
||||
import { Hono } from 'hono';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
vi.mock('@/lib/prisma', () => ({
|
||||
mock.module('@/lib/prisma', () => ({
|
||||
default: {
|
||||
$queryRaw: vi.fn(),
|
||||
$queryRaw: mock(),
|
||||
},
|
||||
getPrismaClient: vi.fn(),
|
||||
getPrismaClient: mock(),
|
||||
UnitOfWork: class {},
|
||||
}));
|
||||
|
||||
@@ -21,7 +21,7 @@ describe('health check routes', () => {
|
||||
});
|
||||
|
||||
it('reports ok when the database answers', async () => {
|
||||
vi.mocked(prisma.$queryRaw).mockResolvedValue([{ '?column?': 1 }]);
|
||||
prisma.$queryRaw.mockResolvedValue([{ '?column?': 1 }]);
|
||||
|
||||
const res = await app.request('/health');
|
||||
|
||||
@@ -32,7 +32,7 @@ describe('health check routes', () => {
|
||||
});
|
||||
|
||||
it('reports degraded when the database is unreachable', async () => {
|
||||
vi.mocked(prisma.$queryRaw).mockRejectedValue(new Error('connection refused'));
|
||||
prisma.$queryRaw.mockRejectedValue(new Error('connection refused'));
|
||||
|
||||
const res = await app.request('/health');
|
||||
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import { beforeEach, describe, expect, it, mock, vi } from 'bun:test';
|
||||
import { Hono } from 'hono';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
vi.mock('@/lib/prisma', () => ({
|
||||
mock.module('@/lib/prisma', () => ({
|
||||
default: {
|
||||
payment: {
|
||||
findMany: vi.fn(),
|
||||
count: vi.fn(),
|
||||
findMany: mock(),
|
||||
count: mock(),
|
||||
},
|
||||
},
|
||||
getPrismaClient: vi.fn(),
|
||||
getPrismaClient: mock(),
|
||||
UnitOfWork: class {},
|
||||
}));
|
||||
|
||||
@@ -37,8 +37,8 @@ describe('payments routes', () => {
|
||||
});
|
||||
|
||||
it('lists payments with pagination and converts Decimal amounts', async () => {
|
||||
vi.mocked(prisma.payment.findMany).mockResolvedValue([payment]);
|
||||
vi.mocked(prisma.payment.count).mockResolvedValue(1);
|
||||
prisma.payment.findMany.mockResolvedValue([payment]);
|
||||
prisma.payment.count.mockResolvedValue(1);
|
||||
|
||||
const res = await app.request('/payments');
|
||||
|
||||
|
||||
@@ -1,18 +1,20 @@
|
||||
import { beforeEach, describe, expect, it, mock, vi } from 'bun:test';
|
||||
import { Hono } from 'hono';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
vi.mock('@/lib/prisma', () => ({
|
||||
default: {
|
||||
$queryRaw: vi.fn(),
|
||||
},
|
||||
getPrismaClient: vi.fn(),
|
||||
const db = {
|
||||
$queryRaw: mock(),
|
||||
};
|
||||
|
||||
mock.module('@/lib/prisma', () => ({
|
||||
default: db,
|
||||
getPrismaClient: mock(() => db),
|
||||
UnitOfWork: class {},
|
||||
}));
|
||||
|
||||
vi.mock('@/modules/auth/auth', () => ({
|
||||
mock.module('@/modules/auth/auth', () => ({
|
||||
auth: {
|
||||
api: {
|
||||
getSession: vi.fn(),
|
||||
getSession: mock(),
|
||||
},
|
||||
},
|
||||
}));
|
||||
@@ -48,7 +50,7 @@ describe('session auth', () => {
|
||||
});
|
||||
|
||||
it('rejects protected requests without a session', async () => {
|
||||
vi.mocked(auth.api.getSession).mockResolvedValue(null);
|
||||
auth.api.getSession.mockResolvedValue(null);
|
||||
|
||||
const app = new Hono();
|
||||
app.use('*', sessionAuthMiddleware);
|
||||
@@ -65,7 +67,7 @@ describe('session auth', () => {
|
||||
user: { id: 'user-1', name: 'Ana' },
|
||||
session: { id: 'session-1' },
|
||||
};
|
||||
vi.mocked(auth.api.getSession).mockResolvedValue(session as never);
|
||||
auth.api.getSession.mockResolvedValue(session as never);
|
||||
|
||||
const app = new Hono();
|
||||
app.use('*', sessionAuthMiddleware);
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import { beforeEach, describe, expect, it, mock, vi } from 'bun:test';
|
||||
import { Hono } from 'hono';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
vi.mock('@/lib/prisma', () => ({
|
||||
mock.module('@/lib/prisma', () => ({
|
||||
default: {
|
||||
student: {
|
||||
findMany: vi.fn(),
|
||||
count: vi.fn(),
|
||||
findMany: mock(),
|
||||
count: mock(),
|
||||
},
|
||||
},
|
||||
getPrismaClient: vi.fn(),
|
||||
getPrismaClient: mock(),
|
||||
UnitOfWork: class {},
|
||||
}));
|
||||
|
||||
@@ -37,8 +37,8 @@ describe('students routes', () => {
|
||||
});
|
||||
|
||||
it('lists students with pagination', async () => {
|
||||
vi.mocked(prisma.student.findMany).mockResolvedValue([student]);
|
||||
vi.mocked(prisma.student.count).mockResolvedValue(21);
|
||||
prisma.student.findMany.mockResolvedValue([student]);
|
||||
prisma.student.count.mockResolvedValue(21);
|
||||
|
||||
const res = await app.request('/students?page=2&pageSize=10');
|
||||
|
||||
@@ -62,8 +62,8 @@ describe('students routes', () => {
|
||||
});
|
||||
|
||||
it('uses default pagination', async () => {
|
||||
vi.mocked(prisma.student.findMany).mockResolvedValue([]);
|
||||
vi.mocked(prisma.student.count).mockResolvedValue(0);
|
||||
prisma.student.findMany.mockResolvedValue([]);
|
||||
prisma.student.count.mockResolvedValue(0);
|
||||
|
||||
const res = await app.request('/students');
|
||||
|
||||
|
||||
@@ -1,14 +1,14 @@
|
||||
import { beforeEach, describe, expect, it, mock, vi } from 'bun:test';
|
||||
import { Hono } from 'hono';
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
vi.mock('@/lib/prisma', () => ({
|
||||
mock.module('@/lib/prisma', () => ({
|
||||
default: {
|
||||
waitlistEntry: {
|
||||
findMany: vi.fn(),
|
||||
count: vi.fn(),
|
||||
findMany: mock(),
|
||||
count: mock(),
|
||||
},
|
||||
},
|
||||
getPrismaClient: vi.fn(),
|
||||
getPrismaClient: mock(),
|
||||
UnitOfWork: class {},
|
||||
}));
|
||||
|
||||
@@ -33,8 +33,8 @@ describe('waitlist routes', () => {
|
||||
});
|
||||
|
||||
it('lists waitlist entries with pagination', async () => {
|
||||
vi.mocked(prisma.waitlistEntry.findMany).mockResolvedValue([entry]);
|
||||
vi.mocked(prisma.waitlistEntry.count).mockResolvedValue(1);
|
||||
prisma.waitlistEntry.findMany.mockResolvedValue([entry]);
|
||||
prisma.waitlistEntry.count.mockResolvedValue(1);
|
||||
|
||||
const res = await app.request('/waitlist');
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
"extends": "@gruperly/config/tsconfig.base.json",
|
||||
"compilerOptions": {
|
||||
"moduleResolution": "bundler",
|
||||
"types": ["node"],
|
||||
"types": ["bun"],
|
||||
"declaration": false,
|
||||
"noEmit": false,
|
||||
"outDir": "./dist",
|
||||
|
||||
@@ -1,15 +0,0 @@
|
||||
import path from 'node:path';
|
||||
import { defineConfig } from 'vitest/config';
|
||||
|
||||
export default defineConfig({
|
||||
test: {
|
||||
globals: true,
|
||||
environment: 'node',
|
||||
},
|
||||
resolve: {
|
||||
alias: {
|
||||
'@': path.resolve(__dirname, 'src'),
|
||||
'@generated': path.resolve(__dirname, 'generated'),
|
||||
},
|
||||
},
|
||||
});
|
||||
Reference in New Issue
Block a user