Files
lettersoup-online/002_source/pocketbase/pb_hooks/seed/050_Customers.js
2025-04-24 20:03:26 +08:00

153 lines
3.8 KiB
JavaScript

//
// RULES: this is not a normal nodejs engine, it is a nodejs provided by golang, so fakerjs cannot be used here
//
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 getId = (id) => id.padStart(15, 0);
// generate from `./project/001_documentation/Requirements/REQ0006/gen_customer/gen_customer.mjs`
const SAMPLE_CUSTOMER_ARRAY = [
[
"000000000000001",
"May",
getAsset("customer1.png"),
"[email protected]",
"281-378-5900 x822",
"Green, Rempel and Hoeger",
{
country: "UK",
state: "Arizona",
city: "Winfieldburgh",
zipCode: "92017-8004",
line1: "1838 Willa Freeway",
line2: "Suite 307",
},
98,
"Asia/Urumqi",
"fr",
"USD",
"active",
],
[
"000000000000002",
"Marilyne",
getAsset("customer2.png"),
"[email protected]",
"(893) 919-2445 x193",
"White - Hessel",
{
country: "US",
state: "Nevada",
city: "Casa Grande",
zipCode: "83831-3843",
line1: "6984 Alberto Radial",
line2: "Suite 154",
},
49,
"Africa/Tunis",
"es",
"EUR",
"pending",
],
[
"000000000000003",
"Jacklyn",
getAsset("customer3.png"),
"[email protected]",
"597-593-0144 x168",
"Rolfson LLC",
{
country: "CA",
state: "Georgia",
city: "New Brodyfort",
zipCode: "18887-7075",
line1: "493 Pfannerstill Meadow",
line2: "Apt. 358",
},
44,
"Asia/Manila",
"en",
"GBP",
"blocked",
],
[
"000000000000004",
"Alana",
getAsset("customer4.png"),
"[email protected]",
"401.212.0386 x31125",
"Friesen, Langworth and Thompson",
{
country: "UK",
state: "North Carolina",
city: "Fort Jerrell",
zipCode: "14211",
line1: "1763 West Street",
line2: "Suite 699",
},
34,
"America/Boa_Vista",
"es",
"USD",
"active",
],
[
"000000000000005",
"Rocky",
getAsset("customer5.png"),
"[email protected]",
"653.964.0412",
"Hayes - Morar",
{
country: "US",
state: "New York",
city: "Kayton",
zipCode: "82048-0645",
line1: "636 Angel Junction",
line2: "Apt. 361",
},
70,
"America/Grand_Turk",
"fr",
"EUR",
"pending",
],
];
let row_array = SAMPLE_CUSTOMER_ARRAY;
dirtyTruncateTable("Customers");
let lt_collection = $app.findCollectionByNameOrId("Customers");
for (let i = 0; i < row_array.length; i++) {
let customer = row_array[i];
let record = new Record(lt_collection);
record.set("id", customer[0]);
record.set("name", customer[1]);
record.set("avatar_file", customer[2]);
record.set("email", customer[3]);
record.set("phone", customer[4]);
record.set("company", customer[5]);
record.set("billingAddress", customer[6]);
record.set("taxId", customer[7]);
record.set("timezone", customer[8]);
record.set("language", customer[9]);
record.set("currency", customer[10]);
record.set("status", customer[11]);
$app.save(record);
}
console.log(`050_Customers 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();
};