Skip to content

Commit

Permalink
feat: fix DMMF issues, update prisma to 5+
Browse files Browse the repository at this point in the history
BREAKING CHANGE: You should now use "getModelByName" helper exported by "@adminjs/prisma".
  • Loading branch information
dziraf committed Jul 18, 2023
1 parent bc4f0e1 commit 69ffa6f
Show file tree
Hide file tree
Showing 20 changed files with 1,706 additions and 1,722 deletions.
37 changes: 30 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Whole code can be found in `example-app` directory in the repository.
import express from 'express'
import AdminJS from 'adminjs'
import AdminJSExpress from '@adminjs/express'
import { Database, Resource } from '@adminjs/prisma'
import { Database, Resource, getModelByName } from '@adminjs/prisma'
import { PrismaClient } from '@prisma/client'
import { DMMFClass } from '@prisma/client/runtime'

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

// `_baseDmmf` contains necessary Model metadata. `PrismaClient` type doesn't have it included
const dmmf = ((prisma as any)._baseDmmf as DMMFClass)

const admin = new AdminJS({
resources: [{
resource: { model: dmmf.modelMap.Post, client: prisma },
resource: { model: getModelByName('Post'), client: prisma },
options: {},
}, {
resource: { model: dmmf.modelMap.Profile, client: prisma },
resource: { model: getModelByName('Profile'), client: prisma },
options: {},
}, {
resource: { model: dmmf.modelMap.User, client: prisma },
resource: { model: getModelByName('Publisher'), client: prisma },
options: {},
}],
})
Expand Down Expand Up @@ -87,6 +84,32 @@ yarn test

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

## Running example app with local code modifications

MySQL database is required. You can use the database from `adminjs-example-app`:
https://github.com/SoftwareBrothers/adminjs-example-app/blob/master/docker-compose.yaml#L24

```
$ yarn
$ yarn build # after making changes or run "yarn dev" and open a new terminal for next command
$ yarn link
$ cd example-app
$ yarn
$ npx prisma generate
$ npx prisma migrate dev
```

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.

Continue in `example-app` folder:
```
$ yarn link "@adminjs/prisma"
$ yarn build
$ yarn start
```

The app should start at port 3000.

## License

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.
Expand Down
10 changes: 5 additions & 5 deletions example-app/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
"@types/express": "^4.17.17",
"@types/node": "^18",
"dotenv-cli": "^7.0.0",
"prisma": "^4.11.0",
"prisma": "^5.0.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.5"
},
"dependencies": {
"@adminjs/express": "^5.1.0",
"@adminjs/prisma": "^3.0.1",
"@prisma/client": "^4.11.0",
"adminjs": "^6.8.7",
"@adminjs/express": "^6.0.0",
"@adminjs/prisma": "^4.0.1",
"@prisma/client": "^5.0.0",
"adminjs": "^7.0.9",
"express": "^4.18.2",
"express-formidable": "^1.2.0",
"express-session": "^1.17.3",
Expand Down
65 changes: 29 additions & 36 deletions example-app/prisma/migrations/20211014132905_init/migration.sql
Original file line number Diff line number Diff line change
@@ -1,47 +1,40 @@
-- CreateEnum
CREATE TYPE "Status" AS ENUM ('ACTIVE', 'REMOVED');

-- CreateTable
CREATE TABLE "Post" (
"id" SERIAL NOT NULL,
"createdAt" TIMESTAMP(3) NOT NULL DEFAULT CURRENT_TIMESTAMP,
"updatedAt" TIMESTAMP(3) NOT NULL,
"title" VARCHAR(255) NOT NULL,
"content" TEXT,
"someJson" JSONB,
"status" "Status" NOT NULL DEFAULT E'ACTIVE',
"published" BOOLEAN NOT NULL DEFAULT false,
"authorId" INTEGER NOT NULL,

CONSTRAINT "Post_pkey" PRIMARY KEY ("id")
);
CREATE TABLE `Post` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`createdAt` DATETIME(3) NOT NULL DEFAULT CURRENT_TIMESTAMP(3),
`updatedAt` DATETIME(3) NOT NULL,
`title` VARCHAR(255) NOT NULL,
`content` VARCHAR(191) NULL,
`someJson` JSON NULL,
`status` ENUM('ACTIVE', 'REMOVED') NOT NULL DEFAULT 'ACTIVE',
`published` BOOLEAN NOT NULL DEFAULT false,
`publisherId` INTEGER NOT NULL,

PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE "Profile" (
"id" SERIAL NOT NULL,
"bio" TEXT,
"userId" INTEGER NOT NULL,
CREATE TABLE `Profile` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`bio` VARCHAR(191) NULL,
`publisherId` INTEGER NOT NULL,

CONSTRAINT "Profile_pkey" PRIMARY KEY ("id")
);
UNIQUE INDEX `Profile_publisherId_key`(`publisherId`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

-- CreateTable
CREATE TABLE "User" (
"id" SERIAL NOT NULL,
"email" TEXT NOT NULL,
"name" TEXT,

CONSTRAINT "User_pkey" PRIMARY KEY ("id")
);

-- CreateIndex
CREATE UNIQUE INDEX "Profile_userId_key" ON "Profile"("userId");
CREATE TABLE `Publisher` (
`id` INTEGER NOT NULL AUTO_INCREMENT,
`email` VARCHAR(191) NOT NULL,
`name` VARCHAR(191) NULL,

-- CreateIndex
CREATE UNIQUE INDEX "User_email_key" ON "User"("email");
UNIQUE INDEX `Publisher_email_key`(`email`),
PRIMARY KEY (`id`)
) DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;

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

-- AddForeignKey
ALTER TABLE "Profile" ADD CONSTRAINT "Profile_userId_fkey" FOREIGN KEY ("userId") REFERENCES "User"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
ALTER TABLE `Profile` ADD CONSTRAINT `Profile_publisherId_fkey` FOREIGN KEY (`publisherId`) REFERENCES `Publisher`(`id`) ON DELETE RESTRICT ON UPDATE CASCADE;
2 changes: 1 addition & 1 deletion example-app/prisma/migrations/migration_lock.toml
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# Please do not edit this file manually
# It should be added in your version-control system (i.e. Git)
provider = "postgresql"
provider = "mysql"
14 changes: 7 additions & 7 deletions example-app/prisma/schema.prisma
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
datasource db {
provider = "postgresql"
provider = "mysql"
url = env("DATABASE_URL")
}

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

model Profile {
id Int @id @default(autoincrement())
bio String?
user User @relation(fields: [userId], references: [id])
userId Int @unique
publisher Publisher @relation(fields: [publisherId], references: [id])
publisherId Int @unique
}

model User {
model Publisher {
id Int @id @default(autoincrement())
email String @unique
name String?
Expand Down
11 changes: 4 additions & 7 deletions example-app/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,8 @@
import express from 'express';
import AdminJS from 'adminjs';
import AdminJSExpress from '@adminjs/express';
import { Database, Resource } from '@adminjs/prisma';
import { PrismaClient } from '@prisma/client';
import { DMMFClass } from '@prisma/client/runtime';
import { Database, Resource, getModelByName } from '@adminjs/prisma';

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

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

const dmmf = ((prisma as any)._baseDmmf as DMMFClass);

const admin = new AdminJS({
resources: [{
resource: { model: dmmf.modelMap.Post, client: prisma },
resource: { model: getModelByName('Post'), client: prisma },
options: {
properties: {
someJson: { type: 'mixed', isArray: true },
Expand All @@ -30,10 +27,10 @@ const run = async () => {
},
},
}, {
resource: { model: dmmf.modelMap.Profile, client: prisma },
resource: { model: getModelByName('Profile'), client: prisma },
options: {},
}, {
resource: { model: dmmf.modelMap.User, client: prisma },
resource: { model: getModelByName('Publisher'), client: prisma },
options: {},
}],
});
Expand Down
Loading

0 comments on commit 69ffa6f

Please sign in to comment.