1
0
Fork 0
mirror of synced 2024-09-17 09:49:11 +12:00
budibase/packages/server/scripts/load/create-many-relationships.js

140 lines
3.3 KiB
JavaScript
Raw Normal View History

2023-12-20 04:59:23 +13:00
#!/bin/node
const { createApp, getTable, createRow, createTable } = require("./utils")
2023-12-20 23:51:35 +13:00
const Chance = require("chance")
const generator = new Chance()
2023-12-21 01:04:16 +13:00
const STUDENT_COUNT = 500
2023-12-21 00:18:29 +13:00
const SUBJECT_COUNT = 10
2023-12-20 04:59:23 +13:00
if (!process.argv[2]) {
console.error("Please specify an API key as script argument.")
process.exit(-1)
}
2023-12-21 01:04:16 +13:00
const start = Date.now()
2023-12-22 05:48:39 +13:00
async function batchCreate(apiKey, appId, table, items, batchSize = 100) {
2023-12-22 05:39:14 +13:00
let i = 0
2023-12-22 05:48:39 +13:00
async function createSingleRow(item) {
const row = await createRow(apiKey, appId, table, item)
2023-12-22 05:39:14 +13:00
console.log(
2023-12-22 05:48:39 +13:00
`${table.name} - ${++i} of ${items.length} created (${
2023-12-22 05:39:14 +13:00
(Date.now() - start) / 1000
}s)`
)
return row
}
const rows = []
2023-12-22 05:48:39 +13:00
for (let j = 0; j < items.length; j += batchSize) {
const batchPromises = items.slice(j, j + batchSize).map(createSingleRow)
2023-12-22 05:39:14 +13:00
const batchRows = await Promise.all(batchPromises)
rows.push(...batchRows)
}
return rows
}
2023-12-20 04:59:23 +13:00
async function run() {
const apiKey = process.argv[2]
const app = await createApp(apiKey)
2023-12-21 00:30:35 +13:00
console.log(`App created: http://localhost:10000/builder/app/${app._id}`)
2023-12-21 00:18:29 +13:00
const studentsTable = await getTable(apiKey, app._id)
if (studentsTable.name !== "Students") {
throw 'Fetched table should be "Students"'
}
console.log(`Table found: ${studentsTable.name}`)
2023-12-20 04:59:23 +13:00
2023-12-21 00:52:25 +13:00
let studentNumber = studentsTable.schema["Auto ID"].lastID
2023-12-22 05:39:14 +13:00
const students = await batchCreate(
apiKey,
app._id,
studentsTable,
2023-12-22 05:48:39 +13:00
Array.from({ length: SUBJECT_COUNT }).map(() => ({
2023-12-22 05:39:14 +13:00
"Student Number": (++studentNumber).toString(),
"First Name": generator.first(),
"Last Name": generator.last(),
Gender: generator.pickone(["M", "F"]),
Grade: generator.pickone(["8", "9", "10", "11"]),
"Tardiness (Days)": generator.integer({ min: 1, max: 100 }),
"Home Number": generator.phone(),
"Attendance_(%)": generator.integer({ min: 0, max: 100 }),
2023-12-22 05:48:39 +13:00
}))
2023-12-22 05:24:48 +13:00
)
2023-12-21 03:02:21 +13:00
2023-12-20 04:59:23 +13:00
const subjectTable = await createTable(apiKey, app._id, {
schema: {
2023-12-20 23:51:35 +13:00
Name: {
2023-12-20 04:59:23 +13:00
name: "Name",
type: "string",
},
},
name: "Subjects",
2023-12-21 00:52:25 +13:00
primaryDisplay: "Name",
2023-12-20 04:59:23 +13:00
})
2023-12-20 23:51:35 +13:00
2023-12-22 05:39:14 +13:00
const subjects = await batchCreate(
apiKey,
app._id,
subjectTable,
2023-12-22 05:48:39 +13:00
Array.from({ length: SUBJECT_COUNT }).map(() => ({
2023-12-22 05:39:14 +13:00
Name: generator.profession(),
2023-12-22 05:48:39 +13:00
}))
2023-12-22 05:24:48 +13:00
)
2023-12-21 03:02:21 +13:00
2023-12-21 00:52:25 +13:00
const gradesTable = await createTable(apiKey, app._id, {
schema: {
Score: {
name: "Score",
type: "number",
},
Student: {
name: "Student",
tableId: studentsTable._id,
constraints: {
presence: true,
type: "array",
},
fieldName: "Grades",
relationshipType: "one-to-many",
type: "link",
},
Subject: {
name: "Subject",
tableId: subjectTable._id,
constraints: {
presence: true,
type: "array",
},
fieldName: "Grades",
relationshipType: "one-to-many",
type: "link",
},
},
name: "Grades",
})
2023-12-22 05:48:39 +13:00
await batchCreate(
apiKey,
app._id,
gradesTable,
students.flatMap(student =>
subjects.map(subject => ({
2023-12-21 00:52:25 +13:00
Score: generator.integer({ min: 0, max: 100 }),
Student: [student],
Subject: [subject],
2023-12-22 05:48:39 +13:00
}))
)
)
2023-12-20 04:59:23 +13:00
}
2023-12-21 01:04:16 +13:00
run()
.then(() => {
2023-12-21 03:02:21 +13:00
console.log(`Done in ${(Date.now() - start) / 1000} seconds`)
2023-12-21 01:04:16 +13:00
})
.catch(err => {
console.error(err)
})