Skip to content

Commit 69ffa6f

Browse files
committed
feat: fix DMMF issues, update prisma to 5+
BREAKING CHANGE: You should now use "getModelByName" helper exported by "@adminjs/prisma".
1 parent bc4f0e1 commit 69ffa6f

20 files changed

+1706
-1722
lines changed

README.md

+30-7
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ Whole code can be found in `example-app` directory in the repository.
2727
import express from 'express'
2828
import AdminJS from 'adminjs'
2929
import AdminJSExpress from '@adminjs/express'
30-
import { Database, Resource } from '@adminjs/prisma'
30+
import { Database, Resource, getModelByName } from '@adminjs/prisma'
3131
import { PrismaClient } from '@prisma/client'
3232
import { DMMFClass } from '@prisma/client/runtime'
3333

@@ -40,18 +40,15 @@ AdminJS.registerAdapter({ Database, Resource })
4040
const run = async () => {
4141
const app = express()
4242

43-
// `_baseDmmf` contains necessary Model metadata. `PrismaClient` type doesn't have it included
44-
const dmmf = ((prisma as any)._baseDmmf as DMMFClass)
45-
4643
const admin = new AdminJS({
4744
resources: [{
48-
resource: { model: dmmf.modelMap.Post, client: prisma },
45+
resource: { model: getModelByName('Post'), client: prisma },
4946
options: {},
5047
}, {
51-
resource: { model: dmmf.modelMap.Profile, client: prisma },
48+
resource: { model: getModelByName('Profile'), client: prisma },
5249
options: {},
5350
}, {
54-
resource: { model: dmmf.modelMap.User, client: prisma },
51+
resource: { model: getModelByName('Publisher'), client: prisma },
5552
options: {},
5653
}],
5754
})
@@ -87,6 +84,32 @@ yarn test
8784

8885
Make sure you have an `.env` file with `DATABASE_URL` specified.
8986

87+
## Running example app with local code modifications
88+
89+
MySQL database is required. You can use the database from `adminjs-example-app`:
90+
https://github.com/SoftwareBrothers/adminjs-example-app/blob/master/docker-compose.yaml#L24
91+
92+
```
93+
$ yarn
94+
$ yarn build # after making changes or run "yarn dev" and open a new terminal for next command
95+
$ yarn link
96+
$ cd example-app
97+
$ yarn
98+
$ npx prisma generate
99+
$ npx prisma migrate dev
100+
```
101+
102+
Now copy `example-app/node_modules/.prisma` folder into `node_modules/.prisma`. This is required because installing library dependencies detects a different Prisma schema in test folder.
103+
104+
Continue in `example-app` folder:
105+
```
106+
$ yarn link "@adminjs/prisma"
107+
$ yarn build
108+
$ yarn start
109+
```
110+
111+
The app should start at port 3000.
112+
90113
## License
91114

92115
AdminJS is copyrighted © 2023 rst.software. It is a free software, and may be redistributed under the terms specified in the [LICENSE](LICENSE.md) file.

example-app/package.json

+5-5
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@
1111
"@types/express": "^4.17.17",
1212
"@types/node": "^18",
1313
"dotenv-cli": "^7.0.0",
14-
"prisma": "^4.11.0",
14+
"prisma": "^5.0.0",
1515
"ts-node": "^10.9.1",
1616
"typescript": "^4.9.5"
1717
},
1818
"dependencies": {
19-
"@adminjs/express": "^5.1.0",
20-
"@adminjs/prisma": "^3.0.1",
21-
"@prisma/client": "^4.11.0",
22-
"adminjs": "^6.8.7",
19+
"@adminjs/express": "^6.0.0",
20+
"@adminjs/prisma": "^4.0.1",
21+
"@prisma/client": "^5.0.0",
22+
"adminjs": "^7.0.9",
2323
"express": "^4.18.2",
2424
"express-formidable": "^1.2.0",
2525
"express-session": "^1.17.3",
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,40 @@
1-
-- CreateEnum
2-
CREATE TYPE "Status" AS ENUM ('ACTIVE', 'REMOVED');
3-
41
-- CreateTable
5-
CREATE TABLE "Post" (
6-
"id" SERIAL NOT NULL,
7-
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
8-
"updatedAt" TIMESTAMP(3) NOT NULL,
9-
"title" VARCHAR(255) NOT NULL,
10-
"content" TEXT,
11-
"someJson" JSONB,
12-
"status" "Status" NOT NULL DEFAULT E'ACTIVE',
13-
"published" BOOLEAN NOT NULL DEFAULT false,
14-
"authorId" INTEGER NOT NULL,
15-
16-
CONSTRAINT "Post_pkey" PRIMARY KEY ("id")
17-
);
2+
CREATE TABLE `Post` (
3+
`id` INTEGER NOT NULL AUTO_INCREMENT,
4+
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
5+
`updatedAt` DATETIME(3) NOT NULL,
6+
`title` VARCHAR(255) NOT NULL,
7+
`content` VARCHAR(191) NULL,
8+
`someJson` JSON NULL,
9+
`status` ENUM('ACTIVE', 'REMOVED') NOT NULL DEFAULT 'ACTIVE',
10+
`published` BOOLEAN NOT NULL DEFAULT false,
11+
`publisherId` INTEGER NOT NULL,
12+
13+
PRIMARY KEY (`id`)
14+
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
1815

1916
-- CreateTable
20-
CREATE TABLE "Profile" (
21-
"id" SERIAL NOT NULL,
22-
"bio" TEXT,
23-
"userId" INTEGER NOT NULL,
17+
CREATE TABLE `Profile` (
18+
`id` INTEGER NOT NULL AUTO_INCREMENT,
19+
`bio` VARCHAR(191) NULL,
20+
`publisherId` INTEGER NOT NULL,
2421

25-
CONSTRAINT "Profile_pkey" PRIMARY KEY ("id")
26-
);
22+
UNIQUE INDEX `Profile_publisherId_key`(`publisherId`),
23+
PRIMARY KEY (`id`)
24+
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
2725

2826
-- CreateTable
29-
CREATE TABLE "User" (
30-
"id" SERIAL NOT NULL,
31-
"email" TEXT NOT NULL,
32-
"name" TEXT,
33-
34-
CONSTRAINT "User_pkey" PRIMARY KEY ("id")
35-
);
36-
37-
-- CreateIndex
38-
CREATE UNIQUE INDEX "Profile_userId_key" ON "Profile"("userId");
27+
CREATE TABLE `Publisher` (
28+
`id` INTEGER NOT NULL AUTO_INCREMENT,
29+
`email` VARCHAR(191) NOT NULL,
30+
`name` VARCHAR(191) NULL,
3931

40-
-- CreateIndex
41-
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
32+
UNIQUE INDEX `Publisher_email_key`(`email`),
33+
PRIMARY KEY (`id`)
34+
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
4235

4336
-- AddForeignKey
44-
ALTER TABLE "Post" ADD CONSTRAINT "Post_authorId_fkey" FOREIGN KEY ("authorId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
37+
ALTER TABLE `Post` ADD CONSTRAINT `Post_publisherId_fkey` FOREIGN KEY (`publisherId`) REFERENCES `Publisher`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
4538

4639
-- AddForeignKey
47-
ALTER TABLE "Profile" ADD CONSTRAINT "Profile_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
40+
ALTER TABLE `Profile` ADD CONSTRAINT `Profile_publisherId_fkey` FOREIGN KEY (`publisherId`) REFERENCES `Publisher`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
# Please do not edit this file manually
22
# It should be added in your version-control system (i.e. Git)
3-
provider = "postgresql"
3+
provider = "mysql"

example-app/prisma/schema.prisma

+7-7
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
datasource db {
2-
provider = "postgresql"
2+
provider = "mysql"
33
url = env("DATABASE_URL")
44
}
55

@@ -18,21 +18,21 @@ model Post {
1818
updatedAt DateTime @updatedAt
1919
title String @db.VarChar(255)
2020
content String?
21-
someJson Json? @db.JsonB
21+
someJson Json? @db.Json
2222
status Status @default(ACTIVE)
2323
published Boolean @default(false)
24-
author User @relation(fields: [authorId], references: [id])
25-
authorId Int
24+
publisher Publisher @relation(fields: [publisherId], references: [id])
25+
publisherId Int
2626
}
2727

2828
model Profile {
2929
id Int @id @default(autoincrement())
3030
bio String?
31-
user User @relation(fields: [userId], references: [id])
32-
userId Int @unique
31+
publisher Publisher @relation(fields: [publisherId], references: [id])
32+
publisherId Int @unique
3333
}
3434

35-
model User {
35+
model Publisher {
3636
id Int @id @default(autoincrement())
3737
email String @unique
3838
name String?

example-app/src/index.ts

+4-7
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
import express from 'express';
33
import AdminJS from 'adminjs';
44
import AdminJSExpress from '@adminjs/express';
5-
import { Database, Resource } from '@adminjs/prisma';
65
import { PrismaClient } from '@prisma/client';
7-
import { DMMFClass } from '@prisma/client/runtime';
6+
import { Database, Resource, getModelByName } from '@adminjs/prisma';
87

98
const PORT = process.env.port || 3000;
109

@@ -15,11 +14,9 @@ AdminJS.registerAdapter({ Database, Resource });
1514
const run = async () => {
1615
const app = express();
1716

18-
const dmmf = ((prisma as any)._baseDmmf as DMMFClass);
19-
2017
const admin = new AdminJS({
2118
resources: [{
22-
resource: { model: dmmf.modelMap.Post, client: prisma },
19+
resource: { model: getModelByName('Post'), client: prisma },
2320
options: {
2421
properties: {
2522
someJson: { type: 'mixed', isArray: true },
@@ -30,10 +27,10 @@ const run = async () => {
3027
},
3128
},
3229
}, {
33-
resource: { model: dmmf.modelMap.Profile, client: prisma },
30+
resource: { model: getModelByName('Profile'), client: prisma },
3431
options: {},
3532
}, {
36-
resource: { model: dmmf.modelMap.User, client: prisma },
33+
resource: { model: getModelByName('Publisher'), client: prisma },
3734
options: {},
3835
}],
3936
});

0 commit comments

Comments
 (0)