Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for D1 #15

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
3 changes: 2 additions & 1 deletion src/cli/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ export const runCli = async (): Promise<CliResults> => {
options: [
{ value: "prisma", label: "Prisma" },
{ value: "drizzle", label: "Drizzle" },
{ value: "d1", label: "D1" },
],
initialValue: "prisma",
});
Expand All @@ -88,7 +89,7 @@ export const runCli = async (): Promise<CliResults> => {
if (results.database === "none") return;
return p.select({
message: "What database provider would you like to use?",
options: [{ value: "turso", label: "Turso" }],
options: [{ value: "turso", label: "Turso" }, { value: "d1", label: "D1" }],
initialValue: "sqlite",
});
},
Expand Down
9 changes: 9 additions & 0 deletions src/helpers/installPackages.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import chalk from "chalk";
import ora from "ora";

import { addPackageDependency } from "~/utils/addPackageDependency.js";
import {
type InstallerOptions,
type PkgInstallerMap,
Expand Down Expand Up @@ -28,5 +29,13 @@ export const installPackages = (options: InstallPackagesOptions) => {
}
}

if (options.databaseProvider === 'd1') {
addPackageDependency({
projectDir: options.projectDir,
dependencies: ['@cloudflare/d1', '@cloudflare/workers-types'],
devMode: true,
});
}

console.info("");
};
13 changes: 13 additions & 0 deletions src/helpers/scaffoldProject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,19 @@ export const scaffoldProject = async ({
spinner.start();

fs.copySync(srcDir, projectDir);

if (databaseProvider === "d1") {
const d1Dir = path.join(PKG_ROOT, "templates/extras/d1");
if (packages.prisma.inUse) {
fs.copySync(path.join(d1Dir, "prisma/database.ts"), path.join(projectDir, "apps/api/src/database.ts"));
fs.copySync(path.join(d1Dir, "prisma/schema.prisma"), path.join(projectDir, "apps/api/prisma/schema.prisma"));
}
if (packages.drizzle.inUse) {
fs.copySync(path.join(d1Dir, "drizzle/database.ts"), path.join(projectDir, "apps/api/src/database.ts"));
fs.copySync(path.join(d1Dir, "drizzle/schema.ts"), path.join(projectDir, "apps/api/src/db/schema.ts"));
}
}

fs.renameSync(
path.join(projectDir, "apps/api/_gitignore"),
path.join(projectDir, "apps/api/.gitignore")
Expand Down
14 changes: 11 additions & 3 deletions src/installers/drizzle.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ export const drizzleInstaller: Installer = ({
dependencies: [
"drizzle-orm",
(
{
({
turso: "@libsql/client",
sqlite: "@libsql/client",
} as const
Expand Down Expand Up @@ -60,12 +60,17 @@ export const drizzleInstaller: Installer = ({
`,\n "packageManager": "${turboManager}" \n }`
);

const extrasDir = path.join(PKG_ROOT, "templates/extras");
const configName =

const configName = databaseProvider === "d1"
? "drizzle-d1-config.ts"
: databaseProvider === "turso"
databaseProvider === "turso"
? "drizzle-turso-config.ts"
: "drizzle-config.ts";
const configFile = path.join(extrasDir, `/drizzle/config/${configName}`);
const databaseSrc = databaseProvider === "d1"
? path.join(extrasDir, "d1/drizzle/database.ts")
: path.join(extrasDir, "drizzle/database.ts");
const configDest = path.join(projectDir, "apps/api/drizzle.config.ts");
const schemaName = databaseProvider === "turso" ? "turso.ts" : `base.ts`;
const schemaSrc = path.join(extrasDir, "drizzle/db/", schemaName);
Expand Down Expand Up @@ -105,6 +110,9 @@ export const drizzleInstaller: Installer = ({
if (databaseProvider === "turso") {
fs.appendFileSync(envPath, `TURSO_DATABASE_URL="YOUR_DATABASE_URL_HERE"\n`);
fs.appendFileSync(envPath, `TURSO_AUTH_TOKEN="YOUR_AUTH_TOKEN"\n`);
} else if (databaseProvider === "d1") {
fs.appendFileSync(envPath, `D1_DATABASE_URL="YOUR_D1_DATABASE_URL"\n`);
fs.appendFileSync(envPath, `D1_AUTH_TOKEN="YOUR_D1_AUTH_TOKEN"\n`);
} else {
fs.appendFileSync(envPath, `DATABASE_URL="file:./dev.db"`);
}
Expand Down
2 changes: 1 addition & 1 deletion src/installers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { fumaInstaller } from "./fumadocs.js";
export const availablePackages = ["prisma", "drizzle", "docs"] as const;
export type AvailablePackages = (typeof availablePackages)[number];

export const databaseProviders = ["sqlite", "turso"] as const;
export const databaseProviders = ["sqlite", "turso", "d1"] as const;
export type DatabaseProvider = (typeof databaseProviders)[number];

export interface InstallerOptions {
Expand Down
19 changes: 18 additions & 1 deletion src/installers/prisma.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,15 @@ export const prismaInstaller: Installer = ({
const schemaSrc = path.join(
extrasDir,
"prisma/schema",
`${databaseProvider === "d1" ? "d1" : "base"}${
databaseProvider === "turso" ? "-turso" : ""
}.prisma`
);
const databaseSrc = path.join(
extrasDir,
`d1/prisma/database.ts`
);
const schemaSrc = path.join(
`${"base"}${databaseProvider === "turso" ? "-turso" : ""}.prisma`
);
let schemaText = fs.readFileSync(schemaSrc, "utf-8");
Expand All @@ -83,8 +92,13 @@ export const prismaInstaller: Installer = ({
const destination = path.join(projectDir, "apps/api/src/routes/posts.ts");
fs.copyFileSync(clientSrc, destination);
// add postinstall and push script to package.json
const packageJsonPath = path.join(projectDir, "apps/api/package.json");
if (databaseProvider === "d1") {
fs.copyFileSync(databaseSrc, databaseDest);
} else {
fs.copyFileSync(databaseSrc, databaseDest);
}

const packageJsonPath = path.join(projectDir, "apps/api/package.json");
const databaseSrc = path.join(extrasDir, "prisma/database.ts");
const databaseDest = path.join(projectDir, "apps/api/src/database.ts");
fs.copyFileSync(databaseSrc, databaseDest);
Expand All @@ -111,6 +125,9 @@ export const prismaInstaller: Installer = ({
if (databaseProvider === "turso") {
fs.appendFileSync(envPath, `TURSO_DATABASE_URL="YOUR_DATABASE_URL_HERE"\n`);
fs.appendFileSync(envPath, `TURSO_AUTH_TOKEN="YOUR_AUTH_TOKEN"\n`);
} else if (databaseProvider === "d1") {
fs.appendFileSync(envPath, `D1_DATABASE_URL="YOUR_D1_DATABASE_URL"\n`);
fs.appendFileSync(envPath, `D1_AUTH_TOKEN="YOUR_D1_AUTH_TOKEN"\n`);
} else {
fs.appendFileSync(envPath, `DATABASE_URL="file:./dev.db"`);
}
Expand Down
8 changes: 8 additions & 0 deletions templates/extras/d1/drizzle/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { drizzle } from 'drizzle-orm/d1';
import { D1Database } from '@cloudflare/d1';
import { schema } from './schema';

export async function connectDatabase(db: D1Database) {
const drizzleInstance = drizzle(db, { schema });
return drizzleInstance;
}
13 changes: 13 additions & 0 deletions templates/extras/d1/drizzle/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { integer, sqliteTable, text } from 'drizzle-orm/sqlite-core';

export const posts = sqliteTable('posts', {
id: integer('id', { mode: 'increment' }).primaryKey(),
title: text('title', { length: 256 }).notNull(),
post: text('post', { length: 256 }).notNull(),
});

export type Post = {
id: number;
title: string;
post: string;
};
11 changes: 11 additions & 0 deletions templates/extras/d1/prisma/database.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { PrismaClient } from '@prisma/client';

export async function connectDatabase(env: { DATABASE_URL: string }) {
const { DATABASE_URL } = env;

const prisma = new PrismaClient({
datasources: { db: { url: DATABASE_URL } },
});

return prisma;
}
14 changes: 14 additions & 0 deletions templates/extras/d1/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
generator client {
provider = "prisma-client-js"
}

datasource db {
provider = "mysql"
url = env("D1_DATABASE_URL")
}

model Post {
id Int @id @default(autoincrement())
title String @db.VarChar(255)
post String @db.Text
}