|
1 | 1 | # sql-data-api-client-js
|
2 | 2 | SQL Data Api client for Javascript
|
3 | 3 |
|
4 |
| -### Usage |
| 4 | +## Install |
5 | 5 |
|
6 | 6 | > npm install sql-data-api
|
7 | 7 |
|
| 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 | + |
8 | 25 | ```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>; |
9 | 130 |
|
10 | 131 | ```
|
11 | 132 |
|
12 |
| -### License |
| 133 | +## License |
13 | 134 |
|
14 | 135 | A permissive MIT License (c) FalconSoft Ltd.
|
0 commit comments