Skip to content

Commit

Permalink
fix: 🐛 prisma-client prisma-client 监听DATABASE_URL,改变重新实例化
Browse files Browse the repository at this point in the history
Closes: #102
  • Loading branch information
meetqy committed Mar 9, 2023
1 parent 20a0ff0 commit 7a4810a
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@
"useWorkspaces": true,
"version": "independent",
"publish": {
"ignoreChanges": ["ignored-file", "*.md"]
"ignoreChanges": ["ignored-file", "*.md", "example/*.ts"]
}
}
6 changes: 1 addition & 5 deletions packages/prisma-client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,4 @@

## Useage

```shell
npx prisma migrate dev --name init
```

If sqlite database exist in you local, please use `npx prisma db push`.
Read [@eagleuse/eagleuse](https://www.npmjs.com/package/@eagleuse/eagleuse).
15 changes: 15 additions & 0 deletions packages/prisma-client/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,24 @@
import { PrismaClient } from "@prisma/client";
import _ from "lodash";
export * from "@prisma/client";
import chokidar from "chokidar";

let prisma: PrismaClient;
let watchDBFile = false;

const updatePrismaClient = _.debounce(() => {
prisma.$disconnect();
prisma = new PrismaClient();
}, 5000);

export const getPrisma = () => {
const { DATABASE_URL } = process.env;
if (DATABASE_URL && !watchDBFile) {
chokidar.watch(DATABASE_URL.replace("file:", "").split("db")[0] + "db").on("change", updatePrismaClient);

watchDBFile = true;
}

if (!prisma) {
prisma = new PrismaClient();
}
Expand Down
8 changes: 7 additions & 1 deletion packages/prisma-client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@
},
"dependencies": {
"@prisma/client": "^4.10.1",
"chokidar": "^3.5.3",
"lodash": "^4.17.21",
"prisma": "^4.10.1"
},
"main": "./dist/index.js",
Expand All @@ -44,5 +46,9 @@
"require": "./dist/index.js"
}
},
"gitHead": "90d70b524804295a65079e065f1edc16cc70833b"
"gitHead": "90d70b524804295a65079e065f1edc16cc70833b",
"devDependencies": {
"@types/lodash": "^4.14.191",
"@types/node": "^18.14.6"
}
}
4 changes: 3 additions & 1 deletion packages/prisma-client/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,13 @@ export default defineConfig({
},
rollupOptions: {
// 确保外部化处理那些你不想打包进库的依赖
external: ["prisma", "@prisma/client"],
external: ["prisma", "@prisma/client", "lodash", "chokidar"],
output: {
// 在 UMD 构建模式下为这些外部化的依赖提供一个全局变量
globals: {
prisma: "prisma",
lodash: "_",
chokidar: "chokidar",
"@prisma/client": "PrismaClient",
},
},
Expand Down

0 comments on commit 7a4810a

Please sign in to comment.