Go to file
Benjamin Toby c6964df00b Updates
2024-11-12 06:47:31 +01:00
client Bugfix 2024-11-08 18:28:55 +01:00
engine Remove SSL directory 2024-11-08 20:15:37 +01:00
functions Updates 2024-11-12 06:47:31 +01:00
package-shared Updates 2024-11-12 06:47:31 +01:00
users Add .d.ts files 2024-11-08 16:44:31 +01:00
utils Add .d.ts files 2024-11-08 16:44:31 +01:00
.gitignore Remove SSL directory 2024-11-08 20:15:37 +01:00
.npmrc Update Registry in README.md 2024-11-08 23:08:59 +01:00
console-colors.js updates 2023-09-21 15:00:04 +01:00
index.d.ts Add Feature: SQL query generator 2024-11-08 19:54:14 +01:00
index.js Add Feature: SQL query generator 2024-11-08 19:54:14 +01:00
package-lock.json Bugfix 2024-11-08 18:28:55 +01:00
package.json Update Types 2024-11-09 07:09:00 +01:00
publish.sh Update publish script 2024-11-08 16:45:41 +01:00
README.md Update Registry in README.md 2024-11-08 23:08:59 +01:00
tsconfig.json Update Types 2024-11-08 16:41:22 +01:00

Datasquirel

This package requires an account with datasquirel, or a self hosted instance of the datasquirel web admin with your URL. You can also use the SQL query aspect if you provide needed credentials for a database connection, however in this instance the static files feature is disabled.

Installation

First add the npm registry to your .npmrc file:

@moduletrace:registry=https://git.tben.me/api/packages/moduletrace/npm/

After setting up the registry you can install the package by running

npm install @moduletrace/datasquirel

Once the package is installed, you can import the library using require approach:

const datasquirel = require("@moduletrace/datasquirel");

Or you can use ES6 module imports:

import datasquirel from "@moduletrace/datasquirel";

Usage

Fetch Data

This method requires a readonly key or fullaccess API key gotten from datasquirel. It uses a basic https get request paired with some query params.

const datasquirel = require("@moduletrace/datasquirel");

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
});

Datasquirel uses all conventional SQL query commands. However you can only use the SELECT command when using a readonly API key.

Post Data

This method requires a fullaccess API key gotten from datasquirel. You can perform a basic fetch with this method, as well as more complex operations like UPDATE, DELETE and INSERT.

const datasquirel = require("@moduletrace/datasquirel");

const postData = await datasquirel.post({
    key: "aldhkf89asdflksdafh908asdfjkhasdf", // Fullaccess API Key
    payload: {
        action: "insert", // OR "update" OR "delete" OR "select"
        data: {
            user_id: "19aisdn123",
            user_first_name: "John",
            user_last_name: "Doe",
        },
        table: "users",
    },
});

You can simply replace the payload object with an SQL string and it does everything you provide in the SQL command.

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'",
});

You can add a condition to the payload object to filter the results

const datasquirel = require("@moduletrace/datasquirel");

const postData = await datasquirel.post({
    key: process.env.FULL_ACCESS_API_KEY,
    payload: {
        action: "delete",
        condition: `WHERE user_id='21adwei9jewr' AND type='buyers'`,
        table: "users",
    },
});

You can use identifierColumnName and identifierValue when updating an entry.

const datasquirel = require("@moduletrace/datasquirel");

const postData = await datasquirel.post({
    key: process.env.FULL_ACCESS_API_KEY,
    payload: {
        action: "update",
        table: "users",
        identifierColumnName: "id",
        identifierValue: "21adwei9jewr",
        data: {
            first_name: "Mary",
            last_name: "Spencer",
        },
    },
});

Upload Image

This method requires is similar to the post method, but with different parameters.

const datasquirel = require("@moduletrace/datasquirel");

const postData = await datasquirel.uploadImage({
    key: process.env.FULL_ACCESS_API_KEY,
    payload: {
        imageData: "6ejsiua2i29ndsajkfn9n==", // Image in base64
        imageName: `awesome-waterfalls`,
        mimeType: "jpg", // optional
        thumbnailSize: 120, // optional === This measurement is in pixels(px)
    },
});

Local Querying

You can query directly from an SQL database if you provide these environment variables in your .env file:

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:

DSQL_API_REMOTE_HOST=
DSQL_API_REMOTE_HOST_PORT=

If these aren't provided it defaults to datasquirel.com.