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

@appku/stashku-sql

v0.8.4

Published

A SQL storage engine for StashKu. Allows RESTful queries to popular SQL databases.

Downloads

43

Readme

NPM License Downloads

StashKu SQL Engine

Configuration

This engine utilizes environmental variables and configuration objects that are passed from StashKu. StashKu will pass any configuration object with the same engine name to the loaded engine itself (as shown in the example below).

let sku = new StashKu({
    engine: '@append/stashku-sql'
    //below, we put all engine-specific configuration in an object with the same name
    '@append/stashku-sql': {
        host: 'localhost',
        post: 1433,
        auth: {
            user: 'first.last',
            password: 'supersecret'
        },
        options: {
            appName: 'my cool app'
        }
    }
});
...

Available Properties

| Property | ENV | Type | Default | Description | |-|-|-|-|-| | driver | STASHKU_SQL_DRIVER | String | "sql-server" | Currently only "sql-server" is supported. | | host | STASHKU_SQL_HOST | String | "localhost" | The network host name of the database server. | | port | STASHKU_SQL_PORT | Number | 1433 | The network port listening on the host. | | database | STASHKU_SQL_DATABASE | String | "localhost" | The database to target. | | encrypt | STASHKU_SQL_ENCRYPT | Boolean | false | A flag indicating whether the connection to the database should be encrypted (if supported) | | trust | STASHKU_SQL_TRUST | Boolean | false | Enable or disable the forced trusting of the server certificate (such as self-signed). | | auth | | Object | | | ↳ auth.type | STASHKU_SQL_AUTH_TYPE | String | "default" | The authentication type to use with the database. For the sql-server driver, this matches the tedious auth types, commonly "ntlm" or "azure-active-directory-password". | | ↳ auth.user | STASHKU_SQL_AUTH_USER | String | | The name of the user used to authenticate against the database server. | | ↳ auth.password | STASHKU_SQL_AUTH_PASSWORD | String | | The user password. | | ↳ auth.domain | STASHKU_SQL_AUTH_DOMAIN | String | | The FQDN of the NTLM/AD domain the user is under. | | batch | | Object | | | ↳ batch.enabled | | Boolean | false | A flag that toggles whether batching should be enabled whenever supported for put and post operations. | | ↳ batch.size | | Number | 100 | The maximum number of queries to include in a single batch chunk. The entire batch will be segmented into chunks - one chunk per call to the database. Typically each object in a put or post generates one query. | | log | | Object | | | ↳ log.queries | STASHKU_SQL_LOG_QUERIES | Boolean | false | A flag to enable or disable the output of generated SQL queries to the debug-severity log. | | ↳ log.sensitive | STASHKU_SQL_LOG_SENSITIVE | Boolean | false | Indicates that SQL query parameters and values will be output in the debug-severity log. These may contain sensitive information, and should only be enabled for debugging purposes. | | pool | | Object | | | ↳ pool.min | | Number | 0 | The minimum number of connections to keep open. | | ↳ pool.max | | Number | 10 | The maximum number of connections to have open at once. | | model | | Object | | | ↳ model.tables | STASHKU_SQL_MODEL_VIEWS | Boolean | "true" | Toggle modelling support of tables through OPTIONS queries. Table support is enabled by default. | | ↳ model.views | STASHKU_SQL_MODEL_VIEWS | Boolean | "false" | Optionally allow views to be modelled through OPTIONS queries. By default, only tables will be modelled and returned. |

You can utilize environmental variables or define the values through a configuration object passed to StashKu. This project also loads .env files in it's package directory.

Permissions

Note that the OPTIONS requests may require additional permissions for the user. In Microsoft SQL Server, the user will need the "VIEW DEFINITIONS" permission in order for the engine to detect a database column default value.

GRANT VIEW DEFINITION ON <sometable> TO [someuser]
--or
GRANT VIEW DEFINITION ON SCHEMA::<someschema> TO [someuser]

StashKu Request Customization

This engine supports additional StashKu request metadata, specifically:

| Property | Type | Description | |-|-|-| | batch | Boolean | Forces a put or post request to operate in "batch" mode, even if the driver configuration is set to disable batching. | | output | Boolean | If false the RESTful response will not include output from the server- this can save memory and improve performance if you are not going to utilize the results of a request. |

For example, the following posts objects (...) to the database server in "batch" mode, without returning the created records.

let res = await stash.post(r => r
    .to('dbo.Locations')
    .objects(...}
    .headers({
        batch: true,
        output: false
    });

Driver-Specific Configuration Properties

If the database driver you are utilizing supports additional configuration properties, you can place these in an .options object (like the appName property shown in the example above).

sql-server Driver

This driver utilizes the tedious package under the rhino wrapper, which supports all tedious options. You can find a listing of the tedious options here.

REST

The StashKu SQL storage engine supports all RESTful StashKu methods, GET, POST, PUT, PATCH, and DELETE.

Example GET Operation

To retrieve information from the database...

let stash = new StashKu({
    engine: '@append/stashku-sql'
});
...
let res = await stash.get(r => r
    .from('dbo.Persons')
    .properties('FirstName', 'LastName', 'Title')
    .where(f => f.and('FirstName', Filter.OP.STARTSWITH, 'K'))
    .skip(0)
    .take(15)
    .sort('LastName', Sort.desc('FirstName'))
);
console.log(res);

Example POST Operation

To add information to the database...

let stash = new StashKu({
    engine: '@append/stashku-sql'
});
...
let res = await stash.post(r => r
    .to('dbo.Locations')
    .objects(
        {
            Name: 'America',
            CostRate: 1,
            Availability: 123
        },
        {
            Name: 'Canada',
            CostRate: 2,
            Availability: 234
        },
        {
            Name: 'Brazil',
            CostRate: 3,
            Availability: 345
        }
    )
);
console.log(res);

Running

Run npm start

Building

This project uses node.js to run and does not have an explicit build process.

Code Documentation

You can generate a static JSDoc site under the docs/ path using the command npm run docs.

Test

Run npm test to run jest unit tests.

Run npm run lint to run ESLint, optionally install the Visual Studio Code ESLint extension to have linting issues show in your "Problems" tab and be highlighted.

If you are writing unit tests, you may need to npm install @types/jest to get intellisense in Visual Studio Code if for some reason it did not get installed.

Setup

This project leverages the docker image chriseaton/adventureworks:light using the AdventureWorks sample database (created by Microsoft) to perform database tests.

You can grab and start the AdventureWorks sample database on your system in the background by running:

docker run -p 1433:1433 -e 'ACCEPT_EULA=Y' -e 'SA_PASSWORD=mySup3r_p4ssw0rd' -d chriseaton/adventureworks:light

The password can be set to whatever you wish. The docker container may take a minute to get initialized once started.

You will need to set the environmental variables to match the docker container's settings, for example:

| ENV | Example Value | |-----|---------------| | STASHKU_SQL_HOST | "localhost" | | STASHKU_SQL_DATABASE | "AdventureWorks" | | STASHKU_SQL_AUTH_USER | "sa" | | STASHKU_SQL_AUTH_PASSWORD | "mySup3r_p4ssw0rd" |

These can also be created using .env files, with the name of the driver being tested, specifically: driver-sql-server.env for Microsoft SQL Server tests, and driver-postgres.env for Postgres tests.