Skip to content

Commit 09d6135

Browse files
committed
feat: add default data initialization
1 parent 6c7f81c commit 09d6135

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ const rateLimiter = require("express-rate-limit");
1414
// Require Internal Dependencies
1515
const server = require("./src/httpServer");
1616
const models = require("./src/models");
17+
const initDefaultData = require("./src/initDefaultData");
18+
19+
// ARGV
20+
const [initData = null] = process.argv.slice(2);
1721

1822
// CONSTANTS
1923
const PORT = process.env.PORT || 1338;
@@ -45,6 +49,9 @@ async function main() {
4549
let isClosed = false;
4650

4751
await sequelize.sync();
52+
if (initData !== null) {
53+
await initDefaultData(tables);
54+
}
4855

4956
// Cleanup Interval
5057
setInterval(async() => {

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
"description": "SlimIO Addons registry",
55
"main": "index.js",
66
"scripts": {
7-
"start": "node index.js",
7+
"start": "node index.js init",
88
"prepublishOnly": "pkg-ok",
99
"hydrate": "node scripts/hydrate.js",
1010
"test": "cross-env psp && node test/test.js",

src/initDefaultData.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"use strict";
2+
3+
// Require Third-party Dependencies
4+
const argon2 = require("argon2");
5+
6+
/**
7+
* @function initDefaultData
8+
* @param {*} tables Sequelize tables
9+
* @returns {void}
10+
*/
11+
async function initDefaultData(tables) {
12+
console.log("INIT DEFAULT DATA!");
13+
const [user] = await tables.Users.findOrCreate({
14+
where: {
15+
username: "fraxken"
16+
},
17+
defaults: {
18+
username: "fraxken",
19+
active: true,
20+
21+
password: await argon2.hash(process.env.ADMIN_PASSWORD || "admin")
22+
}
23+
});
24+
25+
const [org] = await tables.Organisation.findOrCreate({
26+
where: {
27+
name: "SlimIO"
28+
},
29+
defaults: {
30+
name: "SlimIO",
31+
description: "SlimIO Official Organisation",
32+
ownerId: user.id
33+
}
34+
});
35+
36+
await tables.Addons.findOrCreate({
37+
where: {
38+
name: "cpu"
39+
},
40+
defaults: {
41+
name: "cpu",
42+
description: "CPU Addon",
43+
latest: "1.0.0",
44+
version: {
45+
version: "1.0.0",
46+
git: "https://github.com/SlimIO/cpu-addon"
47+
},
48+
authorId: user.id,
49+
organisationId: org.id
50+
}
51+
});
52+
}
53+
54+
module.exports = initDefaultData;

0 commit comments

Comments
 (0)