From 1b0c551602cb1841489e354a1ca0bf211e3476d6 Mon Sep 17 00:00:00 2001 From: Adria Navarro Date: Tue, 19 Dec 2023 16:59:23 +0100 Subject: [PATCH] Create create-many script --- .../scripts/load/create-many-relationships.js | 47 +++++++++++++++++++ packages/server/scripts/load/utils.js | 43 ++++++++++------- 2 files changed, 73 insertions(+), 17 deletions(-) create mode 100755 packages/server/scripts/load/create-many-relationships.js diff --git a/packages/server/scripts/load/create-many-relationships.js b/packages/server/scripts/load/create-many-relationships.js new file mode 100755 index 0000000000..78353c029c --- /dev/null +++ b/packages/server/scripts/load/create-many-relationships.js @@ -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) + }) diff --git a/packages/server/scripts/load/utils.js b/packages/server/scripts/load/utils.js index 97ff8f1e13..7af867ab1d 100644 --- a/packages/server/scripts/load/utils.js +++ b/packages/server/scripts/load/utils.js @@ -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,29 +39,37 @@ 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 = {} - 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 +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 + ? schema.constraints.inclusion[0] + : "a" + break + case "number": + fake = 1 + break + } + body[key] = fake } - 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 +}