From 3b43943f1f148599abaf24fa3a12936cfa260a42 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 3 Feb 2023 19:29:28 +0000 Subject: [PATCH] Some quick scripts for loading a lot of rows and apps. --- .../server/scripts/load/create-many-apps.js | 24 +++++++ .../server/scripts/load/create-many-rows.js | 30 +++++++++ packages/server/scripts/load/utils.js | 66 +++++++++++++++++++ .../src/api/routes/public/applications.ts | 1 - 4 files changed, 120 insertions(+), 1 deletion(-) create mode 100755 packages/server/scripts/load/create-many-apps.js create mode 100755 packages/server/scripts/load/create-many-rows.js create mode 100644 packages/server/scripts/load/utils.js diff --git a/packages/server/scripts/load/create-many-apps.js b/packages/server/scripts/load/create-many-apps.js new file mode 100755 index 0000000000..9532313880 --- /dev/null +++ b/packages/server/scripts/load/create-many-apps.js @@ -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) + }) diff --git a/packages/server/scripts/load/create-many-rows.js b/packages/server/scripts/load/create-many-rows.js new file mode 100755 index 0000000000..85c0c292a0 --- /dev/null +++ b/packages/server/scripts/load/create-many-rows.js @@ -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) + }) diff --git a/packages/server/scripts/load/utils.js b/packages/server/scripts/load/utils.js new file mode 100644 index 0000000000..97ff8f1e13 --- /dev/null +++ b/packages/server/scripts/load/utils.js @@ -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 +} diff --git a/packages/server/src/api/routes/public/applications.ts b/packages/server/src/api/routes/public/applications.ts index 935321cb7b..9d6c380e60 100644 --- a/packages/server/src/api/routes/public/applications.ts +++ b/packages/server/src/api/routes/public/applications.ts @@ -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 = []