Create create-many script

This commit is contained in:
Adria Navarro 2023-12-19 16:59:23 +01:00
parent 6909cbed29
commit 1b0c551602
2 changed files with 73 additions and 17 deletions

View File

@ -0,0 +1,47 @@
#!/bin/node
const { createApp, getTable, createRow, createTable } = require("./utils")
const ROW_COUNT = 10
if (!process.argv[2]) {
console.error("Please specify an API key as script argument.")
process.exit(-1)
}
async function run() {
const apiKey = process.argv[2]
const app = await createApp(apiKey)
console.log(`App created: ${app._id}`)
const table = await getTable(apiKey, app._id)
console.log(`Table found: ${table.name}`)
const subjectTable = await createTable(apiKey, app._id, {
schema: {
title: {
name: "Name",
type: "string",
},
},
name: "Subjects",
})
for (let i = 0; i < 10; i++) {
await createRow(apiKey, app._id, subjectTable)
}
const promises = []
for (let i = 0; i < ROW_COUNT; i++) {
promises.push(await createRow(apiKey, app._id, table))
console.log(`Row ${i + 1} of ${ROW_COUNT} created`)
}
await Promise.all(promises)
console.log(`App created: http://localhost:10000/builder/app/${app._id}`)
}
run()
.then(() => {
console.log(`Finished creating ${ROW_COUNT} rows.`)
})
.catch(err => {
console.error(err)
})

View File

@ -2,7 +2,8 @@ const fetch = require("node-fetch")
const uuid = require("uuid/v4")
const URL_APP = "http://localhost:10000/api/public/v1/applications"
const URL_TABLE = "http://localhost:10000/api/public/v1/tables/search"
const URL_TABLE = "http://localhost:10000/api/public/v1/tables"
const URL_SEARCH_TABLE = "http://localhost:10000/api/public/v1/tables/search"
async function request(apiKey, url, method, body, appId = undefined) {
const headers = {
@ -38,19 +39,20 @@ exports.createApp = async apiKey => {
}
exports.getTable = async (apiKey, appId) => {
const res = await request(apiKey, URL_TABLE, "POST", {}, appId)
const res = await request(apiKey, URL_SEARCH_TABLE, "POST", {}, appId)
const json = await res.json()
return json.data[0]
}
exports.createRow = async (apiKey, appId, table) => {
const body = {}
exports.createRow = async (apiKey, appId, table, body) => {
if (!body) {
body = {}
for (let [key, schema] of Object.entries(table.schema)) {
let fake
switch (schema.type) {
default:
case "string":
fake = schema.constraints.inclusion
fake = schema.constraints?.inclusion
? schema.constraints.inclusion[0]
: "a"
break
@ -60,7 +62,14 @@ exports.createRow = async (apiKey, appId, table) => {
}
body[key] = fake
}
}
const url = `http://localhost:10000/api/public/v1/tables/${table._id}/rows`
const res = await request(apiKey, url, "POST", body, appId)
return (await res.json()).data
}
exports.createTable = async (apiKey, appId, config) => {
const res = await request(apiKey, URL_TABLE, "POST", config, appId)
const json = await res.json()
return json.data
}