diff --git a/dist/engine/schema-to-typedef.js b/dist/engine/schema-to-typedef.js index 8d36683..1a3e3e9 100644 --- a/dist/engine/schema-to-typedef.js +++ b/dist/engine/schema-to-typedef.js @@ -1,5 +1,38 @@ #! /usr/bin/env node "use strict"; +var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + var desc = Object.getOwnPropertyDescriptor(m, k); + if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { + desc = { enumerable: true, get: function() { return m[k]; } }; + } + Object.defineProperty(o, k2, desc); +}) : (function(o, m, k, k2) { + if (k2 === undefined) k2 = k; + o[k2] = m[k]; +})); +var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { + Object.defineProperty(o, "default", { enumerable: true, value: v }); +}) : function(o, v) { + o["default"] = v; +}); +var __importStar = (this && this.__importStar) || (function () { + var ownKeys = function(o) { + ownKeys = Object.getOwnPropertyNames || function (o) { + var ar = []; + for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; + return ar; + }; + return ownKeys(o); + }; + return function (mod) { + if (mod && mod.__esModule) return mod; + var result = {}; + if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); + __setModuleDefault(result, mod); + return result; + }; +})(); var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { @@ -13,116 +46,80 @@ var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); -const fs_1 = __importDefault(require("fs")); +const fs_1 = __importStar(require("fs")); const __1 = __importDefault(require("..")); -const util_1 = require("util"); const db_schema_to_type_1 = __importDefault(require("../package-shared/functions/dsql/db-schema-to-type")); const path_1 = __importDefault(require("path")); const debug_log_1 = __importDefault(require("../package-shared/utils/logging/debug-log")); -const parse_env_1 = __importDefault(require("../package-shared/utils/parse-env")); -const args = (0, util_1.parseArgs)({ - args: process.argv, - options: { - apiKey: { - type: "string", - default: process.env.DSQL_FULL_ACCESS_API_KEY, - short: "k", - }, - database: { - type: "string", - default: process.env.DSQL_DB_NAME, - short: "d", - }, - userid: { - type: "string", - default: process.env.DSQL_API_USER_ID, - short: "i", - }, - outfile: { - type: "string", - short: "o", - }, - envfile: { - type: "string", - short: "e", - }, - debug: { - type: "boolean", - short: "u", - }, - }, - strict: false, -}); -let appendedEnv = {}; -if (args.values.envfile && typeof args.values.envfile == "string") { - const finalEnvPath = path_1.default.resolve(process.cwd(), args.values.envfile); - if (fs_1.default.existsSync(finalEnvPath)) { - const parsedEnv = (0, parse_env_1.default)(finalEnvPath); - appendedEnv = (parsedEnv || {}); - if (args.values.debug) { - (0, debug_log_1.default)({ - log: appendedEnv, - label: "Appended env", - title: "Schema to Typedef", - addTime: true, - }); - } - } +const app_names_1 = require("../package-shared/dict/app-names"); +const configFileName = app_names_1.AppNames["SchemaToTypeDefConfigFileName"]; +const configFilePath = path_1.default.join(process.cwd(), configFileName); +if (!fs_1.default.existsSync(configFilePath)) { + (0, debug_log_1.default)({ + log: `Config File doesn't exist. Please add a '${app_names_1.AppNames["SchemaToTypeDefConfigFileName"]}' in the working directory with relevant configurations.`, + addTime: true, + type: "error", + }); + process.exit(1); +} +const config = JSON.parse((0, fs_1.readFileSync)(path_1.default.join(process.cwd(), configFileName), "utf-8")); +if (!config.database) { + (0, debug_log_1.default)({ + log: `Config File needs a 'database' value.`, + addTime: true, + type: "error", + }); + process.exit(1); +} +if (!config.outDir && !config.outFile) { + (0, debug_log_1.default)({ + log: `Config File needs a 'outDir' or 'outFile' value.`, + addTime: true, + type: "error", + }); + process.exit(1); +} +if (!config.apiConnectionConfig) { + (0, debug_log_1.default)({ + log: `Config File needs a 'apiConnectionConfig' value. This value needs an 'apiKey' value.`, + addTime: true, + type: "error", + }); + process.exit(1); +} +if (!config.apiConnectionConfig.apiKey) { + (0, debug_log_1.default)({ + log: `Config value 'apiConnectionConfig' needs an 'apiKey' value.`, + addTime: true, + type: "error", + }); + process.exit(1); } -const finalEnv = Object.assign(Object.assign({}, process.env), appendedEnv); -process.env = Object.assign(Object.assign({}, process.env), appendedEnv); (() => __awaiter(void 0, void 0, void 0, function* () { try { - const key = args.values.apiKey || finalEnv["DSQL_FULL_ACCESS_API_KEY"]; - const database = args.values.database || finalEnv["DSQL_DB_NAME"]; - const user_id = args.values.userid || finalEnv["DSQL_API_USER_ID"] || "1"; - if (args.values.debug) { + if (config.debug) { (0, debug_log_1.default)({ - log: args.values, - label: "Arguments", - title: "Schema to Typedef", - addTime: true, - }); - (0, debug_log_1.default)({ - log: process.env.DSQL_FULL_ACCESS_API_KEY, - label: "process.env.DSQL_FULL_ACCESS_API_KEY", - title: "Schema to Typedef", - addTime: true, - }); - (0, debug_log_1.default)({ - log: process.env.DSQL_DB_NAME, - label: "process.env.DSQL_DB_NAME", - title: "Schema to Typedef", + log: config, + label: "Config", addTime: true, }); } - if (!database || typeof database !== "string") - throw new Error("Database name is required"); - if (!key || typeof key !== "string") - throw new Error("API key is required"); - if (!args.values.outfile || typeof args.values.outfile !== "string") - throw new Error("Outfile are required"); - if (!user_id || typeof user_id !== "string") - throw new Error("Outfile are required"); - const schema = yield __1.default.getSchema({ - key, - database, - user_id, - env: finalEnv, + const schemaRes = yield __1.default.api.schema.get({ + dbName: config.database || "", + apiConnectionConfig: config.apiConnectionConfig, }); - const dbSchema = schema.payload; - if (args.values.debug) { + const dbSchema = schemaRes.payload; + if (config.debug) { (0, debug_log_1.default)({ - log: schema, + log: schemaRes, label: "schema", - title: "Schema to Typedef", addTime: true, }); } if (!dbSchema) throw new Error("No schema found"); const definitions = (0, db_schema_to_type_1.default)({ dbSchema }); - const finalOutfile = path_1.default.resolve(process.cwd(), args.values.outfile); + const finalOutfile = path_1.default.resolve(process.cwd(), config.outDir ? config.outDir + "/dsql.ts" : config.outFile || ""); const ourfileDir = path_1.default.dirname(finalOutfile); if (!fs_1.default.existsSync(ourfileDir)) { fs_1.default.mkdirSync(ourfileDir, { recursive: true }); diff --git a/dist/index.d.ts b/dist/index.d.ts index caa5154..b17f116 100644 --- a/dist/index.d.ts +++ b/dist/index.d.ts @@ -57,6 +57,9 @@ declare const datasquirel: { validateTempEmailCode: typeof import("./package-shared/actions/users/validate-temp-email-code").default; }; }; + schema: { + get: typeof import("./package-shared/api/schema/get").default; + }; }; /** * Get Action diff --git a/dist/index.js b/dist/index.js index 9df5a58..737c1d8 100644 --- a/dist/index.js +++ b/dist/index.js @@ -29,6 +29,7 @@ const httpsRequest_1 = __importDefault(require("./package-shared/functions/backe const handleNodemailer_1 = __importDefault(require("./package-shared/functions/backend/handleNodemailer")); const grab_dsql_connection_1 = __importDefault(require("./package-shared/utils/grab-dsql-connection")); const grab_dsql_connection_config_1 = __importDefault(require("./package-shared/utils/grab-dsql-connection-config")); +const schema_1 = __importDefault(require("./package-shared/api/schema")); /** * API Functions Object */ @@ -36,6 +37,7 @@ const api = { crud: crud_2.default, media: media_1.default, user: user_1.default, + schema: schema_1.default, }; /** * SQL Utils diff --git a/dist/package-shared/api/schema/get.d.ts b/dist/package-shared/api/schema/get.d.ts new file mode 100644 index 0000000..64e188b --- /dev/null +++ b/dist/package-shared/api/schema/get.d.ts @@ -0,0 +1,13 @@ +import { APIConnectionOptions, APIResponseObject, DSQL_DatabaseSchemaType, DSQL_FieldSchemaType, DSQL_TableSchemaType } from "../../types"; +import { GrabHostNamesReturn } from "../../utils/grab-host-names"; +type Params = { + dbName: string; + tableName?: string; + fieldName?: string; + apiKey?: string; + apiConnectionConfig?: APIConnectionOptions; + grabbedHostnames?: GrabHostNamesReturn; + useDefault?: boolean; +}; +export default function ({ dbName, tableName, apiKey, useDefault, fieldName, apiConnectionConfig, grabbedHostnames, }: Params): Promise>; +export {}; diff --git a/dist/package-shared/api/schema/get.js b/dist/package-shared/api/schema/get.js new file mode 100644 index 0000000..6cea9d1 --- /dev/null +++ b/dist/package-shared/api/schema/get.js @@ -0,0 +1,33 @@ +"use strict"; +var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { + function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } + return new (P || (P = Promise))(function (resolve, reject) { + function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } + function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } + function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } + step((generator = generator.apply(thisArg, _arguments || [])).next()); + }); +}; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.default = default_1; +const path_1 = __importDefault(require("path")); +const query_dsql_api_1 = __importDefault(require("../../functions/api/query-dsql-api")); +const grab_api_base_path_1 = __importDefault(require("../../utils/grab-api-base-path")); +function default_1(_a) { + return __awaiter(this, arguments, void 0, function* ({ dbName, tableName, apiKey, useDefault, fieldName, apiConnectionConfig, grabbedHostnames, }) { + const basePath = (0, grab_api_base_path_1.default)({ paradigm: "schema" }); + const finalPath = path_1.default.join(basePath, dbName, tableName || "", fieldName || ""); + const GET_RES = yield (0, query_dsql_api_1.default)({ + method: "GET", + path: finalPath, + apiKey, + useDefault, + apiConnectionConfig, + grabbedHostnames, + }); + return GET_RES; + }); +} diff --git a/dist/package-shared/api/schema/index.d.ts b/dist/package-shared/api/schema/index.d.ts new file mode 100644 index 0000000..4ca4d77 --- /dev/null +++ b/dist/package-shared/api/schema/index.d.ts @@ -0,0 +1,5 @@ +import get from "./get"; +declare const schema: { + get: typeof get; +}; +export default schema; diff --git a/dist/package-shared/api/schema/index.js b/dist/package-shared/api/schema/index.js new file mode 100644 index 0000000..95c85a7 --- /dev/null +++ b/dist/package-shared/api/schema/index.js @@ -0,0 +1,10 @@ +"use strict"; +var __importDefault = (this && this.__importDefault) || function (mod) { + return (mod && mod.__esModule) ? mod : { "default": mod }; +}; +Object.defineProperty(exports, "__esModule", { value: true }); +const get_1 = __importDefault(require("./get")); +const schema = { + get: get_1.default, +}; +exports.default = schema; diff --git a/dist/package-shared/dict/app-names.d.ts b/dist/package-shared/dict/app-names.d.ts index 13823a5..ac0217e 100644 --- a/dist/package-shared/dict/app-names.d.ts +++ b/dist/package-shared/dict/app-names.d.ts @@ -9,4 +9,5 @@ export declare const AppNames: { readonly ReverseProxyForwardURLHeaderName: "x-original-uri"; readonly PrivateAPIAuthHeaderName: "x-api-auth-key"; readonly StaticProxyForwardURLHeaderName: "x-media-path"; + readonly SchemaToTypeDefConfigFileName: "dsql-schema-to-typedef.json"; }; diff --git a/dist/package-shared/dict/app-names.js b/dist/package-shared/dict/app-names.js index dfad8af..34d736a 100644 --- a/dist/package-shared/dict/app-names.js +++ b/dist/package-shared/dict/app-names.js @@ -12,4 +12,5 @@ exports.AppNames = { ReverseProxyForwardURLHeaderName: "x-original-uri", PrivateAPIAuthHeaderName: "x-api-auth-key", StaticProxyForwardURLHeaderName: "x-media-path", + SchemaToTypeDefConfigFileName: "dsql-schema-to-typedef.json", }; diff --git a/dist/package-shared/functions/api/query-dsql-api.d.ts b/dist/package-shared/functions/api/query-dsql-api.d.ts index 1b045af..4dbbf07 100644 --- a/dist/package-shared/functions/api/query-dsql-api.d.ts +++ b/dist/package-shared/functions/api/query-dsql-api.d.ts @@ -1,4 +1,5 @@ -import { APIResponseObject, DataCrudRequestMethods, DataCrudRequestMethodsLowerCase } from "../../types"; +import { APIConnectionOptions, APIResponseObject, DataCrudRequestMethods, DataCrudRequestMethodsLowerCase } from "../../types"; +import { GrabHostNamesReturn } from "../../utils/grab-host-names"; type Param = { @@ -8,6 +9,8 @@ type Param({ body, query, useDefault, path: passedPath, method, apiKey, }: Param): Promise>; +}>({ body, query, useDefault, path: passedPath, method, apiKey, apiConnectionConfig, grabbedHostnames, }: Param): Promise>; export {}; diff --git a/dist/package-shared/functions/api/query-dsql-api.js b/dist/package-shared/functions/api/query-dsql-api.js index 9079201..44dfd7e 100644 --- a/dist/package-shared/functions/api/query-dsql-api.js +++ b/dist/package-shared/functions/api/query-dsql-api.js @@ -21,9 +21,16 @@ const lodash_1 = __importDefault(require("lodash")); * # Query DSQL API */ function queryDSQLAPI(_a) { - return __awaiter(this, arguments, void 0, function* ({ body, query, useDefault, path: passedPath, method, apiKey, }) { - const grabedHostNames = (0, grab_host_names_1.default)({ useDefault }); + return __awaiter(this, arguments, void 0, function* ({ body, query, useDefault, path: passedPath, method, apiKey, apiConnectionConfig, grabbedHostnames, }) { + const grabedHostNames = grabbedHostnames || (0, grab_host_names_1.default)({ useDefault, apiConnectionConfig }); const { host, port, scheme } = grabedHostNames; + const finalAPIKey = apiKey || + (apiConnectionConfig === null || apiConnectionConfig === void 0 ? void 0 : apiConnectionConfig.apiKey) || + (!method || method == "GET" || method == "get" + ? process.env.DSQL_READ_ONLY_API_KEY + : undefined) || + process.env.DSQL_FULL_ACCESS_API_KEY || + process.env.DSQL_API_KEY; try { /** * Make https request @@ -34,12 +41,7 @@ function queryDSQLAPI(_a) { const reqPayload = body ? JSON.stringify(body) : undefined; let headers = { "Content-Type": "application/json", - Authorization: apiKey || - (!method || method == "GET" || method == "get" - ? process.env.DSQL_READ_ONLY_API_KEY - : undefined) || - process.env.DSQL_FULL_ACCESS_API_KEY || - process.env.DSQL_API_KEY, + Authorization: finalAPIKey, }; if (reqPayload) { headers["Content-Length"] = Buffer.from(reqPayload).length; diff --git a/dist/package-shared/functions/dsql/db-schema-to-type.js b/dist/package-shared/functions/dsql/db-schema-to-type.js index 521830c..fb12554 100644 --- a/dist/package-shared/functions/dsql/db-schema-to-type.js +++ b/dist/package-shared/functions/dsql/db-schema-to-type.js @@ -4,31 +4,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) { }; Object.defineProperty(exports, "__esModule", { value: true }); exports.default = dbSchemaToType; -const fs_1 = __importDefault(require("fs")); -const grab_dir_names_1 = __importDefault(require("../../utils/backend/names/grab-dir-names")); const lodash_1 = __importDefault(require("lodash")); -const ejson_1 = __importDefault(require("../../utils/ejson")); const generate_type_definitions_1 = __importDefault(require("./generate-type-definitions")); const app_names_1 = require("../../dict/app-names"); +const defaultFields_json_1 = __importDefault(require("../../data/defaultFields.json")); function dbSchemaToType(params) { var _a, _b; - let datasquirelSchema; - const { mainShemaJSONFilePath, defaultTableFieldsJSONFilePath } = (0, grab_dir_names_1.default)(); - if (params === null || params === void 0 ? void 0 : params.dbSchema) { - datasquirelSchema = params.dbSchema; - } - else { - const mainSchema = ejson_1.default.parse(fs_1.default.readFileSync(mainShemaJSONFilePath, "utf-8")); - datasquirelSchema = mainSchema.find((sch) => sch.dbFullName == "datasquirel"); - } + let datasquirelSchema = params === null || params === void 0 ? void 0 : params.dbSchema; if (!datasquirelSchema) return; let tableNames = `export const DsqlTables = [\n${datasquirelSchema.tables .map((tbl) => ` "${tbl.tableName}",`) .join("\n")}\n] as const`; - const defaultFields = ejson_1.default.parse(fs_1.default.readFileSync(defaultTableFieldsJSONFilePath, "utf-8")); const dbTablesSchemas = datasquirelSchema.tables.map((tblSchm) => { - let newDefaultFields = lodash_1.default.cloneDeep(defaultFields); + let newDefaultFields = lodash_1.default.cloneDeep(defaultFields_json_1.default); return Object.assign(Object.assign({}, tblSchm), { fields: tblSchm.fields.find((fld) => fld.fieldName == "id") ? tblSchm.fields : [ diff --git a/dist/package-shared/types/index.d.ts b/dist/package-shared/types/index.d.ts index 273adee..cec3cee 100644 --- a/dist/package-shared/types/index.d.ts +++ b/dist/package-shared/types/index.d.ts @@ -2108,4 +2108,13 @@ export type DsqlConnectionParam = { */ config?: ConnectionConfig; }; +export type APIConnectionOptions = { + scheme?: "http" | "https"; + apiKey?: string; + host?: string; + localhostPort?: number; + remoteHost?: string; + remoteHostPort?: number; + isLocalhost?: boolean; +}; export {}; diff --git a/dist/package-shared/utils/grab-host-names.d.ts b/dist/package-shared/utils/grab-host-names.d.ts index 17e9af8..849d73b 100644 --- a/dist/package-shared/utils/grab-host-names.d.ts +++ b/dist/package-shared/utils/grab-host-names.d.ts @@ -1,10 +1,12 @@ import https from "https"; import http from "http"; -type GrabHostNamesReturn = { +import { APIConnectionOptions } from "../types"; +export type GrabHostNamesReturn = { host: string; port: number | string; scheme: typeof http | typeof https; user_id: string | number; + apiKey?: string; }; type Param = { userId?: string | number; @@ -14,6 +16,7 @@ type Param = { remoteHost?: string; remoteHostPort?: string; useDefault?: boolean; + apiConnectionConfig?: APIConnectionOptions; }; /** * # Grab Names For Query diff --git a/dist/package-shared/utils/grab-host-names.js b/dist/package-shared/utils/grab-host-names.js index 9717e8f..8f782f7 100644 --- a/dist/package-shared/utils/grab-host-names.js +++ b/dist/package-shared/utils/grab-host-names.js @@ -11,26 +11,37 @@ const http_1 = __importDefault(require("http")); * # Grab Names For Query */ function grabHostNames(param) { - var _a, _b; + var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k; const finalEnv = (param === null || param === void 0 ? void 0 : param.env) ? Object.assign(Object.assign({}, process.env), param.env) : process.env; - const scheme = finalEnv["DSQL_HTTP_SCHEME"]; - const localHost = finalEnv["DSQL_LOCAL_HOST"]; - const localHostPort = finalEnv["DSQL_LOCAL_HOST_PORT"]; + const scheme = ((_a = param === null || param === void 0 ? void 0 : param.apiConnectionConfig) === null || _a === void 0 ? void 0 : _a.scheme) || finalEnv["DSQL_HTTP_SCHEME"]; + const localHost = ((_b = param === null || param === void 0 ? void 0 : param.apiConnectionConfig) === null || _b === void 0 ? void 0 : _b.isLocalhost) + ? "localhost" + : finalEnv["DSQL_LOCAL_HOST"]; + const localHostPort = ((_c = param === null || param === void 0 ? void 0 : param.apiConnectionConfig) === null || _c === void 0 ? void 0 : _c.localhostPort) || + finalEnv["DSQL_LOCAL_HOST_PORT"]; const remoteHost = (param === null || param === void 0 ? void 0 : param.useDefault) ? undefined - : ((_a = finalEnv["DSQL_API_REMOTE_HOST"]) === null || _a === void 0 ? void 0 : _a.match(/.*\..*/)) - ? finalEnv["DSQL_API_REMOTE_HOST"] - : undefined; + : ((_d = param === null || param === void 0 ? void 0 : param.apiConnectionConfig) === null || _d === void 0 ? void 0 : _d.remoteHost) + ? (_e = param === null || param === void 0 ? void 0 : param.apiConnectionConfig) === null || _e === void 0 ? void 0 : _e.remoteHost + : ((_f = finalEnv["DSQL_API_REMOTE_HOST"]) === null || _f === void 0 ? void 0 : _f.match(/.*\..*/)) + ? finalEnv["DSQL_API_REMOTE_HOST"] + : undefined; const remoteHostPort = (param === null || param === void 0 ? void 0 : param.useDefault) ? undefined - : ((_b = finalEnv["DSQL_API_REMOTE_HOST_PORT"]) === null || _b === void 0 ? void 0 : _b.match(/./)) - ? finalEnv["DSQL_API_REMOTE_HOST_PORT"] - : undefined; + : ((_g = param === null || param === void 0 ? void 0 : param.apiConnectionConfig) === null || _g === void 0 ? void 0 : _g.remoteHostPort) + ? (_h = param === null || param === void 0 ? void 0 : param.apiConnectionConfig) === null || _h === void 0 ? void 0 : _h.remoteHostPort + : ((_j = finalEnv["DSQL_API_REMOTE_HOST_PORT"]) === null || _j === void 0 ? void 0 : _j.match(/./)) + ? finalEnv["DSQL_API_REMOTE_HOST_PORT"] + : undefined; return { host: remoteHost || localHost || "www.datasquirel.com", port: remoteHostPort || localHostPort || 443, scheme: (scheme === null || scheme === void 0 ? void 0 : scheme.match(/^http$/i)) ? http_1.default : https_1.default, user_id: (param === null || param === void 0 ? void 0 : param.userId) || String(finalEnv["DSQL_API_USER_ID"] || 0), + apiKey: ((_k = param === null || param === void 0 ? void 0 : param.apiConnectionConfig) === null || _k === void 0 ? void 0 : _k.apiKey) || + process.env.DSQL_FULL_ACCESS_API_KEY || + process.env.DSQL_API_KEY || + process.env.DSQL_READ_ONLY_API_KEY, }; } diff --git a/engine/schema-to-typedef.ts b/engine/schema-to-typedef.ts index 10ffc4b..3ef92d0 100644 --- a/engine/schema-to-typedef.ts +++ b/engine/schema-to-typedef.ts @@ -1,121 +1,94 @@ #! /usr/bin/env node -import fs from "fs"; +import fs, { readFileSync } from "fs"; import datasquirel from ".."; -import { parseArgs } from "util"; -import { DSQL_DatabaseSchemaType } from "../package-shared/types"; import dbSchemaToType from "../package-shared/functions/dsql/db-schema-to-type"; import path from "path"; import debugLog from "../package-shared/utils/logging/debug-log"; -import parseEnv from "../package-shared/utils/parse-env"; +import { AppNames } from "../package-shared/dict/app-names"; +import { APIConnectionOptions } from "../package-shared/types"; -const args = parseArgs({ - args: process.argv, - options: { - apiKey: { - type: "string", - default: process.env.DSQL_FULL_ACCESS_API_KEY, - short: "k", - }, - database: { - type: "string", - default: process.env.DSQL_DB_NAME, - short: "d", - }, - userid: { - type: "string", - default: process.env.DSQL_API_USER_ID, - short: "i", - }, - outfile: { - type: "string", - short: "o", - }, - envfile: { - type: "string", - short: "e", - }, - debug: { - type: "boolean", - short: "u", - }, - }, - strict: false, -}); +type Config = { + database?: string; + outDir?: string; + outFile?: string; + apiConnectionConfig?: APIConnectionOptions; + debug?: boolean; +}; -let appendedEnv: { [k: string]: string } = {}; +const configFileName = AppNames["SchemaToTypeDefConfigFileName"]; +const configFilePath = path.join(process.cwd(), configFileName); -if (args.values.envfile && typeof args.values.envfile == "string") { - const finalEnvPath = path.resolve(process.cwd(), args.values.envfile); - if (fs.existsSync(finalEnvPath)) { - const parsedEnv = parseEnv(finalEnvPath); - appendedEnv = (parsedEnv || {}) as any; - - if (args.values.debug) { - debugLog({ - log: appendedEnv, - label: "Appended env", - title: "Schema to Typedef", - addTime: true, - }); - } - } +if (!fs.existsSync(configFilePath)) { + debugLog({ + log: `Config File doesn't exist. Please add a '${AppNames["SchemaToTypeDefConfigFileName"]}' in the working directory with relevant configurations.`, + addTime: true, + type: "error", + }); + process.exit(1); } -const finalEnv = { ...process.env, ...appendedEnv } as { [k: string]: string }; -process.env = { ...process.env, ...appendedEnv }; +const config: Config = JSON.parse( + readFileSync(path.join(process.cwd(), configFileName), "utf-8") +); + +if (!config.database) { + debugLog({ + log: `Config File needs a 'database' value.`, + addTime: true, + type: "error", + }); + process.exit(1); +} + +if (!config.outDir && !config.outFile) { + debugLog({ + log: `Config File needs a 'outDir' or 'outFile' value.`, + addTime: true, + type: "error", + }); + process.exit(1); +} + +if (!config.apiConnectionConfig) { + debugLog({ + log: `Config File needs a 'apiConnectionConfig' value. This value needs an 'apiKey' value.`, + addTime: true, + type: "error", + }); + process.exit(1); +} + +if (!config.apiConnectionConfig.apiKey) { + debugLog({ + log: `Config value 'apiConnectionConfig' needs an 'apiKey' value.`, + addTime: true, + type: "error", + }); + process.exit(1); +} (async () => { try { - const key = args.values.apiKey || finalEnv["DSQL_FULL_ACCESS_API_KEY"]; - const database = args.values.database || finalEnv["DSQL_DB_NAME"]; - const user_id = - args.values.userid || finalEnv["DSQL_API_USER_ID"] || "1"; - - if (args.values.debug) { + if (config.debug) { debugLog({ - log: args.values, - label: "Arguments", - title: "Schema to Typedef", - addTime: true, - }); - debugLog({ - log: process.env.DSQL_FULL_ACCESS_API_KEY, - label: "process.env.DSQL_FULL_ACCESS_API_KEY", - title: "Schema to Typedef", - addTime: true, - }); - debugLog({ - log: process.env.DSQL_DB_NAME, - label: "process.env.DSQL_DB_NAME", - title: "Schema to Typedef", + log: config, + label: "Config", addTime: true, }); } - if (!database || typeof database !== "string") - throw new Error("Database name is required"); - if (!key || typeof key !== "string") - throw new Error("API key is required"); - if (!args.values.outfile || typeof args.values.outfile !== "string") - throw new Error("Outfile are required"); - if (!user_id || typeof user_id !== "string") - throw new Error("Outfile are required"); - - const schema = await datasquirel.getSchema({ - key, - database, - user_id, - env: finalEnv, + const schemaRes = await datasquirel.api.schema.get({ + dbName: config.database || "", + apiConnectionConfig: config.apiConnectionConfig, }); - const dbSchema = schema.payload as DSQL_DatabaseSchemaType | undefined; + const dbSchema = schemaRes.payload; - if (args.values.debug) { + if (config.debug) { debugLog({ - log: schema, + log: schemaRes, label: "schema", - title: "Schema to Typedef", addTime: true, }); } @@ -124,8 +97,13 @@ process.env = { ...process.env, ...appendedEnv }; const definitions = dbSchemaToType({ dbSchema }); - const finalOutfile = path.resolve(process.cwd(), args.values.outfile); + const finalOutfile = path.resolve( + process.cwd(), + config.outDir ? config.outDir + "/dsql.ts" : config.outFile || "" + ); + const ourfileDir = path.dirname(finalOutfile); + if (!fs.existsSync(ourfileDir)) { fs.mkdirSync(ourfileDir, { recursive: true }); } diff --git a/index.ts b/index.ts index 15300ff..c0fc835 100644 --- a/index.ts +++ b/index.ts @@ -30,6 +30,7 @@ import httpsRequest from "./package-shared/functions/backend/httpsRequest"; import handleNodemailer from "./package-shared/functions/backend/handleNodemailer"; import grabDSQLConnection from "./package-shared/utils/grab-dsql-connection"; import grabDSQLConnectionConfig from "./package-shared/utils/grab-dsql-connection-config"; +import schema from "./package-shared/api/schema"; /** * API Functions Object @@ -38,6 +39,7 @@ const api = { crud, media, user, + schema, }; /** diff --git a/package-shared/api/schema/get.ts b/package-shared/api/schema/get.ts new file mode 100644 index 0000000..019df2e --- /dev/null +++ b/package-shared/api/schema/get.ts @@ -0,0 +1,56 @@ +import path from "path"; +import queryDSQLAPI from "../../functions/api/query-dsql-api"; +import { + APIConnectionOptions, + APIResponseObject, + DSQL_DatabaseSchemaType, + DSQL_FieldSchemaType, + DSQL_TableSchemaType, +} from "../../types"; +import grabAPIBasePath from "../../utils/grab-api-base-path"; +import { GrabHostNamesReturn } from "../../utils/grab-host-names"; + +type Params = { + dbName: string; + tableName?: string; + fieldName?: string; + apiKey?: string; + apiConnectionConfig?: APIConnectionOptions; + grabbedHostnames?: GrabHostNamesReturn; + useDefault?: boolean; +}; + +export default async function < + T extends + | DSQL_DatabaseSchemaType + | DSQL_TableSchemaType + | DSQL_FieldSchemaType = DSQL_DatabaseSchemaType +>({ + dbName, + tableName, + apiKey, + useDefault, + fieldName, + apiConnectionConfig, + grabbedHostnames, +}: Params): Promise> { + const basePath = grabAPIBasePath({ paradigm: "schema" }); + + const finalPath = path.join( + basePath, + dbName, + tableName || "", + fieldName || "" + ); + + const GET_RES = await queryDSQLAPI({ + method: "GET", + path: finalPath, + apiKey, + useDefault, + apiConnectionConfig, + grabbedHostnames, + }); + + return GET_RES; +} diff --git a/package-shared/api/schema/index.ts b/package-shared/api/schema/index.ts new file mode 100644 index 0000000..a9a59c7 --- /dev/null +++ b/package-shared/api/schema/index.ts @@ -0,0 +1,7 @@ +import get from "./get"; + +const schema = { + get, +}; + +export default schema; diff --git a/package-shared/dict/app-names.ts b/package-shared/dict/app-names.ts index 940bc8b..660749e 100644 --- a/package-shared/dict/app-names.ts +++ b/package-shared/dict/app-names.ts @@ -9,4 +9,5 @@ export const AppNames = { ReverseProxyForwardURLHeaderName: "x-original-uri", PrivateAPIAuthHeaderName: "x-api-auth-key", StaticProxyForwardURLHeaderName: "x-media-path", + SchemaToTypeDefConfigFileName: "dsql-schema-to-typedef.json", } as const; diff --git a/package-shared/functions/api/query-dsql-api.ts b/package-shared/functions/api/query-dsql-api.ts index 053dc2a..f4f1f5e 100644 --- a/package-shared/functions/api/query-dsql-api.ts +++ b/package-shared/functions/api/query-dsql-api.ts @@ -2,11 +2,14 @@ import path from "path"; import { OutgoingHttpHeaders } from "http"; import { + APIConnectionOptions, APIResponseObject, DataCrudRequestMethods, DataCrudRequestMethodsLowerCase, } from "../../types"; -import grabHostNames from "../../utils/grab-host-names"; +import grabHostNames, { + GrabHostNamesReturn, +} from "../../utils/grab-host-names"; import serializeQuery from "../../utils/serialize-query"; import { RequestOptions } from "https"; import _ from "lodash"; @@ -20,6 +23,8 @@ type Param = { | (typeof DataCrudRequestMethods)[number] | (typeof DataCrudRequestMethodsLowerCase)[number]; apiKey?: string; + apiConnectionConfig?: APIConnectionOptions; + grabbedHostnames?: GrabHostNamesReturn; }; /** @@ -35,10 +40,22 @@ export default async function queryDSQLAPI< path: passedPath, method, apiKey, + apiConnectionConfig, + grabbedHostnames, }: Param): Promise> { - const grabedHostNames = grabHostNames({ useDefault }); + const grabedHostNames = + grabbedHostnames || grabHostNames({ useDefault, apiConnectionConfig }); const { host, port, scheme } = grabedHostNames; + const finalAPIKey = + apiKey || + apiConnectionConfig?.apiKey || + (!method || method == "GET" || method == "get" + ? process.env.DSQL_READ_ONLY_API_KEY + : undefined) || + process.env.DSQL_FULL_ACCESS_API_KEY || + process.env.DSQL_API_KEY; + try { /** * Make https request @@ -50,13 +67,7 @@ export default async function queryDSQLAPI< let headers: OutgoingHttpHeaders = { "Content-Type": "application/json", - Authorization: - apiKey || - (!method || method == "GET" || method == "get" - ? process.env.DSQL_READ_ONLY_API_KEY - : undefined) || - process.env.DSQL_FULL_ACCESS_API_KEY || - process.env.DSQL_API_KEY, + Authorization: finalAPIKey, }; if (reqPayload) { diff --git a/package-shared/functions/dsql/db-schema-to-type.ts b/package-shared/functions/dsql/db-schema-to-type.ts index 42b639e..5672107 100644 --- a/package-shared/functions/dsql/db-schema-to-type.ts +++ b/package-shared/functions/dsql/db-schema-to-type.ts @@ -1,35 +1,15 @@ -import fs from "fs"; -import grabDirNames from "../../utils/backend/names/grab-dir-names"; -import { - DSQL_DatabaseSchemaType, - DSQL_FieldSchemaType, - DSQL_TableSchemaType, -} from "../../types"; +import { DSQL_DatabaseSchemaType, DSQL_TableSchemaType } from "../../types"; import _ from "lodash"; -import EJSON from "../../utils/ejson"; import generateTypeDefinition from "./generate-type-definitions"; import { AppNames } from "../../dict/app-names"; +import defaultFields from "../../data/defaultFields.json"; type Params = { dbSchema?: DSQL_DatabaseSchemaType; }; export default function dbSchemaToType(params?: Params): string[] | undefined { - let datasquirelSchema; - const { mainShemaJSONFilePath, defaultTableFieldsJSONFilePath } = - grabDirNames(); - - if (params?.dbSchema) { - datasquirelSchema = params.dbSchema; - } else { - const mainSchema = EJSON.parse( - fs.readFileSync(mainShemaJSONFilePath, "utf-8") - ) as DSQL_DatabaseSchemaType[]; - - datasquirelSchema = mainSchema.find( - (sch) => sch.dbFullName == "datasquirel" - ); - } + let datasquirelSchema = params?.dbSchema; if (!datasquirelSchema) return; @@ -37,10 +17,6 @@ export default function dbSchemaToType(params?: Params): string[] | undefined { .map((tbl) => ` "${tbl.tableName}",`) .join("\n")}\n] as const`; - const defaultFields = EJSON.parse( - fs.readFileSync(defaultTableFieldsJSONFilePath, "utf-8") - ) as DSQL_FieldSchemaType[]; - const dbTablesSchemas = datasquirelSchema.tables.map((tblSchm) => { let newDefaultFields = _.cloneDeep(defaultFields); return { diff --git a/package-shared/types/index.ts b/package-shared/types/index.ts index 2da8402..e1dddb2 100644 --- a/package-shared/types/index.ts +++ b/package-shared/types/index.ts @@ -2747,3 +2747,13 @@ export type DsqlConnectionParam = { */ config?: ConnectionConfig; }; + +export type APIConnectionOptions = { + scheme?: "http" | "https"; + apiKey?: string; + host?: string; + localhostPort?: number; + remoteHost?: string; + remoteHostPort?: number; + isLocalhost?: boolean; +}; diff --git a/package-shared/utils/grab-host-names.ts b/package-shared/utils/grab-host-names.ts index 9ae80d1..45ca6a5 100644 --- a/package-shared/utils/grab-host-names.ts +++ b/package-shared/utils/grab-host-names.ts @@ -2,12 +2,14 @@ import https from "https"; import http from "http"; +import { APIConnectionOptions } from "../types"; -type GrabHostNamesReturn = { +export type GrabHostNamesReturn = { host: string; port: number | string; scheme: typeof http | typeof https; user_id: string | number; + apiKey?: string; }; type Param = { @@ -16,6 +18,7 @@ type Param = { remoteHost?: string; remoteHostPort?: string; useDefault?: boolean; + apiConnectionConfig?: APIConnectionOptions; }; /** @@ -26,17 +29,26 @@ export default function grabHostNames(param?: Param): GrabHostNamesReturn { ? { ...process.env, ...param.env } : process.env; - const scheme = finalEnv["DSQL_HTTP_SCHEME"]; - const localHost = finalEnv["DSQL_LOCAL_HOST"]; - const localHostPort = finalEnv["DSQL_LOCAL_HOST_PORT"]; + const scheme = + param?.apiConnectionConfig?.scheme || finalEnv["DSQL_HTTP_SCHEME"]; + const localHost = param?.apiConnectionConfig?.isLocalhost + ? "localhost" + : finalEnv["DSQL_LOCAL_HOST"]; + const localHostPort = + param?.apiConnectionConfig?.localhostPort || + finalEnv["DSQL_LOCAL_HOST_PORT"]; const remoteHost = param?.useDefault ? undefined + : param?.apiConnectionConfig?.remoteHost + ? param?.apiConnectionConfig?.remoteHost : finalEnv["DSQL_API_REMOTE_HOST"]?.match(/.*\..*/) ? finalEnv["DSQL_API_REMOTE_HOST"] : undefined; const remoteHostPort = param?.useDefault ? undefined + : param?.apiConnectionConfig?.remoteHostPort + ? param?.apiConnectionConfig?.remoteHostPort : finalEnv["DSQL_API_REMOTE_HOST_PORT"]?.match(/./) ? finalEnv["DSQL_API_REMOTE_HOST_PORT"] : undefined; @@ -46,5 +58,10 @@ export default function grabHostNames(param?: Param): GrabHostNamesReturn { port: remoteHostPort || localHostPort || 443, scheme: scheme?.match(/^http$/i) ? http : https, user_id: param?.userId || String(finalEnv["DSQL_API_USER_ID"] || 0), + apiKey: + param?.apiConnectionConfig?.apiKey || + process.env.DSQL_FULL_ACCESS_API_KEY || + process.env.DSQL_API_KEY || + process.env.DSQL_READ_ONLY_API_KEY, }; }