dolphindb 0.0.4 → 0.0.7
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 +68 -7
- package/README.zh.md +67 -7
- package/browser.d.ts +85 -21
- package/browser.js +280 -87
- package/browser.js.map +1 -1
- package/index.d.ts +77 -21
- package/index.js +265 -72
- package/index.js.map +1 -1
- package/package.json +5 -5
- package/test.js +22 -2
- package/test.js.map +1 -1
package/README.md
CHANGED
|
@@ -28,16 +28,24 @@ https://www.npmjs.com/package/dolphindb
|
|
|
28
28
|
|
|
29
29
|
## Installation
|
|
30
30
|
```bash
|
|
31
|
-
# Install the latest version of Node.js and browser on the machine
|
|
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
44
|
## Usage
|
|
38
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')
|
|
@@ -71,7 +79,8 @@ async connect (
|
|
|
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
|
```
|
|
@@ -178,6 +187,40 @@ enum DdbType {
|
|
|
178
187
|
}
|
|
179
188
|
```
|
|
180
189
|
|
|
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
|
+
|
|
181
224
|
#### `call` Method Declaration
|
|
182
225
|
```ts
|
|
183
226
|
async call <T extends DdbObj> (
|
|
@@ -211,15 +254,17 @@ async call <T extends DdbObj> (
|
|
|
211
254
|
### 2. Execute Script
|
|
212
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
|
|
|
@@ -275,3 +320,19 @@ async upload (
|
|
|
275
320
|
): Promise<void>
|
|
276
321
|
```
|
|
277
322
|
|
|
323
|
+
|
|
324
|
+
### Notes
|
|
325
|
+
```ts
|
|
326
|
+
import { nulls, DdbInt, timestamp2str } from 'dolphindb'
|
|
327
|
+
|
|
328
|
+
// create DolphinDB null integer value (the smallest 32-bit integer)
|
|
329
|
+
new DdbInt(nulls.int32)
|
|
330
|
+
|
|
331
|
+
// convert DolphinDB timestamp value to string
|
|
332
|
+
timestamp2str(
|
|
333
|
+
(
|
|
334
|
+
await ddb.call<DdbObj<bigint>>('now', [false])
|
|
335
|
+
).value
|
|
336
|
+
) === '2022.02.23 17:23:13.494'
|
|
337
|
+
```
|
|
338
|
+
|
package/README.zh.md
CHANGED
|
@@ -28,16 +28,24 @@ https://www.npmjs.com/package/dolphindb
|
|
|
28
28
|
|
|
29
29
|
## 安装
|
|
30
30
|
```bash
|
|
31
|
-
# 在机器上安装最新版的 Node.js 及浏览器
|
|
31
|
+
# 1. 在机器上安装最新版的 Node.js 及浏览器
|
|
32
32
|
|
|
33
|
-
#
|
|
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 包
|
|
34
41
|
npm install dolphindb
|
|
35
42
|
```
|
|
36
43
|
|
|
37
44
|
## 用法
|
|
38
45
|
### 0. 初始化并连接到 DolphinDB
|
|
39
46
|
```ts
|
|
40
|
-
import DDB from 'dolphindb'
|
|
47
|
+
import { DDB } from 'dolphindb'
|
|
48
|
+
// 已有的使用 CommonJS 模块的项目的导入方法为 const { DDB } = require('dolphindb')
|
|
41
49
|
|
|
42
50
|
// 创建数据库对象,初始化 WebSocket 连接地址
|
|
43
51
|
let ddb = new DDB('ws://127.0.0.1:8848')
|
|
@@ -71,7 +79,8 @@ async connect (
|
|
|
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
|
```
|
|
@@ -178,6 +187,40 @@ enum DdbType {
|
|
|
178
187
|
}
|
|
179
188
|
```
|
|
180
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
|
+
|
|
181
224
|
#### `call` 方法声明
|
|
182
225
|
```ts
|
|
183
226
|
async call <T extends DdbObj> (
|
|
@@ -211,15 +254,17 @@ async call <T extends DdbObj> (
|
|
|
211
254
|
### 2. 执行脚本
|
|
212
255
|
#### 例子
|
|
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
|
|
|
@@ -275,3 +320,18 @@ async upload (
|
|
|
275
320
|
): Promise<void>
|
|
276
321
|
```
|
|
277
322
|
|
|
323
|
+
|
|
324
|
+
### Notes
|
|
325
|
+
```ts
|
|
326
|
+
import { nulls, DdbInt, timestamp2str } from 'dolphindb'
|
|
327
|
+
|
|
328
|
+
// 创建 DolphinDB 空值 (最小的 32-bit 整数)
|
|
329
|
+
new DdbInt(nulls.int32)
|
|
330
|
+
|
|
331
|
+
// 将 DolphinDB 中的 timestamp 格式化为 string
|
|
332
|
+
timestamp2str(
|
|
333
|
+
(
|
|
334
|
+
await ddb.call<DdbObj<bigint>>('now', [false])
|
|
335
|
+
).value
|
|
336
|
+
) === '2022.02.23 17:23:13.494'
|
|
337
|
+
```
|
package/browser.d.ts
CHANGED
|
@@ -66,16 +66,24 @@ export interface DdbFunctionDefValue {
|
|
|
66
66
|
export interface DdbSymbolExtendedValue {
|
|
67
67
|
base_id: number;
|
|
68
68
|
base: string[];
|
|
69
|
-
|
|
69
|
+
data: Uint32Array;
|
|
70
70
|
}
|
|
71
|
-
export
|
|
72
|
-
|
|
71
|
+
export interface DdbArrayVectorBlock {
|
|
72
|
+
unit: 1 | 2 | 4;
|
|
73
|
+
rows: number;
|
|
74
|
+
lengths: Uint8Array | Uint16Array | Uint32Array;
|
|
75
|
+
data: DdbVectorValue;
|
|
76
|
+
}
|
|
77
|
+
export declare type DdbValue = null | boolean | number | [number, number] | bigint | string | string[] | Uint8Array | Int16Array | Int32Array | Float32Array | Float64Array | BigInt64Array | Uint8Array[] | DdbObj[] | DdbFunctionDefValue | DdbSymbolExtendedValue | DdbArrayVectorBlock[];
|
|
78
|
+
export declare type DdbVectorValue = string | string[] | Uint8Array | Int16Array | Int32Array | Float32Array | Float64Array | BigInt64Array | Uint8Array[] | DdbObj[] | DdbSymbolExtendedValue | DdbArrayVectorBlock[];
|
|
73
79
|
export declare const nulls: {
|
|
74
80
|
readonly char: "";
|
|
75
81
|
readonly int16: -32768;
|
|
76
82
|
readonly int32: -2147483648;
|
|
77
83
|
readonly int64: -9223372036854775808n;
|
|
78
84
|
};
|
|
85
|
+
export declare const timezone_offset: number;
|
|
86
|
+
/** 可以表示所有 DolphinDB 数据库中的数据类型 */
|
|
79
87
|
export declare class DdbObj<T extends DdbValue = DdbValue> {
|
|
80
88
|
static dec: TextDecoder;
|
|
81
89
|
static enc: TextEncoder;
|
|
@@ -128,8 +136,16 @@ export declare class DdbObj<T extends DdbValue = DdbValue> {
|
|
|
128
136
|
pack(): Uint8Array;
|
|
129
137
|
static pack_vector_body(value: DdbVectorValue, type: DdbType, length: number): ArrayBufferView[];
|
|
130
138
|
toString(): string;
|
|
131
|
-
to_cols():
|
|
139
|
+
to_cols(): {
|
|
140
|
+
title: string;
|
|
141
|
+
dataIndex: string;
|
|
142
|
+
render?: any;
|
|
143
|
+
}[];
|
|
132
144
|
to_rows<T extends Record<string, any> = Record<string, any>>(): T[];
|
|
145
|
+
to_dict<T = Record<string, any>>(): T;
|
|
146
|
+
}
|
|
147
|
+
export declare class DdbVoid extends DdbObj<undefined> {
|
|
148
|
+
constructor();
|
|
133
149
|
}
|
|
134
150
|
export declare class DdbBool extends DdbObj<boolean> {
|
|
135
151
|
constructor(value: boolean);
|
|
@@ -164,42 +180,84 @@ export declare class DdbPair extends DdbObj<Int32Array> {
|
|
|
164
180
|
export declare class DdbFunction extends DdbObj<DdbFunctionDefValue> {
|
|
165
181
|
constructor(name: string, type: DdbFunctionType);
|
|
166
182
|
}
|
|
183
|
+
export declare function date2str(date: number): string;
|
|
184
|
+
export declare function timestamp2str(timestamp: bigint): string;
|
|
167
185
|
export declare class DDB {
|
|
168
186
|
/** 当前的 session id (http 或 tcp) */
|
|
169
187
|
sid: string;
|
|
170
188
|
/** utf-8 text decoder */
|
|
171
189
|
dec: TextDecoder;
|
|
172
190
|
enc: TextEncoder;
|
|
173
|
-
|
|
174
|
-
|
|
191
|
+
/** DolphinDB WebSocket URL
|
|
192
|
+
e.g. `ws://127.0.0.1:8848/`, `wss://dolphindb.com`
|
|
193
|
+
*/
|
|
194
|
+
url: string;
|
|
195
|
+
websocket: WebSocket;
|
|
175
196
|
/** little endian (server) */
|
|
176
197
|
le: boolean;
|
|
177
198
|
/** little endian (client) */
|
|
178
199
|
static le_client: boolean;
|
|
200
|
+
/** python session flag (2048) */
|
|
201
|
+
python: boolean;
|
|
202
|
+
message_hook: (message: Uint8Array) => any;
|
|
203
|
+
/** print message handler */
|
|
204
|
+
printer(message: string): void;
|
|
179
205
|
/** resolver, rejector, promise of last rpc */
|
|
180
206
|
presolver(buf: Uint8Array): void;
|
|
181
207
|
prejector(error: Error): void;
|
|
182
208
|
presult: Promise<Uint8Array>;
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
209
|
+
/**
|
|
210
|
+
Initialize an instance of DolphinDB Client using the WebSocket URL
|
|
211
|
+
(without establishing an actual network connection)
|
|
212
|
+
@example
|
|
213
|
+
let ddb = new DDB('ws://127.0.0.1:8848')
|
|
214
|
+
let ddb_secure = new DDB('wss://dolphindb.com')
|
|
215
|
+
*/
|
|
216
|
+
constructor(url?: string);
|
|
217
|
+
/** Establish the actual WebSocket connection to the DolphinDB corresponding to the URL */
|
|
218
|
+
connect({ url, login, username, password, python, }?: {
|
|
219
|
+
/** By default, the WebSocket URL passed in when the instance is initialized is used */
|
|
220
|
+
url?: string;
|
|
221
|
+
/** Whether to automatically log in after the connection is established, the default is true */
|
|
189
222
|
login?: boolean;
|
|
190
|
-
/** DolphinDB
|
|
223
|
+
/** DolphinDB username */
|
|
191
224
|
username?: string;
|
|
192
|
-
/** DolphinDB
|
|
225
|
+
/** DolphinDB password */
|
|
193
226
|
password?: string;
|
|
227
|
+
/** set python session flag */
|
|
228
|
+
python?: boolean;
|
|
194
229
|
}): Promise<void>;
|
|
230
|
+
get_rpc_options({ urgent, secondary, async: _async, pickle, clear, api, compress, cancellable, priority, parallelism, root_id, limit, }?: {
|
|
231
|
+
urgent?: boolean;
|
|
232
|
+
/** API 提交的任务, secondary 必须为 false */
|
|
233
|
+
secondary?: boolean;
|
|
234
|
+
/** 是否异步任务(不返回结果) */
|
|
235
|
+
async?: boolean;
|
|
236
|
+
/** 让服务端以 pickle 协议返回数据 */
|
|
237
|
+
pickle?: boolean;
|
|
238
|
+
/** 本次任务完成后 clear session memory */
|
|
239
|
+
clear?: boolean;
|
|
240
|
+
/** 是否为 api client */
|
|
241
|
+
api?: boolean;
|
|
242
|
+
compress?: boolean;
|
|
243
|
+
/** 任务是否可以取消 */
|
|
244
|
+
cancellable?: boolean;
|
|
245
|
+
priority?: number;
|
|
246
|
+
/** `8` 0 ~ 64, 指定本任务并行度 */
|
|
247
|
+
parallelism?: number;
|
|
248
|
+
/** 根任务编号,内部使用,API中固定为空 */
|
|
249
|
+
root_id?: string;
|
|
250
|
+
/** 指定分块返回的块大小 */
|
|
251
|
+
limit?: boolean;
|
|
252
|
+
}): string;
|
|
195
253
|
disconnect(): void;
|
|
196
|
-
/** rpc through websocket (function command)
|
|
254
|
+
/** rpc through websocket (function/script/variable command)
|
|
197
255
|
- type: API 类型: 'script' | 'function' | 'variable'
|
|
198
256
|
- options:
|
|
199
257
|
- urgent?: 决定 `行为标识` 那一行字符串的取值(只适用于 script 和 function)
|
|
200
258
|
- vars?: type === 'variable' 时必传,variable 指令中待上传的变量名
|
|
201
259
|
*/
|
|
202
|
-
rpc<T extends DdbObj = DdbObj>(type: 'script' | 'function' | 'variable', { script, func, args, vars, urgent, }: {
|
|
260
|
+
rpc<T extends DdbObj = DdbObj>(type: 'script' | 'function' | 'variable' | 'connect', { script, func, args, vars, urgent, }: {
|
|
203
261
|
script?: string;
|
|
204
262
|
func?: string;
|
|
205
263
|
args?: (DdbObj | string | boolean)[];
|
|
@@ -212,14 +270,14 @@ export declare class DDB {
|
|
|
212
270
|
script: string,
|
|
213
271
|
/** 执行选项 */
|
|
214
272
|
{ urgent }?: {
|
|
215
|
-
/** 紧急 flag
|
|
273
|
+
/** 紧急 flag,确保提交的脚本使用 urgent worker 处理,防止被其它作业阻塞 */
|
|
216
274
|
urgent?: boolean;
|
|
217
275
|
}): Promise<T>;
|
|
218
276
|
/** call function through websocket (function command)
|
|
219
277
|
- func: 函数名
|
|
220
278
|
- args?: `[ ]` 调用参数 (传入的原生 string 和 boolean 会被自动转换为 DdbObj<string> 和 DdbObj<boolean>)
|
|
221
279
|
- options?: 调用选项
|
|
222
|
-
- urgent?: 紧急 flag
|
|
280
|
+
- urgent?: 紧急 flag。使用 urgent worker 执行,防止被其它作业阻塞
|
|
223
281
|
- node?: 设置结点 alias 时发送到集群中对应的结点执行 (使用 DolphinDB 中的 rpc 方法)
|
|
224
282
|
- nodes?: 设置多个结点 alias 时发送到集群中对应的多个结点执行 (使用 DolphinDB 中的 pnodeRun 方法)
|
|
225
283
|
- func_type?: 设置 node 参数时必传,需指定函数类型,其它情况下不传
|
|
@@ -234,12 +292,18 @@ export declare class DDB {
|
|
|
234
292
|
}): Promise<T>;
|
|
235
293
|
/** upload variable through websocket (variable command) */
|
|
236
294
|
upload(
|
|
237
|
-
/**
|
|
295
|
+
/** Uploaded variables' name */
|
|
238
296
|
vars: string[],
|
|
239
|
-
/**
|
|
297
|
+
/** Uploaded variables' value */
|
|
240
298
|
args: (DdbObj | string | boolean)[]): Promise<DdbObj<DdbValue>>;
|
|
241
299
|
/** 解析服务端响应报文,返回去掉 header 的 data buf */
|
|
242
|
-
parse_message(buf: Uint8Array):
|
|
300
|
+
parse_message(buf: Uint8Array): {
|
|
301
|
+
type: "message";
|
|
302
|
+
data: string;
|
|
303
|
+
} | {
|
|
304
|
+
type: "object";
|
|
305
|
+
data: Uint8Array;
|
|
306
|
+
};
|
|
243
307
|
/** 自动转换 js string, boolean 为 DdbObj */
|
|
244
308
|
to_ddbobj(value: DdbObj | string | boolean): DdbObj;
|
|
245
309
|
/** 转换 js 数组为 DdbObj[] (in place, 会修改原数组) */
|