
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
sql-data-api
Advanced tools
SQL Data Api client for Javascript
npm install sql-data-api
import { setBaseUrl } from 'sql-data-api';
// ...
setBaseUrl('https://api.worksheet.systems');
All sql-api operations should be authenticated unless public access allowed. Check Worksheet Systems Access Control model
There are two types of authentication.
import { authenticate } from 'sql-data-api';
await authenticate("testUser", "1111111")
Api Access Token generated https://app.worksheet.systems/account/settings/infoimport { setUserAccessToken } from 'sql-data-api';
setUserAccessToken('$ACCESS_TOKEN')
// returns table as array of items
query(tableOrViewName: string, fieldsOrQuery?: string | SqlReadQueryInfo, queryInfoSettings?: SqlReadQueryInfo): Promise<ScalarObject[]>;
// Query specification
export interface SqlReadQueryInfo {
fields?: string;
filter?: string;
filterParams?: Record<string, ScalarType>;
skip?: number;
top?: number;
orderBy?: string;
mainTableAlias?: string;
joins?: [JoinType, string, string][];
}
SQL Data api allows you to safely and securely query data from SQL tables/views. And you can use SQL functions, rename SQL columns, aggregate (groupBy) and even join tables
There are several ways you can define a query to the SQL Database. But, eventually it comes down to the few properties you have to specify:
myTable t, then you have to list your fields as t.Field1 etcfields property is not provided, then all table fields will be returned. Kind of select * from [tableName]. Also, there are several other scenarios:
Country CustomerCountry or cast(TransactionTime as Date) TransactionDateconcat(FirstName, ' ', LastName) FullNamegroupBy|Country, groupBy|City, sum(revenue) Revenue, count(*) Countcountry = 'uk' and city = 'London' or you can use parameters and have filter as country = @country AND city = @city and provide parameters as an object{country: 'UK', city: 'London'}. And you can use SQL functions as well: e.g.: cast(TransactionTime as Date) = '2021-11-21'[JoinType, TableToJoin, JoinCondition, JoinCondition2] or: ['InnerJoin', 'Customers c', 'c.CustomerId = t.CustomerId']A simple queries
import { sqlDataApi } from 'sql-data-api';
// returns all rows from the table
const allRows = await sqlDataApi('connectionName')
.query('someTableOrViewName');
// returns two fields for all rows
const allRowsAndJustTwoFieds = await sqlDataApi('connectionName')
.query('someTableOrViewName', 'Field1, Field2');
// returns two fields for UK
const allRowsAndJustTwoFieds = await sqlDataApi('connectionName')
.query('someTableOrViewName', {
fields: "F1, f2",
filter: "Country = @country",
filterParams: {country: 'UK'},
top: 1000,
orderBy: "F2 DESC",
});
SQL Functions can be used in fields and filter properties
const itwms = await sqlDataApi('connectionName')
.query('someTableOrViewName', {
fields: 'cast(DateTimeField as Date) SomeDate, concat(FirstName, '-', LastName") FullName',
filter: "concat(FirstName, '-', LastName) = @fullName",
filterParams: {fullName: 'Adam Smith'}
});
Add groupBy| prefix to the field you want to group by and use aggregation functions e.g: sum, avg, count ...
const aggData = await sqlDataApi('connectionName')
.query('someTableOrViewName', {
fields: 'groupBy|country, sum(revenue) revenue'
});
or with the same result
const aggData = await sqlDataApi('connectionName')
.query(
'someTableOrViewName',
'groupBy|country, sum(revenue) revenue'
);
Upsert(Merge), Append or BulkInsert an array of items into the table based on save options If third parameter is an array, it will delete records from the table. Only Key Fields must be provided
save(
tableName: string,
items: ScalarObject[],
itemsToDeleteOrSaveOptions?: Record<string, unknown>[] | SqlSaveOptions,
saveOptions?: SqlSaveOptions
): Promise<SqlSaveStatus>;
/**
* Sql Save operation config
*/
export interface SqlSaveOptions {
/**
* save types
*/
method: "Merge" | "Append" | "BulkInsert";
/**
* a batch size for optimization point
*/
batchSize?: number;
/**
* Define a primary key that should be used. Normally primary keys are taken from the table,
* Use this property only if you want to upsert (merge) data on some other fields
*/
primaryKeys?: string[];
/**
* Report progress on batch saving
*/
batchProgressFunc?: (processedCount: number, status: SqlSaveStatus) => void;
}
a simple save (upsert) example
sqlDataApi('someConnection')
.save('someTable', arrayOfItems)
Saves a single record into the database and returns autogenerated ID field value. SQL Table should have Auto Indentity on one of the fields
const person = {
name: 'Adam'
}
// table peopleTable should have Identity column
person.id = await sqlDataApi('someConnection')
.saveWithAutoId('peopleTable', person);
console.log(person)
Updates data in the table based on filter parameter and returns number of rows affected
/**
* Updates data in the table based on filter parameters
* @returns Number of rows affected
*/
async updateData(
tableName: string,
updateData: Record<string, ScalarType>,
filter?: string,
filterParams?: Record<string, ScalarType>
): Promise<number>
Deletes rows from the table based on a primary keys. Only key fields have to be provided
/**
* Deletes rows from the table. Only key fields have to be provided
* @returns success
*/
async delete(
tableName: string,
items: Record<string, ScalarType>[]
): Promise<boolean>
Delete records from the table based on filter criteria
/**
* Delete records from the table based on filter criteria
*/
async deleteFrom(
tableName: string,
filter?: string,
filterParams?: Record<string, ScalarType>
): Promise<number>
Executes sql script in the server and returns either raw table or array of objects
/**
* Executes a SQL Query or stored procedure with parameters
* @returns Raw result (SqlQueryResponse) with a table in it
*/
async sqlExecuteRaw(
sql: string,
params?: ScalarObject,
paramDirections?: Record<string, string>
): Promise<SqlQueryResponse>
/**
* Executes a SQL Query or stored procedure with parameters
* @returns result as a list of arrays
*/
async sqlExecute(
sql: string,
params?: ScalarObject
): Promise<ScalarObject[] | unknown>
You can cancel any of your http request by setting AbortController
setAbortController(abortController: AbortController): SqlDataApi
setAbortController is part of chaining method of sqlDataApi or you can pass it to the constructor and factory method
A permissive MIT License (c) FalconSoft Ltd.
FAQs
Sql Data Api client for Javascript
The npm package sql-data-api receives a total of 3 weekly downloads. As such, sql-data-api popularity was classified as not popular.
We found that sql-data-api demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.