Skip to content

Commit

Permalink
update vercel deploy button
Browse files Browse the repository at this point in the history
  • Loading branch information
notrab committed Aug 28, 2024
1 parent 28373b4 commit 4f6eb99
Show file tree
Hide file tree
Showing 7 changed files with 50 additions and 41 deletions.
11 changes: 4 additions & 7 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
NEXT_PUBLIC_CLERK_SIGN_IN_URL=/sign-in
NEXT_PUBLIC_CLERK_SIGN_UP_URL=/sign-up

# Sign up to Clerk
NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY=
CLERK_SECRET_KEY=
Expand All @@ -10,13 +7,13 @@ CLERK_SECRET_KEY=
CLERK_WEBHOOK_SECRET=

# turso auth api-tokens mint clerk
TURSO_USER_API_TOKEN=
TURSO_API_TOKEN=

# your personal or organization name
TURSO_ORG_NAME=
TURSO_ORG=

# turso db create [database-name] --type schema
TURSO_SCHEMA_DATABASE_NAME=
TURSO_DATABASE_NAME=

# turso group tokens create <group-name>
TURSO_DATABASE_GROUP_AUTH_TOKEN=
TURSO_GROUP_AUTH_TOKEN=
42 changes: 27 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,29 @@ The app below uses a database per user, and is powered by Turso.
Deploy your own Turso powered platform in a few easy steps...

- [![Create a Database](https://sqlite.new/button)](https://sqlite.new?dump=https%3A%2F%2Fraw.githubusercontent.com%2Fnotrab%2Fturso-per-user-starter%2Fmain%2Fdump.sql&type=schema)
- Copy your database name, database URL, and org name
- Create a group token
- Create a user API token
- [Sign up to Clerk](https://clerk.com)
- Create a new application
- Copy your public key and secret key
- [![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fnotrab%2Fturso-per-user-starter&env=NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY,CLERK_SECRET_KEY,TURSO_USER_API_TOKEN,TURSO_ORG_NAME,TURSO_SCHEMA_DATABASE_NAME,TURSO_DATABASE_GROUP_AUTH_TOKEN)
- Add the environment variables from above
- Deploy your app

You may optionally set up webhooks to automate the creation of databases per user in the background &mdash; [learn more](https://github.com/notrab/turso-per-user-starter/wiki/Webhooks#using-webhooks-in-production).
- Note down the following (you'll need these later):
- Database name
- Org name
- Group Token (**Create Group Token** -> **Create Token**)
- Platform API Token (**Create Platform API Token** -> **Insert memorable name** -> **Create Token**))

- [Sign up to Clerk](https://clerk.com)
- Create a new application from the dashboard
- Note down the following (you'll need these later):
- Public key
- Secret key
- [![Deploy with Vercel](https://vercel.com/button)](https://vercel.com/new/clone?repository-url=https%3A%2F%2Fgithub.com%2Fnotrab%2Fturso-per-user-starter&env=NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY,CLERK_SECRET_KEY,TURSO_API_TOKEN,TURSO_ORG,TURSO_DATABASE_NAME,TURSO_GROUP_AUTH_TOKEN&demo-title=Turso%20Per%20User%20Starter&demo-description=Create%20a%20database%20per%20user&demo-image=https://raw.githubusercontent.com/notrab/turso-per-user-starter/28373b4c9c74f814e3749525ee3d53b603176834/app/opengraph-image.png&demo-url=https%3A%2F%2Fturso-per-user-starter.vercel.app)
- Add the following environment variables:
- `NEXT_PUBLIC_CLERK_PUBLISHABLE_KEY` - Clerk public key
- `CLERK_SECRET_KEY` - Clerk secret key
- `TURSO_API_TOKEN` - Platform API Token
- `TURSO_ORG` - Org name
- `TURSO_DATABASE_NAME` - Database name
- `TURSO_GROUP_AUTH_TOKEN` - Group Token
- Click **Deploy** and you're done!

_You may optionally set up webhooks to automate the creation of databases per user in the background &mdash; [learn more](https://github.com/notrab/turso-per-user-starter/wiki/Webhooks#using-webhooks-in-production)._

## Local Development

Expand Down Expand Up @@ -69,11 +81,11 @@ Start building your Turso powered platform in a few simple steps...
Now update `.env` to include your organization, and schema database name:

```bash
TURSO_ORG_NAME=
TURSO_SCHEMA_DATABASE_NAME=
TURSO_ORG=
TURSO_DATABASE_NAME=
```

> The `TURSO_ORG_NAME` can be your personal username, or the name of any organization you have with other users.
> The `TURSO_ORG` can be your personal username, or the name of any organization you have with other users.
</details>

Expand All @@ -89,7 +101,7 @@ Start building your Turso powered platform in a few simple steps...
Now update `.env` to include the group token:

```bash
TURSO_DATABASE_GROUP_AUTH_TOKEN=
TURSO_GROUP_AUTH_TOKEN=
```

> If you didn't already have one, a new group will be created for you with the name `default`.
Expand Down Expand Up @@ -121,7 +133,7 @@ Start building your Turso powered platform in a few simple steps...
Then set the API token in the `.env` file:

```bash
TURSO_USER_API_TOKEN=
TURSO_API_TOKEN=
```

</details>
Expand Down
2 changes: 1 addition & 1 deletion app/(authenticated)/dump.sql/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export async function GET() {
try {
const response = await fetch(url, {
headers: {
Authorization: `Bearer ${process.env.TURSO_DATABASE_GROUP_AUTH_TOKEN}`,
Authorization: `Bearer ${process.env.TURSO_GROUP_AUTH_TOKEN}`,
},
});

Expand Down
8 changes: 4 additions & 4 deletions app/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import { drizzle } from "drizzle-orm/libsql";
import * as schema from "@/db/schema";

const turso = createTursoClient({
token: process.env.TURSO_USER_API_TOKEN!,
org: process.env.TURSO_ORG_NAME!,
token: process.env.TURSO_API_TOKEN!,
org: process.env.TURSO_ORG!,
});

export async function checkDatabaseExists(): Promise<boolean> {
Expand Down Expand Up @@ -38,7 +38,7 @@ export async function getDatabaseClient() {
return drizzle(
createLibsqlClient({
url,
authToken: process.env.TURSO_DATABASE_GROUP_AUTH_TOKEN,
authToken: process.env.TURSO_GROUP_AUTH_TOKEN,
}),
{ schema },
);
Expand All @@ -54,7 +54,7 @@ export function getDatabaseName(): string | null {
}

function getDatabaseUrl(dbName: string | null): string | null {
return dbName ? `${dbName}-${process.env.TURSO_ORG_NAME}.turso.io` : null;
return dbName ? `${dbName}-${process.env.TURSO_ORG}.turso.io` : null;
}

function getLibsqlUrl(): string | null {
Expand Down
6 changes: 3 additions & 3 deletions app/webhooks/clerk/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { WebhookEvent } from "@clerk/nextjs/server";
import md5 from "md5";

const turso = createClient({
token: process.env.TURSO_USER_API_TOKEN!,
org: process.env.TURSO_ORG_NAME!,
token: process.env.TURSO_API_TOKEN!,
org: process.env.TURSO_ORG!,
});

const allowedEvents = ["user.created"];
Expand Down Expand Up @@ -70,7 +70,7 @@ export async function POST(req: Request) {

try {
await turso.databases.create(databaseName, {
schema: process.env.TURSO_SCHEMA_DATABASE_NAME!,
schema: process.env.TURSO_DATABASE_NAME!,
});
} catch (err) {
console.error("Error processing webhook:", err);
Expand Down
6 changes: 3 additions & 3 deletions app/welcome/route.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { createClient } from "@tursodatabase/api";
import { checkDatabaseExists, getDatabaseName } from "../utils";

const turso = createClient({
token: process.env.TURSO_USER_API_TOKEN!,
org: process.env.TURSO_ORG_NAME!,
token: process.env.TURSO_API_TOKEN!,
org: process.env.TURSO_ORG!,
});

export async function GET() {
Expand All @@ -26,7 +26,7 @@ export async function GET() {

try {
await turso.databases.create(dbName, {
schema: process.env.TURSO_SCHEMA_DATABASE_NAME!,
schema: process.env.TURSO_DATABASE_NAME!,
group: "default",
});
} catch (err) {
Expand Down
16 changes: 8 additions & 8 deletions drizzle.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,19 @@ import "./envConfig";

import { defineConfig } from "drizzle-kit";

if (!process.env.TURSO_SCHEMA_DATABASE_NAME) {
throw new Error("TURSO_SCHEMA_DATABASE_NAME is missing");
if (!process.env.TURSO_DATABASE_NAME) {
throw new Error("TURSO_DATABASE_NAME is missing");
}

if (!process.env.TURSO_ORG_NAME) {
throw new Error("TURSO_ORG_NAME is missing");
if (!process.env.TURSO_ORG) {
throw new Error("TURSO_ORG is missing");
}

if (!process.env.TURSO_DATABASE_GROUP_AUTH_TOKEN) {
throw new Error("TURSO_DATABASE_GROUP_AUTH_TOKEN is missing");
if (!process.env.TURSO_GROUP_AUTH_TOKEN) {
throw new Error("TURSO_GROUP_AUTH_TOKEN is missing");
}

const url = `libsql://${process.env.TURSO_SCHEMA_DATABASE_NAME}-${process.env.TURSO_ORG_NAME}.turso.io`;
const url = `libsql://${process.env.TURSO_DATABASE_NAME}-${process.env.TURSO_ORG}.turso.io`;

export default defineConfig({
schema: "./db/schema.ts",
Expand All @@ -23,6 +23,6 @@ export default defineConfig({
driver: "turso",
dbCredentials: {
url,
authToken: process.env.TURSO_DATABASE_GROUP_AUTH_TOKEN,
authToken: process.env.TURSO_GROUP_AUTH_TOKEN,
},
});

0 comments on commit 4f6eb99

Please sign in to comment.