datasquirel/engine/dsql.js

154 lines
4.4 KiB
JavaScript
Raw Normal View History

2023-09-21 14:00:04 +00:00
#! /usr/bin/env node
// @ts-check
const fs = require("fs");
const path = require("path");
const { execSync } = require("child_process");
require("dotenv").config({
path: path.resolve(process.cwd(), ".env"),
});
const datasquirel = require("../index");
const colors = require("../console-colors");
2024-12-06 10:31:24 +00:00
const createDbFromSchema = require("../package-shared/shell/createDbFromSchema");
2023-09-21 14:00:04 +00:00
if (!fs.existsSync(path.resolve(process.cwd(), ".env"))) {
console.log(".env file not found");
process.exit();
}
2024-10-14 06:49:01 +00:00
const {
DSQL_HOST,
DSQL_USER,
DSQL_PASS,
DSQL_DB_NAME,
DSQL_KEY,
DSQL_REF_DB_NAME,
DSQL_FULL_SYNC,
} = process.env;
2023-09-21 14:00:04 +00:00
if (!DSQL_HOST?.match(/./)) {
console.log("DSQL_HOST is required in your `.env` file");
process.exit();
}
if (!DSQL_USER?.match(/./)) {
console.log("DSQL_USER is required in your `.env` file");
process.exit();
}
if (!DSQL_PASS?.match(/./)) {
console.log("DSQL_PASS is required in your `.env` file");
process.exit();
}
const dbSchemaLocalFilePath = path.resolve(process.cwd(), "dsql.schema.json");
async function run() {
2024-10-14 06:49:01 +00:00
/** @type {any} */
2023-09-21 14:00:04 +00:00
let schemaData;
if (DSQL_KEY && DSQL_REF_DB_NAME?.match(/./)) {
const dbSchemaDataResponse = await datasquirel.getSchema({
key: DSQL_KEY,
database: DSQL_REF_DB_NAME || undefined,
});
2024-10-14 06:49:01 +00:00
if (
!dbSchemaDataResponse.payload ||
Array.isArray(dbSchemaDataResponse.payload)
) {
console.log(
"DSQL_KEY+DSQL_REF_DB_NAME => Error in fetching DB schema"
);
2023-09-21 14:00:04 +00:00
console.log(dbSchemaDataResponse);
process.exit();
}
2024-10-23 05:55:53 +00:00
/** @type {import("../package-shared/types").DSQL_DatabaseSchemaType} */ // @ts-ignore
2023-09-21 14:00:04 +00:00
let fetchedDbSchemaObject = dbSchemaDataResponse.payload;
if (DSQL_DB_NAME) fetchedDbSchemaObject.dbFullName = DSQL_DB_NAME;
schemaData = [fetchedDbSchemaObject];
} else if (DSQL_KEY) {
const dbSchemaDataResponse = await datasquirel.getSchema({
key: DSQL_KEY,
database: DSQL_REF_DB_NAME || undefined,
});
2024-10-14 06:49:01 +00:00
if (
!dbSchemaDataResponse.payload ||
!Array.isArray(dbSchemaDataResponse.payload)
) {
2023-09-21 14:00:04 +00:00
console.log("DSQL_KEY => Error in fetching DB schema");
console.log(dbSchemaDataResponse);
process.exit();
}
let fetchedDbSchemaObject = dbSchemaDataResponse.payload;
// fetchedDbSchemaObject.forEach((db, index) => {
// db.dbFullName = db.dbFullName?.replace(/^datasquirel_user_\d+_/, "");
// });
schemaData = fetchedDbSchemaObject;
} else if (fs.existsSync(dbSchemaLocalFilePath)) {
2024-10-14 06:49:01 +00:00
schemaData = [
JSON.parse(fs.readFileSync(dbSchemaLocalFilePath, "utf8")),
];
2023-09-21 14:00:04 +00:00
} else {
2024-10-14 06:49:01 +00:00
console.log(
"No source for DB Schema. Please provide a local `dsql.schema.json` file, or provide `DSQL_KEY` and `DSQL_REF_DB_NAME` environment variables."
);
2023-09-21 14:00:04 +00:00
process.exit();
}
if (!schemaData) {
console.log("No schema found");
process.exit();
}
if (DSQL_FULL_SYNC?.match(/true/i)) {
2024-10-14 06:49:01 +00:00
fs.writeFileSync(
dbSchemaLocalFilePath,
JSON.stringify(schemaData[0], null, 4),
"utf8"
);
2023-09-21 14:00:04 +00:00
}
2024-10-14 06:49:01 +00:00
console.log(
` - ${colors.FgBlue}Info:${colors.Reset} Now generating and mapping databases ...`
);
2023-09-21 14:00:04 +00:00
// deepcode ignore reDOS: <please specify a reason of ignoring this>
2024-12-06 10:31:24 +00:00
await createDbFromSchema({
dbSchemaData: schemaData,
});
2024-10-14 06:49:01 +00:00
console.log(
` - ${colors.FgGreen}Success:${colors.Reset} Databases created Successfully!`
);
2023-09-21 14:00:04 +00:00
}
// let timeout;
let interval;
if (fs.existsSync(dbSchemaLocalFilePath) && !DSQL_KEY?.match(/....../)) {
fs.watchFile(dbSchemaLocalFilePath, { interval: 1000 }, (curr, prev) => {
2024-10-14 06:49:01 +00:00
console.log(
` - ${colors.FgBlue}Info:${colors.Reset} Syncing Databases Locally ...`
);
2023-09-21 14:00:04 +00:00
run();
});
} else if (DSQL_KEY?.match(/....../)) {
interval = setInterval(() => {
2024-10-14 06:49:01 +00:00
console.log(
` - ${colors.FgMagenta}Info:${colors.Reset} Syncing Databases from the cloud ...`
);
2023-09-21 14:00:04 +00:00
run();
}, 20000);
}
run();