Allow reusing app

This commit is contained in:
Adria Navarro 2023-12-22 11:46:49 +01:00
parent 3e991cc2f1
commit c5a50a911f
2 changed files with 47 additions and 20 deletions

View File

@ -1,5 +1,11 @@
#!/bin/node #!/bin/node
const yargs = require("yargs") const {
createApp,
getTable,
createRow,
createTable,
getApp,
} = require("./utils")
const Chance = require("chance") const Chance = require("chance")
@ -8,12 +14,12 @@ const generator = new Chance()
const STUDENT_COUNT = 500 const STUDENT_COUNT = 500
const SUBJECT_COUNT = 10 const SUBJECT_COUNT = 10
const { apiKey, appId } = require("yargs") let { apiKey, appId } = require("yargs")
.demandOption(["apiKey"]) .demandOption(["apiKey"])
.option("appId").argv .option("appId").argv
const start = Date.now() const start = Date.now()
async function batchCreate(apiKey, appId, table, items, batchSize = 1000) { async function batchCreate(apiKey, appId, table, items, batchSize = 100) {
let i = 0 let i = 0
let errors = 0 let errors = 0
@ -62,20 +68,34 @@ async function batchCreate(apiKey, appId, table, items, batchSize = 1000) {
return rows return rows
} }
async function run() { const useExistingApp = !!appId
const app = await createApp(apiKey)
console.log(`App created: http://localhost:10000/builder/app/${app._id}`)
const studentsTable = await getTable(apiKey, app._id) async function upsertTable(appId, tableName, tableData) {
if (studentsTable.name !== "Students") { if (useExistingApp) {
throw 'Fetched table should be "Students"' return await getTable(apiKey, appId, tableName)
} }
console.log(`Table found: ${studentsTable.name}`)
const table = await createTable(apiKey, appId, {
...tableData,
name: tableName,
})
return table
}
async function run() {
if (!appId) {
const app = appId ? await getApp(apiKey, appId) : await createApp(apiKey)
appId = app._id
}
console.log(`App created: http://localhost:10000/builder/app/${appId}`)
const studentsTable = await getTable(apiKey, appId, "Students")
let studentNumber = studentsTable.schema["Auto ID"].lastID let studentNumber = studentsTable.schema["Auto ID"].lastID
const students = await batchCreate( const students = await batchCreate(
apiKey, apiKey,
app._id, appId,
studentsTable, studentsTable,
Array.from({ length: STUDENT_COUNT }).map(() => ({ Array.from({ length: STUDENT_COUNT }).map(() => ({
"Student Number": (++studentNumber).toString(), "Student Number": (++studentNumber).toString(),
@ -89,27 +109,26 @@ async function run() {
})) }))
) )
const subjectTable = await createTable(apiKey, app._id, { const subjectTable = await upsertTable(appId, "Subjects", {
schema: { schema: {
Name: { Name: {
name: "Name", name: "Name",
type: "string", type: "string",
}, },
}, },
name: "Subjects",
primaryDisplay: "Name", primaryDisplay: "Name",
}) })
const subjects = await batchCreate( const subjects = await batchCreate(
apiKey, apiKey,
app._id, appId,
subjectTable, subjectTable,
Array.from({ length: SUBJECT_COUNT }).map(() => ({ Array.from({ length: SUBJECT_COUNT }).map(() => ({
Name: generator.profession(), Name: generator.profession(),
})) }))
) )
const gradesTable = await createTable(apiKey, app._id, { const gradesTable = await upsertTable(appId, "Grades", {
schema: { schema: {
Score: { Score: {
name: "Score", name: "Score",
@ -138,12 +157,11 @@ async function run() {
type: "link", type: "link",
}, },
}, },
name: "Grades",
}) })
await batchCreate( await batchCreate(
apiKey, apiKey,
app._id, appId,
gradesTable, gradesTable,
students.flatMap(student => students.flatMap(student =>
subjects.map(subject => ({ subjects.map(subject => ({
@ -155,7 +173,7 @@ async function run() {
) )
console.log( console.log(
`Access the app here: http://localhost:10000/builder/app/${app._id}` `Access the app here: http://localhost:10000/builder/app/${appId}`
) )
} }

View File

@ -38,6 +38,11 @@ exports.createApp = async apiKey => {
return json.data return json.data
} }
exports.getApp = async (apiKey, appId) => {
const res = await request(apiKey, `${URL_APP}/${appId}`, "GET")
const json = await res.json()
return json.data
}
exports.searchApps = async apiKey => { exports.searchApps = async apiKey => {
const res = await request(apiKey, `${URL_APP}/search`, "POST", {}) const res = await request(apiKey, `${URL_APP}/search`, "POST", {})
const json = await res.json() const json = await res.json()
@ -49,10 +54,14 @@ exports.deleteApp = async (apiKey, appId) => {
return res return res
} }
exports.getTable = async (apiKey, appId) => { exports.getTable = async (apiKey, appId, tableName) => {
const res = await request(apiKey, URL_SEARCH_TABLE, "POST", {}, appId) const res = await request(apiKey, URL_SEARCH_TABLE, "POST", {}, appId)
const json = await res.json() const json = await res.json()
return json.data[0] const table = json.data.find(t => t.name === tableName)
if (!table) {
throw `Table '${tableName} not found`
}
return table
} }
exports.createRow = async (apiKey, appId, table, body) => { exports.createRow = async (apiKey, appId, table, body) => {