This commit is contained in:
Benjamin Toby 2025-04-17 11:42:53 +01:00
parent ba1a4aeeb8
commit ee23d71b66
11 changed files with 74 additions and 23 deletions

View File

@ -33,6 +33,11 @@ const args = (0, util_1.parseArgs)({
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",
@ -64,10 +69,12 @@ if (args.values.envfile && typeof args.values.envfile == "string") {
}
}
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) {
(0, debug_log_1.default)({
log: args.values,
@ -94,12 +101,22 @@ const finalEnv = Object.assign(Object.assign({}, process.env), appendedEnv);
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: 1,
user_id,
});
const dbSchema = schema.payload;
if (args.values.debug) {
(0, debug_log_1.default)({
log: schema,
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 });

View File

@ -6,5 +6,5 @@ type GetSchemaReturn = {
/**
* # Get Schema for Database, table, or field *
*/
export default function getSchema({ key, database, field, table, user_id, }: GetSchemaAPIParam): Promise<GetSchemaReturn>;
export default function getSchema({ key, database, field, table, user_id, env, }: GetSchemaAPIParam): Promise<GetSchemaReturn>;
export {};

View File

@ -18,8 +18,8 @@ const grab_host_names_1 = __importDefault(require("../utils/grab-host-names"));
* # Get Schema for Database, table, or field *
*/
function getSchema(_a) {
return __awaiter(this, arguments, void 0, function* ({ key, database, field, table, user_id, }) {
const grabedHostNames = (0, grab_host_names_1.default)();
return __awaiter(this, arguments, void 0, function* ({ key, database, field, table, user_id, env, }) {
const grabedHostNames = (0, grab_host_names_1.default)({ env });
const { host, port, scheme } = grabedHostNames;
/**
* Make https request

View File

@ -290,6 +290,9 @@ export interface GetSchemaRequestQuery {
table?: string;
field?: string;
user_id?: string | number;
env?: {
[k: string]: string;
};
}
export interface GetSchemaAPICredentialsParam {
key: string;

View File

@ -8,6 +8,9 @@ type GrabHostNamesReturn = {
};
type Param = {
userId?: string | number;
env?: {
[k: string]: string;
};
};
/**
* # Grab Names For Query

View File

@ -12,19 +12,21 @@ const http_1 = __importDefault(require("http"));
*/
function grabHostNames(param) {
var _a, _b;
const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
const remoteHost = ((_a = process.env.DSQL_API_REMOTE_HOST) === null || _a === void 0 ? void 0 : _a.match(/.*\..*/))
? process.env.DSQL_API_REMOTE_HOST
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 remoteHost = ((_a = finalEnv["DSQL_API_REMOTE_HOST"]) === null || _a === void 0 ? void 0 : _a.match(/.*\..*/))
? finalEnv["DSQL_API_REMOTE_HOST"]
: undefined;
const remoteHostPort = ((_b = process.env.DSQL_API_REMOTE_HOST_PORT) === null || _b === void 0 ? void 0 : _b.match(/./))
? process.env.DSQL_API_REMOTE_HOST_PORT
const remoteHostPort = ((_b = finalEnv["DSQL_API_REMOTE_HOST_PORT"]) === null || _b === void 0 ? void 0 : _b.match(/./))
? finalEnv["DSQL_API_REMOTE_HOST_PORT"]
: undefined;
return {
host: remoteHost || localHost || "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(process.env.DSQL_API_USER_ID || 0),
user_id: (param === null || param === void 0 ? void 0 : param.userId) || String(finalEnv["DSQL_API_USER_ID"] || 0),
};
}

View File

@ -23,6 +23,11 @@ const args = parseArgs({
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",
@ -58,11 +63,14 @@ if (args.values.envfile && typeof args.values.envfile == "string") {
}
const finalEnv = { ...process.env, ...appendedEnv };
process.env = { ...process.env, ...appendedEnv };
(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) {
debugLog({
@ -91,15 +99,26 @@ const finalEnv = { ...process.env, ...appendedEnv };
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: 1,
user_id,
});
const dbSchema = schema.payload as DSQL_DatabaseSchemaType | undefined;
if (args.values.debug) {
debugLog({
log: schema,
label: "schema",
title: "Schema to Typedef",
addTime: true,
});
}
if (!dbSchema) throw new Error("No schema found");
const definitions = dbSchemaToType({ dbSchema });

View File

@ -25,8 +25,9 @@ export default async function getSchema({
field,
table,
user_id,
env,
}: GetSchemaAPIParam): Promise<GetSchemaReturn> {
const grabedHostNames = grabHostNames();
const grabedHostNames = grabHostNames({ env });
const { host, port, scheme } = grabedHostNames;
/**

View File

@ -305,6 +305,7 @@ export interface GetSchemaRequestQuery {
table?: string;
field?: string;
user_id?: string | number;
env?: { [k: string]: string };
}
export interface GetSchemaAPICredentialsParam {

View File

@ -12,26 +12,31 @@ type GrabHostNamesReturn = {
type Param = {
userId?: string | number;
env?: { [k: string]: string };
};
/**
* # Grab Names For Query
*/
export default function grabHostNames(param?: Param): GrabHostNamesReturn {
const scheme = process.env.DSQL_HTTP_SCHEME;
const localHost = process.env.DSQL_LOCAL_HOST;
const localHostPort = process.env.DSQL_LOCAL_HOST_PORT;
const remoteHost = process.env.DSQL_API_REMOTE_HOST?.match(/.*\..*/)
? process.env.DSQL_API_REMOTE_HOST
const finalEnv = param?.env
? { ...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 remoteHost = finalEnv["DSQL_API_REMOTE_HOST"]?.match(/.*\..*/)
? finalEnv["DSQL_API_REMOTE_HOST"]
: undefined;
const remoteHostPort = process.env.DSQL_API_REMOTE_HOST_PORT?.match(/./)
? process.env.DSQL_API_REMOTE_HOST_PORT
const remoteHostPort = finalEnv["DSQL_API_REMOTE_HOST_PORT"]?.match(/./)
? finalEnv["DSQL_API_REMOTE_HOST_PORT"]
: undefined;
return {
host: remoteHost || localHost || "datasquirel.com",
port: remoteHostPort || localHostPort || 443,
scheme: scheme?.match(/^http$/i) ? http : https,
user_id: param?.userId || String(process.env.DSQL_API_USER_ID || 0),
user_id: param?.userId || String(finalEnv["DSQL_API_USER_ID"] || 0),
};
}

View File

@ -1,6 +1,6 @@
{
"name": "@moduletrace/datasquirel",
"version": "4.3.5",
"version": "4.3.6",
"description": "Cloud-based SQL data management tool",
"main": "dist/index.js",
"bin": {