Skip to content
This repository has been archived by the owner on Apr 7, 2024. It is now read-only.

Pro 156/seed data #74

Merged
merged 3 commits into from
Jan 17, 2024
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
29 changes: 17 additions & 12 deletions apps/web/pages/docs/installation.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,7 @@ pnpm i
- Keep this page open. You will need to get your `App ID`, `Client ID`, `Client secret`, and `Private key` handy for later

### Create your database
- If you haven't already, create a PlanetScale database. You can do it through their dashboard.
- Remember the database name for later.
- Setup a MySQL database. You can do this locally or through a cloud provider. Floe uses PlanetScale.

### Setup Vercel
- In Vercel, create a Project. You can create it from the GitHub repository you just forked.
Expand Down Expand Up @@ -66,8 +65,10 @@ Create a `.env` by copying the `.env.example` file in the following directories:
</FileTree>

### `DATABASE_URL`
- Set the `DATABASE_URL` to your MySQL database. If you are using PlanetScale,
you can get this by running `pscale connect <DATABASE_NAME> --port 3309` and
copying the `DATABASE_URL` from the output.
- Set the `DATABASE_URL` to `mysql://[email protected]:3309/<DATABASE_NAME>?connection_limit=100`
- Replace `<DATABASE_NAME>` with the name of the database you created earlier

### `NEXTAUTH_SECRET`
- Generate a cryptographically secure string and set it as the
Expand All @@ -87,15 +88,18 @@ Create a `.env` by copying the `.env.example` file in the following directories:

## Running the app
<Steps>
### Connect to PlanetScale and push the schema
Connect to your PlanetScale database.
### Sync your database
Run this from the `packages/db` directory.
```bash copy
pscale connect <DATABASE_NAME> initial-setup --port 3309
npx prisma db push
```
Now push your schema.

### Seed your database
Run this from the `packages/db` directory.
```bash copy
npx prisma db push
npx prisma db seed
```
Copy the output at the end. This will be needed to login.

### Start the app
Let's start the app (finally 🎉)!
Expand All @@ -109,9 +113,10 @@ This should have started 3 dev servers:
- Dashboard: http://localhost:3001
- API: http://localhost:4000

### Create a user
Go to http://localhost:3001 and signup. Assuming you setup your `SENDGRID_API`
(and everything else), you should receive an email with a link to verify your
account. That's it!
### Login
Go to http://localhost:3001 and enter the auth cookie you copied earlier into
your console. Or, if you entered a `SENDGRID_API` key you can enter an email to
signup manually. You should receive an email with a link to verify your account.
That's it!

</Steps>
6 changes: 3 additions & 3 deletions apps/web/pages/docs/prerequisites.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ macOS, Windows, and Linux are supported.
### Accounts
- [GitHub](https://github.com/)
- [Vercel](https://vercel.com/)
- [PlanetScale](https://planetscale.com/)
- [SendGrid](https://sendgrid.com/)
- [OpenAI](https://openai.com/)
- [SendGrid](https://sendgrid.com/) (Optional)
- [LangFuse](https://langfuse.com/) (Optional)
- [PlanetScale](https://planetscale.com/) (Optional)

### Packages
- [Node.js 18.19](https://nodejs.org/en) or later
- [pnpm 8.x](https://pnpm.io)
- [PlanetScale CLI](https://planetscale.com/features/cli)
- [PlanetScale CLI](https://planetscale.com/features/cli) (If using PlanetScale)
6 changes: 5 additions & 1 deletion packages/db/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
"db:generate": "prisma generate",
"db:push": "prisma db push",
"db:studio": "prisma studio",
"postinstall": "prisma generate"
"postinstall": "prisma generate",
"seed": "ts-node prisma/seed.ts"
},
"prisma": {
"seed": "ts-node --compiler-options {\"module\":\"CommonJS\"} prisma/seed.ts"
},
"author": "",
"devDependencies": {
Expand Down
1 change: 1 addition & 0 deletions packages/db/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ model WorkspaceMembership {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt

@@unique([workspaceId, userId])
@@index([userId])
@@index([workspaceId])
}
Expand Down
74 changes: 74 additions & 0 deletions packages/db/prisma/seed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { randomBytes } from "node:crypto";
import { PrismaClient } from "@prisma/client";

const prisma = new PrismaClient();

async function main() {
const workspace = await prisma.workspace.upsert({
where: { slug: "test-workspace" },
update: {},
create: {
name: "Test Workspace",
slug: "test-workspace",
},
});

console.log("💼 Workspace created: ", workspace);

const user = await prisma.user.upsert({
where: { email: "[email protected]" },
update: {},
create: {
email: "[email protected]",
name: "Testy McTestface",
},
});

console.log("👤 User created: ", user);

const workspaceMembership = await prisma.workspaceMembership.upsert({
where: {
workspaceId_userId: {
workspaceId: workspace.id,
userId: user.id,
},
},
update: {},
create: {
workspaceId: workspace.id,
userId: user.id,
},
});

console.log("👥 WorkspaceMembership created: ", workspaceMembership);

const fakeSession = randomBytes(16).toString("hex");

const session = await prisma.session.upsert({
where: { sessionToken: fakeSession },
update: {
expires: new Date("2030-03-25"),
},
create: {
sessionToken: fakeSession,
userId: user.id,
expires: new Date("2030-03-25"),
},
});

console.log("🔐 Session created: ", session);
console.log(
"ℹ️ To log in, start the dev server and go to localhost:3001. Then run the following command in your console:\n"
);
console.log(`document.cookie="next-auth.session-token=${fakeSession}"`);
}

main()
.then(async () => {
await prisma.$disconnect();
})
.catch(async (e) => {
console.error(e);
await prisma.$disconnect();
process.exit(1);
});