datasquirel/README.md

167 lines
4.8 KiB
Markdown
Raw Permalink Normal View History

2023-09-21 14:00:04 +00:00
# Datasquirel
2024-11-08 21:58:25 +00:00
This package requires an account with [datasquirel](https://datasquirel.com/create-account), 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.
2023-09-21 14:00:04 +00:00
2024-07-13 06:29:40 +00:00
## Installation
2023-09-21 14:00:04 +00:00
2024-11-08 21:58:25 +00:00
First add the npm registry to your .npmrc file:
```bash
2024-11-08 22:08:59 +00:00
@moduletrace:registry=https://git.tben.me/api/packages/moduletrace/npm/
2024-11-08 21:58:25 +00:00
```
After setting up the registry you can install the package by running
2023-09-21 14:00:04 +00:00
```bash
2024-11-08 21:58:25 +00:00
npm install @moduletrace/datasquirel
2023-09-21 14:00:04 +00:00
```
Once the package is installed, you can import the library using `require` approach:
```js
2024-11-08 21:58:25 +00:00
const datasquirel = require("@moduletrace/datasquirel");
```
Or you can use ES6 module imports:
```js
import datasquirel from "@moduletrace/datasquirel";
2023-09-21 14:00:04 +00:00
```
## Usage
### Fetch Data
This method requires a readonly key or fullaccess API key gotten from [datasquirel](https://datasquirel.com/). It uses a basic https get request paired with some query params.
```js
2024-11-08 21:58:25 +00:00
const datasquirel = require("@moduletrace/datasquirel");
2023-09-21 14:00:04 +00:00
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
2023-09-21 14:00:04 +00:00
});
```
> NOTE: You can skip the `user_id` parameter by adding an environment variable named `DSQL_API_USER_ID`
2023-09-21 14:00:04 +00:00
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](https://datasquirel.com/). You can perform a basic fetch with this method, as well as more complex operations like `UPDATE`, `DELETE` and `INSERT`.
```js
2024-11-08 21:58:25 +00:00
const datasquirel = require("@moduletrace/datasquirel");
2023-09-21 14:00:04 +00:00
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",
user_id: 271,
2023-09-21 14:00:04 +00:00
},
});
```
You can simply replace the `payload` object with an SQL string and it does everything you provide in the SQL command.
```js
2024-11-08 21:58:25 +00:00
const datasquirel = require("@moduletrace/datasquirel");
2023-09-21 14:00:04 +00:00
const postData = await datasquirel.post({
key: process.env.FULL_ACCESS_API_KEY,
payload: "SELECT * FROM blog_posts WHERE user_id='as09d7nasd90'",
user_id: 271,
2023-09-21 14:00:04 +00:00
});
```
You can add a condition to the `payload` object to filter the results
```js
2024-11-08 21:58:25 +00:00
const datasquirel = require("@moduletrace/datasquirel");
2023-09-21 14:00:04 +00:00
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",
},
user_id: 271,
2023-09-21 14:00:04 +00:00
});
```
You can use `identifierColumnName` and `identifierValue` when updating an entry.
```js
2024-11-08 21:58:25 +00:00
const datasquirel = require("@moduletrace/datasquirel");
2023-09-21 14:00:04 +00:00
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",
},
},
user_id: 271,
2023-09-21 14:00:04 +00:00
});
```
### Upload Image
This method requires is similar to the `post` method, but with different parameters.
```js
2024-11-08 21:58:25 +00:00
const datasquirel = require("@moduletrace/datasquirel");
2023-09-21 14:00:04 +00:00
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)
},
user_id: 271,
2023-09-21 14:00:04 +00:00
});
```
2024-11-06 09:30:00 +00:00
### 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`.