Skip to content

Commit 5606ed3

Browse files
committed
readme and comments
1 parent b169b9a commit 5606ed3

File tree

2 files changed

+201
-2
lines changed

2 files changed

+201
-2
lines changed

README.md

Lines changed: 123 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,135 @@
11
# sql-data-api-client-js
22
SQL Data Api client for Javascript
33

4-
### Usage
4+
## Install
55

66
> npm install sql-data-api
77
8+
## Set base URL
9+
10+
```js
11+
import { setBaseUrl } from 'sql-data-api';
12+
13+
// ...
14+
15+
setBaseUrl('https://api.worksheet.systems');
16+
17+
```
18+
19+
## Authentication
20+
21+
There are two types of authentication.
22+
23+
1. you can set user name and password (use your Worksheet Systems account)
24+
825
```js
26+
import { authenticate } from 'sql-data-api';
27+
28+
await authenticate("testUser", "1111111")
29+
30+
```
31+
32+
2. Use `Api Access Token` generated https://app.worksheet.systems/account/settings/info
33+
34+
```js
35+
import { setUserAccessToken } from 'sql-data-api';
36+
37+
setUserAccessToken('$ACCESS_TOKEN')
38+
```
39+
40+
## Query Data
41+
42+
```js
43+
44+
// returns table as array of items
45+
query(tableOrViewName: string, fieldsOrQuery?: string | SqlReadQueryInfo, queryInfoSettings?: SqlReadQueryInfo): Promise<ScalarObject[]>;
46+
47+
// Query specification
48+
export interface SqlReadQueryInfo {
49+
fields?: string;
50+
filter?: string;
51+
filterParams?: Record<string, ScalarType>;
52+
skip?: number;
53+
top?: number;
54+
orderBy?: string;
55+
mainTableAlias?: string;
56+
joins?: [JoinType, string, string][];
57+
}
58+
59+
```
60+
### Query Usage
61+
62+
#### A simple queries
63+
64+
```js
65+
import { sqlDataApi } from 'sql-data-api';
66+
67+
// returns all rows from the table
68+
const allRows = await sqlDataApi('connectionName')
69+
.query('someTableOrViewName');
70+
71+
// returns two fields for all rows
72+
const allRowsAndJustTwoFieds = await sqlDataApi('connectionName')
73+
.query('someTableOrViewName', 'Field1, Field2');
74+
75+
// returns two fields for UK
76+
const allRowsAndJustTwoFieds = await sqlDataApi('connectionName')
77+
.query('someTableOrViewName', {
78+
fields: "F1, f2",
79+
filter: "Country = @country",
80+
filterParams: {country: 'UK'},
81+
top: 1000,
82+
orderBy: "F2 DESC",
83+
});
84+
85+
```
86+
87+
#### SQL Functions
88+
89+
SQL Functions can be used in `fields` and `filter` properties
90+
91+
```js
92+
93+
const itwms = await sqlDataApi('connectionName')
94+
.query('someTableOrViewName', {
95+
fields: 'cast(DateTimeField as Date) SomeDate, concat(FirstName, '-', LastName") FullName',
96+
filter: "concat(FirstName, '-', LastName) = @fullName",
97+
filterParams: {fullName: 'Adam Smith'}
98+
});
99+
100+
```
101+
#### Aggregated query
102+
103+
Add `groupBy|` prefix to the field and use aggregation functions e.g: sum, avg, count ...
104+
105+
```js
106+
107+
const aggData = await sqlDataApi('connectionName')
108+
.query('someTableOrViewName', {
109+
fields: 'groupBy|country, sum(revenue) revenue'
110+
});
111+
112+
```
113+
114+
115+
## Save a array of items
116+
117+
```js
118+
save(tableName: string, items: ScalarObject[], itemsToDeleteOrSaveOptions?: Record<string, unknown>[] | SqlSaveOptions, saveOptions?: SqlSaveOptions): Promise<SqlSaveStatus>;
119+
120+
export interface SqlSaveOptions {
121+
method: "Merge" | "Append" | "BulkInsert";
122+
batchSize?: number;
123+
primaryKeys?: string[];
124+
batchProgressFunc?: (processedCount: number, status: SqlSaveStatus) => void;
125+
}
126+
127+
saveWithAutoId(tableName: string, item: ScalarObject): Promise<number>;
128+
129+
sqlExecute(sql: string, params?: ScalarObject): Promise<ScalarObject[] | unknown>;
9130

10131
```
11132

12-
### License
133+
## License
13134

14135
A permissive MIT License (c) FalconSoft Ltd.

src/sql-data.api.ts

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,105 @@ const appHttpHeaders = {
1616
"Content-Type": "application/json",
1717
};
1818

19+
/**
20+
* Interface that represents a SQL save result
21+
*/
1922
export interface SqlSaveStatus {
23+
/**
24+
* Number of inserted row
25+
*/
2026
inserted: number;
27+
28+
/**
29+
* Number of updated row
30+
*/
2131
updated: number;
32+
33+
/**
34+
* Number of deleted row
35+
*/
2236
deleted: number;
2337
}
2438

39+
/**
40+
* Sql Query configuration
41+
*/
2542
export interface SqlReadQueryInfo {
43+
/**
44+
* A list of comma separated fields to select.
45+
* Examples:
46+
* - 'Field1, Field2'
47+
* - 't.Field1, t.Field2' - where t is an alias
48+
* - 'concat(FirstName, ' ', LastName) FullName, AnotherField' - you can use SQL functions
49+
* - 'groupBy|Country, sum(profit) Profit' - aggregated query.
50+
*/
2651
fields?: string;
52+
53+
/**
54+
* A filter expression. Where you can use SQL functions, aliases and parameters
55+
* Examples:
56+
* - "Country = 'UK'"
57+
* - "Country = @country AND City = @city"
58+
* - "concat(t.FirstName, ' ', t.LastName) = @fullName"
59+
*/
2760
filter?: string;
61+
62+
/**
63+
* A filter parameters used in an expresiion
64+
* e.g: {country: 'UK', fullName: 'Adam Smith'}
65+
*/
2866
filterParams?: Record<string, ScalarType>;
67+
68+
/**
69+
* Number of rows to skip
70+
*/
2971
skip?: number;
72+
73+
/**
74+
* top rows to take
75+
*/
3076
top?: number;
77+
78+
/**
79+
* Define a sortings fields
80+
*/
3181
orderBy?: string;
82+
83+
/**
84+
* Alias for main table. But default it is undefined
85+
*/
3286
mainTableAlias?: string;
87+
88+
/**
89+
* joins tables together
90+
* Examples:
91+
* - ['InnerJoin', 'Customers c', 't.CustomerId = c.CustomerID'] - inner joins mainTable to Cutomers table
92+
*/
3393
joins?: [JoinType, string, string][];
3494
}
3595

96+
/**
97+
* Sql Save operation config
98+
*/
3699
export interface SqlSaveOptions {
100+
/**
101+
* save types
102+
*/
37103
method: "Merge" | "Append" | "BulkInsert";
104+
105+
/**
106+
* a batch size for optimization point
107+
*/
38108
batchSize?: number;
109+
/**
110+
* Define a primary key that should be used. Normally primary keys are taken from the table,
111+
* Use this property only if you want to upsert (merge) data on some other fields
112+
*/
39113
primaryKeys?: string[];
114+
115+
/**
116+
* Report progress on batch saving
117+
*/
40118
batchProgressFunc?: (processedCount: number, status: SqlSaveStatus) => void;
41119
}
42120

0 commit comments

Comments
 (0)