dolphindb 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/LICENSE +21 -0
  2. package/README.en.md +277 -0
  3. package/README.md +15 -11
  4. package/package.json +1 -1
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2022 DolphinDB
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.en.md ADDED
@@ -0,0 +1,277 @@
1
+ # DolphinDB JavaScript API
2
+
3
+ <p align='center'>
4
+ <img src='./ddb.svg' alt='DolphinDB' width='256'>
5
+ </p>
6
+
7
+ <p align='center'>
8
+ <a href='https://www.npmjs.com/package/dolphindb' target='_blank'>
9
+ <img alt='npm version' src='https://img.shields.io/npm/v/dolphindb.svg?style=flat-square&color=brightgreen' />
10
+ </a>
11
+ <a href='https://www.npmjs.com/package/dolphindb' target='_blank'>
12
+ <img alt='npm downloads' src='https://img.shields.io/npm/dt/dolphindb?style=flat-square&color=brightgreen' />
13
+ </a>
14
+ </p>
15
+
16
+ ## [中文](./README.md) | English
17
+
18
+ ## Overview
19
+ DolphinDB JavaScript API is a JavaScript library that encapsulates the ability to operate the DolphinDB database, such as: connecting to the database, executing scripts, calling functions, uploading variables, etc.
20
+
21
+ https://www.npmjs.com/package/dolphindb
22
+
23
+ ## Features
24
+ - Communicate with DolphinDB database using WebSocket, exchange data in binary format
25
+ - Support running in browser environment and Node.js environment
26
+ - Use TypedArray such as Int32Array in JavaScript to process binary data, with high performance
27
+ - A single call supports serialized upload of up to 2GB of data, and the amount of downloaded data is not limited
28
+
29
+ ## Installation
30
+ ```bash
31
+ # Install the latest version of Node.js and browser on the machine
32
+
33
+ # Install npm packages in your project
34
+ npm install dolphindb
35
+ ```
36
+
37
+ ## Usage
38
+ ### 0. Initialize and connect to DolphinDB
39
+ ```ts
40
+ import DDB from 'dolphindb'
41
+
42
+ // Create a database object and initialize the WebSocket URL
43
+ let ddb = new DDB('ws://127.0.0.1:8848')
44
+
45
+ // Establish a WebSocket connection to DolphinDB (requires DolphinDB database version at least 1.30.16 or 2.00.4)
46
+ await ddb.connect()
47
+ ```
48
+
49
+ #### Connect Method Declaration
50
+ ```ts
51
+ async connect (
52
+ options?: {
53
+ /** by default, the WebSocket URL passed in when the instance is initialized is used */
54
+ ws_url?: string
55
+
56
+ /** whether to automatically log in after the connection is established, the default is true */
57
+ login?: boolean
58
+
59
+ /** DolphinDB username */
60
+ username?: string
61
+
62
+ /** DolphinDB password */
63
+ password?: string
64
+ } = { }
65
+ ): Promise<void>
66
+ ```
67
+
68
+
69
+ ### 1. Call Functions
70
+ #### Example
71
+ ```ts
72
+ import { DdbInt } from 'dolphindb'
73
+
74
+ const result = await ddb.call<DdbInt>('add', [new DdbInt(1), new DdbInt(1)])
75
+
76
+ console.log(result.value === 2) // true
77
+ ```
78
+
79
+ #### The DolphinDB JavaScript API uses DdbObj objects to represent data types in DolphinDB
80
+ In the above example, two parameters 1 (corresponding to the int type in DolphinDB) are uploaded to the DolphinDB database as parameters of the add function, then the result of the function call is received.
81
+
82
+ `<DdbInt>` is used by TypeScript to infer the type of the return value
83
+
84
+ - result is a `DdbInt`, which is also a `DdbObj<number>`
85
+ - result.form is a `DdbForm.scalar`
86
+ - result.type is a `DdbType.int`
87
+ - result.value is native `number` in JavaScript (the value range and precision of int can be accurately represented by JavaScript number)
88
+
89
+ ```ts
90
+ /** Can represent all data types in DolphinDB databases */
91
+ class DdbObj <T extends DdbValue = DdbValue> {
92
+ /** is it little endian */
93
+ le: boolean
94
+
95
+ /** data form https://www.dolphindb.cn/cn/help/DataTypesandStructures/DataForms/index.html */
96
+ form: DdbForm
97
+
98
+ /** data type https://www.dolphindb.cn/cn/help/DataTypesandStructures/DataTypes/index.html */
99
+ type: DdbType
100
+
101
+ /** consumed length in buf parsed */
102
+ length: number
103
+
104
+ /** table name / column name */
105
+ name?: string
106
+
107
+ /**
108
+ Lowest dimension
109
+ - vector: rows = n, cols = 1
110
+ - pair: rows = 2, cols = 1
111
+ - matrix: rows = n, cols = m
112
+ - set: the same as vector
113
+ - dict: include keys, values vector
114
+ - table: the same as matrix
115
+ */
116
+ rows?: number
117
+
118
+ /** 2nd dimension */
119
+ cols?: number
120
+
121
+ /** the type of the value in matrix (only matrix has this field) */
122
+ datatype?: DdbType
123
+
124
+ /** the actual data. Different DdbForm, DdbType use different types in DdbValue to represent actual data */
125
+ value: T
126
+
127
+ constructor (data: Partial<DdbObj> & { form: DdbForm, type: DdbType, length: number }) {
128
+ Object.assign(this, data)
129
+ }
130
+ }
131
+
132
+ class DdbInt extends DdbObj<number> {
133
+ constructor (value: number) {
134
+ super({
135
+ form: DdbForm.scalar,
136
+ type: DdbType.int,
137
+ length: 4,
138
+ value
139
+ })
140
+ }
141
+ }
142
+
143
+ // ... There are also many utility classes, such as DdbString, DdbLong, DdbDouble, DdbVectorDouble, DdbVectorAny, etc.
144
+
145
+ type DdbValue =
146
+ null | boolean | number | [number, number] | bigint | string | string[] |
147
+ Uint8Array | Int16Array | Int32Array | Float32Array | Float64Array | BigInt64Array | Uint8Array[] |
148
+ DdbObj[] | DdbFunctionDefValue | DdbSymbolExtendedValue
149
+
150
+
151
+ enum DdbForm {
152
+ scalar = 0,
153
+ vector = 1,
154
+ pair = 2,
155
+ matrix = 3,
156
+ set = 4,
157
+ dict = 5,
158
+ table = 6,
159
+ chart = 7,
160
+ chunk = 8,
161
+ }
162
+
163
+
164
+ enum DdbType {
165
+ void = 0,
166
+ bool = 1,
167
+ char = 2,
168
+ short = 3,
169
+ int = 4,
170
+ long = 5,
171
+ // ...
172
+ timestamp = 12,
173
+ // ...
174
+ double = 16,
175
+ symbol = 17,
176
+ string = 18,
177
+ // ...
178
+ }
179
+ ```
180
+
181
+ #### `call` Method Declaration
182
+ ```ts
183
+ async call <T extends DdbObj> (
184
+ /** function name */
185
+ func: string,
186
+
187
+ /** function arguments (The incoming native string and boolean will be automatically converted to DdbObj<string> and DdbObj<boolean>) */
188
+ args?: (DdbObj | string | boolean)[] = [ ],
189
+
190
+ /** calling options */
191
+ options?: {
192
+ /** Urgent flag. Use urgent worker to execute to prevent being blocked by other jobs */
193
+ urgent?: boolean
194
+
195
+ /** When the node alias is set, the function is sent to the corresponding node in the cluster for execution (using the rpc method in DolphinDB) */
196
+ node?: string
197
+
198
+ /** When setting multiple node aliases, send them to the corresponding multiple nodes in the cluster for execution (using the pnodeRun method in DolphinDB) */
199
+ nodes?: string[]
200
+
201
+ /** It must be passed when setting the node parameter, the function type needs to be specified, and it is not passed in other cases */
202
+ func_type?: DdbFunctionType
203
+
204
+ /** It may be passed when setting the nodes parameter, otherwise may not be passed */
205
+ add_node_alias?: boolean
206
+ } = { }
207
+ ): Promise<T>
208
+ ```
209
+
210
+
211
+ ### 2. Execute Script
212
+ #### Example
213
+ ```ts
214
+ import type { DdbLong } from 'dolphindb'
215
+
216
+ const result = await ddb.eval<DdbLong>(
217
+ 'def foo (a, b) {\n' +
218
+ ' return a + b\n' +
219
+ '}\n' +
220
+ 'foo(1l, 1l)\n'
221
+ )
222
+
223
+ console.log(result.value === 2n) // true
224
+ ```
225
+
226
+ In the above example, a script is uploaded through a string to the DolphinDB database for execution, and the execution result of the last statement `foo(1l, 1l)` is received.
227
+
228
+ `<DdbLong>` is used by TypeScript to infer the type of the return value
229
+
230
+ - result is a `DdbLong`, which is also a `DdbObj<bigint>`
231
+ - result.form is `DdbForm.scalar`
232
+ - result.type is `DdbType.long`
233
+ - result.value is the native `bigint` in JavaScript (the precision of long cannot be accurately represented by JavaScript number, but it can be represented by bigint)
234
+
235
+ As long as the WebSocket connection is not disconnected, the custom function `foo` will always exist in the subsequent session and can be reused, for example, you can use `await ddb.call<DdbInt>('foo', [new DdbInt(1), new DdbInt(1)])` to call this custom function
236
+
237
+ #### `eval` Method Declaration
238
+ ```ts
239
+ async eval <T extends DdbObj> (
240
+ /** the script to execute */
241
+ script: string,
242
+
243
+ /** calling options */
244
+ options: {
245
+ /** Urgent flag. Use urgent worker to execute to prevent being blocked by other jobs */
246
+ urgent?: boolean
247
+ } = { }
248
+ ): Promise<T>
249
+ ```
250
+
251
+
252
+ ### 3. Upload Variables
253
+ #### Example
254
+ ```ts
255
+ import { DdbVectorDouble } from 'dolphindb'
256
+
257
+ let a = new Array(10000)
258
+ a.fill(1.0)
259
+
260
+ ddb.upload(['bar1', 'bar2'], [new DdbVectorDouble(a), new DdbVectorDouble(a)])
261
+ ```
262
+
263
+ In the above example, two variables `bar1`, `bar2` are uploaded, and the variable value is a double vector of length 10000
264
+
265
+ As long as the WebSocket connection is not disconnected, the variables `bar1`, `bar2` will always exist in the subsequent session and can be reused
266
+
267
+ #### `upload` Method Declaration
268
+ ```ts
269
+ async upload (
270
+ /** variable names */
271
+ vars: string[],
272
+
273
+ /** variable values */
274
+ args: (DdbObj | string | boolean)[]
275
+ ): Promise<void>
276
+ ```
277
+
package/README.md CHANGED
@@ -13,8 +13,12 @@
13
13
  </a>
14
14
  </p>
15
15
 
16
+ ## 中文 | [English](./README.en.md)
17
+
16
18
  ## 简介
17
- DolphinDB JavaScript API 封装了操作 DolphinDB 数据库的能力,如:连接数据库、执行脚本、调用函数、上传变量等
19
+ DolphinDB JavaScript API 是一个 JavaScript 库,封装了操作 DolphinDB 数据库的能力,如:连接数据库、执行脚本、调用函数、上传变量等
20
+
21
+ https://www.npmjs.com/package/dolphindb
18
22
 
19
23
  ## 特性
20
24
  - 使用 WebSocket 与 DolphinDB 数据库通信,用二进制格式进行数据交换
@@ -35,10 +39,10 @@ npm install dolphindb
35
39
  ```ts
36
40
  import DDB from 'dolphindb'
37
41
 
38
- // 初始化 WebSocket 连接地址
42
+ // 创建数据库对象,初始化 WebSocket 连接地址
39
43
  let ddb = new DDB('ws://127.0.0.1:8848')
40
44
 
41
- // 建立到 DolphinDB 的 WebSocket 连接
45
+ // 建立到 DolphinDB 的 WebSocket 连接(要求 DolphinDB 数据库版本不低于 1.30.16 或 2.00.4)
42
46
  await ddb.connect()
43
47
  ```
44
48
 
@@ -46,7 +50,7 @@ await ddb.connect()
46
50
  ```ts
47
51
  async connect (
48
52
  options?: {
49
- /** 默认使用实例初始化时传入的 WebSocket 链接地址 */
53
+ /** 默认使用实例初始化时传入的 WebSocket 链接 */
50
54
  ws_url?: string
51
55
 
52
56
  /** 是否在建立连接后自动登录,默认 true */
@@ -83,7 +87,7 @@ console.log(result.value === 2) // true
83
87
  - result.value 是 JavaScript 中原生的 `number` (int 的取值范围及精度可以用 JavaScript 的 number 准确表示)
84
88
 
85
89
  ```ts
86
- /** 可以表示所有 DolphinDB 数据库中的数据 */
90
+ /** 可以表示所有 DolphinDB 数据库中的数据类型 */
87
91
  class DdbObj <T extends DdbValue = DdbValue> {
88
92
  /** 是否为小端 (little endian) */
89
93
  le: boolean
@@ -174,7 +178,7 @@ enum DdbType {
174
178
  }
175
179
  ```
176
180
 
177
- #### call 方法声明
181
+ #### `call` 方法声明
178
182
  ```ts
179
183
  async call <T extends DdbObj> (
180
184
  /** 函数名 */
@@ -185,7 +189,7 @@ async call <T extends DdbObj> (
185
189
 
186
190
  /** 调用选项 */
187
191
  options?: {
188
- /** 紧急 flag,使用 urgent worker 处理,防止被其它作业阻塞 */
192
+ /** 紧急 flag。使用 urgent worker 执行,防止被其它作业阻塞 */
189
193
  urgent?: boolean
190
194
 
191
195
  /** 设置结点 alias 时发送到集群中对应的结点执行 (使用 DolphinDB 中的 rpc 方法) */
@@ -221,7 +225,7 @@ console.log(result.value === 2n) // true
221
225
 
222
226
  上面例子中,通过字符串上传了一段脚本到 DolphinDB 数据库执行,并接收最后一条语句 `foo(1l, 1l)` 执行结果 result
223
227
 
224
- `<DdbInt>` 用于 TypeScript 推断返回值的类型
228
+ `<DdbLong>` 用于 TypeScript 推断返回值的类型
225
229
 
226
230
  - result 是一个 `DdbLong`,也是 `DdbObj<bigint>`
227
231
  - result.form 是 `DdbForm.scalar`
@@ -230,7 +234,7 @@ console.log(result.value === 2n) // true
230
234
 
231
235
  只要 WebSocket 连接不断开,在后续的会话中 `foo` 这个自定义函数会一直存在,可复用,比如后续通过 `await ddb.call<DdbInt>('foo', [new DdbInt(1), new DdbInt(1)])` 调用这个自定义函数
232
236
 
233
- #### eval 方法声明
237
+ #### `eval` 方法声明
234
238
  ```ts
235
239
  async eval <T extends DdbObj> (
236
240
  /** 执行的脚本 */
@@ -238,7 +242,7 @@ async eval <T extends DdbObj> (
238
242
 
239
243
  /** 执行选项 */
240
244
  options: {
241
- /** 紧急 flag,使用 urgent worker 处理,防止被其它作业阻塞 */
245
+ /** 紧急 flag,确保提交的脚本使用 urgent worker 处理,防止被其它作业阻塞 */
242
246
  urgent?: boolean
243
247
  } = { }
244
248
  ): Promise<T>
@@ -260,7 +264,7 @@ ddb.upload(['bar1', 'bar2'], [new DdbVectorDouble(a), new DdbVectorDouble(a)])
260
264
 
261
265
  只要 WebSocket 连接不断开,在后续的会话中 `bar1`, `bar2` 这些变量会一直存在,可复用
262
266
 
263
- #### upload 方法声明
267
+ #### `upload` 方法声明
264
268
  ```ts
265
269
  async upload (
266
270
  /** 上传的变量名 */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "dolphindb",
3
3
  "description": "DolphinDB JavaScript API",
4
- "version": "0.0.1",
4
+ "version": "0.0.2",
5
5
  "type": "commonjs",
6
6
  "main": "./index.js",
7
7
  "browser": "./browser.js",