@friggframework/devtools 2.0.0-next.40 → 2.0.0-next.42
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/frigg-cli/__tests__/unit/commands/build.test.js +173 -405
- package/frigg-cli/__tests__/unit/commands/db-setup.test.js +548 -0
- package/frigg-cli/__tests__/unit/commands/install.test.js +359 -377
- package/frigg-cli/__tests__/unit/commands/ui.test.js +266 -512
- package/frigg-cli/__tests__/unit/utils/database-validator.test.js +366 -0
- package/frigg-cli/__tests__/unit/utils/error-messages.test.js +304 -0
- package/frigg-cli/__tests__/unit/utils/prisma-runner.test.js +486 -0
- package/frigg-cli/__tests__/utils/prisma-mock.js +194 -0
- package/frigg-cli/__tests__/utils/test-setup.js +22 -21
- package/frigg-cli/db-setup-command/index.js +186 -0
- package/frigg-cli/generate-command/__tests__/generate-command.test.js +151 -162
- package/frigg-cli/generate-iam-command.js +7 -4
- package/frigg-cli/index.js +9 -1
- package/frigg-cli/install-command/index.js +1 -1
- package/frigg-cli/jest.config.js +124 -0
- package/frigg-cli/package.json +4 -1
- package/frigg-cli/start-command/index.js +101 -2
- package/frigg-cli/start-command/start-command.test.js +297 -0
- package/frigg-cli/utils/database-validator.js +158 -0
- package/frigg-cli/utils/error-messages.js +257 -0
- package/frigg-cli/utils/prisma-runner.js +280 -0
- package/infrastructure/CLAUDE.md +481 -0
- package/infrastructure/IAM-POLICY-TEMPLATES.md +30 -12
- package/infrastructure/create-frigg-infrastructure.js +0 -2
- package/infrastructure/iam-generator.js +18 -38
- package/infrastructure/iam-generator.test.js +40 -8
- package/infrastructure/serverless-template.js +25 -4
- package/infrastructure/serverless-template.test.js +45 -0
- package/package.json +6 -6
- package/test/index.js +2 -4
- package/test/mock-integration.js +4 -14
- package/frigg-cli/__tests__/jest.config.js +0 -102
- package/frigg-cli/__tests__/utils/command-tester.js +0 -170
- package/test/auther-definition-tester.js +0 -125
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
const path = require('path');
|
|
2
|
+
const fs = require('fs');
|
|
3
|
+
const { getDatabaseType: getDatabaseTypeFromCore } = require('@friggframework/core/database/config');
|
|
4
|
+
const { connectPrisma, disconnectPrisma } = require('@friggframework/core/database/prisma');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Database Validation Utility
|
|
8
|
+
* Validates database configuration and connectivity for Frigg applications
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Validates that DATABASE_URL environment variable exists and has a value
|
|
13
|
+
* @returns {Object} { valid: boolean, url?: string, error?: string }
|
|
14
|
+
*/
|
|
15
|
+
function validateDatabaseUrl() {
|
|
16
|
+
const databaseUrl = process.env.DATABASE_URL;
|
|
17
|
+
|
|
18
|
+
if (!databaseUrl) {
|
|
19
|
+
return {
|
|
20
|
+
valid: false,
|
|
21
|
+
error: 'DATABASE_URL environment variable not found'
|
|
22
|
+
};
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
if (databaseUrl.trim() === '') {
|
|
26
|
+
return {
|
|
27
|
+
valid: false,
|
|
28
|
+
error: 'DATABASE_URL environment variable is empty'
|
|
29
|
+
};
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
return {
|
|
33
|
+
valid: true,
|
|
34
|
+
url: databaseUrl
|
|
35
|
+
};
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Determines database type from backend app definition
|
|
40
|
+
* Reuses core logic from @friggframework/core/database/config
|
|
41
|
+
*
|
|
42
|
+
* @returns {Object} { dbType?: 'mongodb'|'postgresql', error?: string }
|
|
43
|
+
*/
|
|
44
|
+
function getDatabaseType() {
|
|
45
|
+
try {
|
|
46
|
+
// Use the core database config logic (same logic used at runtime)
|
|
47
|
+
const dbType = getDatabaseTypeFromCore();
|
|
48
|
+
return { dbType };
|
|
49
|
+
} catch (error) {
|
|
50
|
+
// Convert thrown errors to error object format for CLI
|
|
51
|
+
// Strip [Frigg] prefix from error messages for cleaner CLI output
|
|
52
|
+
const errorMessage = error.message.replace(/^\[Frigg\]\s*/, '');
|
|
53
|
+
return { error: errorMessage };
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Tests database connectivity by attempting to connect
|
|
59
|
+
* Uses the same Prisma client configuration as runtime
|
|
60
|
+
*
|
|
61
|
+
* @param {string} databaseUrl - Database connection URL (for validation purposes)
|
|
62
|
+
* @param {'mongodb'|'postgresql'} dbType - Database type to determine appropriate health check
|
|
63
|
+
* @param {number} timeout - Connection timeout in milliseconds (default: 5000)
|
|
64
|
+
* @returns {Promise<Object>} { connected: boolean, error?: string }
|
|
65
|
+
*/
|
|
66
|
+
async function testDatabaseConnection(databaseUrl, dbType, timeout = 5000) {
|
|
67
|
+
try {
|
|
68
|
+
// Use the core Prisma client (same client used at runtime)
|
|
69
|
+
// This automatically uses the correct client based on DB_TYPE
|
|
70
|
+
const connectPromise = connectPrisma();
|
|
71
|
+
const timeoutPromise = new Promise((_, reject) =>
|
|
72
|
+
setTimeout(() => reject(new Error('Connection timeout')), timeout)
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
const client = await Promise.race([connectPromise, timeoutPromise]);
|
|
76
|
+
|
|
77
|
+
// Test with database-appropriate health check
|
|
78
|
+
// MongoDB doesn't support SQL, so we use the native ping command
|
|
79
|
+
if (dbType === 'mongodb') {
|
|
80
|
+
// Use MongoDB's native ping command via $runCommandRaw
|
|
81
|
+
await client.$runCommandRaw({ ping: 1 });
|
|
82
|
+
} else {
|
|
83
|
+
// PostgreSQL: use a simple SQL query
|
|
84
|
+
await client.$queryRaw`SELECT 1`;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
await disconnectPrisma();
|
|
88
|
+
|
|
89
|
+
return { connected: true };
|
|
90
|
+
|
|
91
|
+
} catch (error) {
|
|
92
|
+
try {
|
|
93
|
+
await disconnectPrisma();
|
|
94
|
+
} catch (disconnectError) {
|
|
95
|
+
// Ignore disconnect errors
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
connected: false,
|
|
100
|
+
error: error.message
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Checks if Prisma client is generated for the database type
|
|
107
|
+
* Uses require.resolve to find the client in node_modules
|
|
108
|
+
*
|
|
109
|
+
* @param {'mongodb'|'postgresql'} dbType - Database type
|
|
110
|
+
* @param {string} projectRoot - Project root directory (used for require.resolve context)
|
|
111
|
+
* @returns {Object} { generated: boolean, path?: string, error?: string }
|
|
112
|
+
*/
|
|
113
|
+
function checkPrismaClientGenerated(dbType, projectRoot = process.cwd()) {
|
|
114
|
+
const clientPackageName = `@prisma-${dbType}/client`;
|
|
115
|
+
|
|
116
|
+
try {
|
|
117
|
+
// First, resolve where @friggframework/core actually is
|
|
118
|
+
// This handles file: dependencies and symlinks correctly
|
|
119
|
+
const corePackagePath = require.resolve('@friggframework/core', {
|
|
120
|
+
paths: [projectRoot]
|
|
121
|
+
});
|
|
122
|
+
const corePackageDir = path.dirname(corePackagePath);
|
|
123
|
+
|
|
124
|
+
// Now look for the Prisma client within the resolved core package
|
|
125
|
+
const clientPath = require.resolve(clientPackageName, {
|
|
126
|
+
paths: [
|
|
127
|
+
corePackageDir, // Look in the actual core package location
|
|
128
|
+
projectRoot // Fallback to project root
|
|
129
|
+
]
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
return {
|
|
133
|
+
generated: true,
|
|
134
|
+
path: path.dirname(clientPath)
|
|
135
|
+
};
|
|
136
|
+
|
|
137
|
+
} catch (error) {
|
|
138
|
+
// require.resolve throws MODULE_NOT_FOUND if the client doesn't exist
|
|
139
|
+
if (error.code === 'MODULE_NOT_FOUND') {
|
|
140
|
+
return {
|
|
141
|
+
generated: false,
|
|
142
|
+
error: `Prisma client for ${dbType} (${clientPackageName}) not found. Run 'frigg db:setup' to generate it.`
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
return {
|
|
147
|
+
generated: false,
|
|
148
|
+
error: `Failed to check Prisma client: ${error.message}`
|
|
149
|
+
};
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
module.exports = {
|
|
154
|
+
validateDatabaseUrl,
|
|
155
|
+
getDatabaseType,
|
|
156
|
+
testDatabaseConnection,
|
|
157
|
+
checkPrismaClientGenerated
|
|
158
|
+
};
|
|
@@ -0,0 +1,257 @@
|
|
|
1
|
+
const chalk = require('chalk');
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Error Messages Module
|
|
5
|
+
* Provides helpful, context-aware error messages for database setup issues
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* Database URL examples for both supported database types
|
|
10
|
+
*/
|
|
11
|
+
const DATABASE_URL_EXAMPLES = {
|
|
12
|
+
mongodb: 'mongodb://localhost:27017/frigg?replicaSet=rs0',
|
|
13
|
+
postgresql: 'postgresql://postgres:postgres@localhost:5432/frigg?schema=public'
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* Gets helpful error message for missing DATABASE_URL
|
|
18
|
+
* @returns {string} Formatted error message
|
|
19
|
+
*/
|
|
20
|
+
function getDatabaseUrlMissingError() {
|
|
21
|
+
return `
|
|
22
|
+
${chalk.red('❌ DATABASE_URL environment variable not found')}
|
|
23
|
+
|
|
24
|
+
${chalk.bold('Add DATABASE_URL to your .env file:')}
|
|
25
|
+
|
|
26
|
+
${chalk.cyan('For MongoDB:')}
|
|
27
|
+
${chalk.gray('DATABASE_URL')}=${chalk.green(`"${DATABASE_URL_EXAMPLES.mongodb}"`)}
|
|
28
|
+
|
|
29
|
+
${chalk.cyan('For PostgreSQL:')}
|
|
30
|
+
${chalk.gray('DATABASE_URL')}=${chalk.green(`"${DATABASE_URL_EXAMPLES.postgresql}"`)}
|
|
31
|
+
|
|
32
|
+
${chalk.yellow('Then run:')} ${chalk.cyan('frigg db:setup')}
|
|
33
|
+
`;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* Gets helpful error message for missing database type configuration
|
|
38
|
+
* @returns {string} Formatted error message
|
|
39
|
+
*/
|
|
40
|
+
function getDatabaseTypeNotConfiguredError() {
|
|
41
|
+
return `
|
|
42
|
+
${chalk.red('❌ Database type not configured in app definition')}
|
|
43
|
+
|
|
44
|
+
${chalk.bold('Add database configuration to your app definition file:')}
|
|
45
|
+
${chalk.gray('(backend/index.js or index.js)')}
|
|
46
|
+
|
|
47
|
+
${chalk.cyan('For PostgreSQL:')}
|
|
48
|
+
${chalk.gray(`
|
|
49
|
+
const appDefinition = {
|
|
50
|
+
// ... other configuration
|
|
51
|
+
database: {
|
|
52
|
+
postgres: { enable: true }
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
`)}
|
|
56
|
+
|
|
57
|
+
${chalk.cyan('For MongoDB:')}
|
|
58
|
+
${chalk.gray(`
|
|
59
|
+
const appDefinition = {
|
|
60
|
+
// ... other configuration
|
|
61
|
+
database: {
|
|
62
|
+
mongoDB: { enable: true }
|
|
63
|
+
}
|
|
64
|
+
};
|
|
65
|
+
`)}
|
|
66
|
+
|
|
67
|
+
${chalk.cyan('For AWS DocumentDB (MongoDB-compatible):')}
|
|
68
|
+
${chalk.gray(`
|
|
69
|
+
const appDefinition = {
|
|
70
|
+
// ... other configuration
|
|
71
|
+
database: {
|
|
72
|
+
documentDB: { enable: true }
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
`)}
|
|
76
|
+
`;
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* Gets helpful error message for database connection failure
|
|
81
|
+
* @param {string} error - Connection error message
|
|
82
|
+
* @param {'mongodb'|'postgresql'} dbType - Database type
|
|
83
|
+
* @returns {string} Formatted error message
|
|
84
|
+
*/
|
|
85
|
+
function getDatabaseConnectionError(error, dbType) {
|
|
86
|
+
const troubleshootingSteps = dbType === 'mongodb'
|
|
87
|
+
? getMongoDatabaseTroubleshooting()
|
|
88
|
+
: getPostgresTroubleshooting();
|
|
89
|
+
|
|
90
|
+
return `
|
|
91
|
+
${chalk.red('❌ Failed to connect to database')}
|
|
92
|
+
|
|
93
|
+
${chalk.bold('Connection error:')}
|
|
94
|
+
${chalk.gray(error)}
|
|
95
|
+
|
|
96
|
+
${chalk.bold('Troubleshooting steps:')}
|
|
97
|
+
${troubleshootingSteps}
|
|
98
|
+
|
|
99
|
+
${chalk.yellow('Verify your DATABASE_URL:')} ${chalk.cyan(process.env.DATABASE_URL || 'not set')}
|
|
100
|
+
`;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Gets MongoDB-specific troubleshooting steps
|
|
105
|
+
* @returns {string} Formatted troubleshooting steps
|
|
106
|
+
*/
|
|
107
|
+
function getMongoDatabaseTroubleshooting() {
|
|
108
|
+
return `
|
|
109
|
+
${chalk.gray('1.')} Verify MongoDB is running
|
|
110
|
+
${chalk.cyan('docker ps')} ${chalk.gray('(if using Docker)')}
|
|
111
|
+
${chalk.cyan('mongosh --eval "db.version()"')} ${chalk.gray('(test connection)')}
|
|
112
|
+
|
|
113
|
+
${chalk.gray('2.')} Check if replica set is initialized
|
|
114
|
+
${chalk.gray('MongoDB requires a replica set for Prisma')}
|
|
115
|
+
${chalk.cyan('mongosh')}
|
|
116
|
+
${chalk.cyan('rs.status()')} ${chalk.gray('(should show replica set info)')}
|
|
117
|
+
|
|
118
|
+
${chalk.gray('3.')} Initialize replica set if needed
|
|
119
|
+
${chalk.cyan('docker exec -it <mongodb-container> mongosh')}
|
|
120
|
+
${chalk.cyan('rs.initiate()')}
|
|
121
|
+
|
|
122
|
+
${chalk.gray('4.')} Verify connection string format
|
|
123
|
+
${chalk.gray('mongodb://[username:password@]host[:port]/database[?options]')}
|
|
124
|
+
${chalk.green('Example: mongodb://localhost:27017/frigg?replicaSet=rs0')}
|
|
125
|
+
|
|
126
|
+
${chalk.gray('5.')} Check network/firewall settings
|
|
127
|
+
${chalk.gray('Ensure port 27017 (default) is accessible')}
|
|
128
|
+
`;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Gets PostgreSQL-specific troubleshooting steps
|
|
133
|
+
* @returns {string} Formatted troubleshooting steps
|
|
134
|
+
*/
|
|
135
|
+
function getPostgresTroubleshooting() {
|
|
136
|
+
return `
|
|
137
|
+
${chalk.gray('1.')} Verify PostgreSQL is running
|
|
138
|
+
${chalk.cyan('docker ps')} ${chalk.gray('(if using Docker)')}
|
|
139
|
+
${chalk.cyan('pg_isready')} ${chalk.gray('(test if server is ready)')}
|
|
140
|
+
|
|
141
|
+
${chalk.gray('2.')} Check connection string format
|
|
142
|
+
${chalk.gray('postgresql://[username:password@]host[:port]/database[?options]')}
|
|
143
|
+
${chalk.green('Example: postgresql://postgres:postgres@localhost:5432/frigg?schema=public')}
|
|
144
|
+
|
|
145
|
+
${chalk.gray('3.')} Verify database exists
|
|
146
|
+
${chalk.cyan('psql -U postgres -l')} ${chalk.gray('(list databases)')}
|
|
147
|
+
${chalk.cyan('CREATE DATABASE frigg;')} ${chalk.gray('(if needed)')}
|
|
148
|
+
|
|
149
|
+
${chalk.gray('4.')} Check pg_hba.conf allows connections
|
|
150
|
+
${chalk.gray('Location: /var/lib/postgresql/data/pg_hba.conf (Docker)')}
|
|
151
|
+
${chalk.gray('Ensure trust/md5 authentication is enabled for your host')}
|
|
152
|
+
|
|
153
|
+
${chalk.gray('5.')} Verify network/firewall settings
|
|
154
|
+
${chalk.gray('Ensure port 5432 (default) is accessible')}
|
|
155
|
+
`;
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Gets helpful error message for missing Prisma client
|
|
160
|
+
* @param {'mongodb'|'postgresql'} dbType - Database type
|
|
161
|
+
* @returns {string} Formatted error message
|
|
162
|
+
*/
|
|
163
|
+
function getPrismaClientNotGeneratedError(dbType) {
|
|
164
|
+
const clientName = `@prisma-${dbType}/client`;
|
|
165
|
+
|
|
166
|
+
return `
|
|
167
|
+
${chalk.red(`❌ Prisma client not generated for ${dbType}`)}
|
|
168
|
+
|
|
169
|
+
${chalk.bold('The Prisma client needs to be generated before starting the application.')}
|
|
170
|
+
|
|
171
|
+
${chalk.yellow('Run:')} ${chalk.cyan('frigg db:setup')}
|
|
172
|
+
|
|
173
|
+
${chalk.gray('This will:')}
|
|
174
|
+
${chalk.gray(' • Generate the Prisma client')} ${chalk.gray(`(${clientName})`)}
|
|
175
|
+
${chalk.gray(' • Set up database schema')}
|
|
176
|
+
${chalk.gray(' • Run migrations (PostgreSQL) or db push (MongoDB)')}
|
|
177
|
+
`;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/**
|
|
181
|
+
* Gets helpful error message for Prisma command failures
|
|
182
|
+
* @param {string} command - Command that failed (generate, migrate, push)
|
|
183
|
+
* @param {string} error - Error message
|
|
184
|
+
* @returns {string} Formatted error message
|
|
185
|
+
*/
|
|
186
|
+
function getPrismaCommandError(command, error) {
|
|
187
|
+
return `
|
|
188
|
+
${chalk.red(`❌ Prisma ${command} failed`)}
|
|
189
|
+
|
|
190
|
+
${chalk.bold('Error:')}
|
|
191
|
+
${chalk.gray(error)}
|
|
192
|
+
|
|
193
|
+
${chalk.bold('Common causes:')}
|
|
194
|
+
${chalk.gray(' • Database schema has conflicts')}
|
|
195
|
+
${chalk.gray(' • Database connection lost during operation')}
|
|
196
|
+
${chalk.gray(' • Insufficient permissions')}
|
|
197
|
+
${chalk.gray(' • Schema file is invalid')}
|
|
198
|
+
|
|
199
|
+
${chalk.yellow('Try:')}
|
|
200
|
+
${chalk.cyan(' frigg db:setup')} ${chalk.gray('(re-run setup)')}
|
|
201
|
+
${chalk.cyan(' Check your DATABASE_URL')} ${chalk.gray('(verify connection string)')}
|
|
202
|
+
${chalk.cyan(' Review Prisma schema')} ${chalk.gray('(node_modules/@friggframework/core/prisma-*/schema.prisma)')}
|
|
203
|
+
`;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Gets success message for database setup completion
|
|
208
|
+
* @param {'mongodb'|'postgresql'} dbType - Database type
|
|
209
|
+
* @param {string} stage - Deployment stage
|
|
210
|
+
* @returns {string} Formatted success message
|
|
211
|
+
*/
|
|
212
|
+
function getDatabaseSetupSuccess(dbType, stage) {
|
|
213
|
+
return `
|
|
214
|
+
${chalk.green('✅ Database setup completed successfully!')}
|
|
215
|
+
|
|
216
|
+
${chalk.bold('Configuration:')}
|
|
217
|
+
${chalk.gray(' Database type:')} ${chalk.cyan(dbType)}
|
|
218
|
+
${chalk.gray(' Stage:')} ${chalk.cyan(stage)}
|
|
219
|
+
${chalk.gray(' Connection:')} ${chalk.green('verified')}
|
|
220
|
+
|
|
221
|
+
${chalk.bold('What happened:')}
|
|
222
|
+
${chalk.gray(' ✓')} Prisma client generated
|
|
223
|
+
${chalk.gray(' ✓')} Database connection verified
|
|
224
|
+
${chalk.gray(' ✓')} ${dbType === 'postgresql' ? 'Migrations applied' : 'Schema pushed to database'}
|
|
225
|
+
|
|
226
|
+
${chalk.yellow('Next steps:')}
|
|
227
|
+
${chalk.cyan(' frigg start')} ${chalk.gray('(start your application)')}
|
|
228
|
+
`;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/**
|
|
232
|
+
* Gets warning message for database already up-to-date
|
|
233
|
+
* @returns {string} Formatted warning message
|
|
234
|
+
*/
|
|
235
|
+
function getDatabaseAlreadyUpToDate() {
|
|
236
|
+
return `
|
|
237
|
+
${chalk.yellow('⚠️ Database is already up-to-date')}
|
|
238
|
+
|
|
239
|
+
${chalk.gray('No migrations or schema changes detected.')}
|
|
240
|
+
|
|
241
|
+
${chalk.yellow('If you expected changes:')}
|
|
242
|
+
${chalk.gray(' • Check if schema was modified in node_modules/@friggframework/core/prisma-*/')}
|
|
243
|
+
${chalk.gray(' • Verify DATABASE_URL points to the correct database')}
|
|
244
|
+
${chalk.gray(' • Check if you\'re in the correct project directory')}
|
|
245
|
+
`;
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
module.exports = {
|
|
249
|
+
DATABASE_URL_EXAMPLES,
|
|
250
|
+
getDatabaseUrlMissingError,
|
|
251
|
+
getDatabaseTypeNotConfiguredError,
|
|
252
|
+
getDatabaseConnectionError,
|
|
253
|
+
getPrismaClientNotGeneratedError,
|
|
254
|
+
getPrismaCommandError,
|
|
255
|
+
getDatabaseSetupSuccess,
|
|
256
|
+
getDatabaseAlreadyUpToDate
|
|
257
|
+
};
|
|
@@ -0,0 +1,280 @@
|
|
|
1
|
+
const { execSync, spawn } = require('child_process');
|
|
2
|
+
const path = require('path');
|
|
3
|
+
const fs = require('fs');
|
|
4
|
+
const chalk = require('chalk');
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Prisma Command Runner Utility
|
|
8
|
+
* Handles execution of Prisma CLI commands for database setup
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Gets the path to the Prisma schema file for the database type
|
|
13
|
+
* @param {'mongodb'|'postgresql'} dbType - Database type
|
|
14
|
+
* @param {string} projectRoot - Project root directory
|
|
15
|
+
* @returns {string} Absolute path to schema file
|
|
16
|
+
* @throws {Error} If schema file doesn't exist
|
|
17
|
+
*/
|
|
18
|
+
function getPrismaSchemaPath(dbType, projectRoot = process.cwd()) {
|
|
19
|
+
// Try multiple locations for the schema file
|
|
20
|
+
// Priority order:
|
|
21
|
+
// 1. Local node_modules (where @friggframework/core is installed - production scenario)
|
|
22
|
+
// 2. Parent node_modules (workspace/monorepo setup)
|
|
23
|
+
const possiblePaths = [
|
|
24
|
+
// Check where Frigg is installed via npm (production scenario)
|
|
25
|
+
path.join(projectRoot, 'node_modules', '@friggframework', 'core', `prisma-${dbType}`, 'schema.prisma'),
|
|
26
|
+
path.join(projectRoot, '..', 'node_modules', '@friggframework', 'core', `prisma-${dbType}`, 'schema.prisma')
|
|
27
|
+
];
|
|
28
|
+
|
|
29
|
+
for (const schemaPath of possiblePaths) {
|
|
30
|
+
if (fs.existsSync(schemaPath)) {
|
|
31
|
+
return schemaPath;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// If not found in any location, throw error
|
|
36
|
+
throw new Error(
|
|
37
|
+
`Prisma schema not found at:\n${possiblePaths.join('\n')}\n\n` +
|
|
38
|
+
'Ensure @friggframework/core is installed.'
|
|
39
|
+
);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Runs prisma generate for the specified database type
|
|
44
|
+
* @param {'mongodb'|'postgresql'} dbType - Database type
|
|
45
|
+
* @param {boolean} verbose - Enable verbose output
|
|
46
|
+
* @returns {Promise<Object>} { success: boolean, output?: string, error?: string }
|
|
47
|
+
*/
|
|
48
|
+
async function runPrismaGenerate(dbType, verbose = false) {
|
|
49
|
+
try {
|
|
50
|
+
const schemaPath = getPrismaSchemaPath(dbType);
|
|
51
|
+
|
|
52
|
+
if (verbose) {
|
|
53
|
+
console.log(chalk.gray(`Running: npx prisma generate --schema=${schemaPath}`));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
const output = execSync(
|
|
57
|
+
`npx prisma generate --schema=${schemaPath}`,
|
|
58
|
+
{
|
|
59
|
+
encoding: 'utf8',
|
|
60
|
+
stdio: verbose ? 'inherit' : 'pipe',
|
|
61
|
+
env: {
|
|
62
|
+
...process.env,
|
|
63
|
+
// Suppress Prisma telemetry prompts
|
|
64
|
+
PRISMA_HIDE_UPDATE_MESSAGE: '1'
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
);
|
|
68
|
+
|
|
69
|
+
return {
|
|
70
|
+
success: true,
|
|
71
|
+
output: verbose ? 'Generated successfully' : output
|
|
72
|
+
};
|
|
73
|
+
|
|
74
|
+
} catch (error) {
|
|
75
|
+
return {
|
|
76
|
+
success: false,
|
|
77
|
+
error: error.message,
|
|
78
|
+
output: error.stdout?.toString() || error.stderr?.toString()
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Checks database migration status
|
|
85
|
+
* @param {'mongodb'|'postgresql'} dbType - Database type
|
|
86
|
+
* @returns {Promise<Object>} { upToDate: boolean, pendingMigrations?: number, error?: string }
|
|
87
|
+
*/
|
|
88
|
+
async function checkDatabaseState(dbType) {
|
|
89
|
+
try {
|
|
90
|
+
// Only applicable for PostgreSQL (MongoDB uses db push)
|
|
91
|
+
if (dbType !== 'postgresql') {
|
|
92
|
+
return { upToDate: true };
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
const schemaPath = getPrismaSchemaPath(dbType);
|
|
96
|
+
|
|
97
|
+
const output = execSync(
|
|
98
|
+
`npx prisma migrate status --schema=${schemaPath}`,
|
|
99
|
+
{
|
|
100
|
+
encoding: 'utf8',
|
|
101
|
+
stdio: 'pipe',
|
|
102
|
+
env: {
|
|
103
|
+
...process.env,
|
|
104
|
+
PRISMA_HIDE_UPDATE_MESSAGE: '1'
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
);
|
|
108
|
+
|
|
109
|
+
if (output.includes('Database schema is up to date')) {
|
|
110
|
+
return { upToDate: true };
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
// Parse pending migrations count
|
|
114
|
+
const pendingMatch = output.match(/(\d+) migration/);
|
|
115
|
+
const pendingMigrations = pendingMatch ? parseInt(pendingMatch[1]) : 0;
|
|
116
|
+
|
|
117
|
+
return {
|
|
118
|
+
upToDate: false,
|
|
119
|
+
pendingMigrations
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
} catch (error) {
|
|
123
|
+
// If migrate status fails, database might not be initialized
|
|
124
|
+
return {
|
|
125
|
+
upToDate: false,
|
|
126
|
+
error: error.message
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/**
|
|
132
|
+
* Runs Prisma migrate for PostgreSQL
|
|
133
|
+
* @param {'dev'|'deploy'} command - Migration command (dev or deploy)
|
|
134
|
+
* @param {boolean} verbose - Enable verbose output
|
|
135
|
+
* @returns {Promise<Object>} { success: boolean, output?: string, error?: string }
|
|
136
|
+
*/
|
|
137
|
+
async function runPrismaMigrate(command = 'dev', verbose = false) {
|
|
138
|
+
return new Promise((resolve) => {
|
|
139
|
+
try {
|
|
140
|
+
const schemaPath = getPrismaSchemaPath('postgresql');
|
|
141
|
+
|
|
142
|
+
const args = [
|
|
143
|
+
'prisma',
|
|
144
|
+
'migrate',
|
|
145
|
+
command,
|
|
146
|
+
'--schema',
|
|
147
|
+
schemaPath
|
|
148
|
+
];
|
|
149
|
+
|
|
150
|
+
if (verbose) {
|
|
151
|
+
console.log(chalk.gray(`Running: npx ${args.join(' ')}`));
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const proc = spawn('npx', args, {
|
|
155
|
+
stdio: 'inherit',
|
|
156
|
+
env: {
|
|
157
|
+
...process.env,
|
|
158
|
+
PRISMA_HIDE_UPDATE_MESSAGE: '1'
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
proc.on('error', (error) => {
|
|
163
|
+
resolve({
|
|
164
|
+
success: false,
|
|
165
|
+
error: error.message
|
|
166
|
+
});
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
proc.on('close', (code) => {
|
|
170
|
+
if (code === 0) {
|
|
171
|
+
resolve({
|
|
172
|
+
success: true,
|
|
173
|
+
output: 'Migration completed successfully'
|
|
174
|
+
});
|
|
175
|
+
} else {
|
|
176
|
+
resolve({
|
|
177
|
+
success: false,
|
|
178
|
+
error: `Migration process exited with code ${code}`
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
} catch (error) {
|
|
184
|
+
resolve({
|
|
185
|
+
success: false,
|
|
186
|
+
error: error.message
|
|
187
|
+
});
|
|
188
|
+
}
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* Runs Prisma db push for MongoDB
|
|
194
|
+
* Interactive - will prompt user if data loss detected
|
|
195
|
+
* @param {boolean} verbose - Enable verbose output
|
|
196
|
+
* @returns {Promise<Object>} { success: boolean, output?: string, error?: string }
|
|
197
|
+
*/
|
|
198
|
+
async function runPrismaDbPush(verbose = false) {
|
|
199
|
+
return new Promise((resolve) => {
|
|
200
|
+
try {
|
|
201
|
+
const schemaPath = getPrismaSchemaPath('mongodb');
|
|
202
|
+
|
|
203
|
+
const args = [
|
|
204
|
+
'prisma',
|
|
205
|
+
'db',
|
|
206
|
+
'push',
|
|
207
|
+
'--schema',
|
|
208
|
+
schemaPath,
|
|
209
|
+
'--skip-generate' // We generate separately
|
|
210
|
+
];
|
|
211
|
+
|
|
212
|
+
if (verbose) {
|
|
213
|
+
console.log(chalk.gray(`Running: npx ${args.join(' ')}`));
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
console.log(chalk.yellow('⚠️ Interactive mode: You may be prompted if schema changes cause data loss'));
|
|
217
|
+
|
|
218
|
+
const proc = spawn('npx', args, {
|
|
219
|
+
stdio: 'inherit', // Interactive mode - user can respond to prompts
|
|
220
|
+
env: {
|
|
221
|
+
...process.env,
|
|
222
|
+
PRISMA_HIDE_UPDATE_MESSAGE: '1'
|
|
223
|
+
}
|
|
224
|
+
});
|
|
225
|
+
|
|
226
|
+
proc.on('error', (error) => {
|
|
227
|
+
resolve({
|
|
228
|
+
success: false,
|
|
229
|
+
error: error.message
|
|
230
|
+
});
|
|
231
|
+
});
|
|
232
|
+
|
|
233
|
+
proc.on('close', (code) => {
|
|
234
|
+
if (code === 0) {
|
|
235
|
+
resolve({
|
|
236
|
+
success: true,
|
|
237
|
+
output: 'Database push completed successfully'
|
|
238
|
+
});
|
|
239
|
+
} else {
|
|
240
|
+
resolve({
|
|
241
|
+
success: false,
|
|
242
|
+
error: `Database push process exited with code ${code}`
|
|
243
|
+
});
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
|
|
247
|
+
} catch (error) {
|
|
248
|
+
resolve({
|
|
249
|
+
success: false,
|
|
250
|
+
error: error.message
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
/**
|
|
257
|
+
* Determines migration command based on STAGE environment variable
|
|
258
|
+
* @param {string} stage - Stage from CLI option or environment
|
|
259
|
+
* @returns {'dev'|'deploy'}
|
|
260
|
+
*/
|
|
261
|
+
function getMigrationCommand(stage) {
|
|
262
|
+
const normalizedStage = (stage || process.env.STAGE || 'development').toLowerCase();
|
|
263
|
+
|
|
264
|
+
const developmentStages = ['dev', 'local', 'test', 'development'];
|
|
265
|
+
|
|
266
|
+
if (developmentStages.includes(normalizedStage)) {
|
|
267
|
+
return 'dev';
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
return 'deploy';
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
module.exports = {
|
|
274
|
+
getPrismaSchemaPath,
|
|
275
|
+
runPrismaGenerate,
|
|
276
|
+
checkDatabaseState,
|
|
277
|
+
runPrismaMigrate,
|
|
278
|
+
runPrismaDbPush,
|
|
279
|
+
getMigrationCommand
|
|
280
|
+
};
|