Some quick scripts for loading a lot of rows and apps.
This commit is contained in:
parent
13bc8c434a
commit
3b43943f1f
|
@ -0,0 +1,24 @@
|
|||
#!/bin/node
|
||||
const { createApp } = require("./utils")
|
||||
|
||||
const APP_COUNT = 100
|
||||
|
||||
if (!process.argv[2]) {
|
||||
console.error("Please specify an API key as script argument.")
|
||||
process.exit(-1)
|
||||
}
|
||||
|
||||
async function run() {
|
||||
for (let i = 0; i < APP_COUNT; i++) {
|
||||
const app = await createApp(process.argv[2])
|
||||
console.log(`App created: ${app._id}`)
|
||||
}
|
||||
}
|
||||
|
||||
run()
|
||||
.then(() => {
|
||||
console.log(`Finished creating ${APP_COUNT} apps.`)
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(err)
|
||||
})
|
|
@ -0,0 +1,30 @@
|
|||
#!/bin/node
|
||||
const { createApp, getTable, createRow } = require("./utils")
|
||||
|
||||
const ROW_COUNT = 1000
|
||||
|
||||
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 promises = []
|
||||
for (let i = 0; i < ROW_COUNT; i++) {
|
||||
promises.push(await createRow(apiKey, app._id, table))
|
||||
}
|
||||
await Promise.all(promises)
|
||||
}
|
||||
|
||||
run()
|
||||
.then(() => {
|
||||
console.log(`Finished creating ${ROW_COUNT} rows.`)
|
||||
})
|
||||
.catch(err => {
|
||||
console.error(err)
|
||||
})
|
|
@ -0,0 +1,66 @@
|
|||
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"
|
||||
|
||||
async function request(apiKey, url, method, body, appId = undefined) {
|
||||
const headers = {
|
||||
"x-budibase-api-key": apiKey,
|
||||
"Content-Type": "application/json",
|
||||
}
|
||||
if (appId) {
|
||||
headers["x-budibase-app-id"] = appId
|
||||
}
|
||||
const res = await fetch(url, {
|
||||
method,
|
||||
headers,
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
if (res.status !== 200) {
|
||||
throw new Error(await res.text())
|
||||
}
|
||||
return res
|
||||
}
|
||||
|
||||
exports.createApp = async apiKey => {
|
||||
const name = uuid().replace(/-/g, "")
|
||||
const body = {
|
||||
name,
|
||||
url: `/${name}`,
|
||||
useTemplate: "true",
|
||||
templateKey: "app/school-admin-panel",
|
||||
templateName: "School Admin Panel",
|
||||
}
|
||||
const res = await request(apiKey, URL_APP, "POST", body)
|
||||
const json = await res.json()
|
||||
return json.data
|
||||
}
|
||||
|
||||
exports.getTable = async (apiKey, appId) => {
|
||||
const res = await request(apiKey, URL_TABLE, "POST", {}, appId)
|
||||
const json = await res.json()
|
||||
return json.data[0]
|
||||
}
|
||||
|
||||
exports.createRow = async (apiKey, appId, table) => {
|
||||
const body = {}
|
||||
for (let [key, schema] of Object.entries(table.schema)) {
|
||||
let fake
|
||||
switch (schema.type) {
|
||||
default:
|
||||
case "string":
|
||||
fake = schema.constraints.inclusion
|
||||
? schema.constraints.inclusion[0]
|
||||
: "a"
|
||||
break
|
||||
case "number":
|
||||
fake = 1
|
||||
break
|
||||
}
|
||||
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
|
||||
}
|
|
@ -1,7 +1,6 @@
|
|||
import controller from "../../controllers/public/applications"
|
||||
import Endpoint from "./utils/Endpoint"
|
||||
const { nameValidator, applicationValidator } = require("../utils/validators")
|
||||
import { db } from "@budibase/backend-core"
|
||||
|
||||
const read = [],
|
||||
write = []
|
||||
|
|
Loading…
Reference in New Issue