This commit is contained in:
Benjamin Toby 2025-08-13 11:16:28 +01:00
parent a1e56bb1b0
commit 700a704abd
24 changed files with 400 additions and 260 deletions

View File

@ -1,5 +1,38 @@
#! /usr/bin/env node #! /usr/bin/env node
"use strict"; "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) { var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) { 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 }; return (mod && mod.__esModule) ? mod : { "default": mod };
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs")); const fs_1 = __importStar(require("fs"));
const __1 = __importDefault(require("..")); 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 db_schema_to_type_1 = __importDefault(require("../package-shared/functions/dsql/db-schema-to-type"));
const path_1 = __importDefault(require("path")); const path_1 = __importDefault(require("path"));
const debug_log_1 = __importDefault(require("../package-shared/utils/logging/debug-log")); const debug_log_1 = __importDefault(require("../package-shared/utils/logging/debug-log"));
const parse_env_1 = __importDefault(require("../package-shared/utils/parse-env")); const app_names_1 = require("../package-shared/dict/app-names");
const args = (0, util_1.parseArgs)({ const configFileName = app_names_1.AppNames["SchemaToTypeDefConfigFileName"];
args: process.argv, const configFilePath = path_1.default.join(process.cwd(), configFileName);
options: { if (!fs_1.default.existsSync(configFilePath)) {
apiKey: { (0, debug_log_1.default)({
type: "string", log: `Config File doesn't exist. Please add a '${app_names_1.AppNames["SchemaToTypeDefConfigFileName"]}' in the working directory with relevant configurations.`,
default: process.env.DSQL_FULL_ACCESS_API_KEY, addTime: true,
short: "k", type: "error",
}, });
database: { process.exit(1);
type: "string", }
default: process.env.DSQL_DB_NAME, const config = JSON.parse((0, fs_1.readFileSync)(path_1.default.join(process.cwd(), configFileName), "utf-8"));
short: "d", if (!config.database) {
}, (0, debug_log_1.default)({
userid: { log: `Config File needs a 'database' value.`,
type: "string", addTime: true,
default: process.env.DSQL_API_USER_ID, type: "error",
short: "i", });
}, process.exit(1);
outfile: { }
type: "string", if (!config.outDir && !config.outFile) {
short: "o", (0, debug_log_1.default)({
}, log: `Config File needs a 'outDir' or 'outFile' value.`,
envfile: { addTime: true,
type: "string", type: "error",
short: "e", });
}, process.exit(1);
debug: { }
type: "boolean", if (!config.apiConnectionConfig) {
short: "u", (0, debug_log_1.default)({
}, log: `Config File needs a 'apiConnectionConfig' value. This value needs an 'apiKey' value.`,
}, addTime: true,
strict: false, type: "error",
}); });
let appendedEnv = {}; process.exit(1);
if (args.values.envfile && typeof args.values.envfile == "string") { }
const finalEnvPath = path_1.default.resolve(process.cwd(), args.values.envfile); if (!config.apiConnectionConfig.apiKey) {
if (fs_1.default.existsSync(finalEnvPath)) { (0, debug_log_1.default)({
const parsedEnv = (0, parse_env_1.default)(finalEnvPath); log: `Config value 'apiConnectionConfig' needs an 'apiKey' value.`,
appendedEnv = (parsedEnv || {}); addTime: true,
if (args.values.debug) { type: "error",
(0, debug_log_1.default)({ });
log: appendedEnv, process.exit(1);
label: "Appended env",
title: "Schema to Typedef",
addTime: true,
});
}
}
} }
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* () { (() => __awaiter(void 0, void 0, void 0, function* () {
try { try {
const key = args.values.apiKey || finalEnv["DSQL_FULL_ACCESS_API_KEY"]; if (config.debug) {
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) {
(0, debug_log_1.default)({ (0, debug_log_1.default)({
log: args.values, log: config,
label: "Arguments", label: "Config",
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",
addTime: true, addTime: true,
}); });
} }
if (!database || typeof database !== "string") const schemaRes = yield __1.default.api.schema.get({
throw new Error("Database name is required"); dbName: config.database || "",
if (!key || typeof key !== "string") apiConnectionConfig: config.apiConnectionConfig,
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 dbSchema = schema.payload; const dbSchema = schemaRes.payload;
if (args.values.debug) { if (config.debug) {
(0, debug_log_1.default)({ (0, debug_log_1.default)({
log: schema, log: schemaRes,
label: "schema", label: "schema",
title: "Schema to Typedef",
addTime: true, addTime: true,
}); });
} }
if (!dbSchema) if (!dbSchema)
throw new Error("No schema found"); throw new Error("No schema found");
const definitions = (0, db_schema_to_type_1.default)({ dbSchema }); 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); const ourfileDir = path_1.default.dirname(finalOutfile);
if (!fs_1.default.existsSync(ourfileDir)) { if (!fs_1.default.existsSync(ourfileDir)) {
fs_1.default.mkdirSync(ourfileDir, { recursive: true }); fs_1.default.mkdirSync(ourfileDir, { recursive: true });

3
dist/index.d.ts vendored
View File

@ -57,6 +57,9 @@ declare const datasquirel: {
validateTempEmailCode: typeof import("./package-shared/actions/users/validate-temp-email-code").default; validateTempEmailCode: typeof import("./package-shared/actions/users/validate-temp-email-code").default;
}; };
}; };
schema: {
get: typeof import("./package-shared/api/schema/get").default;
};
}; };
/** /**
* Get Action * Get Action

2
dist/index.js vendored
View File

@ -29,6 +29,7 @@ const httpsRequest_1 = __importDefault(require("./package-shared/functions/backe
const handleNodemailer_1 = __importDefault(require("./package-shared/functions/backend/handleNodemailer")); 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_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 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 * API Functions Object
*/ */
@ -36,6 +37,7 @@ const api = {
crud: crud_2.default, crud: crud_2.default,
media: media_1.default, media: media_1.default,
user: user_1.default, user: user_1.default,
schema: schema_1.default,
}; };
/** /**
* SQL Utils * SQL Utils

13
dist/package-shared/api/schema/get.d.ts vendored Normal file
View File

@ -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 <T extends DSQL_DatabaseSchemaType | DSQL_TableSchemaType | DSQL_FieldSchemaType = DSQL_DatabaseSchemaType>({ dbName, tableName, apiKey, useDefault, fieldName, apiConnectionConfig, grabbedHostnames, }: Params): Promise<APIResponseObject<T>>;
export {};

33
dist/package-shared/api/schema/get.js vendored Normal file
View File

@ -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;
});
}

View File

@ -0,0 +1,5 @@
import get from "./get";
declare const schema: {
get: typeof get;
};
export default schema;

10
dist/package-shared/api/schema/index.js vendored Normal file
View File

@ -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;

View File

@ -9,4 +9,5 @@ export declare const AppNames: {
readonly ReverseProxyForwardURLHeaderName: "x-original-uri"; readonly ReverseProxyForwardURLHeaderName: "x-original-uri";
readonly PrivateAPIAuthHeaderName: "x-api-auth-key"; readonly PrivateAPIAuthHeaderName: "x-api-auth-key";
readonly StaticProxyForwardURLHeaderName: "x-media-path"; readonly StaticProxyForwardURLHeaderName: "x-media-path";
readonly SchemaToTypeDefConfigFileName: "dsql-schema-to-typedef.json";
}; };

View File

@ -12,4 +12,5 @@ exports.AppNames = {
ReverseProxyForwardURLHeaderName: "x-original-uri", ReverseProxyForwardURLHeaderName: "x-original-uri",
PrivateAPIAuthHeaderName: "x-api-auth-key", PrivateAPIAuthHeaderName: "x-api-auth-key",
StaticProxyForwardURLHeaderName: "x-media-path", StaticProxyForwardURLHeaderName: "x-media-path",
SchemaToTypeDefConfigFileName: "dsql-schema-to-typedef.json",
}; };

View File

@ -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<T = { type Param<T = {
[k: string]: any; [k: string]: any;
}> = { }> = {
@ -8,6 +9,8 @@ type Param<T = {
path: string; path: string;
method?: (typeof DataCrudRequestMethods)[number] | (typeof DataCrudRequestMethodsLowerCase)[number]; method?: (typeof DataCrudRequestMethods)[number] | (typeof DataCrudRequestMethodsLowerCase)[number];
apiKey?: string; apiKey?: string;
apiConnectionConfig?: APIConnectionOptions;
grabbedHostnames?: GrabHostNamesReturn;
}; };
/** /**
* # Query DSQL API * # Query DSQL API
@ -16,5 +19,5 @@ export default function queryDSQLAPI<T = {
[k: string]: any; [k: string]: any;
}, P = { }, P = {
[k: string]: any; [k: string]: any;
}>({ body, query, useDefault, path: passedPath, method, apiKey, }: Param<T>): Promise<APIResponseObject<P>>; }>({ body, query, useDefault, path: passedPath, method, apiKey, apiConnectionConfig, grabbedHostnames, }: Param<T>): Promise<APIResponseObject<P>>;
export {}; export {};

View File

@ -21,9 +21,16 @@ const lodash_1 = __importDefault(require("lodash"));
* # Query DSQL API * # Query DSQL API
*/ */
function queryDSQLAPI(_a) { function queryDSQLAPI(_a) {
return __awaiter(this, arguments, void 0, function* ({ body, query, useDefault, path: passedPath, method, apiKey, }) { return __awaiter(this, arguments, void 0, function* ({ body, query, useDefault, path: passedPath, method, apiKey, apiConnectionConfig, grabbedHostnames, }) {
const grabedHostNames = (0, grab_host_names_1.default)({ useDefault }); const grabedHostNames = grabbedHostnames || (0, grab_host_names_1.default)({ useDefault, apiConnectionConfig });
const { host, port, scheme } = grabedHostNames; 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 { try {
/** /**
* Make https request * Make https request
@ -34,12 +41,7 @@ function queryDSQLAPI(_a) {
const reqPayload = body ? JSON.stringify(body) : undefined; const reqPayload = body ? JSON.stringify(body) : undefined;
let headers = { let headers = {
"Content-Type": "application/json", "Content-Type": "application/json",
Authorization: apiKey || Authorization: finalAPIKey,
(!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,
}; };
if (reqPayload) { if (reqPayload) {
headers["Content-Length"] = Buffer.from(reqPayload).length; headers["Content-Length"] = Buffer.from(reqPayload).length;

View File

@ -4,31 +4,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
}; };
Object.defineProperty(exports, "__esModule", { value: true }); Object.defineProperty(exports, "__esModule", { value: true });
exports.default = dbSchemaToType; 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 lodash_1 = __importDefault(require("lodash"));
const ejson_1 = __importDefault(require("../../utils/ejson"));
const generate_type_definitions_1 = __importDefault(require("./generate-type-definitions")); const generate_type_definitions_1 = __importDefault(require("./generate-type-definitions"));
const app_names_1 = require("../../dict/app-names"); const app_names_1 = require("../../dict/app-names");
const defaultFields_json_1 = __importDefault(require("../../data/defaultFields.json"));
function dbSchemaToType(params) { function dbSchemaToType(params) {
var _a, _b; var _a, _b;
let datasquirelSchema; let datasquirelSchema = params === null || params === void 0 ? void 0 : params.dbSchema;
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");
}
if (!datasquirelSchema) if (!datasquirelSchema)
return; return;
let tableNames = `export const DsqlTables = [\n${datasquirelSchema.tables let tableNames = `export const DsqlTables = [\n${datasquirelSchema.tables
.map((tbl) => ` "${tbl.tableName}",`) .map((tbl) => ` "${tbl.tableName}",`)
.join("\n")}\n] as const`; .join("\n")}\n] as const`;
const defaultFields = ejson_1.default.parse(fs_1.default.readFileSync(defaultTableFieldsJSONFilePath, "utf-8"));
const dbTablesSchemas = datasquirelSchema.tables.map((tblSchm) => { 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") return Object.assign(Object.assign({}, tblSchm), { fields: tblSchm.fields.find((fld) => fld.fieldName == "id")
? tblSchm.fields ? tblSchm.fields
: [ : [

View File

@ -2108,4 +2108,13 @@ export type DsqlConnectionParam = {
*/ */
config?: ConnectionConfig; config?: ConnectionConfig;
}; };
export type APIConnectionOptions = {
scheme?: "http" | "https";
apiKey?: string;
host?: string;
localhostPort?: number;
remoteHost?: string;
remoteHostPort?: number;
isLocalhost?: boolean;
};
export {}; export {};

View File

@ -1,10 +1,12 @@
import https from "https"; import https from "https";
import http from "http"; import http from "http";
type GrabHostNamesReturn = { import { APIConnectionOptions } from "../types";
export type GrabHostNamesReturn = {
host: string; host: string;
port: number | string; port: number | string;
scheme: typeof http | typeof https; scheme: typeof http | typeof https;
user_id: string | number; user_id: string | number;
apiKey?: string;
}; };
type Param = { type Param = {
userId?: string | number; userId?: string | number;
@ -14,6 +16,7 @@ type Param = {
remoteHost?: string; remoteHost?: string;
remoteHostPort?: string; remoteHostPort?: string;
useDefault?: boolean; useDefault?: boolean;
apiConnectionConfig?: APIConnectionOptions;
}; };
/** /**
* # Grab Names For Query * # Grab Names For Query

View File

@ -11,26 +11,37 @@ const http_1 = __importDefault(require("http"));
* # Grab Names For Query * # Grab Names For Query
*/ */
function grabHostNames(param) { 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) const finalEnv = (param === null || param === void 0 ? void 0 : param.env)
? Object.assign(Object.assign({}, process.env), param.env) : process.env; ? Object.assign(Object.assign({}, process.env), param.env) : process.env;
const scheme = finalEnv["DSQL_HTTP_SCHEME"]; 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 = finalEnv["DSQL_LOCAL_HOST"]; const localHost = ((_b = param === null || param === void 0 ? void 0 : param.apiConnectionConfig) === null || _b === void 0 ? void 0 : _b.isLocalhost)
const localHostPort = finalEnv["DSQL_LOCAL_HOST_PORT"]; ? "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) const remoteHost = (param === null || param === void 0 ? void 0 : param.useDefault)
? undefined ? undefined
: ((_a = finalEnv["DSQL_API_REMOTE_HOST"]) === null || _a === void 0 ? void 0 : _a.match(/.*\..*/)) : ((_d = param === null || param === void 0 ? void 0 : param.apiConnectionConfig) === null || _d === void 0 ? void 0 : _d.remoteHost)
? finalEnv["DSQL_API_REMOTE_HOST"] ? (_e = param === null || param === void 0 ? void 0 : param.apiConnectionConfig) === null || _e === void 0 ? void 0 : _e.remoteHost
: undefined; : ((_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) const remoteHostPort = (param === null || param === void 0 ? void 0 : param.useDefault)
? undefined ? undefined
: ((_b = finalEnv["DSQL_API_REMOTE_HOST_PORT"]) === null || _b === void 0 ? void 0 : _b.match(/./)) : ((_g = param === null || param === void 0 ? void 0 : param.apiConnectionConfig) === null || _g === void 0 ? void 0 : _g.remoteHostPort)
? finalEnv["DSQL_API_REMOTE_HOST_PORT"] ? (_h = param === null || param === void 0 ? void 0 : param.apiConnectionConfig) === null || _h === void 0 ? void 0 : _h.remoteHostPort
: undefined; : ((_j = finalEnv["DSQL_API_REMOTE_HOST_PORT"]) === null || _j === void 0 ? void 0 : _j.match(/./))
? finalEnv["DSQL_API_REMOTE_HOST_PORT"]
: undefined;
return { return {
host: remoteHost || localHost || "www.datasquirel.com", host: remoteHost || localHost || "www.datasquirel.com",
port: remoteHostPort || localHostPort || 443, port: remoteHostPort || localHostPort || 443,
scheme: (scheme === null || scheme === void 0 ? void 0 : scheme.match(/^http$/i)) ? http_1.default : https_1.default, 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), 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,
}; };
} }

View File

@ -1,121 +1,94 @@
#! /usr/bin/env node #! /usr/bin/env node
import fs from "fs"; import fs, { readFileSync } from "fs";
import datasquirel from ".."; 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 dbSchemaToType from "../package-shared/functions/dsql/db-schema-to-type";
import path from "path"; import path from "path";
import debugLog from "../package-shared/utils/logging/debug-log"; 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({ type Config = {
args: process.argv, database?: string;
options: { outDir?: string;
apiKey: { outFile?: string;
type: "string", apiConnectionConfig?: APIConnectionOptions;
default: process.env.DSQL_FULL_ACCESS_API_KEY, debug?: boolean;
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: { [k: string]: string } = {}; const configFileName = AppNames["SchemaToTypeDefConfigFileName"];
const configFilePath = path.join(process.cwd(), configFileName);
if (args.values.envfile && typeof args.values.envfile == "string") { if (!fs.existsSync(configFilePath)) {
const finalEnvPath = path.resolve(process.cwd(), args.values.envfile); debugLog({
if (fs.existsSync(finalEnvPath)) { log: `Config File doesn't exist. Please add a '${AppNames["SchemaToTypeDefConfigFileName"]}' in the working directory with relevant configurations.`,
const parsedEnv = parseEnv(finalEnvPath); addTime: true,
appendedEnv = (parsedEnv || {}) as any; type: "error",
});
if (args.values.debug) { process.exit(1);
debugLog({
log: appendedEnv,
label: "Appended env",
title: "Schema to Typedef",
addTime: true,
});
}
}
} }
const finalEnv = { ...process.env, ...appendedEnv } as { [k: string]: string }; const config: Config = JSON.parse(
process.env = { ...process.env, ...appendedEnv }; 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 () => { (async () => {
try { try {
const key = args.values.apiKey || finalEnv["DSQL_FULL_ACCESS_API_KEY"]; if (config.debug) {
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) {
debugLog({ debugLog({
log: args.values, log: config,
label: "Arguments", label: "Config",
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",
addTime: true, addTime: true,
}); });
} }
if (!database || typeof database !== "string") const schemaRes = await datasquirel.api.schema.get({
throw new Error("Database name is required"); dbName: config.database || "",
if (!key || typeof key !== "string") apiConnectionConfig: config.apiConnectionConfig,
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 dbSchema = schema.payload as DSQL_DatabaseSchemaType | undefined; const dbSchema = schemaRes.payload;
if (args.values.debug) { if (config.debug) {
debugLog({ debugLog({
log: schema, log: schemaRes,
label: "schema", label: "schema",
title: "Schema to Typedef",
addTime: true, addTime: true,
}); });
} }
@ -124,8 +97,13 @@ process.env = { ...process.env, ...appendedEnv };
const definitions = dbSchemaToType({ dbSchema }); 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); const ourfileDir = path.dirname(finalOutfile);
if (!fs.existsSync(ourfileDir)) { if (!fs.existsSync(ourfileDir)) {
fs.mkdirSync(ourfileDir, { recursive: true }); fs.mkdirSync(ourfileDir, { recursive: true });
} }

View File

@ -30,6 +30,7 @@ import httpsRequest from "./package-shared/functions/backend/httpsRequest";
import handleNodemailer from "./package-shared/functions/backend/handleNodemailer"; import handleNodemailer from "./package-shared/functions/backend/handleNodemailer";
import grabDSQLConnection from "./package-shared/utils/grab-dsql-connection"; import grabDSQLConnection from "./package-shared/utils/grab-dsql-connection";
import grabDSQLConnectionConfig from "./package-shared/utils/grab-dsql-connection-config"; import grabDSQLConnectionConfig from "./package-shared/utils/grab-dsql-connection-config";
import schema from "./package-shared/api/schema";
/** /**
* API Functions Object * API Functions Object
@ -38,6 +39,7 @@ const api = {
crud, crud,
media, media,
user, user,
schema,
}; };
/** /**

View File

@ -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<APIResponseObject<T>> {
const basePath = grabAPIBasePath({ paradigm: "schema" });
const finalPath = path.join(
basePath,
dbName,
tableName || "",
fieldName || ""
);
const GET_RES = await queryDSQLAPI<any, T>({
method: "GET",
path: finalPath,
apiKey,
useDefault,
apiConnectionConfig,
grabbedHostnames,
});
return GET_RES;
}

View File

@ -0,0 +1,7 @@
import get from "./get";
const schema = {
get,
};
export default schema;

View File

@ -9,4 +9,5 @@ export const AppNames = {
ReverseProxyForwardURLHeaderName: "x-original-uri", ReverseProxyForwardURLHeaderName: "x-original-uri",
PrivateAPIAuthHeaderName: "x-api-auth-key", PrivateAPIAuthHeaderName: "x-api-auth-key",
StaticProxyForwardURLHeaderName: "x-media-path", StaticProxyForwardURLHeaderName: "x-media-path",
SchemaToTypeDefConfigFileName: "dsql-schema-to-typedef.json",
} as const; } as const;

View File

@ -2,11 +2,14 @@ import path from "path";
import { OutgoingHttpHeaders } from "http"; import { OutgoingHttpHeaders } from "http";
import { import {
APIConnectionOptions,
APIResponseObject, APIResponseObject,
DataCrudRequestMethods, DataCrudRequestMethods,
DataCrudRequestMethodsLowerCase, DataCrudRequestMethodsLowerCase,
} from "../../types"; } from "../../types";
import grabHostNames from "../../utils/grab-host-names"; import grabHostNames, {
GrabHostNamesReturn,
} from "../../utils/grab-host-names";
import serializeQuery from "../../utils/serialize-query"; import serializeQuery from "../../utils/serialize-query";
import { RequestOptions } from "https"; import { RequestOptions } from "https";
import _ from "lodash"; import _ from "lodash";
@ -20,6 +23,8 @@ type Param<T = { [k: string]: any }> = {
| (typeof DataCrudRequestMethods)[number] | (typeof DataCrudRequestMethods)[number]
| (typeof DataCrudRequestMethodsLowerCase)[number]; | (typeof DataCrudRequestMethodsLowerCase)[number];
apiKey?: string; apiKey?: string;
apiConnectionConfig?: APIConnectionOptions;
grabbedHostnames?: GrabHostNamesReturn;
}; };
/** /**
@ -35,10 +40,22 @@ export default async function queryDSQLAPI<
path: passedPath, path: passedPath,
method, method,
apiKey, apiKey,
apiConnectionConfig,
grabbedHostnames,
}: Param<T>): Promise<APIResponseObject<P>> { }: Param<T>): Promise<APIResponseObject<P>> {
const grabedHostNames = grabHostNames({ useDefault }); const grabedHostNames =
grabbedHostnames || grabHostNames({ useDefault, apiConnectionConfig });
const { host, port, scheme } = grabedHostNames; 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 { try {
/** /**
* Make https request * Make https request
@ -50,13 +67,7 @@ export default async function queryDSQLAPI<
let headers: OutgoingHttpHeaders = { let headers: OutgoingHttpHeaders = {
"Content-Type": "application/json", "Content-Type": "application/json",
Authorization: Authorization: finalAPIKey,
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,
}; };
if (reqPayload) { if (reqPayload) {

View File

@ -1,35 +1,15 @@
import fs from "fs"; import { DSQL_DatabaseSchemaType, DSQL_TableSchemaType } from "../../types";
import grabDirNames from "../../utils/backend/names/grab-dir-names";
import {
DSQL_DatabaseSchemaType,
DSQL_FieldSchemaType,
DSQL_TableSchemaType,
} from "../../types";
import _ from "lodash"; import _ from "lodash";
import EJSON from "../../utils/ejson";
import generateTypeDefinition from "./generate-type-definitions"; import generateTypeDefinition from "./generate-type-definitions";
import { AppNames } from "../../dict/app-names"; import { AppNames } from "../../dict/app-names";
import defaultFields from "../../data/defaultFields.json";
type Params = { type Params = {
dbSchema?: DSQL_DatabaseSchemaType; dbSchema?: DSQL_DatabaseSchemaType;
}; };
export default function dbSchemaToType(params?: Params): string[] | undefined { export default function dbSchemaToType(params?: Params): string[] | undefined {
let datasquirelSchema; let datasquirelSchema = params?.dbSchema;
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"
);
}
if (!datasquirelSchema) return; if (!datasquirelSchema) return;
@ -37,10 +17,6 @@ export default function dbSchemaToType(params?: Params): string[] | undefined {
.map((tbl) => ` "${tbl.tableName}",`) .map((tbl) => ` "${tbl.tableName}",`)
.join("\n")}\n] as const`; .join("\n")}\n] as const`;
const defaultFields = EJSON.parse(
fs.readFileSync(defaultTableFieldsJSONFilePath, "utf-8")
) as DSQL_FieldSchemaType[];
const dbTablesSchemas = datasquirelSchema.tables.map((tblSchm) => { const dbTablesSchemas = datasquirelSchema.tables.map((tblSchm) => {
let newDefaultFields = _.cloneDeep(defaultFields); let newDefaultFields = _.cloneDeep(defaultFields);
return { return {

View File

@ -2747,3 +2747,13 @@ export type DsqlConnectionParam = {
*/ */
config?: ConnectionConfig; config?: ConnectionConfig;
}; };
export type APIConnectionOptions = {
scheme?: "http" | "https";
apiKey?: string;
host?: string;
localhostPort?: number;
remoteHost?: string;
remoteHostPort?: number;
isLocalhost?: boolean;
};

View File

@ -2,12 +2,14 @@
import https from "https"; import https from "https";
import http from "http"; import http from "http";
import { APIConnectionOptions } from "../types";
type GrabHostNamesReturn = { export type GrabHostNamesReturn = {
host: string; host: string;
port: number | string; port: number | string;
scheme: typeof http | typeof https; scheme: typeof http | typeof https;
user_id: string | number; user_id: string | number;
apiKey?: string;
}; };
type Param = { type Param = {
@ -16,6 +18,7 @@ type Param = {
remoteHost?: string; remoteHost?: string;
remoteHostPort?: string; remoteHostPort?: string;
useDefault?: boolean; useDefault?: boolean;
apiConnectionConfig?: APIConnectionOptions;
}; };
/** /**
@ -26,17 +29,26 @@ export default function grabHostNames(param?: Param): GrabHostNamesReturn {
? { ...process.env, ...param.env } ? { ...process.env, ...param.env }
: process.env; : process.env;
const scheme = finalEnv["DSQL_HTTP_SCHEME"]; const scheme =
const localHost = finalEnv["DSQL_LOCAL_HOST"]; param?.apiConnectionConfig?.scheme || finalEnv["DSQL_HTTP_SCHEME"];
const localHostPort = finalEnv["DSQL_LOCAL_HOST_PORT"]; const localHost = param?.apiConnectionConfig?.isLocalhost
? "localhost"
: finalEnv["DSQL_LOCAL_HOST"];
const localHostPort =
param?.apiConnectionConfig?.localhostPort ||
finalEnv["DSQL_LOCAL_HOST_PORT"];
const remoteHost = param?.useDefault const remoteHost = param?.useDefault
? undefined ? undefined
: param?.apiConnectionConfig?.remoteHost
? param?.apiConnectionConfig?.remoteHost
: finalEnv["DSQL_API_REMOTE_HOST"]?.match(/.*\..*/) : finalEnv["DSQL_API_REMOTE_HOST"]?.match(/.*\..*/)
? finalEnv["DSQL_API_REMOTE_HOST"] ? finalEnv["DSQL_API_REMOTE_HOST"]
: undefined; : undefined;
const remoteHostPort = param?.useDefault const remoteHostPort = param?.useDefault
? undefined ? undefined
: param?.apiConnectionConfig?.remoteHostPort
? param?.apiConnectionConfig?.remoteHostPort
: finalEnv["DSQL_API_REMOTE_HOST_PORT"]?.match(/./) : finalEnv["DSQL_API_REMOTE_HOST_PORT"]?.match(/./)
? finalEnv["DSQL_API_REMOTE_HOST_PORT"] ? finalEnv["DSQL_API_REMOTE_HOST_PORT"]
: undefined; : undefined;
@ -46,5 +58,10 @@ export default function grabHostNames(param?: Param): GrabHostNamesReturn {
port: remoteHostPort || localHostPort || 443, port: remoteHostPort || localHostPort || 443,
scheme: scheme?.match(/^http$/i) ? http : https, scheme: scheme?.match(/^http$/i) ? http : https,
user_id: param?.userId || String(finalEnv["DSQL_API_USER_ID"] || 0), 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,
}; };
} }