WIP for Prisma integration and account and membership schema

This commit is contained in:
Michael Dausmann
2023-01-29 01:06:01 +11:00
parent 7246c0c5a4
commit 791192a1ae
6 changed files with 200 additions and 3 deletions

View File

@@ -70,4 +70,25 @@ modules: ['@nuxtjs/supabase']
Follow these instructions to add google oath https://supabase.com/docs/guides/auth/social-login/auth-google
Then I frigged around trying to get the nuxt-supabase module to work properly for the oauth flow. It's a bit of a mess TBH.
Then I frigged around trying to get the nuxt-supabase module to work properly for the oauth flow. It's a bit of a mess TBH. Eventually I looked at the demo https://github.com/nuxt-modules/supabase/tree/main/demo like a chump and got it working
Integrating Prisma...
This was a tricky decision I think. the Subabase client has some pseudo sql Ormy sort of features already
but Prisma has this awesome schema management support and autogeneration of a typed client.... and I already had a schema lying
around that was nearly what I needed and it was nice to be able to re-use it.
```
npm install prisma --save-dev
npx prisma init
```
go to Supabase -> settings -> database -> connection string -> URI.. and copy the URI into the
DATABASE_URL setting created with prisma init.
still in database, go to 'Database password' and reset/set it and copy the password into the [YOUR-PASSWORD] placeholder in the URI
Then I manually hand coded the schema.prisma file based on something else I already had.
```
npx prisma db push
npm install @prisma/client --save-dev
npx prisma generate
```

85
package-lock.json generated
View File

@@ -7,7 +7,9 @@
"hasInstallScript": true,
"devDependencies": {
"@nuxtjs/supabase": "^0.3.0",
"nuxt": "^3.1.1"
"@prisma/client": "^4.9.0",
"nuxt": "^3.1.1",
"prisma": "^4.9.0"
}
},
"node_modules/@ampproject/remapping": {
@@ -1237,6 +1239,40 @@
"pathe": "^1.0.0"
}
},
"node_modules/@prisma/client": {
"version": "4.9.0",
"resolved": "https://registry.npmjs.org/@prisma/client/-/client-4.9.0.tgz",
"integrity": "sha512-bz6QARw54sWcbyR1lLnF2QHvRW5R/Jxnbbmwh3u+969vUKXtBkXgSgjDA85nji31ZBlf7+FrHDy5x+5ydGyQDg==",
"dev": true,
"hasInstallScript": true,
"dependencies": {
"@prisma/engines-version": "4.9.0-42.ceb5c99003b99c9ee2c1d2e618e359c14aef2ea5"
},
"engines": {
"node": ">=14.17"
},
"peerDependencies": {
"prisma": "*"
},
"peerDependenciesMeta": {
"prisma": {
"optional": true
}
}
},
"node_modules/@prisma/engines": {
"version": "4.9.0",
"resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-4.9.0.tgz",
"integrity": "sha512-t1pt0Gsp+HcgPJrHFc+d/ZSAaKKWar2G/iakrE07yeKPNavDP3iVKPpfXP22OTCHZUWf7OelwKJxQgKAm5hkgw==",
"dev": true,
"hasInstallScript": true
},
"node_modules/@prisma/engines-version": {
"version": "4.9.0-42.ceb5c99003b99c9ee2c1d2e618e359c14aef2ea5",
"resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-4.9.0-42.ceb5c99003b99c9ee2c1d2e618e359c14aef2ea5.tgz",
"integrity": "sha512-M16aibbxi/FhW7z1sJCX8u+0DriyQYY5AyeTH7plQm9MLnURoiyn3CZBqAyIoQ+Z1pS77usCIibYJWSgleBMBA==",
"dev": true
},
"node_modules/@rollup/plugin-alias": {
"version": "4.0.3",
"resolved": "https://registry.npmjs.org/@rollup/plugin-alias/-/plugin-alias-4.0.3.tgz",
@@ -6139,6 +6175,23 @@
"url": "https://github.com/sponsors/sindresorhus"
}
},
"node_modules/prisma": {
"version": "4.9.0",
"resolved": "https://registry.npmjs.org/prisma/-/prisma-4.9.0.tgz",
"integrity": "sha512-bS96oZ5oDFXYgoF2l7PJ3Mp1wWWfLOo8B/jAfbA2Pn0Wm5Z/owBHzaMQKS3i1CzVBDWWPVnOohmbJmjvkcHS5w==",
"dev": true,
"hasInstallScript": true,
"dependencies": {
"@prisma/engines": "4.9.0"
},
"bin": {
"prisma": "build/index.js",
"prisma2": "build/index.js"
},
"engines": {
"node": ">=14.17"
}
},
"node_modules/process-nextick-args": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz",
@@ -9228,6 +9281,27 @@
"pathe": "^1.0.0"
}
},
"@prisma/client": {
"version": "4.9.0",
"resolved": "https://registry.npmjs.org/@prisma/client/-/client-4.9.0.tgz",
"integrity": "sha512-bz6QARw54sWcbyR1lLnF2QHvRW5R/Jxnbbmwh3u+969vUKXtBkXgSgjDA85nji31ZBlf7+FrHDy5x+5ydGyQDg==",
"dev": true,
"requires": {
"@prisma/engines-version": "4.9.0-42.ceb5c99003b99c9ee2c1d2e618e359c14aef2ea5"
}
},
"@prisma/engines": {
"version": "4.9.0",
"resolved": "https://registry.npmjs.org/@prisma/engines/-/engines-4.9.0.tgz",
"integrity": "sha512-t1pt0Gsp+HcgPJrHFc+d/ZSAaKKWar2G/iakrE07yeKPNavDP3iVKPpfXP22OTCHZUWf7OelwKJxQgKAm5hkgw==",
"dev": true
},
"@prisma/engines-version": {
"version": "4.9.0-42.ceb5c99003b99c9ee2c1d2e618e359c14aef2ea5",
"resolved": "https://registry.npmjs.org/@prisma/engines-version/-/engines-version-4.9.0-42.ceb5c99003b99c9ee2c1d2e618e359c14aef2ea5.tgz",
"integrity": "sha512-M16aibbxi/FhW7z1sJCX8u+0DriyQYY5AyeTH7plQm9MLnURoiyn3CZBqAyIoQ+Z1pS77usCIibYJWSgleBMBA==",
"dev": true
},
"@rollup/plugin-alias": {
"version": "4.0.3",
"resolved": "https://registry.npmjs.org/@rollup/plugin-alias/-/plugin-alias-4.0.3.tgz",
@@ -12875,6 +12949,15 @@
"integrity": "sha512-6UqkYefdogmzqAZWzJ7laYeJnaXDy2/J+ZqiiMtS7t7OfpXWTlaeGMwX8U6EFvPV/YWWEKRkS8hKS4k60WHTOg==",
"dev": true
},
"prisma": {
"version": "4.9.0",
"resolved": "https://registry.npmjs.org/prisma/-/prisma-4.9.0.tgz",
"integrity": "sha512-bS96oZ5oDFXYgoF2l7PJ3Mp1wWWfLOo8B/jAfbA2Pn0Wm5Z/owBHzaMQKS3i1CzVBDWWPVnOohmbJmjvkcHS5w==",
"dev": true,
"requires": {
"@prisma/engines": "4.9.0"
}
},
"process-nextick-args": {
"version": "2.0.1",
"resolved": "https://registry.npmjs.org/process-nextick-args/-/process-nextick-args-2.0.1.tgz",

View File

@@ -9,6 +9,8 @@
},
"devDependencies": {
"@nuxtjs/supabase": "^0.3.0",
"nuxt": "^3.1.1"
"@prisma/client": "^4.9.0",
"nuxt": "^3.1.1",
"prisma": "^4.9.0"
}
}

View File

@@ -3,10 +3,13 @@
definePageMeta({
middleware: ['auth'],
});
const notes = await $fetch('/api/notes')
</script>
<template>
<div>
<h3>{{ user?.user_metadata.full_name }}'s Dashboard</h3>
<p v-for="note in notes">{{ note.note_text }}</p><!-- TODO - wtf.. typing-->
<div>Stuff</div>
</div>
</template>

63
prisma/schema.prisma Normal file
View File

@@ -0,0 +1,63 @@
// This is your Prisma schema file,
// learn more about it in the docs: https://pris.ly/d/prisma-schema
generator client {
provider = "prisma-client-js"
}
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
model User {
id Int @id @default(autoincrement())
supabase_uid String
display_name String?
membership Membership?
@@map("users")
}
model Membership {
id Int @id @default(autoincrement())
user_id Int @unique
account_id Int
account Account @relation(fields: [account_id], references: [id])
user User @relation(fields: [user_id], references: [id])
@@map("membership")
}
model Account {
id Int @id @default(autoincrement())
name String
current_period_ends DateTime @default(now())
features String[]
plan_id Int
plan Plan @relation(fields: [plan_id], references: [id])
members Membership[]
notes Note[]
@@map("account")
}
model Plan {
id Int @id @default(autoincrement())
name String
features String[]
accounts Account[]
@@map("plan")
}
model Note {
id Int @id @default(autoincrement())
account_id Int?
account Account? @relation(fields: [account_id], references: [id])
note_text String
@@map("note")
}

25
server/api/notes.ts Normal file
View File

@@ -0,0 +1,25 @@
import { PrismaClient } from '@prisma/client';
import { serverSupabaseClient } from '#supabase/server';
const prisma = new PrismaClient();
export default defineEventHandler(async (event) => {
const client = serverSupabaseClient(event)
const user = await client.auth.getUser();
const dbUser = await prisma.user.findFirstOrThrow({
where: {
supabase_uid: user.data.id // TODO - this shit is messy.. typing
},
include: {
membership: true, // Return all fields
},
});
const data = await prisma.note.findMany({
where:{
account_id: dbUser.membership?.account_id
}
});
return data;
})