Major refactoring: Add user id to API routes

This commit is contained in:
Benjamin Toby 2024-11-27 11:02:03 +01:00
parent a529d0bd08
commit b047d6557c
21 changed files with 144 additions and 49 deletions

View File

@ -41,9 +41,12 @@ const getData = await datasquirel.get({
key: "aldhkf89asdflksdafh908asdfjkhasdf", // Readonly API Key
db: "my_database", // Database name slug (Eg. Db Name => My Database, Db Slug => my_database)
query: "SELECT * FROM blog_posts", // SQL Query
user_id: 129, // Your User Id: check settings page
});
```
> NOTE: You can skip the `user_id` parameter by adding an environment variable named `DSQL_API_USER_ID`
Datasquirel uses all conventional SQL query commands. However you can only use the `SELECT` command when using a readonly API key.
### Post Data
@ -63,6 +66,7 @@ const postData = await datasquirel.post({
user_last_name: "Doe",
},
table: "users",
user_id: 271,
},
});
```
@ -75,6 +79,7 @@ const datasquirel = require("@moduletrace/datasquirel");
const postData = await datasquirel.post({
key: process.env.FULL_ACCESS_API_KEY,
payload: "SELECT * FROM blog_posts WHERE user_id='as09d7nasd90'",
user_id: 271,
});
```
@ -90,6 +95,7 @@ const postData = await datasquirel.post({
condition: `WHERE user_id='21adwei9jewr' AND type='buyers'`,
table: "users",
},
user_id: 271,
});
```
@ -110,6 +116,7 @@ const postData = await datasquirel.post({
last_name: "Spencer",
},
},
user_id: 271,
});
```
@ -128,6 +135,7 @@ const postData = await datasquirel.uploadImage({
mimeType: "jpg", // optional
thumbnailSize: 120, // optional === This measurement is in pixels(px)
},
user_id: 271,
});
```

View File

@ -39,7 +39,7 @@ async function updateApiSchemaFromLocalDb() {
const key = process.env.DSQL_KEY || "";
const dbSchema = JSON.parse(fs.readFileSync(dbSchemaPath, "utf8"));
const { host, port, scheme } = grabHostNames();
const { host, port, scheme, user_id } = grabHostNames();
/**
* Make https request
@ -79,7 +79,7 @@ async function updateApiSchemaFromLocalDb() {
},
port,
hostname: host,
path: `/api/query/update-schema-from-single-database`,
path: `/api/query/${user_id}/update-schema-from-single-database`,
},
/**

View File

@ -178,7 +178,11 @@ function sqlGenerator({ tableName, genObject }) {
}
if (genObject.order)
queryString += ` ORDER BY ${genObject.order.field} ${genObject.order.strategy}`;
queryString += ` ORDER BY ${
genObject.join
? `${tableName}.${genObject.order.field}`
: genObject.order.field
} ${genObject.order.strategy}`;
if (genObject.limit) queryString += ` LIMIT ${genObject.limit}`;
return {

View File

@ -4,8 +4,9 @@ const fs = require("fs");
const decrypt = require("./decrypt");
/** @type {import("../../types").CheckApiCredentialsFn} */
const grabApiCred = ({ key, database, table }) => {
const grabApiCred = ({ key, database, table, user_id }) => {
if (!key) return null;
if (!user_id) return null;
try {
const allowedKeysPath = process.env.DSQL_API_KEYS_PATH;
@ -22,6 +23,8 @@ const grabApiCred = ({ key, database, table }) => {
`${allowedKeysPath}/${ApiObject.sign}`
);
if (String(ApiObject.user_id) !== String(user_id)) return null;
if (!isApiKeyValid) return null;
if (!ApiObject.target_database) return ApiObject;
if (!database && ApiObject.target_database) return null;

View File

@ -335,6 +335,7 @@ export interface GetSchemaRequestQuery {
database?: string;
table?: string;
field?: string;
user_id?: string | number;
}
export interface GetSchemaAPICredentialsParam {
@ -1095,6 +1096,7 @@ export type CheckApiCredentialsFnParam = {
key?: string;
database?: string;
table?: string;
user_id?: string | number;
};
export type FetchApiFn = (
@ -1218,3 +1220,12 @@ export type SqlGeneratorFn = (Param0: {
values: string[];
}
| undefined;
export type ApiConnectBody = {
url: string;
key: string;
database: DSQL_MYSQL_user_databases_Type;
dbSchema: DSQL_DatabaseSchemaType;
type: "pull" | "push";
user_id?: string | number;
};

View File

@ -8,6 +8,7 @@ const http = require("http");
* @property {string} host
* @property {number | string} port
* @property {typeof http | typeof https} scheme
* @property {string | number} user_id
*/
/**
@ -29,6 +30,7 @@ function grabHostNames() {
host: remoteHost || localHost || "datasquirel.com",
port: remoteHostPort || localHostPort || 443,
scheme: scheme?.match(/^http$/i) ? http : https,
user_id: String(process.env.DSQL_API_USER_ID || 0),
};
}

View File

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

View File

@ -12,12 +12,13 @@ const grabHostNames = require("../package-shared/utils/grab-host-names");
* ==============================================================================
* @async
*
* @param {object} props - Single object passed
* @param {string} props.key - FULL ACCESS API Key
* @param {string} props.database - Database Name
* @param {import("../package-shared/types").UserDataPayload} props.payload - User Data Payload
* @param {string} props.encryptionKey
* @param {string} [props.encryptionSalt]
* @param {object} param - Single object passed
* @param {string} param.key - FULL ACCESS API Key
* @param {string} param.database - Database Name
* @param {import("../package-shared/types").UserDataPayload} param.payload - User Data Payload
* @param {string} param.encryptionKey
* @param {string} [param.encryptionSalt]
* @param {boolean} [param.user_id] - User ID
*
* @returns { Promise<import("../package-shared/types").AddUserFunctionReturn> }
*/
@ -27,6 +28,7 @@ async function addUser({
database,
encryptionKey,
encryptionSalt,
user_id,
}) {
/**
* Check for local DB settings
@ -43,7 +45,8 @@ async function addUser({
DSQL_FULL_SYNC,
} = process.env;
const { host, port, scheme } = grabHostNames();
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
if (
DSQL_HOST?.match(/./) &&
@ -97,7 +100,9 @@ async function addUser({
},
port,
hostname: host,
path: `/api/user/add-user`,
path: `/api/user/${
user_id || grabedHostNames.user_id
}/add-user`,
},
/**

View File

@ -30,10 +30,11 @@ const grabHostNames = require("../package-shared/utils/grab-host-names");
* @param {String} params.database - Target Database
* @param {number} params.userId - user id
* @param {string[]} [params.fields] - fields to select
* @param {boolean} [params.user_id] - User ID
*
* @returns { Promise<import("../package-shared/types").GetUserFunctionReturn>}
*/
async function getUser({ key, userId, database, fields }) {
async function getUser({ key, userId, database, fields, user_id }) {
/**
* Initialize
*/
@ -63,7 +64,8 @@ async function getUser({ key, userId, database, fields }) {
fields: [...new Set(updatedFields)],
});
const { host, port, scheme } = grabHostNames();
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
/**
* Check for local DB settings
@ -125,7 +127,9 @@ async function getUser({ key, userId, database, fields }) {
},
port,
hostname: host,
path: `/api/user/get-user`,
path: `/api/user/${
user_id || grabedHostNames.user_id
}/get-user`,
},
/**

View File

@ -41,6 +41,7 @@ const grabHostNames = require("../package-shared/utils/grab-host-names");
* @param {string} [params.email_login_code] - Email login code
* @param {string} [params.temp_code_field] - Database table field name for temporary code
* @param {boolean} [params.token] - Send access key as part of response body?
* @param {boolean} [params.user_id] - User ID
*
* @returns { Promise<import("../package-shared/types").AuthenticatedUser>}
*/
@ -56,8 +57,10 @@ async function loginUser({
email_login_code,
temp_code_field,
token,
user_id,
}) {
const { host, port, scheme } = grabHostNames();
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
const defaultTempLoginFieldName = "temp_login_code";
const emailLoginTempCodeFieldName = email_login
@ -183,7 +186,9 @@ async function loginUser({
},
port,
hostname: host,
path: `/api/user/login-user`,
path: `/api/user/${
user_id || grabedHostNames.user_id
}/login-user`,
},
/**

View File

@ -38,6 +38,7 @@ const grabHostNames = require("../package-shared/utils/grab-host-names");
* @param {String} params.encryptionSalt - Encryption Salt
* @param {string[]} [params.additionalFields] - Additional Fields to be added to the user object
* @param {string} [params.token] - access token to use instead of getting from cookie header
* @param {boolean} [params.user_id] - User ID
*
* @returns { Promise<import("../package-shared/types").ReauthUserFunctionReturn> }
*/
@ -51,13 +52,15 @@ async function reauthUser({
encryptionSalt,
additionalFields,
token,
user_id,
}) {
/**
* Check Encryption Keys
*
* @description Check Encryption Keys
*/
const { host, port, scheme } = grabHostNames();
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
const existingUser = userAuth({
database,
@ -146,7 +149,9 @@ async function reauthUser({
},
port,
hostname: host,
path: `/api/user/reauth-user`,
path: `/api/user/${
user_id || grabedHostNames.user_id
}/reauth-user`,
},
/**

View File

@ -39,6 +39,7 @@ const grabHostNames = require("../package-shared/utils/grab-host-names");
* @param {string} [params.mail_password]
* @param {number} [params.mail_port]
* @param {string} [params.sender]
* @param {boolean} [params.user_id] - User ID
*
* @returns { Promise<boolean>}
*/
@ -54,8 +55,10 @@ async function sendEmailCode({
mail_username,
mail_port,
sender,
user_id,
}) {
const { host, port, scheme } = grabHostNames();
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
const defaultTempLoginFieldName = "temp_login_code";
const emailLoginTempCodeFieldName = temp_code_field
@ -176,7 +179,9 @@ async function sendEmailCode({
},
port,
hostname: host,
path: `/api/user/send-email-code`,
path: `/api/user/${
user_id || grabedHostNames.user_id
}/send-email-code`,
},
/**

View File

@ -45,6 +45,7 @@ const grabHostNames = require("../../package-shared/utils/grab-host-names");
* @param {string} params.encryptionKey - Encryption key
* @param {string} params.encryptionSalt - Encryption salt
* @param {object} [params.additionalFields] - Additional Fields to be added to the user object
* @param {boolean} [params.user_id] - User ID
*
* @returns { Promise<FunctionReturn | undefined> }
*/
@ -59,13 +60,15 @@ async function githubAuth({
encryptionKey,
encryptionSalt,
additionalFields,
user_id,
}) {
/**
* Check inputs
*
* @description Check inputs
*/
const { host, port, scheme } = grabHostNames();
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
if (!key || key?.match(/ /)) {
return {
@ -205,7 +208,9 @@ async function githubAuth({
},
port,
hostname: host,
path: `/api/user/github-login`,
path: `/api/user/${
user_id || grabedHostNames.user_id
}/github-login`,
},
/**

View File

@ -43,6 +43,7 @@ const grabHostNames = require("../../package-shared/utils/grab-host-names");
* @param {string} params.encryptionKey - Encryption key
* @param {string} params.encryptionSalt - Encryption salt
* @param {object} [params.additionalFields] - Additional Fields to be added to the user object
* @param {boolean} [params.user_id] - User ID
*
* @returns { Promise<FunctionReturn> }
*/
@ -55,8 +56,10 @@ async function googleAuth({
encryptionKey,
encryptionSalt,
additionalFields,
user_id,
}) {
const { host, port, scheme } = grabHostNames();
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
/**
* Check inputs
@ -201,7 +204,9 @@ async function googleAuth({
},
port,
hostname: host,
path: `/api/user/google-login`,
path: `/api/user/${
user_id || grabedHostNames.user_id
}/google-login`,
},
/**

View File

@ -15,10 +15,11 @@ const grabHostNames = require("../package-shared/utils/grab-host-names");
* @param {String} params.key - API Key
* @param {String} params.database - Target Database
* @param {{ id: number } & Object.<string, any>} params.payload - User Object: ID is required
* @param {boolean} [params.user_id] - User ID
*
* @returns { Promise<import("../package-shared/types").UpdateUserFunctionReturn>}
*/
async function updateUser({ key, payload, database }) {
async function updateUser({ key, payload, database, user_id }) {
/**
* Check for local DB settings
*
@ -34,7 +35,8 @@ async function updateUser({ key, payload, database }) {
DSQL_FULL_SYNC,
} = process.env;
const { host, port, scheme } = grabHostNames();
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
if (
DSQL_HOST?.match(/./) &&
@ -85,7 +87,9 @@ async function updateUser({ key, payload, database }) {
},
port,
hostname: host,
path: `/api/user/update-user`,
path: `/api/user/${
user_id || grabedHostNames.user_id
}/update-user`,
},
/**

View File

@ -21,11 +21,13 @@ const grabHostNames = require("../package-shared/utils/grab-host-names");
* @param {Object} params - Single Param object containing params
* @param {String} params.key - *FULL ACCESS API Key
* @param { string } params.url - File URL
* @param { string | number } [params.user_id]
*
* @returns { Promise<FunctionReturn> } - Image Url
*/
async function uploadImage({ key, url }) {
const { host, port, scheme } = grabHostNames();
async function deleteFile({ key, url, user_id }) {
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
try {
/**
@ -49,7 +51,9 @@ async function uploadImage({ key, url }) {
},
port,
hostname: host,
path: `/api/query/delete-file`,
path: `/api/query/${
user_id || grabedHostNames.user_id
}/delete-file`,
},
/**
@ -90,4 +94,4 @@ async function uploadImage({ key, url }) {
}
}
module.exports = uploadImage;
module.exports = deleteFile;

View File

@ -16,8 +16,9 @@ const grabHostNames = require("../package-shared/utils/grab-host-names");
*
* @returns { Promise<GetSchemaReturn> } - Return Object
*/
async function getSchema({ key, database, field, table }) {
const { host, port, scheme } = grabHostNames();
async function getSchema({ key, database, field, table, user_id }) {
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
/**
* Make https request
@ -48,8 +49,9 @@ async function getSchema({ key, database, field, table }) {
port,
hostname: host,
path:
"/api/query/get-schema" +
(query?.match(/./) ? `?${query}` : ""),
`/api/query/${
user_id || grabedHostNames.user_id
}/get-schema` + (query?.match(/./) ? `?${query}` : ""),
},
/**

View File

@ -20,11 +20,21 @@ const grabHostNames = require("../package-shared/utils/grab-host-names");
* @param {string[]} [params.queryValues] - An array of query values if using "?" placeholders
* @param {string} [params.tableName] - Name of the table to query
* @param {boolean} [params.useLocal] - Whether to use a remote database instead of API
* @param {boolean} [params.user_id] - User ID
*
* @returns { Promise<import("../package-shared/types").GetReturn> } - Return Object
*/
async function get({ key, db, query, queryValues, tableName, useLocal }) {
const { host, port, scheme } = grabHostNames();
async function get({
key,
db,
query,
queryValues,
tableName,
useLocal,
user_id,
}) {
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
/**
* Check for local DB settings
@ -83,7 +93,9 @@ async function get({ key, db, query, queryValues, tableName, useLocal }) {
const queryString = serializeQuery({ query: queryObject });
let path = `/api/query/get${queryString}`;
let path = `/api/query/${
user_id || grabedHostNames.user_id
}/get${queryString}`;
/** @type {https.RequestOptions} */
const requestObject = {

View File

@ -16,6 +16,7 @@ const grabHostNames = require("../package-shared/utils/grab-host-names");
* @param {any[]} [params.queryValues] - Query Values if using "?" placeholders
* @param {string} [params.tableName] - Name of the table to query
* @param {boolean} [params.useLocal] - Whether to use a remote database instead of API
* @param {boolean} [params.user_id] - User ID
*
* @returns { Promise<import("../package-shared/types").PostReturn> } - Return Object
*/
@ -26,8 +27,10 @@ async function post({
database,
tableName,
useLocal,
user_id,
}) {
const { host, port, scheme } = grabHostNames();
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
/**
* Check for local DB settings
@ -106,7 +109,7 @@ async function post({
},
port,
hostname: host,
path: `/api/query/post`,
path: `/api/query/${user_id || grabedHostNames.user_id}/post`,
},
/**

View File

@ -24,13 +24,15 @@ const grabHostNames = require("../package-shared/utils/grab-host-names");
* fileName: string,
* mimeType?: string,
* folder?: string,
* isPrivate?: boolean,
* isPrivate?: boolean
* }} params.payload - Image Data Eg.
* @param {boolean} [params.user_id] - User ID
*
* @returns { Promise<FunctionReturn> } - Return Object
*/
async function uploadImage({ key, payload }) {
const { host, port, scheme } = grabHostNames();
async function uploadImage({ key, payload, user_id }) {
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
try {
/**
@ -54,7 +56,9 @@ async function uploadImage({ key, payload }) {
},
port,
hostname: host,
path: `/api/query/add-file`,
path: `/api/query/${
user_id || grabedHostNames.user_id
}/add-file`,
},
/**

View File

@ -28,11 +28,13 @@ const grabHostNames = require("../package-shared/utils/grab-host-names");
* folder?: string,
* isPrivate?: boolean,
* }} params.payload - Image Data Eg.
* @param {boolean} [params.user_id] - User ID
*
* @returns { Promise<FunctionReturn> } - Return Object
*/
async function uploadImage({ key, payload }) {
const { host, port, scheme } = grabHostNames();
async function uploadImage({ key, payload, user_id }) {
const grabedHostNames = grabHostNames();
const { host, port, scheme } = grabedHostNames;
try {
/**
@ -56,7 +58,9 @@ async function uploadImage({ key, payload }) {
},
port,
hostname: host,
path: `/api/query/add-image`,
path: `/api/query/${
user_id || grabedHostNames.user_id
}/add-image`,
},
/**