aboutsummaryrefslogtreecommitdiff
path: root/src/api/database/seeds/initial.js
diff options
context:
space:
mode:
authorPitu <[email protected]>2019-02-21 23:49:29 +0900
committerPitu <[email protected]>2019-02-21 23:49:29 +0900
commit44e6fd31d2fa7761c90ff1d6932cf69d163b22e8 (patch)
treecaa404d59b5817e5a302df55d66a740765fc24d2 /src/api/database/seeds/initial.js
parentderp (diff)
downloadhost.fuwn.me-44e6fd31d2fa7761c90ff1d6932cf69d163b22e8.tar.xz
host.fuwn.me-44e6fd31d2fa7761c90ff1d6932cf69d163b22e8.zip
Database migration and seeding
Diffstat (limited to 'src/api/database/seeds/initial.js')
-rw-r--r--src/api/database/seeds/initial.js32
1 files changed, 32 insertions, 0 deletions
diff --git a/src/api/database/seeds/initial.js b/src/api/database/seeds/initial.js
new file mode 100644
index 0000000..d4a343c
--- /dev/null
+++ b/src/api/database/seeds/initial.js
@@ -0,0 +1,32 @@
+const bcrypt = require('bcrypt');
+const moment = require('moment');
+const randomstring = require('randomstring');
+
+exports.seed = async db => {
+ const now = moment.utc().toDate();
+ const user = await db.table('users').where({ username: 'root' }).first();
+ if (user) return;
+ try {
+ const hash = await bcrypt.hash(process.env.ADMIN_PASSWORD, 10);
+ await db.table('users').insert({
+ username: process.env.ADMIN_ACCOUNT,
+ password: hash,
+ apiKey: randomstring.generate(64),
+ passwordEditedAt: now,
+ apiKeyEditedAt: now,
+ createdAt: now,
+ editedAt: now,
+ isAdmin: true
+ });
+ console.log();
+ console.log('====================================================');
+ console.log('== Successfully created the admin account. ==');
+ console.log('====================================================');
+ console.log('== Run `yarn api` and `yarn site` next ==');
+ console.log('== preferably with pm2 or tmux to keep them alive ==');
+ console.log('====================================================');
+ console.log();
+ } catch (error) {
+ console.error(error);
+ }
+}