Skip to content

Commit 00b76cc

Browse files
authored
Prisma Adapter package (#23)
1 parent 1aa1c45 commit 00b76cc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

48 files changed

+1907
-1810
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ yarn-error.log*
2929
.env.development.local
3030
.env.test.local
3131
.env.production.local
32+
.env
3233

3334
# turbo
3435
.turbo

.npmrc

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
hoist=true
1+
node-linker=hoisted
2+
strict-peer-dependencies=false

README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,12 @@ Data is stored in local sqlite file!
66
# Packages
77

88
[ra-data-simple-prisma](./packages/ra-data-simple-prisma/)
9+
[next-auth-prisma](./packages/next-auth-prisma/)
910

1011
# Examples
1112

12-
[example admin app](./apps/admin/) Debug, test, and develop the package, but also use it as the admin of the website!
13-
[example website](./apps/website/) A nextjs boilerplate to show the data (very much under construction yet)
13+
[example admin app](./apps/admin/) Debug, test, and develop the packages, but also use it as the admin/CMS for the website!
14+
[example website](./apps/website/) A nextjs/mui boilerplate to show the data (very much under construction yet)
1415

1516
### Development
1617

@@ -32,4 +33,3 @@ this will spin both the apps and package in dev mode!
3233

3334
- [ ] Add all combos in README
3435
- [ ] create an amazing website showing all the data
35-
- [ ] Merge admin db with website db

apps/admin/.env.sample

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
GOOGLE_CLIENT_ID=
2+
GOOGLE_CLIENT_SECRET=
3+
NEXTAUTH_URL=http://localhost:3020
4+
NEXTAUTH_SECRET=
5+
DATABASE_URL=

apps/admin/.gitignore

-36
This file was deleted.

apps/admin/README.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
## Getting Started
22

3-
First, run the development server:
3+
First, add `.env` file adding the missing values
4+
5+
Then, run the development server:
46

57
```bash
68
pnpm dev
79
```
8-
9-
When making changes, the data will be saved in a local sqlite db!
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
import { AutocompleteInput, ReferenceInput } from "react-admin";
2+
3+
export const AutocompleteFilter = ({
4+
source,
5+
reference,
6+
field,
7+
label,
8+
alwaysOn,
9+
}: {
10+
source: string; //the local field that points to the foreign field (e.g. creator_id)
11+
reference: string; //the name of the foreign resource (e.g. users)
12+
field: string; //the field of the foreign resource (e.g. handle)
13+
label?: string;
14+
alwaysOn?: boolean;
15+
}) => (
16+
<ReferenceInput source={source} reference={reference} alwaysOn={alwaysOn}>
17+
<AutocompleteInput
18+
optionText={field}
19+
label={label || `${reference} ${field}`}
20+
filterToQuery={(searchText) => ({ [field]: searchText })}
21+
/>
22+
</ReferenceInput>
23+
);

apps/admin/package.json

+2-4
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,19 @@
77
"build": "next build",
88
"start": "next start",
99
"lint": "next lint",
10-
"postinstall": "prisma generate",
1110
"kill": "kill -9 $(lsof -t -i:3020)"
1211
},
1312
"dependencies": {
1413
"@babel/core": "7.17.9",
1514
"@mui/icons-material": "^5.6.2",
1615
"@mui/material": "^5.6.4",
1716
"@mui/styles": "^5.6.2",
18-
"@next-auth/prisma-adapter": "^1.0.3",
19-
"@prisma/client": "^3.12.0",
2017
"axios": "^0.27.2",
2118
"next": "12.1.5",
2219
"next-auth": "^4.3.4",
2320
"ra-data-simple-prisma": "workspace:*",
21+
"db": "workspace:*",
22+
"next-auth-prisma-adapter": "workspace:*",
2423
"react": "18.0.0",
2524
"react-admin": "^4.0.2",
2625
"react-dom": "18.0.0"
@@ -32,7 +31,6 @@
3231
"config": "workspace:*",
3332
"eslint": "8.13.0",
3433
"eslint-config-next": "12.1.5",
35-
"prisma": "^3.12.0",
3634
"typescript": "4.6.3"
3735
}
3836
}

apps/admin/pages/api/[resource].ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import { prismaWebClient } from "./../../../website/prisma/prismaWebClient";
21
import type { NextApiRequest, NextApiResponse } from "next";
32
import { defaultHandler } from "ra-data-simple-prisma";
43
import { apiHandler } from "../../middlewares/apiHandler";
4+
import prismaClient from "db";
55

66
export default apiHandler(async (req: NextApiRequest, res: NextApiResponse) => {
7-
return await defaultHandler(req, res, prismaWebClient);
7+
return await defaultHandler(req, res, prismaClient);
88
});

apps/admin/pages/api/admin.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1+
import prismaClient from "db";
12
import type { NextApiRequest, NextApiResponse } from "next";
23
import { defaultHandler } from "ra-data-simple-prisma";
34
import { apiHandler } from "../../middlewares/apiHandler";
4-
import { prismaAdminClient } from "../../prisma/prismaAdminClient";
55

66
export default apiHandler((req: NextApiRequest, res: NextApiResponse) => {
7-
return defaultHandler(req, res, prismaAdminClient);
7+
return defaultHandler(req, res, prismaClient);
88
});
+37-27
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,55 @@
1-
import NextAuth, { Session, User } from "next-auth";
1+
import NextAuth from "next-auth";
22
import GoogleProvider from "next-auth/providers/google";
3-
import { PrismaAdapter } from "@next-auth/prisma-adapter";
4-
import { prismaAdminClient } from "../../../prisma/prismaAdminClient";
3+
import { PrismaAdapter } from "next-auth-prisma-adapter";
4+
import prismaClient from "db";
55

66
export default async function auth(req: any, res: any) {
77
return await NextAuth(req, res, {
88
//debug: true,
9-
adapter: PrismaAdapter(prismaAdminClient),
9+
adapter: PrismaAdapter(prismaClient, {
10+
userModel: "adminUser",
11+
accountModel: "adminAccount",
12+
sessionModel: "adminSession",
13+
verificationTokenModel: "adminVerificationToken",
14+
}),
1015
providers: [
1116
GoogleProvider({
1217
clientId: process.env.GOOGLE_CLIENT_ID!,
1318
clientSecret: process.env.GOOGLE_CLIENT_SECRET!,
1419
}),
1520
],
16-
// callbacks: {
17-
// signIn: async (user, account, profile) => {
18-
// //security
19-
// if (account.provider === "google" && profile.verified_email === true)
20-
// return Promise.resolve(false);
21+
callbacks: {
22+
signIn: async ({ user, account, profile }) => {
23+
//security
24+
if (account.provider === "google" && profile.verified_email === true)
25+
return Promise.resolve(false);
2126

22-
// //restrict domain
23-
// if (
24-
// process.env.RESTRICT_EMAIL_DOMAIN &&
25-
// !profile?.email?.endsWith(`@${process.env.RESTRICT_EMAIL_DOMAIN}`)
26-
// ) {
27-
// return Promise.resolve(false);
28-
// }
27+
//restrict domain
28+
if (
29+
process.env.RESTRICT_EMAIL_DOMAIN &&
30+
!profile?.email?.endsWith(`@${process.env.RESTRICT_EMAIL_DOMAIN}`)
31+
) {
32+
return Promise.resolve(false);
33+
}
2934

30-
// //ok
31-
// return Promise.resolve(true);
32-
// },
33-
// session: async (session: Session, user: User) => {
34-
// //always add this, very handy
35-
// session.userId = user.id;
35+
//ok
36+
return Promise.resolve(true);
37+
},
38+
session: async ({ session, user, token }) => {
39+
//always add this, very handy
40+
session.userId = user.id;
3641

37-
// const userDoc = await prisma..findById(user.id, "role").lean();
42+
const admin = await prismaClient.adminUser.findUnique({
43+
select: {
44+
role: true,
45+
},
46+
where: { id: user.id },
47+
});
3848

39-
// session.role = userDoc?.role;
49+
session.role = admin?.role;
4050

41-
// return Promise.resolve(session);
42-
// },
43-
// },
51+
return Promise.resolve(session);
52+
},
53+
},
4454
});
4555
}

apps/admin/pages/api/post.ts

+14-19
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import prismaClient from "db";
12
import type { NextApiRequest, NextApiResponse } from "next";
23
import {
34
defaultHandler,
@@ -9,46 +10,40 @@ import {
910
updateHandler,
1011
} from "ra-data-simple-prisma";
1112
import { apiHandler } from "../../middlewares/apiHandler";
12-
import { prismaWebClient } from "./../../../website/prisma/prismaWebClient";
1313

1414
export default apiHandler((req: NextApiRequest, res: NextApiResponse) => {
1515
switch (req.body.method) {
1616
case "create":
17-
return createHandler(req, res, prismaWebClient["post"], {
17+
return createHandler(req, res, prismaClient["post"], {
1818
connect: {
1919
tags: "id",
2020
},
2121
});
2222
case "getList":
23-
return getListHandler(
24-
req as GetListRequest,
25-
res,
26-
prismaWebClient["post"],
27-
{
28-
include: { tags: true },
29-
transform: (posts: any[]) => {
30-
posts.forEach((post: any) => {
31-
post.tags = post.tags.map((tag: any) => tag.id);
32-
post._tags_count = post.tags.length;
33-
});
34-
},
35-
}
36-
);
23+
return getListHandler(req as GetListRequest, res, prismaClient["post"], {
24+
include: { tags: true },
25+
transform: (posts: any[]) => {
26+
posts.forEach((post: any) => {
27+
post.tags = post.tags.map((tag: any) => tag.id);
28+
post._tags_count = post.tags.length;
29+
});
30+
},
31+
});
3732
case "getOne":
38-
return getOneHandler(req as GetOneRequest, res, prismaWebClient["post"], {
33+
return getOneHandler(req as GetOneRequest, res, prismaClient["post"], {
3934
include: { tags: true },
4035
transform: (post: any) => {
4136
post.tags = post.tags.map((tag: any) => tag.id);
4237
post._tags_count = post.tags.length;
4338
},
4439
});
4540
case "update":
46-
return updateHandler(req, res, prismaWebClient["post"], {
41+
return updateHandler(req, res, prismaClient["post"], {
4742
set: {
4843
tags: "id",
4944
},
5045
});
5146
default:
52-
return defaultHandler(req, res, prismaWebClient);
47+
return defaultHandler(req, res, prismaClient);
5348
}
5449
});

apps/admin/prisma/admin.local.db

-68 KB
Binary file not shown.

apps/admin/prisma/migrations/20220503182548_init/migration.sql

-56
This file was deleted.

apps/admin/prisma/migrations/migration_lock.toml

-3
This file was deleted.

apps/admin/prisma/prismaAdminClient.ts

-3
This file was deleted.

0 commit comments

Comments
 (0)