Skip to content

Commit 3114359

Browse files
authored
feat(cpa): add sqlite (#7470)
Add `sqlite` as an option for create-payload-app.
1 parent f752804 commit 3114359

File tree

3 files changed

+26
-6
lines changed

3 files changed

+26
-6
lines changed

packages/create-payload-app/src/lib/replacements.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,22 @@ const postgresReplacement: DbAdapterReplacement = {
2929
packageName: '@payloadcms/db-postgres',
3030
}
3131

32+
const sqliteReplacement: DbAdapterReplacement = {
33+
configReplacement: (envName = 'DATABASE_URI') => [
34+
' db: sqliteAdapter({',
35+
' client: {',
36+
` url: process.env.${envName} || '',`,
37+
' },',
38+
' }),',
39+
],
40+
importReplacement: "import { sqliteAdapter } from '@payloadcms/db-sqlite'",
41+
packageName: '@payloadcms/db-sqlite',
42+
}
43+
3244
export const dbReplacements: Record<DbType, DbAdapterReplacement> = {
3345
mongodb: mongodbReplacement,
3446
postgres: postgresReplacement,
47+
sqlite: sqliteReplacement,
3548
}
3649

3750
type StorageAdapterReplacement = {

packages/create-payload-app/src/lib/select-db.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import type { CliArgs, DbDetails, DbType } from '../types.js'
55

66
type DbChoice = {
77
dbConnectionPrefix: `${string}/`
8+
dbConnectionSuffix?: string
89
title: string
910
value: DbType
1011
}
@@ -20,6 +21,12 @@ const dbChoiceRecord: Record<DbType, DbChoice> = {
2021
title: 'PostgreSQL (beta)',
2122
value: 'postgres',
2223
},
24+
sqlite: {
25+
dbConnectionPrefix: 'file:./',
26+
dbConnectionSuffix: '.db',
27+
title: 'SQLite (beta)',
28+
value: 'sqlite',
29+
},
2330
}
2431

2532
export async function selectDb(args: CliArgs, projectName: string): Promise<DbDetails> {
@@ -37,10 +44,10 @@ export async function selectDb(args: CliArgs, projectName: string): Promise<DbDe
3744
dbType = await p.select<{ label: string; value: DbType }[], DbType>({
3845
initialValue: 'mongodb',
3946
message: `Select a database`,
40-
options: [
41-
{ label: 'MongoDB', value: 'mongodb' },
42-
{ label: 'Postgres', value: 'postgres' },
43-
],
47+
options: Object.values(dbChoiceRecord).map((dbChoice) => ({
48+
label: dbChoice.title,
49+
value: dbChoice.value,
50+
})),
4451
})
4552
if (p.isCancel(dbType)) process.exit(0)
4653
}
@@ -50,7 +57,7 @@ export async function selectDb(args: CliArgs, projectName: string): Promise<DbDe
5057
let dbUri: string | symbol | undefined = undefined
5158
const initialDbUri = `${dbChoice.dbConnectionPrefix}${
5259
projectName === '.' ? `payload-${getRandomDigitSuffix()}` : slugify(projectName)
53-
}`
60+
}${dbChoice.dbConnectionSuffix || ''}`
5461

5562
if (args['--db-accept-recommended']) {
5663
dbUri = initialDbUri

packages/create-payload-app/src/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ interface Template {
5757

5858
export type PackageManager = 'bun' | 'npm' | 'pnpm' | 'yarn'
5959

60-
export type DbType = 'mongodb' | 'postgres'
60+
export type DbType = 'mongodb' | 'postgres' | 'sqlite'
6161

6262
export type DbDetails = {
6363
dbUri: string

0 commit comments

Comments
 (0)