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
+56
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;
}
+7
View File
@@ -0,0 +1,7 @@
import get from "./get";
const schema = {
get,
};
export default schema;
+1
View File
@@ -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;
+20 -9
View File
@@ -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<T = { [k: string]: any }> = {
| (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<T>): Promise<APIResponseObject<P>> {
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) {
@@ -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 {
+10
View File
@@ -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;
};
+21 -4
View File
@@ -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,
};
}