0
# Database Setup
1
2
Database configuration and setup utilities for multiple database backends including SQLite, PostgreSQL, and MySQL with connection testing and environment configuration.
3
4
## Capabilities
5
6
### Database Information Interface
7
8
Core database configuration structure for connection details and client settings.
9
10
```typescript { .api }
11
/**
12
* Database connection configuration
13
*/
14
interface DatabaseInfo {
15
/** Database client type */
16
client?: string;
17
/** Connection configuration object */
18
connection: {
19
/** Database host address */
20
host?: string;
21
/** Database port number */
22
port?: string;
23
/** Database name */
24
database?: string;
25
/** Database username */
26
username?: string;
27
/** Database password */
28
password?: string;
29
/** SQLite database file path */
30
filename?: string;
31
/** Enable SSL connection */
32
ssl?: boolean;
33
};
34
/** Use NULL as default value for SQLite */
35
useNullAsDefault?: boolean;
36
}
37
```
38
39
### Supported Database Clients
40
41
Type-safe enumeration of supported database client types.
42
43
```typescript { .api }
44
/**
45
* Supported database client names
46
*/
47
type ClientName = 'mysql' | 'mysql2' | 'postgres' | 'sqlite' | 'sqlite-legacy';
48
```
49
50
### Database Configuration Object
51
52
Complete database configuration including client, connection, and dependencies.
53
54
```typescript { .api }
55
/**
56
* Complete database configuration for project generation
57
*/
58
interface Configuration {
59
/** Database client identifier */
60
client: string;
61
/** Database connection information */
62
connection: DatabaseInfo;
63
/** Required database client dependencies */
64
dependencies: Record<string, string>;
65
}
66
```
67
68
## Database Configuration Utilities
69
70
### Default Database Configurations
71
72
Pre-configured database settings for each supported client type.
73
74
```typescript { .api }
75
/**
76
* Default database configurations for each client type
77
* Includes optimized settings for development environments
78
*/
79
const defaultConfigs: Record<ClientName, DatabaseInfo>;
80
```
81
82
**Default Configurations:**
83
84
```typescript
85
// SQLite (default)
86
{
87
client: 'sqlite',
88
connection: {
89
filename: '.tmp/data.db'
90
},
91
useNullAsDefault: true
92
}
93
94
// PostgreSQL
95
{
96
client: 'postgres',
97
connection: {
98
host: 'localhost',
99
port: '5432',
100
database: 'strapi',
101
username: 'strapi',
102
password: 'strapi'
103
}
104
}
105
106
// MySQL
107
{
108
client: 'mysql',
109
connection: {
110
host: 'localhost',
111
port: '3306',
112
database: 'strapi',
113
username: 'strapi',
114
password: 'strapi'
115
}
116
}
117
```
118
119
### Database Client Dependencies
120
121
Mapping of database clients to their required npm dependencies.
122
123
```typescript { .api }
124
/**
125
* Gets required dependencies for a database client
126
* @param options - Object containing client type
127
* @returns Object with dependency names and versions
128
*/
129
function clientDependencies(options: { client: string }): Record<string, string>;
130
```
131
132
**Dependency Mappings:**
133
134
```typescript
135
// SQLite
136
{ 'better-sqlite3': '^8.0.0' }
137
138
// PostgreSQL
139
{ 'pg': '^8.8.0' }
140
141
// MySQL
142
{ 'mysql2': '^2.3.0' }
143
```
144
145
### Interactive Database Configuration
146
147
Interactive prompts for database client selection and connection configuration.
148
149
```typescript { .api }
150
/**
151
* Interactive database configuration questions for each client type
152
* Provides client-specific prompts for connection details
153
*/
154
const dbQuestions: Record<string, Array<Function>>;
155
156
/**
157
* Prompts user for database client selection
158
* @returns Selected database client ('sqlite' | 'postgres' | 'mysql')
159
*/
160
function askDatabaseClient(): Promise<string>;
161
162
/**
163
* Prompts for database connection details based on selected client
164
* @param client - Selected database client type
165
* @param scope - Project configuration scope
166
* @returns Database connection configuration
167
*/
168
function askDatabaseConnection(client: string, scope: Scope): Promise<DatabaseInfo>;
169
```
170
171
## Database Environment Configuration
172
173
### Database Configuration File Generation
174
175
Generates database configuration files for both TypeScript and JavaScript projects.
176
177
```typescript { .api }
178
/**
179
* Creates database configuration file content
180
* @param options - Configuration options including TypeScript flag
181
* @returns Database configuration file content as string
182
*/
183
function createDatabaseConfig(options: { useTypescript: boolean }): string;
184
```
185
186
**Generated Configuration Examples:**
187
188
TypeScript (`config/database.ts`):
189
```typescript
190
export default ({ env }) => ({
191
connection: {
192
client: env('DATABASE_CLIENT', 'sqlite'),
193
connection:
194
env('DATABASE_CLIENT', 'sqlite') === 'sqlite'
195
? {
196
filename: path.join(__dirname, '..', env('DATABASE_FILENAME', '.tmp/data.db')),
197
}
198
: {
199
host: env('DATABASE_HOST', 'localhost'),
200
port: env.int('DATABASE_PORT', 5432),
201
database: env('DATABASE_NAME', 'strapi'),
202
user: env('DATABASE_USERNAME', 'strapi'),
203
password: env('DATABASE_PASSWORD', 'strapi'),
204
ssl: env.bool('DATABASE_SSL', false),
205
},
206
},
207
});
208
```
209
210
### Environment Variables Generation
211
212
Generates environment variables for database configuration.
213
214
```typescript { .api }
215
/**
216
* Generates database environment variables for .env file
217
* @param params - Database client and connection configuration
218
* @returns Environment variable declarations as string
219
*/
220
function generateDbEnvariables(params: {
221
client: string;
222
connection: DatabaseInfo;
223
}): string;
224
```
225
226
**Generated Environment Variables:**
227
228
```bash
229
# Database
230
DATABASE_CLIENT=postgres
231
DATABASE_HOST=localhost
232
DATABASE_PORT=5432
233
DATABASE_NAME=myapp
234
DATABASE_USERNAME=postgres
235
DATABASE_PASSWORD=password
236
DATABASE_SSL=false
237
```
238
239
## Database Client Utilities
240
241
### Client Name Resolution
242
243
Utilities for resolving and validating database client names.
244
245
```typescript { .api }
246
/**
247
* Resolves database client name to standardized format
248
* @param client - Raw client name from user input
249
* @returns Standardized client name
250
*/
251
function resolveClientName(client: string): ClientName;
252
253
/**
254
* Validates if a client name is supported
255
* @param client - Client name to validate
256
* @returns true if client is supported
257
*/
258
function isValidClient(client: string): boolean;
259
```
260
261
## Usage Examples
262
263
### Basic Database Configuration
264
265
```typescript
266
import { generateNewApp } from "@strapi/generate-new";
267
268
// SQLite (default)
269
await generateNewApp("./my-app", {
270
quickstart: true
271
});
272
273
// PostgreSQL
274
await generateNewApp("./my-app", {
275
dbclient: "postgres",
276
dbhost: "localhost",
277
dbport: "5432",
278
dbname: "myapp",
279
dbusername: "postgres",
280
dbpassword: "password"
281
});
282
283
// MySQL with SSL
284
await generateNewApp("./my-app", {
285
dbclient: "mysql",
286
dbhost: "mysql.example.com",
287
dbport: "3306",
288
dbname: "production_db",
289
dbusername: "app_user",
290
dbpassword: "secure_password",
291
dbssl: "true"
292
});
293
```
294
295
### Environment-Based Configuration
296
297
```typescript
298
// Using environment variables
299
await generateNewApp("./my-app", {
300
dbclient: process.env.DB_CLIENT || "postgres",
301
dbhost: process.env.DB_HOST || "localhost",
302
dbport: process.env.DB_PORT || "5432",
303
dbname: process.env.DB_NAME || "strapi",
304
dbusername: process.env.DB_USER || "strapi",
305
dbpassword: process.env.DB_PASS || "strapi"
306
});
307
```