Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 31 additions & 83 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,116 +8,64 @@ This is an official [AdminJS](https://github.com/SoftwareBrothers/adminjs) adapt

- npm: `npm install @adminjs/prisma`

## Usage
## Configuration

The plugin can be registered using standard `AdminJS.registerAdapter` method.
1. Add the `adminjs-prisma` generator to your `schema.prisma` file:

```typescript
import { Database, Resource } from '@adminjs/prisma'
import AdminJS from 'adminjs'
```prisma
generator adminjs {
provider = "node node_modules/@adminjs/prisma/lib/generator.js"
}
```

AdminJS.registerAdapter({ Database, Resource })
2. Run `prisma generate` to generate the metadata file:

```bash
npx prisma generate
```

## Example (Basic)
## Usage

Whole code can be found in `example-app` directory in the repository.
The plugin can be registered using standard `AdminJS.registerAdapter` method.

```typescript
import express from 'express'
import { Database, Resource } from '@adminjs/prisma'
import { prismaMetadata } from './generated/adminjs/metadata.js' // Adjust path to where generate output is
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()
})
```

## Example (Custom Client Output Path)

If you defined a custom client output path in your Prisma's schema, for example:

```prisma
generator client {
provider = "prisma-client-js"
output = "./client-prisma"
}
const adminJs = new AdminJS({
databases: [{
client: prisma,
dmmf: prismaMetadata, // Pass the generated metadata here
}],
// ...
})
```

You must:
* import your custom Prisma client
* provide it to each resource which uses that Prisma client
## Example (Resource-based)

*Example*:
If you prefer to add resources manually:

```typescript
// other imports
import PrismaModule from '../prisma/client-prisma/index.js';

// ...

const prisma = new PrismaModule.PrismaClient();
import { getModelByName } from '@adminjs/prisma'
import { prismaMetadata } from './generated/adminjs/metadata.js'

// ...

// Notice `clientModule` per resource
const admin = new AdminJS({
const adminJs = 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('Post', prismaMetadata),
client: prisma,
},
}, {
resource: { model: getModelByName('Profile', PrismaModule), client: prisma, clientModule: PrismaModule },
options: {},
}, {
resource: { model: getModelByName('Publisher', PrismaModule), client: prisma, clientModule: PrismaModule },
options: {},
}],
});

// ...
})
```

## ManyToOne / ManyToMany
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,14 +11,14 @@
"@types/express": "^4.17.17",
"@types/node": "^18",
"dotenv-cli": "^7.0.0",
"prisma": "^5.0.0",
"prisma": "^7.0.0",
"ts-node": "^10.9.1",
"typescript": "^4.9.5"
"typescript": "^5.0.0"
},
"dependencies": {
"@adminjs/express": "^6.0.0",
"@adminjs/prisma": "^4.0.1",
"@prisma/client": "^5.0.0",
"@adminjs/prisma": "file:..",
"@prisma/client": "^7.0.0",
"adminjs": "^7.0.9",
"express": "^4.18.2",
"express-formidable": "^1.2.0",
Expand All @@ -27,4 +27,4 @@
"reflect-metadata": "^0.1.13",
"tslib": "^2.5.0"
}
}
}
Loading