1.0.16 • Published 2 months ago

@stofloos/stofware-client v1.0.16

Weekly downloads
-
License
MIT
Repository
-
Last release
2 months ago

StofwareClient

The StofwareClient is a TypeScript SDK to interact with the Stofware API. It provides comprehensive functionalities for CRUD operations, data aggregation, and interactions with views.

Features

  • Seamless CRUD operations on models.
  • Aggregation functions for both models and views.
  • Access to database views.
  • JWT token-based authentication.
  • Query filter chaining and fluent API design.

Installation

Before you publish to npm:

npm install @stofloos/stofware-client

Usage

Initialization

Import and initialize StofwareClient:

import { StofwareClient } from "@stofloos/stofware-client";

const client = new StofwareClient("<BASE_URL>", "<OPTIONAL_JWT_TOKEN>");

Set or Update JWT Token

To set or update the JWT token:

client.setToken("<YOUR_JWT_TOKEN>");

CRUD Operations on Models

  1. Fetch data using setFilter:

    let filterGroup: QueryParametersFilterGroup = {
     operator: BooleanOperator.And,
     items: [
         {
             name: 'fieldName1',
             operator: QueryOperator.Equals,
             value: 'value1'
         }
     ],
     groups: [
         {
             operator: BooleanOperator.Or,
             items: [
                 {
                     name: 'fieldName2',
                     operator: QueryOperator.Equals,
                     value: 'value2'
                 },
                 {
                     name: 'fieldName3',
                     operator: QueryOperator.Equals,
                     value: 'value3'
                 }
             ]
         }
     ]
    };
    const data = client.model("entityName").setFilter(filterGroup).page(1).pageLimit(10).orderBy("fieldName", "DESC").getAll();
  2. Fetch data using appendFilter:

    const data = client.model("entityName").appendFilter("fieldName", "operator", "value", "AND").page(1).pageLimit(10).orderBy("fieldName", "DESC").getAll();
  3. (Deprecated) Fetch data using filters:

    const data = client.model("entityName").filter("fieldName", "EQ", "value").page(1).pageLimit(10).orderBy("fieldName", "DESC").getAll();
  4. Aggregation on models:

    const aggregatedData = client.model("entityName").aggregate(
    	[
    		{ field: "price", function: "sum" },
    		{ field: "quantity", function: "count" },
    	],
    	{ groupBy: "created_at", groupByFormat: "month" }
    );
  5. Create a new record:

    client.model("entityName").post({ ...data });
  6. Update a record:

    client.model("entityName").put(1, { ...data });
  7. Delete a record:

    client.model("entityName").delete(1);

Operations on Views

  1. Fetch data from a view with setFilter:

    let filterGroup: QueryParametersFilterGroup = {
     operator: BooleanOperator.And,
     items: [
         {
             name: 'fieldName1',
             operator: QueryOperator.Equals,
             value: 'value1'
         }
     ],
     groups: [
         {
             operator: BooleanOperator.Or,
             items: [
                 {
                     name: 'fieldName2',
                     operator: QueryOperator.Equals,
                     value: 'value2'
                 },
                 {
                     name: 'fieldName3',
                     operator: QueryOperator.Equals,
                     value: 'value3'
                 }
             ]
         }
     ]
    };
    const data = client.model("viewName").setFilter(filterGroup).page(1).pageLimit(10).orderBy("fieldName", "DESC").getAll();
  2. Fetch data from a view with appendFilter:

    const data = client.model("viewName").appendFilter("fieldName", "operator", "value", "AND").page(1).pageLimit(10).orderBy("fieldName", "DESC").getAll();
  3. Fetch data from a view with filters:

    const viewData = client.view("viewName").filter("fieldName", "EQ", "value").orderBy("fieldName", "DESC").page(1).pageLimit(10).getAll();
  4. Aggregation on views:

    const viewAggregatedData = client.view("viewName").aggregate(
    	[
    		{ field: "price", function: "sum" },
    		{ field: "quantity", function: "count" },
    	],
    	{ groupBy: "company_name" }
    );

Enhanced Data Functions

EnhancedDataFunctions is a set of utility methods to provide more powerful and flexible operations on data arrays, similar to how tools like Pandas allow manipulation and operations on dataframes. The provided functions are pctChange, compareWith, add, sub, mul, div, and the ability to set custom indices via setIndex.

Setting Up

Firstly, wrap your dataset with enhanceData function:

const enhancedData2023 = enhanceData(aggregatedData2023);

Setting a Custom Index

To better target specific rows for operations, you can set a custom index. By default, the index is set to id.

enhancedData2023.setIndex("organisation");

Percentage Change

Compute the percentage change between the current and previous element:

const result = enhancedData2023.pctChange(["sales", "revenue"]);

This will generate new columns like salesPctChange and revenuePctChange.

Comparison with Another Dataset

To compare two datasets:

const result = enhancedData2023.compareWith(otherData2023, ["sales", "revenue"]);

This will produce new columns like salesCompared and revenueCompared.

Mathematical Operations

You can perform element-wise mathematical operations on the datasets:

// Addition
const added = enhancedData2023.add(otherData2023, ["sales", "revenue"]);

// Subtraction
const subtracted = enhancedData2023.sub(otherData2023, ["sales", "revenue"]);

// Multiplication
const multiplied = enhancedData2023.mul(otherData2023, ["sales", "revenue"]);

// Division
const divided = enhancedData2023.div(otherData2023, ["sales", "revenue"]);

Each operation will work on the specified columns and return the result in the same columns.

Chaining Operations

One of the powerful features is the ability to chain multiple operations:

const result = enhancedData2023.pctChange(["sales"]).add(otherData2023, ["revenue"]).compareWith(yetAnotherData2023, ["revenue"]);

The above will first compute the percentage change for sales, then add the revenue from otherData2023, and finally compare revenue with yetAnotherData2023.


License

This SDK is under the MIT license. See the LICENSE file for more details.

1.0.16

2 months ago

1.0.15

2 months ago

1.0.14

2 months ago

1.0.13

3 months ago

1.0.12

3 months ago

1.0.11

3 months ago

1.0.10

3 months ago

1.0.9

7 months ago

1.0.8

8 months ago

1.0.7

8 months ago

1.0.6

9 months ago

1.0.5

9 months ago

1.0.4

9 months ago

1.0.3

9 months ago

1.0.2

9 months ago

1.0.1

9 months ago

1.0.0

9 months ago