Updates
This commit is contained in:
parent
a1e56bb1b0
commit
700a704abd
177
dist/engine/schema-to-typedef.js
vendored
177
dist/engine/schema-to-typedef.js
vendored
@ -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 });
|
||||
|
3
dist/index.d.ts
vendored
3
dist/index.d.ts
vendored
@ -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
|
||||
|
2
dist/index.js
vendored
2
dist/index.js
vendored
@ -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
|
||||
|
13
dist/package-shared/api/schema/get.d.ts
vendored
Normal file
13
dist/package-shared/api/schema/get.d.ts
vendored
Normal 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
33
dist/package-shared/api/schema/get.js
vendored
Normal 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;
|
||||
});
|
||||
}
|
5
dist/package-shared/api/schema/index.d.ts
vendored
Normal file
5
dist/package-shared/api/schema/index.d.ts
vendored
Normal 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
10
dist/package-shared/api/schema/index.js
vendored
Normal 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;
|
1
dist/package-shared/dict/app-names.d.ts
vendored
1
dist/package-shared/dict/app-names.d.ts
vendored
@ -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";
|
||||
};
|
||||
|
1
dist/package-shared/dict/app-names.js
vendored
1
dist/package-shared/dict/app-names.js
vendored
@ -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",
|
||||
};
|
||||
|
@ -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 = {
|
||||
[k: string]: any;
|
||||
}> = {
|
||||
@ -8,6 +9,8 @@ type Param<T = {
|
||||
path: string;
|
||||
method?: (typeof DataCrudRequestMethods)[number] | (typeof DataCrudRequestMethodsLowerCase)[number];
|
||||
apiKey?: string;
|
||||
apiConnectionConfig?: APIConnectionOptions;
|
||||
grabbedHostnames?: GrabHostNamesReturn;
|
||||
};
|
||||
/**
|
||||
* # Query DSQL API
|
||||
@ -16,5 +19,5 @@ export default function queryDSQLAPI<T = {
|
||||
[k: string]: any;
|
||||
}, P = {
|
||||
[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 {};
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
: [
|
||||
|
9
dist/package-shared/types/index.d.ts
vendored
9
dist/package-shared/types/index.d.ts
vendored
@ -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 {};
|
||||
|
@ -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
|
||||
|
31
dist/package-shared/utils/grab-host-names.js
vendored
31
dist/package-shared/utils/grab-host-names.js
vendored
@ -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,
|
||||
};
|
||||
}
|
||||
|
@ -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 });
|
||||
}
|
||||
|
2
index.ts
2
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,
|
||||
};
|
||||
|
||||
/**
|
||||
|
56
package-shared/api/schema/get.ts
Normal file
56
package-shared/api/schema/get.ts
Normal 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
package-shared/api/schema/index.ts
Normal file
7
package-shared/api/schema/index.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import get from "./get";
|
||||
|
||||
const schema = {
|
||||
get,
|
||||
};
|
||||
|
||||
export default schema;
|
@ -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;
|
||||
|
@ -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 {
|
||||
|
@ -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;
|
||||
};
|
||||
|
@ -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,
|
||||
};
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user