diff --git a/README.md b/README.md index ad0edc9..614d184 100644 --- a/README.md +++ b/README.md @@ -116,3 +116,29 @@ const postData = await datasquirel.uploadImage({ }, }); ``` + +### Local Querying + +You can query directly from an SQL database if you provide these environment variables in your `.env` file: + +```conf +DSQL_DB_HOST= +DSQL_DB_PORT= +DSQL_DB_USERNAME= +DSQL_DB_PASSWORD= +DSQL_DB_NAME= +DSQL_SSL_DIR= +``` + +The ssl directory **_must_** contain a file named `ca-cert.pem`. `DSQL_DB_PORT` defaults to **3306** if not provided. + +### Remote Querying + +You can query from a self hosted installation of datasquirel. Just add these environment variables: + +```conf +DSQL_API_REMOTE_HOST= +DSQL_API_REMOTE_HOST_PORT= +``` + +If these aren't provided it defaults to `datasquirel.com`. diff --git a/package-shared/functions/backend/dbHandler.js b/package-shared/functions/backend/dbHandler.js index 283ad90..2d7543f 100644 --- a/package-shared/functions/backend/dbHandler.js +++ b/package-shared/functions/backend/dbHandler.js @@ -6,7 +6,8 @@ const serverError = require("./serverError"); const mysql = require("serverless-mysql"); const path = require("path"); -const SSL_DIR = "/app/ssl"; +const SSL_DIR = + process.env.DSQL_SSL_DIR || path.resolve(__dirname, "../../../ssl"); const connection = mysql({ config: { diff --git a/package-shared/utils/backend/global-db/DB_HANDLER.js b/package-shared/utils/backend/global-db/DB_HANDLER.js index b6806bb..28d5cb3 100644 --- a/package-shared/utils/backend/global-db/DB_HANDLER.js +++ b/package-shared/utils/backend/global-db/DB_HANDLER.js @@ -4,7 +4,8 @@ const fs = require("fs"); const path = require("path"); const mysql = require("serverless-mysql"); -const SSL_DIR = "/app/ssl"; +const SSL_DIR = + process.env.DSQL_SSL_DIR || path.resolve(__dirname, "../../../../ssl"); const MASTER = mysql({ config: { @@ -12,7 +13,9 @@ const MASTER = mysql({ user: process.env.DSQL_DB_USERNAME, password: process.env.DSQL_DB_PASSWORD, database: process.env.DSQL_DB_NAME, - port: process.env.DB_PORT ? Number(process.env.DB_PORT) : undefined, + port: process.env.DSQL_DB_PORT + ? Number(process.env.DSQL_DB_PORT) + : undefined, charset: "utf8mb4", ssl: { ca: fs.readFileSync(`${SSL_DIR}/ca-cert.pem`), diff --git a/package-shared/utils/backend/global-db/DSQL_USER_DB_HANDLER.js b/package-shared/utils/backend/global-db/DSQL_USER_DB_HANDLER.js index 559fae7..9a222a3 100644 --- a/package-shared/utils/backend/global-db/DSQL_USER_DB_HANDLER.js +++ b/package-shared/utils/backend/global-db/DSQL_USER_DB_HANDLER.js @@ -5,7 +5,8 @@ const path = require("path"); const mysql = require("serverless-mysql"); -const SSL_DIR = "/app/ssl"; +const SSL_DIR = + process.env.DSQL_SSL_DIR || path.resolve(__dirname, "../../../../ssl"); let DSQL_USER = mysql({ config: { diff --git a/package-shared/utils/backend/global-db/NO_DB_HANDLER.js b/package-shared/utils/backend/global-db/NO_DB_HANDLER.js index 1a867a1..1357401 100644 --- a/package-shared/utils/backend/global-db/NO_DB_HANDLER.js +++ b/package-shared/utils/backend/global-db/NO_DB_HANDLER.js @@ -14,7 +14,8 @@ const path = require("path"); const mysql = require("serverless-mysql"); -const SSL_DIR = "/app/ssl"; +const SSL_DIR = + process.env.DSQL_SSL_DIR || path.resolve(__dirname, "../../../../ssl"); let NO_DB = mysql({ config: { diff --git a/package-shared/utils/backend/global-db/ROOT_DB_HANDLER.js b/package-shared/utils/backend/global-db/ROOT_DB_HANDLER.js index d18613c..c5383ad 100644 --- a/package-shared/utils/backend/global-db/ROOT_DB_HANDLER.js +++ b/package-shared/utils/backend/global-db/ROOT_DB_HANDLER.js @@ -5,7 +5,8 @@ const path = require("path"); const mysql = require("serverless-mysql"); -const SSL_DIR = "/app/ssl"; +const SSL_DIR = + process.env.DSQL_SSL_DIR || path.resolve(__dirname, "../../../../ssl"); let NO_DB = mysql({ config: { diff --git a/package.json b/package.json index aefac5e..1c38e3e 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "datasquirel", - "version": "2.3.6", + "version": "2.3.7", "description": "Cloud-based SQL data management tool", "main": "index.js", "bin": { diff --git a/utils/get.js b/utils/get.js index b8b9e44..a756087 100644 --- a/utils/get.js +++ b/utils/get.js @@ -49,17 +49,13 @@ async function get({ key, db, query, queryValues, tableName }) { * * @description Look for local db settings in `.env` file and by pass the http request if available */ - const { - DSQL_DB_HOST, - DSQL_DB_USERNAME, - DSQL_MARIADB_ROOT_PASSWORD, - DSQL_DB_NAME, - } = process.env; + const { DSQL_DB_HOST, DSQL_DB_USERNAME, DSQL_DB_PASSWORD, DSQL_DB_NAME } = + process.env; if ( DSQL_DB_HOST?.match(/./) && DSQL_DB_USERNAME?.match(/./) && - DSQL_MARIADB_ROOT_PASSWORD?.match(/./) && + DSQL_DB_PASSWORD?.match(/./) && DSQL_DB_NAME?.match(/./) ) { /** @type {import("../package-shared/types").DSQL_DatabaseSchemaType | undefined} */