dolphindb 0.0.3 → 0.0.4
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.
- package/README.md +71 -71
- package/README.zh.md +277 -0
- package/package.json +1 -1
- package/README.en.md +0 -277
package/README.md
CHANGED
|
@@ -13,61 +13,61 @@
|
|
|
13
13
|
</a>
|
|
14
14
|
</p>
|
|
15
15
|
|
|
16
|
-
##
|
|
16
|
+
## English | [中文](./README.zh.md)
|
|
17
17
|
|
|
18
|
-
##
|
|
19
|
-
DolphinDB JavaScript API
|
|
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
20
|
|
|
21
21
|
https://www.npmjs.com/package/dolphindb
|
|
22
22
|
|
|
23
|
-
##
|
|
24
|
-
-
|
|
25
|
-
-
|
|
26
|
-
-
|
|
27
|
-
-
|
|
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
28
|
|
|
29
|
-
##
|
|
29
|
+
## Installation
|
|
30
30
|
```bash
|
|
31
|
-
#
|
|
31
|
+
# Install the latest version of Node.js and browser on the machine
|
|
32
32
|
|
|
33
|
-
#
|
|
33
|
+
# Install npm packages in your project
|
|
34
34
|
npm install dolphindb
|
|
35
35
|
```
|
|
36
36
|
|
|
37
|
-
##
|
|
38
|
-
### 0.
|
|
37
|
+
## Usage
|
|
38
|
+
### 0. Initialize and connect to DolphinDB
|
|
39
39
|
```ts
|
|
40
40
|
import DDB from 'dolphindb'
|
|
41
41
|
|
|
42
|
-
//
|
|
42
|
+
// Create a database object and initialize the WebSocket URL
|
|
43
43
|
let ddb = new DDB('ws://127.0.0.1:8848')
|
|
44
44
|
|
|
45
|
-
//
|
|
45
|
+
// Establish a WebSocket connection to DolphinDB (requires DolphinDB database version at least 1.30.16 or 2.00.4)
|
|
46
46
|
await ddb.connect()
|
|
47
47
|
```
|
|
48
48
|
|
|
49
|
-
####
|
|
49
|
+
#### Connect Method Declaration
|
|
50
50
|
```ts
|
|
51
51
|
async connect (
|
|
52
52
|
options?: {
|
|
53
|
-
/**
|
|
53
|
+
/** by default, the WebSocket URL passed in when the instance is initialized is used */
|
|
54
54
|
ws_url?: string
|
|
55
55
|
|
|
56
|
-
/**
|
|
56
|
+
/** whether to automatically log in after the connection is established, the default is true */
|
|
57
57
|
login?: boolean
|
|
58
58
|
|
|
59
|
-
/** DolphinDB
|
|
59
|
+
/** DolphinDB username */
|
|
60
60
|
username?: string
|
|
61
61
|
|
|
62
|
-
/** DolphinDB
|
|
62
|
+
/** DolphinDB password */
|
|
63
63
|
password?: string
|
|
64
64
|
} = { }
|
|
65
65
|
): Promise<void>
|
|
66
66
|
```
|
|
67
67
|
|
|
68
68
|
|
|
69
|
-
### 1.
|
|
70
|
-
####
|
|
69
|
+
### 1. Call Functions
|
|
70
|
+
#### Example
|
|
71
71
|
```ts
|
|
72
72
|
import { DdbInt } from 'dolphindb'
|
|
73
73
|
|
|
@@ -76,52 +76,52 @@ const result = await ddb.call<DdbInt>('add', [new DdbInt(1), new DdbInt(1)])
|
|
|
76
76
|
console.log(result.value === 2) // true
|
|
77
77
|
```
|
|
78
78
|
|
|
79
|
-
#### DolphinDB JavaScript API
|
|
80
|
-
|
|
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
81
|
|
|
82
|
-
`<DdbInt>`
|
|
82
|
+
`<DdbInt>` is used by TypeScript to infer the type of the return value
|
|
83
83
|
|
|
84
|
-
- result
|
|
85
|
-
- result.form
|
|
86
|
-
- result.type
|
|
87
|
-
- result.value
|
|
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
88
|
|
|
89
89
|
```ts
|
|
90
|
-
/**
|
|
90
|
+
/** Can represent all data types in DolphinDB databases */
|
|
91
91
|
class DdbObj <T extends DdbValue = DdbValue> {
|
|
92
|
-
/**
|
|
92
|
+
/** is it little endian */
|
|
93
93
|
le: boolean
|
|
94
94
|
|
|
95
|
-
/**
|
|
95
|
+
/** data form https://www.dolphindb.cn/cn/help/DataTypesandStructures/DataForms/index.html */
|
|
96
96
|
form: DdbForm
|
|
97
97
|
|
|
98
|
-
/**
|
|
98
|
+
/** data type https://www.dolphindb.cn/cn/help/DataTypesandStructures/DataTypes/index.html */
|
|
99
99
|
type: DdbType
|
|
100
100
|
|
|
101
|
-
/**
|
|
101
|
+
/** consumed length in buf parsed */
|
|
102
102
|
length: number
|
|
103
103
|
|
|
104
104
|
/** table name / column name */
|
|
105
105
|
name?: string
|
|
106
106
|
|
|
107
107
|
/**
|
|
108
|
-
|
|
108
|
+
Lowest dimension
|
|
109
109
|
- vector: rows = n, cols = 1
|
|
110
110
|
- pair: rows = 2, cols = 1
|
|
111
111
|
- matrix: rows = n, cols = m
|
|
112
|
-
- set:
|
|
113
|
-
- dict:
|
|
114
|
-
- table:
|
|
112
|
+
- set: the same as vector
|
|
113
|
+
- dict: include keys, values vector
|
|
114
|
+
- table: the same as matrix
|
|
115
115
|
*/
|
|
116
116
|
rows?: number
|
|
117
117
|
|
|
118
|
-
/**
|
|
118
|
+
/** 2nd dimension */
|
|
119
119
|
cols?: number
|
|
120
120
|
|
|
121
|
-
/** matrix
|
|
121
|
+
/** the type of the value in matrix (only matrix has this field) */
|
|
122
122
|
datatype?: DdbType
|
|
123
123
|
|
|
124
|
-
/**
|
|
124
|
+
/** the actual data. Different DdbForm, DdbType use different types in DdbValue to represent actual data */
|
|
125
125
|
value: T
|
|
126
126
|
|
|
127
127
|
constructor (data: Partial<DdbObj> & { form: DdbForm, type: DdbType, length: number }) {
|
|
@@ -140,7 +140,7 @@ class DdbInt extends DdbObj<number> {
|
|
|
140
140
|
}
|
|
141
141
|
}
|
|
142
142
|
|
|
143
|
-
// ...
|
|
143
|
+
// ... There are also many utility classes, such as DdbString, DdbLong, DdbDouble, DdbVectorDouble, DdbVectorAny, etc.
|
|
144
144
|
|
|
145
145
|
type DdbValue =
|
|
146
146
|
null | boolean | number | [number, number] | bigint | string | string[] |
|
|
@@ -178,38 +178,38 @@ enum DdbType {
|
|
|
178
178
|
}
|
|
179
179
|
```
|
|
180
180
|
|
|
181
|
-
#### `call`
|
|
181
|
+
#### `call` Method Declaration
|
|
182
182
|
```ts
|
|
183
183
|
async call <T extends DdbObj> (
|
|
184
|
-
/**
|
|
184
|
+
/** function name */
|
|
185
185
|
func: string,
|
|
186
186
|
|
|
187
|
-
/**
|
|
187
|
+
/** function arguments (The incoming native string and boolean will be automatically converted to DdbObj<string> and DdbObj<boolean>) */
|
|
188
188
|
args?: (DdbObj | string | boolean)[] = [ ],
|
|
189
189
|
|
|
190
|
-
/**
|
|
190
|
+
/** calling options */
|
|
191
191
|
options?: {
|
|
192
|
-
/**
|
|
192
|
+
/** Urgent flag. Use urgent worker to execute to prevent being blocked by other jobs */
|
|
193
193
|
urgent?: boolean
|
|
194
194
|
|
|
195
|
-
/**
|
|
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
196
|
node?: string
|
|
197
197
|
|
|
198
|
-
/**
|
|
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
199
|
nodes?: string[]
|
|
200
200
|
|
|
201
|
-
/**
|
|
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
202
|
func_type?: DdbFunctionType
|
|
203
203
|
|
|
204
|
-
/**
|
|
204
|
+
/** It may be passed when setting the nodes parameter, otherwise may not be passed */
|
|
205
205
|
add_node_alias?: boolean
|
|
206
206
|
} = { }
|
|
207
207
|
): Promise<T>
|
|
208
208
|
```
|
|
209
209
|
|
|
210
210
|
|
|
211
|
-
### 2.
|
|
212
|
-
####
|
|
211
|
+
### 2. Execute Script
|
|
212
|
+
#### Example
|
|
213
213
|
```ts
|
|
214
214
|
import type { DdbLong } from 'dolphindb'
|
|
215
215
|
|
|
@@ -223,34 +223,34 @@ const result = await ddb.eval<DdbLong>(
|
|
|
223
223
|
console.log(result.value === 2n) // true
|
|
224
224
|
```
|
|
225
225
|
|
|
226
|
-
|
|
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
227
|
|
|
228
|
-
`<DdbLong>`
|
|
228
|
+
`<DdbLong>` is used by TypeScript to infer the type of the return value
|
|
229
229
|
|
|
230
|
-
- result
|
|
231
|
-
- result.form
|
|
232
|
-
- result.type
|
|
233
|
-
- result.value
|
|
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
234
|
|
|
235
|
-
|
|
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
236
|
|
|
237
|
-
#### `eval`
|
|
237
|
+
#### `eval` Method Declaration
|
|
238
238
|
```ts
|
|
239
239
|
async eval <T extends DdbObj> (
|
|
240
|
-
/**
|
|
240
|
+
/** the script to execute */
|
|
241
241
|
script: string,
|
|
242
242
|
|
|
243
|
-
/**
|
|
243
|
+
/** calling options */
|
|
244
244
|
options: {
|
|
245
|
-
/**
|
|
245
|
+
/** Urgent flag. Use urgent worker to execute to prevent being blocked by other jobs */
|
|
246
246
|
urgent?: boolean
|
|
247
247
|
} = { }
|
|
248
248
|
): Promise<T>
|
|
249
249
|
```
|
|
250
250
|
|
|
251
251
|
|
|
252
|
-
### 3.
|
|
253
|
-
####
|
|
252
|
+
### 3. Upload Variables
|
|
253
|
+
#### Example
|
|
254
254
|
```ts
|
|
255
255
|
import { DdbVectorDouble } from 'dolphindb'
|
|
256
256
|
|
|
@@ -260,17 +260,17 @@ a.fill(1.0)
|
|
|
260
260
|
ddb.upload(['bar1', 'bar2'], [new DdbVectorDouble(a), new DdbVectorDouble(a)])
|
|
261
261
|
```
|
|
262
262
|
|
|
263
|
-
|
|
263
|
+
In the above example, two variables `bar1`, `bar2` are uploaded, and the variable value is a double vector of length 10000
|
|
264
264
|
|
|
265
|
-
|
|
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
266
|
|
|
267
|
-
#### `upload`
|
|
267
|
+
#### `upload` Method Declaration
|
|
268
268
|
```ts
|
|
269
269
|
async upload (
|
|
270
|
-
/**
|
|
270
|
+
/** variable names */
|
|
271
271
|
vars: string[],
|
|
272
272
|
|
|
273
|
-
/**
|
|
273
|
+
/** variable values */
|
|
274
274
|
args: (DdbObj | string | boolean)[]
|
|
275
275
|
): Promise<void>
|
|
276
276
|
```
|
package/README.zh.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
|
+
## [English](./README.md) | 中文
|
|
17
|
+
|
|
18
|
+
## 简介
|
|
19
|
+
DolphinDB JavaScript API 是一个 JavaScript 库,封装了操作 DolphinDB 数据库的能力,如:连接数据库、执行脚本、调用函数、上传变量等
|
|
20
|
+
|
|
21
|
+
https://www.npmjs.com/package/dolphindb
|
|
22
|
+
|
|
23
|
+
## 特性
|
|
24
|
+
- 使用 WebSocket 与 DolphinDB 数据库通信,用二进制格式进行数据交换
|
|
25
|
+
- 支持在浏览器环境和 Node.js 环境中运行
|
|
26
|
+
- 使用了 JavaScript 中的 Int32Array 等 TypedArray 处理二进制数据,性能较高
|
|
27
|
+
- 单次调用支持最大 2 GB 数据的序列化上传,下载数据量不受限制
|
|
28
|
+
|
|
29
|
+
## 安装
|
|
30
|
+
```bash
|
|
31
|
+
# 在机器上安装最新版的 Node.js 及浏览器
|
|
32
|
+
|
|
33
|
+
# 在项目中安装 npm 包
|
|
34
|
+
npm install dolphindb
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## 用法
|
|
38
|
+
### 0. 初始化并连接到 DolphinDB
|
|
39
|
+
```ts
|
|
40
|
+
import DDB from 'dolphindb'
|
|
41
|
+
|
|
42
|
+
// 创建数据库对象,初始化 WebSocket 连接地址
|
|
43
|
+
let ddb = new DDB('ws://127.0.0.1:8848')
|
|
44
|
+
|
|
45
|
+
// 建立到 DolphinDB 的 WebSocket 连接(要求 DolphinDB 数据库版本不低于 1.30.16 或 2.00.4)
|
|
46
|
+
await ddb.connect()
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
#### connect 方法声明
|
|
50
|
+
```ts
|
|
51
|
+
async connect (
|
|
52
|
+
options?: {
|
|
53
|
+
/** 默认使用实例初始化时传入的 WebSocket 链接 */
|
|
54
|
+
ws_url?: string
|
|
55
|
+
|
|
56
|
+
/** 是否在建立连接后自动登录,默认 true */
|
|
57
|
+
login?: boolean
|
|
58
|
+
|
|
59
|
+
/** DolphinDB 登录用户名 */
|
|
60
|
+
username?: string
|
|
61
|
+
|
|
62
|
+
/** DolphinDB 登录密码 */
|
|
63
|
+
password?: string
|
|
64
|
+
} = { }
|
|
65
|
+
): Promise<void>
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
|
|
69
|
+
### 1. 调用函数
|
|
70
|
+
#### 例子
|
|
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
|
+
#### DolphinDB JavaScript API 用 DdbObj 对象来表示 DolphinDB 中的数据类型
|
|
80
|
+
上面例子中,上传了两个参数 1 (对应 DolphinDB 中的 int 类型) 到 DolphinDB 数据库,作为 add 函数的参数,并接收函数调用的结果 result
|
|
81
|
+
|
|
82
|
+
`<DdbInt>` 用于 TypeScript 推断返回值的类型
|
|
83
|
+
|
|
84
|
+
- result 是一个 `DdbInt`,也是 `DdbObj<number>`
|
|
85
|
+
- result.form 是 `DdbForm.scalar`
|
|
86
|
+
- result.type 是 `DdbType.int`
|
|
87
|
+
- result.value 是 JavaScript 中原生的 `number` (int 的取值范围及精度可以用 JavaScript 的 number 准确表示)
|
|
88
|
+
|
|
89
|
+
```ts
|
|
90
|
+
/** 可以表示所有 DolphinDB 数据库中的数据类型 */
|
|
91
|
+
class DdbObj <T extends DdbValue = DdbValue> {
|
|
92
|
+
/** 是否为小端 (little endian) */
|
|
93
|
+
le: boolean
|
|
94
|
+
|
|
95
|
+
/** 数据形式 https://www.dolphindb.cn/cn/help/DataTypesandStructures/DataForms/index.html */
|
|
96
|
+
form: DdbForm
|
|
97
|
+
|
|
98
|
+
/** 数据类型 https://www.dolphindb.cn/cn/help/DataTypesandStructures/DataTypes/index.html */
|
|
99
|
+
type: DdbType
|
|
100
|
+
|
|
101
|
+
/** 占用 parse 时传入的 buf 的长度 */
|
|
102
|
+
length: number
|
|
103
|
+
|
|
104
|
+
/** table name / column name */
|
|
105
|
+
name?: string
|
|
106
|
+
|
|
107
|
+
/**
|
|
108
|
+
最低维、第 1 维
|
|
109
|
+
- vector: rows = n, cols = 1
|
|
110
|
+
- pair: rows = 2, cols = 1
|
|
111
|
+
- matrix: rows = n, cols = m
|
|
112
|
+
- set: 同 vector
|
|
113
|
+
- dict: 包含 keys, values 向量
|
|
114
|
+
- table: 同 matrix
|
|
115
|
+
*/
|
|
116
|
+
rows?: number
|
|
117
|
+
|
|
118
|
+
/** 第 2 维 */
|
|
119
|
+
cols?: number
|
|
120
|
+
|
|
121
|
+
/** matrix 中值的类型,仅 matrix 才有 */
|
|
122
|
+
datatype?: DdbType
|
|
123
|
+
|
|
124
|
+
/** 实际数据。不同的 DdbForm, DdbType 使用 DdbValue 中不同的类型来表示实际数据 */
|
|
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
|
+
// ... 还有很多快捷类,如 DdbString, DdbLong, DdbDouble, DdbVectorDouble, DdbVectorAny 等
|
|
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` 方法声明
|
|
182
|
+
```ts
|
|
183
|
+
async call <T extends DdbObj> (
|
|
184
|
+
/** 函数名 */
|
|
185
|
+
func: string,
|
|
186
|
+
|
|
187
|
+
/** 调用参数 (传入的原生 string 和 boolean 会被自动转换为 DdbObj<string> 和 DdbObj<boolean>) */
|
|
188
|
+
args?: (DdbObj | string | boolean)[] = [ ],
|
|
189
|
+
|
|
190
|
+
/** 调用选项 */
|
|
191
|
+
options?: {
|
|
192
|
+
/** 紧急 flag。使用 urgent worker 执行,防止被其它作业阻塞 */
|
|
193
|
+
urgent?: boolean
|
|
194
|
+
|
|
195
|
+
/** 设置结点 alias 时发送到集群中对应的结点执行 (使用 DolphinDB 中的 rpc 方法) */
|
|
196
|
+
node?: string
|
|
197
|
+
|
|
198
|
+
/** 设置多个结点 alias 时发送到集群中对应的多个结点执行 (使用 DolphinDB 中的 pnodeRun 方法) */
|
|
199
|
+
nodes?: string[]
|
|
200
|
+
|
|
201
|
+
/** 设置 node 参数时必传,需指定函数类型,其它情况下不传 */
|
|
202
|
+
func_type?: DdbFunctionType
|
|
203
|
+
|
|
204
|
+
/** 设置 nodes 参数时选传,其它情况不传 */
|
|
205
|
+
add_node_alias?: boolean
|
|
206
|
+
} = { }
|
|
207
|
+
): Promise<T>
|
|
208
|
+
```
|
|
209
|
+
|
|
210
|
+
|
|
211
|
+
### 2. 执行脚本
|
|
212
|
+
#### 例子
|
|
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
|
+
上面例子中,通过字符串上传了一段脚本到 DolphinDB 数据库执行,并接收最后一条语句 `foo(1l, 1l)` 执行结果 result
|
|
227
|
+
|
|
228
|
+
`<DdbLong>` 用于 TypeScript 推断返回值的类型
|
|
229
|
+
|
|
230
|
+
- result 是一个 `DdbLong`,也是 `DdbObj<bigint>`
|
|
231
|
+
- result.form 是 `DdbForm.scalar`
|
|
232
|
+
- result.type 是 `DdbType.long`
|
|
233
|
+
- result.value 是 JavaScript 中原生的 `bigint` (long 的精度不能用 JavaScript 的 number 准确表示,但可以用 bigint 表示)
|
|
234
|
+
|
|
235
|
+
只要 WebSocket 连接不断开,在后续的会话中 `foo` 这个自定义函数会一直存在,可复用,比如后续通过 `await ddb.call<DdbInt>('foo', [new DdbInt(1), new DdbInt(1)])` 调用这个自定义函数
|
|
236
|
+
|
|
237
|
+
#### `eval` 方法声明
|
|
238
|
+
```ts
|
|
239
|
+
async eval <T extends DdbObj> (
|
|
240
|
+
/** 执行的脚本 */
|
|
241
|
+
script: string,
|
|
242
|
+
|
|
243
|
+
/** 执行选项 */
|
|
244
|
+
options: {
|
|
245
|
+
/** 紧急 flag,确保提交的脚本使用 urgent worker 处理,防止被其它作业阻塞 */
|
|
246
|
+
urgent?: boolean
|
|
247
|
+
} = { }
|
|
248
|
+
): Promise<T>
|
|
249
|
+
```
|
|
250
|
+
|
|
251
|
+
|
|
252
|
+
### 3. 上传变量
|
|
253
|
+
#### 例子
|
|
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
|
+
上面的例子中,上传了 `bar1`, `bar2` 两个变量,变量值是长度为 10000 的 double 向量
|
|
264
|
+
|
|
265
|
+
只要 WebSocket 连接不断开,在后续的会话中 `bar1`, `bar2` 这些变量会一直存在,可复用
|
|
266
|
+
|
|
267
|
+
#### `upload` 方法声明
|
|
268
|
+
```ts
|
|
269
|
+
async upload (
|
|
270
|
+
/** 上传的变量名 */
|
|
271
|
+
vars: string[],
|
|
272
|
+
|
|
273
|
+
/** 上传的变量值 */
|
|
274
|
+
args: (DdbObj | string | boolean)[]
|
|
275
|
+
): Promise<void>
|
|
276
|
+
```
|
|
277
|
+
|
package/package.json
CHANGED
package/README.en.md
DELETED
|
@@ -1,277 +0,0 @@
|
|
|
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
|
-
|