npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@stofloos/stofware-client

v1.0.16

Published

A client SDK for interacting with the Stofware API.

Downloads

250

Readme

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.