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.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
|
-
|