Skip to content
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
8 changes: 8 additions & 0 deletions src/generators/node/database.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ describe('generateDatabase — src/database/index.ts', () => {
const content = await read('src/database/index.ts');
expect(content).toContain('postgres');
expect(content).toContain('drizzle-orm/postgres-js');
expect(content).toContain('onnotice');
expect(content).toContain("'42P06'");
expect(content).toContain("'42P07'");
expect(content).toContain('export');
});

Expand All @@ -81,6 +84,7 @@ describe('generateDatabase — src/database/index.ts', () => {
const content = await read('src/database/index.ts');
expect(content).toContain('mysql2');
expect(content).toContain('drizzle-orm/mysql2');
expect(content).toContain("mode: 'default'");
});

it('sqlite client uses @libsql/client', async () => {
Expand Down Expand Up @@ -218,6 +222,7 @@ describe('generateDatabase — docker-compose.yml', () => {
expect(await exists(join(tmpDir, 'docker-compose.yml'))).toBe(true);
const content = await read('docker-compose.yml');
expect(content).toContain('postgres:16');
expect(content).toContain('postgres_data:/var/lib/postgresql/data');
expect(content).toContain('pg_isready');
expect(content).toContain('healthcheck');
});
Expand All @@ -227,6 +232,9 @@ describe('generateDatabase — docker-compose.yml', () => {
await generateDatabase(tmpDir, mysqlOptions);
const content = await read('docker-compose.yml');
expect(content).toContain('mysql:8');
expect(content).toContain('127.0.0.1:3307:3306');
expect(content).not.toContain('3306:3306');
expect(content).toContain('mysql_data:/var/lib/mysql');
expect(content).toContain('mysqladmin');
expect(content).toContain('healthcheck');
});
Expand Down
19 changes: 12 additions & 7 deletions src/generators/node/database.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,12 @@ function dbClientContent(database: GeneratorOptions['database']): string {
import postgres from 'postgres';
import * as schema from './schema.js';

const client = postgres(process.env.DATABASE_URL!);
const client = postgres(process.env.DATABASE_URL!, {
onnotice: (notice) => {
if (notice.code === '42P06' || notice.code === '42P07') return;
console.warn(notice);
},
});
export const db = drizzle(client, { schema });
`;
}
Expand All @@ -122,7 +127,7 @@ function dbClientContent(database: GeneratorOptions['database']): string {
import * as schema from './schema.js';

const pool = mysql.createPool(process.env.DATABASE_URL!);
export const db = drizzle(pool, { schema });
export const db = drizzle(pool, { schema, mode: 'default' });
`;
}

Expand Down Expand Up @@ -264,15 +269,15 @@ async function generateDockerCompose(outputDir: string, options: GeneratorOption
ports:
- '5432:5432'
volumes:
- db_data:/var/lib/postgresql/data
- postgres_data:/var/lib/postgresql/data
healthcheck:
test: ['CMD', 'pg_isready', '-U', 'app']
interval: 5s
timeout: 5s
retries: 5

volumes:
db_data:
postgres_data:
`
: dedent`
services:
Expand All @@ -284,17 +289,17 @@ async function generateDockerCompose(outputDir: string, options: GeneratorOption
MYSQL_USER: app
MYSQL_PASSWORD: app
ports:
- '3306:3306'
- '127.0.0.1:3307:3306'
volumes:
- db_data:/var/lib/mysql
- mysql_data:/var/lib/mysql
healthcheck:
test: ['CMD', 'mysqladmin', 'ping', '-h', 'localhost', '-u', 'app', '--password=app']
interval: 5s
timeout: 5s
retries: 5

volumes:
db_data:
mysql_data:
`;
await writeFile(join(outputDir, 'docker-compose.yml'), content);
}
Expand Down
33 changes: 33 additions & 0 deletions src/generators/node/projectBuilder.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { describe, expect, it } from 'vitest';
import { readFile, rm } from 'node:fs/promises';
import { join } from 'node:path';
import { tmpdir } from 'node:os';
import type { GeneratorOptions } from '../interface.js';
import { NodeProjectBuilder } from './projectBuilder.js';
import type { BuildStep } from './projectBuilder.js';
Expand All @@ -10,6 +13,9 @@ const options: GeneratorOptions = {
appExtensions: [],
};

const tmpDir = join(tmpdir(), 'cpa-project-builder-test');
const read = (p: string) => readFile(join(tmpDir, p), 'utf-8');

function spyStep(tracker: string[], label: string): BuildStep {
return {
execute: async () => {
Expand Down Expand Up @@ -50,4 +56,31 @@ describe('NodeProjectBuilder', () => {
expect(builder.addStep(spyStep([], 'x'))).toBe(builder);
expect(builder.when(false, () => {})).toBe(builder);
});

it('generates MySQL env example with the non-default host port', async () => {
await rm(tmpDir, { recursive: true, force: true });

await new NodeProjectBuilder(tmpDir, { ...options, database: 'mysql' }).addEnvExample().build();

const content = await read('.env.example');
expect(content).toContain('DATABASE_URL=mysql://app:app@localhost:3307/test-app');
expect(content).not.toContain('DATABASE_URL=mysql://app:app@localhost:3306/test-app');

await rm(tmpDir, { recursive: true, force: true });
});

it('generates server entry that retries database startup', async () => {
await rm(tmpDir, { recursive: true, force: true });

await new NodeProjectBuilder(tmpDir, options).addServerEntry().build();

const content = await read('src/index.ts');
expect(content).toContain('STARTUP_RETRY_ATTEMPTS = 60');
expect(content).toContain('STARTUP_RETRY_DELAY_MS = 1000');
expect(content).toContain('async function waitForDatabase()');
expect(content).toContain('await waitForDatabase()');
expect(content).toContain('Database is not ready yet');

await rm(tmpDir, { recursive: true, force: true });
});
});
34 changes: 29 additions & 5 deletions src/generators/node/projectBuilder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,27 @@ class ServerEntryStep implements BuildStep {
import app from './app.js';

const PORT = ${envVarAccess('PORT', '3000')};

await runMigrations();
const STARTUP_RETRY_ATTEMPTS = 60;
const STARTUP_RETRY_DELAY_MS = 1000;

async function waitForDatabase(): Promise<void> {
for (let attempt = 1; attempt <= STARTUP_RETRY_ATTEMPTS; attempt++) {
try {
await runMigrations();
return;
} catch (error) {
if (attempt === STARTUP_RETRY_ATTEMPTS) throw error;

const message = error instanceof Error ? error.message : String(error);
console.warn(
\`Database is not ready yet (\${attempt}/\${STARTUP_RETRY_ATTEMPTS}): \${message}\`,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need \ if we're using backticks ` ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needed in the generator

);
await new Promise<void>((resolve) => setTimeout(resolve, STARTUP_RETRY_DELAY_MS));
}
}
}

await waitForDatabase();
app.listen(PORT, () => {
console.log(\`Server running on port \${PORT}\`);
});
Expand Down Expand Up @@ -88,7 +107,7 @@ class PackageJsonStep implements BuildStep {
version: '0.1.0',
type: 'module',
scripts: {
'dev': 'tsx src/index.ts',
'dev': 'tsx watch --env-file=.env src/index.ts',
'build': 'tsc',
'typecheck': 'tsc --noEmit',
'db:migrate': 'drizzle-kit migrate',
Expand Down Expand Up @@ -132,14 +151,19 @@ class TsConfigStep implements BuildStep {
}

class EnvExampleStep implements BuildStep {
async execute(outputDir: string, _options: GeneratorOptions): Promise<void> {
async execute(outputDir: string, options: GeneratorOptions): Promise<void> {
const databaseUrlExample: Record<GeneratorOptions['database'], string> = {
postgres: `postgresql://app:app@localhost:5432/${options.projectName}`,
mysql: `mysql://app:app@localhost:3307/${options.projectName}`,
sqlite: 'file:./data.db',
};
await writeFile(
join(outputDir, '.env.example'),
dedent`
PIPEDRIVE_CLIENT_ID=
PIPEDRIVE_CLIENT_SECRET=
PIPEDRIVE_REDIRECT_URI=http://localhost:3000/oauth/callback
DATABASE_URL=
DATABASE_URL=${databaseUrlExample[options.database]}
PORT=3000
Comment on lines +154 to 167
`,
);
Expand Down
Loading