dolphindb 0.0.2 → 0.0.6
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 +121 -76
- package/README.zh.md +322 -0
- package/browser.d.ts +35 -9
- package/browser.js +164 -25
- package/browser.js.map +1 -1
- package/index.d.ts +27 -8
- package/index.js +154 -16
- package/index.js.map +1 -1
- package/package.json +8 -6
- package/test.js +28 -0
- package/test.js.map +1 -1
- package/README.en.md +0 -277
package/README.md
CHANGED
|
@@ -13,115 +13,124 @@
|
|
|
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
|
+
# 1. Install the latest version of Node.js and browser on the machine
|
|
32
32
|
|
|
33
|
-
#
|
|
33
|
+
# 2. Create a new project (skip this step if you already have a project)
|
|
34
|
+
mkdir dolphindb-example
|
|
35
|
+
cd dolphindb-example
|
|
36
|
+
npm init --yes
|
|
37
|
+
# Open the package.json file with an editor, add the line "type": "module", below "main": "./index.js",
|
|
38
|
+
# This enables the use of ECMAScript modules, and in the code behind you can use import { DDB } from 'dolphindb' to import npm packages
|
|
39
|
+
|
|
40
|
+
# 3. Install npm packages in your project
|
|
34
41
|
npm install dolphindb
|
|
35
42
|
```
|
|
36
43
|
|
|
37
|
-
##
|
|
38
|
-
### 0.
|
|
44
|
+
## Usage
|
|
45
|
+
### 0. Initialize and connect to DolphinDB
|
|
39
46
|
```ts
|
|
40
|
-
import DDB from 'dolphindb'
|
|
47
|
+
import { DDB } from 'dolphindb'
|
|
48
|
+
// The import method for existing projects using CommonJS modules is const { DDB } = require('dolphindb')
|
|
41
49
|
|
|
42
|
-
//
|
|
50
|
+
// Create a database object and initialize the WebSocket URL
|
|
43
51
|
let ddb = new DDB('ws://127.0.0.1:8848')
|
|
44
52
|
|
|
45
|
-
//
|
|
53
|
+
// Establish a WebSocket connection to DolphinDB (requires DolphinDB database version at least 1.30.16 or 2.00.4)
|
|
46
54
|
await ddb.connect()
|
|
47
55
|
```
|
|
48
56
|
|
|
49
|
-
####
|
|
57
|
+
#### Connect Method Declaration
|
|
50
58
|
```ts
|
|
51
59
|
async connect (
|
|
52
60
|
options?: {
|
|
53
|
-
/**
|
|
61
|
+
/** by default, the WebSocket URL passed in when the instance is initialized is used */
|
|
54
62
|
ws_url?: string
|
|
55
63
|
|
|
56
|
-
/**
|
|
64
|
+
/** whether to automatically log in after the connection is established, the default is true */
|
|
57
65
|
login?: boolean
|
|
58
66
|
|
|
59
|
-
/** DolphinDB
|
|
67
|
+
/** DolphinDB username */
|
|
60
68
|
username?: string
|
|
61
69
|
|
|
62
|
-
/** DolphinDB
|
|
70
|
+
/** DolphinDB password */
|
|
63
71
|
password?: string
|
|
64
72
|
} = { }
|
|
65
73
|
): Promise<void>
|
|
66
74
|
```
|
|
67
75
|
|
|
68
76
|
|
|
69
|
-
### 1.
|
|
70
|
-
####
|
|
77
|
+
### 1. Call Functions
|
|
78
|
+
#### Example
|
|
71
79
|
```ts
|
|
72
80
|
import { DdbInt } from 'dolphindb'
|
|
73
81
|
|
|
74
|
-
const result = await ddb.call
|
|
82
|
+
const result = await ddb.call('add', [new DdbInt(1), new DdbInt(1)])
|
|
83
|
+
// TypeScript: const result = await ddb.call<DdbInt>('add', [new DdbInt(1), new DdbInt(1)])
|
|
75
84
|
|
|
76
85
|
console.log(result.value === 2) // true
|
|
77
86
|
```
|
|
78
87
|
|
|
79
|
-
#### DolphinDB JavaScript API
|
|
80
|
-
|
|
88
|
+
#### The DolphinDB JavaScript API uses DdbObj objects to represent data types in DolphinDB
|
|
89
|
+
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
90
|
|
|
82
|
-
`<DdbInt>`
|
|
91
|
+
`<DdbInt>` is used by TypeScript to infer the type of the return value
|
|
83
92
|
|
|
84
|
-
- result
|
|
85
|
-
- result.form
|
|
86
|
-
- result.type
|
|
87
|
-
- result.value
|
|
93
|
+
- result is a `DdbInt`, which is also a `DdbObj<number>`
|
|
94
|
+
- result.form is a `DdbForm.scalar`
|
|
95
|
+
- result.type is a `DdbType.int`
|
|
96
|
+
- result.value is native `number` in JavaScript (the value range and precision of int can be accurately represented by JavaScript number)
|
|
88
97
|
|
|
89
98
|
```ts
|
|
90
|
-
/**
|
|
99
|
+
/** Can represent all data types in DolphinDB databases */
|
|
91
100
|
class DdbObj <T extends DdbValue = DdbValue> {
|
|
92
|
-
/**
|
|
101
|
+
/** is it little endian */
|
|
93
102
|
le: boolean
|
|
94
103
|
|
|
95
|
-
/**
|
|
104
|
+
/** data form https://www.dolphindb.cn/cn/help/DataTypesandStructures/DataForms/index.html */
|
|
96
105
|
form: DdbForm
|
|
97
106
|
|
|
98
|
-
/**
|
|
107
|
+
/** data type https://www.dolphindb.cn/cn/help/DataTypesandStructures/DataTypes/index.html */
|
|
99
108
|
type: DdbType
|
|
100
109
|
|
|
101
|
-
/**
|
|
110
|
+
/** consumed length in buf parsed */
|
|
102
111
|
length: number
|
|
103
112
|
|
|
104
113
|
/** table name / column name */
|
|
105
114
|
name?: string
|
|
106
115
|
|
|
107
116
|
/**
|
|
108
|
-
|
|
117
|
+
Lowest dimension
|
|
109
118
|
- vector: rows = n, cols = 1
|
|
110
119
|
- pair: rows = 2, cols = 1
|
|
111
120
|
- matrix: rows = n, cols = m
|
|
112
|
-
- set:
|
|
113
|
-
- dict:
|
|
114
|
-
- table:
|
|
121
|
+
- set: the same as vector
|
|
122
|
+
- dict: include keys, values vector
|
|
123
|
+
- table: the same as matrix
|
|
115
124
|
*/
|
|
116
125
|
rows?: number
|
|
117
126
|
|
|
118
|
-
/**
|
|
127
|
+
/** 2nd dimension */
|
|
119
128
|
cols?: number
|
|
120
129
|
|
|
121
|
-
/** matrix
|
|
130
|
+
/** the type of the value in matrix (only matrix has this field) */
|
|
122
131
|
datatype?: DdbType
|
|
123
132
|
|
|
124
|
-
/**
|
|
133
|
+
/** the actual data. Different DdbForm, DdbType use different types in DdbValue to represent actual data */
|
|
125
134
|
value: T
|
|
126
135
|
|
|
127
136
|
constructor (data: Partial<DdbObj> & { form: DdbForm, type: DdbType, length: number }) {
|
|
@@ -140,7 +149,7 @@ class DdbInt extends DdbObj<number> {
|
|
|
140
149
|
}
|
|
141
150
|
}
|
|
142
151
|
|
|
143
|
-
// ...
|
|
152
|
+
// ... There are also many utility classes, such as DdbString, DdbLong, DdbDouble, DdbVectorDouble, DdbVectorAny, etc.
|
|
144
153
|
|
|
145
154
|
type DdbValue =
|
|
146
155
|
null | boolean | number | [number, number] | bigint | string | string[] |
|
|
@@ -178,79 +187,115 @@ enum DdbType {
|
|
|
178
187
|
}
|
|
179
188
|
```
|
|
180
189
|
|
|
181
|
-
|
|
190
|
+
##### For types that are not in the utility classes, such as the datetime type in DolphinDB, we can specify form and type to create a DdbObj object
|
|
191
|
+
```ts
|
|
192
|
+
const obj = new DdbObj({
|
|
193
|
+
form: DdbForm.scalar,
|
|
194
|
+
type: DdbType.datetime,
|
|
195
|
+
value: 1644573600,
|
|
196
|
+
length: 0
|
|
197
|
+
})
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
The corresponding type and value of value property in js can refer to the result returned by `ddb.eval` (see below: `eval` Method Declaration)
|
|
201
|
+
|
|
202
|
+
```ts
|
|
203
|
+
const obj = await ddb.eval('2022.02.11 10:00:00')
|
|
204
|
+
console.log(obj.form === DdbForm.scalar)
|
|
205
|
+
console.log(obj.type === DdbType.datetime)
|
|
206
|
+
console.log(obj.value)
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
##### Another example is to create a set
|
|
210
|
+
```ts
|
|
211
|
+
// refer to ddb.eval
|
|
212
|
+
// const obj = await ddb.eval('set([1, 2, 3])')
|
|
213
|
+
// console.log(obj.value)
|
|
214
|
+
|
|
215
|
+
const obj = new DdbObj({
|
|
216
|
+
form: DdbForm.set,
|
|
217
|
+
type: DdbType.int,
|
|
218
|
+
value: Int32Array.of(1, 2, 3),
|
|
219
|
+
length: 0
|
|
220
|
+
})
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
#### `call` Method Declaration
|
|
182
225
|
```ts
|
|
183
226
|
async call <T extends DdbObj> (
|
|
184
|
-
/**
|
|
227
|
+
/** function name */
|
|
185
228
|
func: string,
|
|
186
229
|
|
|
187
|
-
/**
|
|
230
|
+
/** function arguments (The incoming native string and boolean will be automatically converted to DdbObj<string> and DdbObj<boolean>) */
|
|
188
231
|
args?: (DdbObj | string | boolean)[] = [ ],
|
|
189
232
|
|
|
190
|
-
/**
|
|
233
|
+
/** calling options */
|
|
191
234
|
options?: {
|
|
192
|
-
/**
|
|
235
|
+
/** Urgent flag. Use urgent worker to execute to prevent being blocked by other jobs */
|
|
193
236
|
urgent?: boolean
|
|
194
237
|
|
|
195
|
-
/**
|
|
238
|
+
/** 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
239
|
node?: string
|
|
197
240
|
|
|
198
|
-
/**
|
|
241
|
+
/** When setting multiple node aliases, send them to the corresponding multiple nodes in the cluster for execution (using the pnodeRun method in DolphinDB) */
|
|
199
242
|
nodes?: string[]
|
|
200
243
|
|
|
201
|
-
/**
|
|
244
|
+
/** 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
245
|
func_type?: DdbFunctionType
|
|
203
246
|
|
|
204
|
-
/**
|
|
247
|
+
/** It may be passed when setting the nodes parameter, otherwise may not be passed */
|
|
205
248
|
add_node_alias?: boolean
|
|
206
249
|
} = { }
|
|
207
250
|
): Promise<T>
|
|
208
251
|
```
|
|
209
252
|
|
|
210
253
|
|
|
211
|
-
### 2.
|
|
212
|
-
####
|
|
254
|
+
### 2. Execute Script
|
|
255
|
+
#### Example
|
|
213
256
|
```ts
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
const result = await ddb.eval<DdbLong>(
|
|
257
|
+
const result = await ddb.eval(
|
|
217
258
|
'def foo (a, b) {\n' +
|
|
218
259
|
' return a + b\n' +
|
|
219
260
|
'}\n' +
|
|
220
261
|
'foo(1l, 1l)\n'
|
|
221
262
|
)
|
|
222
263
|
|
|
264
|
+
// TypeScript:
|
|
265
|
+
// import type { DdbLong } from 'dolphindb'
|
|
266
|
+
// const result = await ddb.eval<DdbLong>(...)
|
|
267
|
+
|
|
223
268
|
console.log(result.value === 2n) // true
|
|
224
269
|
```
|
|
225
270
|
|
|
226
|
-
|
|
271
|
+
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
272
|
|
|
228
|
-
`<DdbLong>`
|
|
273
|
+
`<DdbLong>` is used by TypeScript to infer the type of the return value
|
|
229
274
|
|
|
230
|
-
- result
|
|
231
|
-
- result.form
|
|
232
|
-
- result.type
|
|
233
|
-
- result.value
|
|
275
|
+
- result is a `DdbLong`, which is also a `DdbObj<bigint>`
|
|
276
|
+
- result.form is `DdbForm.scalar`
|
|
277
|
+
- result.type is `DdbType.long`
|
|
278
|
+
- 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
279
|
|
|
235
|
-
|
|
280
|
+
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
281
|
|
|
237
|
-
#### `eval`
|
|
282
|
+
#### `eval` Method Declaration
|
|
238
283
|
```ts
|
|
239
284
|
async eval <T extends DdbObj> (
|
|
240
|
-
/**
|
|
285
|
+
/** the script to execute */
|
|
241
286
|
script: string,
|
|
242
287
|
|
|
243
|
-
/**
|
|
288
|
+
/** calling options */
|
|
244
289
|
options: {
|
|
245
|
-
/**
|
|
290
|
+
/** Urgent flag. Use urgent worker to execute to prevent being blocked by other jobs */
|
|
246
291
|
urgent?: boolean
|
|
247
292
|
} = { }
|
|
248
293
|
): Promise<T>
|
|
249
294
|
```
|
|
250
295
|
|
|
251
296
|
|
|
252
|
-
### 3.
|
|
253
|
-
####
|
|
297
|
+
### 3. Upload Variables
|
|
298
|
+
#### Example
|
|
254
299
|
```ts
|
|
255
300
|
import { DdbVectorDouble } from 'dolphindb'
|
|
256
301
|
|
|
@@ -260,17 +305,17 @@ a.fill(1.0)
|
|
|
260
305
|
ddb.upload(['bar1', 'bar2'], [new DdbVectorDouble(a), new DdbVectorDouble(a)])
|
|
261
306
|
```
|
|
262
307
|
|
|
263
|
-
|
|
308
|
+
In the above example, two variables `bar1`, `bar2` are uploaded, and the variable value is a double vector of length 10000
|
|
264
309
|
|
|
265
|
-
|
|
310
|
+
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
311
|
|
|
267
|
-
#### `upload`
|
|
312
|
+
#### `upload` Method Declaration
|
|
268
313
|
```ts
|
|
269
314
|
async upload (
|
|
270
|
-
/**
|
|
315
|
+
/** variable names */
|
|
271
316
|
vars: string[],
|
|
272
317
|
|
|
273
|
-
/**
|
|
318
|
+
/** variable values */
|
|
274
319
|
args: (DdbObj | string | boolean)[]
|
|
275
320
|
): Promise<void>
|
|
276
321
|
```
|
package/README.zh.md
ADDED
|
@@ -0,0 +1,322 @@
|
|
|
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
|
+
# 1. 在机器上安装最新版的 Node.js 及浏览器
|
|
32
|
+
|
|
33
|
+
# 2. 创建新项目
|
|
34
|
+
mkdir dolphindb-example
|
|
35
|
+
cd dolphindb-example
|
|
36
|
+
npm init --yes
|
|
37
|
+
# 用编辑器打开 package.json 文件,在 "main": "./index.js", 下面加入一行 "type": "module",
|
|
38
|
+
# 这样能够启用 ECMAScript modules,在后面代码中可以使用 import { DDB } from 'dolphindb' 导入 npm 包
|
|
39
|
+
|
|
40
|
+
# 3. 在项目中安装 npm 包
|
|
41
|
+
npm install dolphindb
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
## 用法
|
|
45
|
+
### 0. 初始化并连接到 DolphinDB
|
|
46
|
+
```ts
|
|
47
|
+
import { DDB } from 'dolphindb'
|
|
48
|
+
// 已有的使用 CommonJS 模块的项目的导入方法为 const { DDB } = require('dolphindb')
|
|
49
|
+
|
|
50
|
+
// 创建数据库对象,初始化 WebSocket 连接地址
|
|
51
|
+
let ddb = new DDB('ws://127.0.0.1:8848')
|
|
52
|
+
|
|
53
|
+
// 建立到 DolphinDB 的 WebSocket 连接(要求 DolphinDB 数据库版本不低于 1.30.16 或 2.00.4)
|
|
54
|
+
await ddb.connect()
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
#### connect 方法声明
|
|
58
|
+
```ts
|
|
59
|
+
async connect (
|
|
60
|
+
options?: {
|
|
61
|
+
/** 默认使用实例初始化时传入的 WebSocket 链接 */
|
|
62
|
+
ws_url?: string
|
|
63
|
+
|
|
64
|
+
/** 是否在建立连接后自动登录,默认 true */
|
|
65
|
+
login?: boolean
|
|
66
|
+
|
|
67
|
+
/** DolphinDB 登录用户名 */
|
|
68
|
+
username?: string
|
|
69
|
+
|
|
70
|
+
/** DolphinDB 登录密码 */
|
|
71
|
+
password?: string
|
|
72
|
+
} = { }
|
|
73
|
+
): Promise<void>
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
### 1. 调用函数
|
|
78
|
+
#### 例子
|
|
79
|
+
```ts
|
|
80
|
+
import { DdbInt } from 'dolphindb'
|
|
81
|
+
|
|
82
|
+
const result = await ddb.call('add', [new DdbInt(1), new DdbInt(1)])
|
|
83
|
+
// TypeScript: const result = await ddb.call<DdbInt>('add', [new DdbInt(1), new DdbInt(1)])
|
|
84
|
+
|
|
85
|
+
console.log(result.value === 2) // true
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
#### DolphinDB JavaScript API 用 DdbObj 对象来表示 DolphinDB 中的数据类型
|
|
89
|
+
上面例子中,上传了两个参数 1 (对应 DolphinDB 中的 int 类型) 到 DolphinDB 数据库,作为 add 函数的参数,并接收函数调用的结果 result
|
|
90
|
+
|
|
91
|
+
`<DdbInt>` 用于 TypeScript 推断返回值的类型
|
|
92
|
+
|
|
93
|
+
- result 是一个 `DdbInt`,也是 `DdbObj<number>`
|
|
94
|
+
- result.form 是 `DdbForm.scalar`
|
|
95
|
+
- result.type 是 `DdbType.int`
|
|
96
|
+
- result.value 是 JavaScript 中原生的 `number` (int 的取值范围及精度可以用 JavaScript 的 number 准确表示)
|
|
97
|
+
|
|
98
|
+
```ts
|
|
99
|
+
/** 可以表示所有 DolphinDB 数据库中的数据类型 */
|
|
100
|
+
class DdbObj <T extends DdbValue = DdbValue> {
|
|
101
|
+
/** 是否为小端 (little endian) */
|
|
102
|
+
le: boolean
|
|
103
|
+
|
|
104
|
+
/** 数据形式 https://www.dolphindb.cn/cn/help/DataTypesandStructures/DataForms/index.html */
|
|
105
|
+
form: DdbForm
|
|
106
|
+
|
|
107
|
+
/** 数据类型 https://www.dolphindb.cn/cn/help/DataTypesandStructures/DataTypes/index.html */
|
|
108
|
+
type: DdbType
|
|
109
|
+
|
|
110
|
+
/** 占用 parse 时传入的 buf 的长度 */
|
|
111
|
+
length: number
|
|
112
|
+
|
|
113
|
+
/** table name / column name */
|
|
114
|
+
name?: string
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
最低维、第 1 维
|
|
118
|
+
- vector: rows = n, cols = 1
|
|
119
|
+
- pair: rows = 2, cols = 1
|
|
120
|
+
- matrix: rows = n, cols = m
|
|
121
|
+
- set: 同 vector
|
|
122
|
+
- dict: 包含 keys, values 向量
|
|
123
|
+
- table: 同 matrix
|
|
124
|
+
*/
|
|
125
|
+
rows?: number
|
|
126
|
+
|
|
127
|
+
/** 第 2 维 */
|
|
128
|
+
cols?: number
|
|
129
|
+
|
|
130
|
+
/** matrix 中值的类型,仅 matrix 才有 */
|
|
131
|
+
datatype?: DdbType
|
|
132
|
+
|
|
133
|
+
/** 实际数据。不同的 DdbForm, DdbType 使用 DdbValue 中不同的类型来表示实际数据 */
|
|
134
|
+
value: T
|
|
135
|
+
|
|
136
|
+
constructor (data: Partial<DdbObj> & { form: DdbForm, type: DdbType, length: number }) {
|
|
137
|
+
Object.assign(this, data)
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
class DdbInt extends DdbObj<number> {
|
|
142
|
+
constructor (value: number) {
|
|
143
|
+
super({
|
|
144
|
+
form: DdbForm.scalar,
|
|
145
|
+
type: DdbType.int,
|
|
146
|
+
length: 4,
|
|
147
|
+
value
|
|
148
|
+
})
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
// ... 还有很多快捷类,如 DdbString, DdbLong, DdbDouble, DdbVectorDouble, DdbVectorAny 等
|
|
153
|
+
|
|
154
|
+
type DdbValue =
|
|
155
|
+
null | boolean | number | [number, number] | bigint | string | string[] |
|
|
156
|
+
Uint8Array | Int16Array | Int32Array | Float32Array | Float64Array | BigInt64Array | Uint8Array[] |
|
|
157
|
+
DdbObj[] | DdbFunctionDefValue | DdbSymbolExtendedValue
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
enum DdbForm {
|
|
161
|
+
scalar = 0,
|
|
162
|
+
vector = 1,
|
|
163
|
+
pair = 2,
|
|
164
|
+
matrix = 3,
|
|
165
|
+
set = 4,
|
|
166
|
+
dict = 5,
|
|
167
|
+
table = 6,
|
|
168
|
+
chart = 7,
|
|
169
|
+
chunk = 8,
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
enum DdbType {
|
|
174
|
+
void = 0,
|
|
175
|
+
bool = 1,
|
|
176
|
+
char = 2,
|
|
177
|
+
short = 3,
|
|
178
|
+
int = 4,
|
|
179
|
+
long = 5,
|
|
180
|
+
// ...
|
|
181
|
+
timestamp = 12,
|
|
182
|
+
// ...
|
|
183
|
+
double = 16,
|
|
184
|
+
symbol = 17,
|
|
185
|
+
string = 18,
|
|
186
|
+
// ...
|
|
187
|
+
}
|
|
188
|
+
```
|
|
189
|
+
|
|
190
|
+
##### 不在快捷类中的类型,比如 DolphinDB 中的 datetime 类型, 可以指定 form 和 type 创建 DdbObj 对象
|
|
191
|
+
```ts
|
|
192
|
+
const obj = new DdbObj({
|
|
193
|
+
form: DdbForm.scalar,
|
|
194
|
+
type: DdbType.datetime,
|
|
195
|
+
value: 1644573600,
|
|
196
|
+
length: 0
|
|
197
|
+
})
|
|
198
|
+
```
|
|
199
|
+
|
|
200
|
+
value 的在 js 中对应类型及取值可以参考 ddb.eval 返回的结果 (见后文 `eval` 方法声明)
|
|
201
|
+
|
|
202
|
+
```ts
|
|
203
|
+
const obj = await ddb.eval('2022.02.11 10:00:00')
|
|
204
|
+
console.log(obj.form === DdbForm.scalar)
|
|
205
|
+
console.log(obj.type === DdbType.datetime)
|
|
206
|
+
console.log(obj.value)
|
|
207
|
+
```
|
|
208
|
+
|
|
209
|
+
##### 再比如创建一个 set
|
|
210
|
+
```ts
|
|
211
|
+
// 参考 ddb.eval
|
|
212
|
+
// const obj = await ddb.eval('set([1, 2, 3])')
|
|
213
|
+
// console.log(obj.value)
|
|
214
|
+
|
|
215
|
+
const obj = new DdbObj({
|
|
216
|
+
form: DdbForm.set,
|
|
217
|
+
type: DdbType.int,
|
|
218
|
+
value: Int32Array.of(1, 2, 3),
|
|
219
|
+
length: 0
|
|
220
|
+
})
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
|
|
224
|
+
#### `call` 方法声明
|
|
225
|
+
```ts
|
|
226
|
+
async call <T extends DdbObj> (
|
|
227
|
+
/** 函数名 */
|
|
228
|
+
func: string,
|
|
229
|
+
|
|
230
|
+
/** 调用参数 (传入的原生 string 和 boolean 会被自动转换为 DdbObj<string> 和 DdbObj<boolean>) */
|
|
231
|
+
args?: (DdbObj | string | boolean)[] = [ ],
|
|
232
|
+
|
|
233
|
+
/** 调用选项 */
|
|
234
|
+
options?: {
|
|
235
|
+
/** 紧急 flag。使用 urgent worker 执行,防止被其它作业阻塞 */
|
|
236
|
+
urgent?: boolean
|
|
237
|
+
|
|
238
|
+
/** 设置结点 alias 时发送到集群中对应的结点执行 (使用 DolphinDB 中的 rpc 方法) */
|
|
239
|
+
node?: string
|
|
240
|
+
|
|
241
|
+
/** 设置多个结点 alias 时发送到集群中对应的多个结点执行 (使用 DolphinDB 中的 pnodeRun 方法) */
|
|
242
|
+
nodes?: string[]
|
|
243
|
+
|
|
244
|
+
/** 设置 node 参数时必传,需指定函数类型,其它情况下不传 */
|
|
245
|
+
func_type?: DdbFunctionType
|
|
246
|
+
|
|
247
|
+
/** 设置 nodes 参数时选传,其它情况不传 */
|
|
248
|
+
add_node_alias?: boolean
|
|
249
|
+
} = { }
|
|
250
|
+
): Promise<T>
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
### 2. 执行脚本
|
|
255
|
+
#### 例子
|
|
256
|
+
```ts
|
|
257
|
+
const result = await ddb.eval(
|
|
258
|
+
'def foo (a, b) {\n' +
|
|
259
|
+
' return a + b\n' +
|
|
260
|
+
'}\n' +
|
|
261
|
+
'foo(1l, 1l)\n'
|
|
262
|
+
)
|
|
263
|
+
|
|
264
|
+
// TypeScript:
|
|
265
|
+
// import type { DdbLong } from 'dolphindb'
|
|
266
|
+
// const result = await ddb.eval<DdbLong>(...)
|
|
267
|
+
|
|
268
|
+
console.log(result.value === 2n) // true
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
上面例子中,通过字符串上传了一段脚本到 DolphinDB 数据库执行,并接收最后一条语句 `foo(1l, 1l)` 执行结果 result
|
|
272
|
+
|
|
273
|
+
`<DdbLong>` 用于 TypeScript 推断返回值的类型
|
|
274
|
+
|
|
275
|
+
- result 是一个 `DdbLong`,也是 `DdbObj<bigint>`
|
|
276
|
+
- result.form 是 `DdbForm.scalar`
|
|
277
|
+
- result.type 是 `DdbType.long`
|
|
278
|
+
- result.value 是 JavaScript 中原生的 `bigint` (long 的精度不能用 JavaScript 的 number 准确表示,但可以用 bigint 表示)
|
|
279
|
+
|
|
280
|
+
只要 WebSocket 连接不断开,在后续的会话中 `foo` 这个自定义函数会一直存在,可复用,比如后续通过 `await ddb.call<DdbInt>('foo', [new DdbInt(1), new DdbInt(1)])` 调用这个自定义函数
|
|
281
|
+
|
|
282
|
+
#### `eval` 方法声明
|
|
283
|
+
```ts
|
|
284
|
+
async eval <T extends DdbObj> (
|
|
285
|
+
/** 执行的脚本 */
|
|
286
|
+
script: string,
|
|
287
|
+
|
|
288
|
+
/** 执行选项 */
|
|
289
|
+
options: {
|
|
290
|
+
/** 紧急 flag,确保提交的脚本使用 urgent worker 处理,防止被其它作业阻塞 */
|
|
291
|
+
urgent?: boolean
|
|
292
|
+
} = { }
|
|
293
|
+
): Promise<T>
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
|
|
297
|
+
### 3. 上传变量
|
|
298
|
+
#### 例子
|
|
299
|
+
```ts
|
|
300
|
+
import { DdbVectorDouble } from 'dolphindb'
|
|
301
|
+
|
|
302
|
+
let a = new Array(10000)
|
|
303
|
+
a.fill(1.0)
|
|
304
|
+
|
|
305
|
+
ddb.upload(['bar1', 'bar2'], [new DdbVectorDouble(a), new DdbVectorDouble(a)])
|
|
306
|
+
```
|
|
307
|
+
|
|
308
|
+
上面的例子中,上传了 `bar1`, `bar2` 两个变量,变量值是长度为 10000 的 double 向量
|
|
309
|
+
|
|
310
|
+
只要 WebSocket 连接不断开,在后续的会话中 `bar1`, `bar2` 这些变量会一直存在,可复用
|
|
311
|
+
|
|
312
|
+
#### `upload` 方法声明
|
|
313
|
+
```ts
|
|
314
|
+
async upload (
|
|
315
|
+
/** 上传的变量名 */
|
|
316
|
+
vars: string[],
|
|
317
|
+
|
|
318
|
+
/** 上传的变量值 */
|
|
319
|
+
args: (DdbObj | string | boolean)[]
|
|
320
|
+
): Promise<void>
|
|
321
|
+
```
|
|
322
|
+
|