校长
安装
-
线条 :
yarn add @adminjs/prisma -
无 :
npm install @adminjs/prisma
使用量
插件可以使用标准注册 AdminJS.registerAdapter 方法.
import { Database, Resource } from '@adminjs/prisma'
import AdminJS from 'adminjs'
AdminJS.registerAdapter({ Database, Resource })
示例(基本)
整个代码可见于 example-app 目录在仓库中.
import express from 'express'
import AdminJS from 'adminjs'
import AdminJSExpress from '@adminjs/express'
import { Database, Resource, getModelByName } from '@adminjs/prisma'
import { PrismaClient } from '@prisma/client'
import { DMMFClass } from '@prisma/client/runtime'
const PORT = process.env.port || 3000
const prisma = new PrismaClient()
AdminJS.registerAdapter({ Database, Resource })
const run = async () => {
const app = express()
const admin = new AdminJS({
resources: [{
resource: { model: getModelByName('Post'), client: prisma },
options: {},
}, {
resource: { model: getModelByName('Profile'), client: prisma },
options: {},
}, {
resource: { model: getModelByName('Publisher'), client: prisma },
options: {},
}],
})
const router = AdminJSExpress.buildRouter(admin)
app.use(admin.options.rootPath, router)
app.listen(PORT, () => {
console.log(`Example app listening at http://localhost:${PORT}`)
})
}
run()
.finally(async () => {
await prisma.$disconnect()
})
示例( 自定义客户端输出路径)
如果您定义了您 Prisma 计划中的自定义客户端输出路径, 例如 :
generator client {
provider = "prisma-client-js"
output = "./client-prisma"
}
你必须:
- 导入您的自定义 Prisma 客户端
- 提供给每个使用 Prisma 客户端的资源
示例数字 :
// other imports
import PrismaModule from '../prisma/client-prisma/index.js';
// ...
const prisma = new PrismaModule.PrismaClient();
// ...
// Notice `clientModule` per resource
const admin = new AdminJS({
resources: [{
resource: { model: getModelByName('Post', PrismaModule), client: prisma, clientModule: PrismaModule },
options: {
properties: {
someJson: { type: 'mixed', isArray: true },
'someJson.number': { type: 'number' },
'someJson.string': { type: 'string' },
'someJson.boolean': { type: 'boolean' },
'someJson.date': { type: 'datetime' },
},
},
}, {
resource: { model: getModelByName('Profile', PrismaModule), client: prisma, clientModule: PrismaModule },
options: {},
}, {
resource: { model: getModelByName('Publisher', PrismaModule), client: prisma, clientModule: PrismaModule },
options: {},
}],
});
// ...
ManyTo One / Many 变数
这些关系目前没有默认支持. 您可以使用自定义动作和组件管理它们 .
调取请求
在你做公关前 确定所有的测试都通过 和你的代码不会引起linter错误. 你可以通过运行来做到这一点:
yarn lint
yarn test
确定你有一个 .env 文件为 DATABASE_URL 页:1.
运行本地代码修改的示例应用程序
需要 MySQL 数据库 。 您可以使用数据库 adminjs-example-app数字 :
https://github.com/软件兄弟会/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
现在复制 example-app/node_modules/.prisma 文件夹进入 node_modules/.prisma。需要这样做,因为安装库依赖性在测试文件夹中检测到不同的Prisma schema.
继续 example-app 文件夹 :
$ yarn link "@adminjs/prisma"
$ yarn build
$ yarn start
该应用程序应从3000端口开始.
许可证
AdminJS拥有2023rst.software的版权. 它是一种自由软件,可按下列规定重新分配: 自由主义 文档.
关于 rst. 软件
我们是一个开放、友好的团队.