"add admin user seed script and refactor common seed utilities"

This commit is contained in:
louiscklaw
2025-05-11 10:34:48 +08:00
parent c29ab4b920
commit 60eed00cb2
26 changed files with 1426 additions and 506 deletions

View File

@@ -1,23 +1,25 @@
const config = require("/pb_hooks/seed/config.js");
const utils = require("/pb_hooks/seed/utils.js");
module.exports = ($app) => {
const ASSETS_DIR = "/pb_hooks/assets";
const getAsset = (name) => $filesystem.fileFromPath(ASSETS_DIR + "/" + name);
const id_v = "1".padStart(15, 0); //id_vocabulary
const id_c = "2".padStart(15, 0); //id_connectives
const { CR_cat_id_news, CR_cat_id_technology } = config;
const { getId, getAsset } = utils;
let row_array = [
["1".padStart(15, 0), "user1@123.com", "user1@123.com", "user1@123.com", true, true, "test_user_1"],
["2".padStart(15, 0), "user2@123.com", "user2@123.com", "user2@123.com", true, true, "test_user_2"],
["3".padStart(15, 0), "user3@123.com", "user3@123.com", "user3@123.com", true, true, "test_user_3"],
["4".padStart(15, 0), "user4@123.com", "user4@123.com", "user4@123.com", true, true, "test_user_4"],
["5".padStart(15, 0), "user5@123.com", "user5@123.com", "user5@123.com", true, true, "test_user_5"],
[getId("1"), "user1@123.com", "user1@123.com", "user1@123.com", true, true, "test_student_1"],
[getId("2"), "user2@123.com", "user2@123.com", "user2@123.com", true, true, "test_student_2"],
[getId("3"), "user3@123.com", "user3@123.com", "user3@123.com", true, true, "test_student_3"],
[getId("4"), "user4@123.com", "user4@123.com", "user4@123.com", true, true, "test_student_4"],
[getId("5"), "user5@123.com", "user5@123.com", "user5@123.com", true, true, "test_student_5"],
];
// um = user_meta
let um_row_array = [
["1".padStart(15, 0), "user1@123.com", "active", "1".padStart(15, 0), JSON.stringify({}), getAsset("people1.png"), "student"],
["2".padStart(15, 0), "user2@123.com", "active", "2".padStart(15, 0), JSON.stringify({}), getAsset("people2.png"), "student"],
["3".padStart(15, 0), "user3@123.com", "active", "3".padStart(15, 0), JSON.stringify({}), getAsset("people3.png"), "student"],
["4".padStart(15, 0), "user4@123.com", "active", "4".padStart(15, 0), JSON.stringify({}), getAsset("people4.png"), "student"],
["5".padStart(15, 0), "user5@123.com", "active", "5".padStart(15, 0), JSON.stringify({}), getAsset("people5.png"), "student"],
[getId("1"), "user1@123.com", "active", getId("1"), JSON.stringify({}), getAsset("people1.png"), "student", "student_1"],
[getId("2"), "user2@123.com", "pending", getId("2"), JSON.stringify({}), getAsset("people2.png"), "student", "student_2"],
[getId("3"), "user3@123.com", "blocked", getId("3"), JSON.stringify({}), getAsset("people3.png"), "student", "student_3"],
[getId("4"), "user4@123.com", "active", getId("4"), JSON.stringify({}), getAsset("people4.png"), "student", "student_4"],
[getId("5"), "user5@123.com", "pending", getId("5"), JSON.stringify({}), getAsset("people5.png"), "student", "student_5"],
];
let users_collection = $app.findCollectionByNameOrId("users");
@@ -40,19 +42,29 @@ module.exports = ($app) => {
let um_record = new Record(user_metas_collection);
um_record.set("id", um[0]);
um_record.set("helloworld", um[1]);
um_record.set("state", um[2]);
um_record.set("status", um[2]);
um_record.set("user_id", um[3]);
um_record.set("meta", um[4]);
// NOTE: obslete "avatar" and use "avatar_file"
um_record.set("avatar", um[5]);
um_record.set("avatar_file", um[5]);
//
um_record.set("role", um[6]);
um_record.set("name", um[7]);
um_record.set("email", user[3]);
um_record.set("phone", "9123456" + i.toString());
$app.save(um_record);
}
console.log("006 add student user done");
};
const dirtyTruncateTable = (COLLECTION_NAME) => {
console.log(`perform dirty method to truncate table "${COLLECTION_NAME}"`);
const cmd_to_exec = $os.cmd("sqlite3", "/pb_data/data.db", `DELETE from ${COLLECTION_NAME};`);
cmd_to_exec.output();
};
// TODO: delete this ?
// const dirtyTruncateTable = (COLLECTION_NAME) => {
// console.log(`perform dirty method to truncate table "${COLLECTION_NAME}"`);
// const cmd_to_exec = $os.cmd("sqlite3", "/pb_data/data.db", `DELETE from ${COLLECTION_NAME};`);
// cmd_to_exec.output();
// };