nodestatus-server 1.2.0-beta → 1.2.2-beta

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -6,7 +6,7 @@
6
6
  <link rel="icon" href="/favicon.ico" />
7
7
  <link rel="stylesheet" href="//cdn.jsdelivr.net/npm/semantic-ui/dist/semantic.min.css">
8
8
  <title>NodeStatus</title>
9
- <script type="module" crossorigin src="/assets/index.3da88005.js"></script>
9
+ <script type="module" crossorigin src="/assets/index.90dd675e.js"></script>
10
10
  <link rel="modulepreload" href="/assets/vendor.5caed3d3.js">
11
11
  <link rel="stylesheet" href="/assets/index.f03b9a48.css">
12
12
  </head>
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "nodestatus-server",
3
- "version": "1.2.0-beta",
3
+ "version": "1.2.2-beta",
4
4
  "repository": {
5
5
  "type": "git",
6
6
  "url": "https://github.com/cokemine/nodestatus.git",
@@ -20,9 +20,10 @@
20
20
  "prisma"
21
21
  ],
22
22
  "dependencies": {
23
- "@prisma/client": "^3.3.0",
23
+ "@prisma/client": "3.4.2",
24
24
  "dotenv": "^10.0.0",
25
25
  "readable-stream": "^3.6.0",
26
+ "replace-in-file": "^6.3.2",
26
27
  "telegraf": "^4.4.2"
27
28
  },
28
29
  "devDependencies": {
@@ -53,7 +54,7 @@
53
54
  "koa2-connect-history-api-fallback": "^0.1.3",
54
55
  "log4js": "^6.3.0",
55
56
  "pm2": "^5.1.2",
56
- "prisma": "^3.3.0",
57
+ "prisma": "3.4.2",
57
58
  "typescript": "^4.4.4",
58
59
  "vite": "^2.6.13",
59
60
  "ws": "^8.2.3"
@@ -63,8 +64,8 @@
63
64
  "utf-8-validate": "^5.0.7"
64
65
  },
65
66
  "scripts": {
66
- "postinstall": "node scripts/postinstall.js",
67
- "prestart": "node scripts/prestart.js",
67
+ "postinstall": "node scripts/init.js",
68
+ "prestart": "node scripts/init.js",
68
69
  "start": "node build/app.js",
69
70
  "dev": "cross-env NODE_ENV=development rollup -c --watch",
70
71
  "build": "rollup -c",
@@ -32,3 +32,13 @@ model Option {
32
32
 
33
33
  @@map("options")
34
34
  }
35
+
36
+ model Event {
37
+ id Int @id @default(autoincrement())
38
+ username String
39
+ resolved Boolean @default(false)
40
+ created_at DateTime @default(now())
41
+ updated_at DateTime @updatedAt
42
+
43
+ @@map("events")
44
+ }
@@ -0,0 +1,83 @@
1
+ /*
2
+ * Prisma 在 https://github.com/prisma/prisma/issues/3834 移除了对多 Provider 的支持。
3
+ * 这使得我们需要在每次运行时重新根据 DATABASE_URL 手动修改 schema
4
+ * 并重新生成 @prisma/client 来实现修改成正确的 Provider
5
+ * 同时重新调用 `prisma db push` 来实现类似于 Sequelize 的 sync 效果 来应对可能的数据库结构变化
6
+ * 如果有更好的方案欢迎提出
7
+ * */
8
+ const fs = require('fs');
9
+ const { platform, homedir } = require('os');
10
+ const { resolve } = require('path');
11
+ const path = require('path');
12
+ const cp = require('child_process');
13
+ const replace = require('replace-in-file');
14
+ const dotenv = require('dotenv');
15
+
16
+ function backupDatabase(dbPath) {
17
+ if (!fs.existsSync(dbPath)) return;
18
+ console.log('The database file is detected to already exist.');
19
+ console.log('Trying to update database schema.....');
20
+ const date = new Date();
21
+ fs.copyFileSync(
22
+ dbPath,
23
+ `${dbPath}_${date.getFullYear()}_${date.getMonth() + 1}_${date.getDate()}_${date.getHours()}_${date.getMinutes()}_${date.getSeconds()}.bak`
24
+ );
25
+ }
26
+
27
+ function checkDatabaseType() {
28
+ const dbPath = process.env.DATABASE || '';
29
+ if (dbPath.includes('mysql:')) {
30
+ return 'mysql';
31
+ }
32
+ if (dbPath.includes('postgresql:')) {
33
+ return 'postgresql';
34
+ }
35
+ return 'sqlite';
36
+ }
37
+
38
+ function initDatabase() {
39
+ const databaseType = checkDatabaseType();
40
+ const dbPath = process.env.DATABASE || (
41
+ platform() === 'win32'
42
+ ? `file:${resolve(homedir(), '.nodestatus/db.sqlite')}`
43
+ : 'file:/usr/local/NodeStatus/server/db.sqlite'
44
+ );
45
+ const envOption = { BINARY_TARGETS: '["native"]', ...process.env };
46
+
47
+ /* backup database if is sqlite */
48
+ if (databaseType === 'sqlite') {
49
+ envOption.DATABASE_URL = dbPath.includes('file:') ? dbPath : `file:${dbPath}`;
50
+ backupDatabase(dbPath.replace('file:', ''));
51
+ } else {
52
+ envOption.DATABASE_URL = dbPath;
53
+ }
54
+
55
+ /* Replace provider since prisma dropped provider array notation support */
56
+ replace.replaceInFile({
57
+ files: path.resolve(__dirname, '../prisma/schema.prisma'),
58
+ from: /provider = "\w+"/,
59
+ to: `provider = "${databaseType}"`
60
+ }).then(() => {
61
+ let cmd = 'prisma';
62
+ platform() === 'win32' && (cmd += '.cmd');
63
+
64
+ /* Regenerate correct prisma client */
65
+ const prisma = cp.spawn(cmd, ['db', 'push'], {
66
+ env: envOption,
67
+ cwd: resolve(__dirname, '../'),
68
+ stdio: 'inherit'
69
+ });
70
+
71
+ prisma.on('close', code => {
72
+ if (code) {
73
+ console.log('Something wrong while updating database schema.');
74
+ process.exit(code);
75
+ } else {
76
+ console.log(`Database file location: ${dbPath}`);
77
+ }
78
+ });
79
+ });
80
+ }
81
+
82
+ dotenv.config({ path: path.resolve(homedir(), '.nodestatus/.env.local') });
83
+ initDatabase();