From c2328116da3c7c9712bfd6a7b554c0cee058247c Mon Sep 17 00:00:00 2001 From: Benjamin Toby Date: Tue, 22 Oct 2024 18:17:59 +0100 Subject: [PATCH] Updates --- package-shared/types/index.d.ts | 42 +++++------------------------ package-shared/types/index.js | 47 +++++---------------------------- package.json | 2 +- tsconfig.json | 1 - utils/get-schema.js | 22 ++++++++------- 5 files changed, 28 insertions(+), 86 deletions(-) diff --git a/package-shared/types/index.d.ts b/package-shared/types/index.d.ts index 01da643..d7229c1 100644 --- a/package-shared/types/index.d.ts +++ b/package-shared/types/index.d.ts @@ -316,42 +316,14 @@ export interface GetReturn { schema?: DSQL_TableSchemaType; } -interface PostReturn { - success: boolean; - payload?: PostInsertReturn | Object[] | string; +interface GetSchemaRequestQuery { + database?: string; + table?: string; + field?: string; } -interface PostInsertReturn { - fieldCount: number; - affectedRows: number; - insertId: number; - serverStatus: number; - warningCount: number; - message: string; - protocol41: boolean; - changedRows: number; +interface GetSchemaAPICredentialsParam { + key: string; } -interface PostDataPayload { - action: "insert" | "update" | "delete"; - table: string; - data?: object; - identifierColumnName?: string; - identifierValue?: string; - duplicateColumnName?: string; - duplicateColumnValue?: string; - update?: boolean; -} - -interface LocalPostReturn { - success: boolean; - payload?: PostInsertReturn | Object[] | string; - msg?: string; - error?: string; -} - -interface LocalPostQueryObject { - query: string | import("../../package-shared/types").PostDataPayload; - tableName?: string; - queryValues?: string[]; -} +type GetSchemaAPIParam = GetSchemaRequestQuery & GetSchemaAPICredentialsParam; diff --git a/package-shared/types/index.js b/package-shared/types/index.js index 4f9edd0..f98e8f0 100644 --- a/package-shared/types/index.js +++ b/package-shared/types/index.js @@ -355,50 +355,17 @@ const http = require("http"); */ /** - * @typedef {Object} PostReturn - * @property {boolean} success - Did the function run successfully? - * @property { PostInsertReturn | Object[] | string } [payload] - The Y Coordinate + * @typedef {object} GetSchemaRequestQuery + * @property {string} [database] - Db Name. eg `test_database` + * @property {string} [table] - Table Name (slug) e.g `test_table` + * @property {string} [field] */ /** - * @typedef {object} PostInsertReturn - * @property {number} fieldCount - * @property {number} affectedRows - * @property {number} insertId - * @property {number} serverStatus - * @property {number} warningCount - * @property {string} message - * @property {boolean} protocol41 - * @property {number} changedRows + * @typedef {object} GetSchemaAPICredentialsParam + * @property {string} key - API FULL ACCESS Key */ /** - * @typedef {object} PostDataPayload - * @property {"insert" | "update" | "delete"} action - The target action to take - * @property {string} table - Table name(slug) eg "blog_posts" - * @property {object} [data] - Table insert payload object => This must have keys that match - * table fields - * @property {string?} [identifierColumnName] - Table identifier field name => eg. "id" OR "email" - * @property {string?} [identifierValue] - Corresponding value of the selected field name => This - * checks identifies a the target row for "update" or "delete". Not needed for "insert" - * @property {string?} [duplicateColumnName] - Duplicate column name to check for - * @property {string?} [duplicateColumnValue] - Duplicate column value to match. If no "update" param - * provided, function will return null - * @property {boolean?} [update] - Should the "insert" action update the existing entry if indeed - * the entry with "duplicateColumnValue" exists? - */ - -/** - * @typedef {Object} LocalPostReturn - * @property {boolean} success - Did the function run successfully? - * @property { PostInsertReturn | Object[] | string } [payload] - GET request results - * @property {string} [msg] - Message - * @property {string} [error] - Error Message - */ - -/** - * @typedef {Object} LocalPostQueryObject - * @property {string | import("../../package-shared/types").PostDataPayload} query - Table Name - * @property {string} [tableName] - Table Name - * @property {string[]} [queryValues] - GET request results + * @typedef {GetSchemaRequestQuery & GetSchemaAPICredentialsParam} GetSchemaAPIParam */ diff --git a/package.json b/package.json index 211c04a..f2d8c4b 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "datasquirel", - "version": "2.2.7", + "version": "2.2.8", "description": "Cloud-based SQL data management tool", "main": "index.js", "bin": { diff --git a/tsconfig.json b/tsconfig.json index 392c662..d6d7cc2 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,7 +9,6 @@ // "skipLibCheck": true, "lib": ["dom", "dom.iterable", "esnext"], "allowJs": true, - "noEmit": true, "incremental": true, "resolveJsonModule": true, "isolatedModules": true, diff --git a/utils/get-schema.js b/utils/get-schema.js index c31314a..e10976f 100644 --- a/utils/get-schema.js +++ b/utils/get-schema.js @@ -22,17 +22,12 @@ const https = require("https"); */ /** - * Make a get request to Datasquirel API - * ============================================================================== - * @async - * - * @param {Object} params - Single object passed - * @param {string} params.key - `FULL ACCESS` API Key - * @param {string} [params.database] - The database schema to get + * # Get Schema for Database, table, or field * + * @param {import("../package-shared/types").GetSchemaAPIParam} params * * @returns { Promise } - Return Object */ -async function getSchema({ key, database }) { +async function getSchema({ key, database, field, table }) { const scheme = process.env.DSQL_HTTP_SCHEME; const localHost = process.env.DSQL_LOCAL_HOST; const localHostPort = process.env.DSQL_LOCAL_HOST_PORT; @@ -43,6 +38,15 @@ async function getSchema({ key, database }) { * @description make a request to datasquirel.com */ const httpResponse = await new Promise((resolve, reject) => { + /** @type {import("../package-shared/types").GetSchemaRequestQuery} */ + const queryObject = { database, field, table }; + let query = Object.keys(queryObject) + // @ts-ignore + .filter((k) => queryObject[k]) + // @ts-ignore + .map((k) => `${k}=${queryObject[k]}`) + .join("&"); + (scheme?.match(/^http$/i) ? http : https) .request( { @@ -55,7 +59,7 @@ async function getSchema({ key, database }) { hostname: localHost || "datasquirel.com", path: "/api/query/get-schema" + - (database ? `?database=${database}` : ""), + (query?.match(/./) ? `?${query}` : ""), }, /**