dolphindb 0.0.1

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 ADDED
@@ -0,0 +1,273 @@
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
+ ## 简介
17
+ DolphinDB JavaScript API 封装了操作 DolphinDB 数据库的能力,如:连接数据库、执行脚本、调用函数、上传变量等
18
+
19
+ ## 特性
20
+ - 使用 WebSocket 与 DolphinDB 数据库通信,用二进制格式进行数据交换
21
+ - 支持在浏览器环境和 Node.js 环境中运行
22
+ - 使用了 JavaScript 中的 Int32Array 等 TypedArray 处理二进制数据,性能较高
23
+ - 单次调用支持最大 2 GB 数据的序列化上传,下载数据量不受限制
24
+
25
+ ## 安装
26
+ ```bash
27
+ # 在机器上安装最新版的 Node.js 及浏览器
28
+
29
+ # 在项目中安装 npm 包
30
+ npm install dolphindb
31
+ ```
32
+
33
+ ## 用法
34
+ ### 0. 初始化并连接到 DolphinDB
35
+ ```ts
36
+ import DDB from 'dolphindb'
37
+
38
+ // 初始化 WebSocket 连接地址
39
+ let ddb = new DDB('ws://127.0.0.1:8848')
40
+
41
+ // 建立到 DolphinDB 的 WebSocket 连接
42
+ await ddb.connect()
43
+ ```
44
+
45
+ #### connect 方法声明
46
+ ```ts
47
+ async connect (
48
+ options?: {
49
+ /** 默认使用实例初始化时传入的 WebSocket 链接地址 */
50
+ ws_url?: string
51
+
52
+ /** 是否在建立连接后自动登录,默认 true */
53
+ login?: boolean
54
+
55
+ /** DolphinDB 登录用户名 */
56
+ username?: string
57
+
58
+ /** DolphinDB 登录密码 */
59
+ password?: string
60
+ } = { }
61
+ ): Promise<void>
62
+ ```
63
+
64
+
65
+ ### 1. 调用函数
66
+ #### 例子
67
+ ```ts
68
+ import { DdbInt } from 'dolphindb'
69
+
70
+ const result = await ddb.call<DdbInt>('add', [new DdbInt(1), new DdbInt(1)])
71
+
72
+ console.log(result.value === 2) // true
73
+ ```
74
+
75
+ #### DolphinDB JavaScript API 用 DdbObj 对象来表示 DolphinDB 中的数据类型
76
+ 上面例子中,上传了两个参数 1 (对应 DolphinDB 中的 int 类型) 到 DolphinDB 数据库,作为 add 函数的参数,并接收函数调用的结果 result
77
+
78
+ `<DdbInt>` 用于 TypeScript 推断返回值的类型
79
+
80
+ - result 是一个 `DdbInt`,也是 `DdbObj<number>`
81
+ - result.form 是 `DdbForm.scalar`
82
+ - result.type 是 `DdbType.int`
83
+ - result.value 是 JavaScript 中原生的 `number` (int 的取值范围及精度可以用 JavaScript 的 number 准确表示)
84
+
85
+ ```ts
86
+ /** 可以表示所有 DolphinDB 数据库中的数据 */
87
+ class DdbObj <T extends DdbValue = DdbValue> {
88
+ /** 是否为小端 (little endian) */
89
+ le: boolean
90
+
91
+ /** 数据形式 https://www.dolphindb.cn/cn/help/DataTypesandStructures/DataForms/index.html */
92
+ form: DdbForm
93
+
94
+ /** 数据类型 https://www.dolphindb.cn/cn/help/DataTypesandStructures/DataTypes/index.html */
95
+ type: DdbType
96
+
97
+ /** 占用 parse 时传入的 buf 的长度 */
98
+ length: number
99
+
100
+ /** table name / column name */
101
+ name?: string
102
+
103
+ /**
104
+ 最低维、第 1 维
105
+ - vector: rows = n, cols = 1
106
+ - pair: rows = 2, cols = 1
107
+ - matrix: rows = n, cols = m
108
+ - set: 同 vector
109
+ - dict: 包含 keys, values 向量
110
+ - table: 同 matrix
111
+ */
112
+ rows?: number
113
+
114
+ /** 第 2 维 */
115
+ cols?: number
116
+
117
+ /** matrix 中值的类型,仅 matrix 才有 */
118
+ datatype?: DdbType
119
+
120
+ /** 实际数据。不同的 DdbForm, DdbType 使用 DdbValue 中不同的类型来表示实际数据 */
121
+ value: T
122
+
123
+ constructor (data: Partial<DdbObj> & { form: DdbForm, type: DdbType, length: number }) {
124
+ Object.assign(this, data)
125
+ }
126
+ }
127
+
128
+ class DdbInt extends DdbObj<number> {
129
+ constructor (value: number) {
130
+ super({
131
+ form: DdbForm.scalar,
132
+ type: DdbType.int,
133
+ length: 4,
134
+ value
135
+ })
136
+ }
137
+ }
138
+
139
+ // ... 还有很多快捷类,如 DdbString, DdbLong, DdbDouble, DdbVectorDouble, DdbVectorAny 等
140
+
141
+ type DdbValue =
142
+ null | boolean | number | [number, number] | bigint | string | string[] |
143
+ Uint8Array | Int16Array | Int32Array | Float32Array | Float64Array | BigInt64Array | Uint8Array[] |
144
+ DdbObj[] | DdbFunctionDefValue | DdbSymbolExtendedValue
145
+
146
+
147
+ enum DdbForm {
148
+ scalar = 0,
149
+ vector = 1,
150
+ pair = 2,
151
+ matrix = 3,
152
+ set = 4,
153
+ dict = 5,
154
+ table = 6,
155
+ chart = 7,
156
+ chunk = 8,
157
+ }
158
+
159
+
160
+ enum DdbType {
161
+ void = 0,
162
+ bool = 1,
163
+ char = 2,
164
+ short = 3,
165
+ int = 4,
166
+ long = 5,
167
+ // ...
168
+ timestamp = 12,
169
+ // ...
170
+ double = 16,
171
+ symbol = 17,
172
+ string = 18,
173
+ // ...
174
+ }
175
+ ```
176
+
177
+ #### call 方法声明
178
+ ```ts
179
+ async call <T extends DdbObj> (
180
+ /** 函数名 */
181
+ func: string,
182
+
183
+ /** 调用参数 (传入的原生 string 和 boolean 会被自动转换为 DdbObj<string> 和 DdbObj<boolean>) */
184
+ args?: (DdbObj | string | boolean)[] = [ ],
185
+
186
+ /** 调用选项 */
187
+ options?: {
188
+ /** 紧急 flag,使用 urgent worker 处理,防止被其它作业阻塞 */
189
+ urgent?: boolean
190
+
191
+ /** 设置结点 alias 时发送到集群中对应的结点执行 (使用 DolphinDB 中的 rpc 方法) */
192
+ node?: string
193
+
194
+ /** 设置多个结点 alias 时发送到集群中对应的多个结点执行 (使用 DolphinDB 中的 pnodeRun 方法) */
195
+ nodes?: string[]
196
+
197
+ /** 设置 node 参数时必传,需指定函数类型,其它情况下不传 */
198
+ func_type?: DdbFunctionType
199
+
200
+ /** 设置 nodes 参数时选传,其它情况不传 */
201
+ add_node_alias?: boolean
202
+ } = { }
203
+ ): Promise<T>
204
+ ```
205
+
206
+
207
+ ### 2. 执行脚本
208
+ #### 例子
209
+ ```ts
210
+ import type { DdbLong } from 'dolphindb'
211
+
212
+ const result = await ddb.eval<DdbLong>(
213
+ 'def foo (a, b) {\n' +
214
+ ' return a + b\n' +
215
+ '}\n' +
216
+ 'foo(1l, 1l)\n'
217
+ )
218
+
219
+ console.log(result.value === 2n) // true
220
+ ```
221
+
222
+ 上面例子中,通过字符串上传了一段脚本到 DolphinDB 数据库执行,并接收最后一条语句 `foo(1l, 1l)` 执行结果 result
223
+
224
+ `<DdbInt>` 用于 TypeScript 推断返回值的类型
225
+
226
+ - result 是一个 `DdbLong`,也是 `DdbObj<bigint>`
227
+ - result.form 是 `DdbForm.scalar`
228
+ - result.type 是 `DdbType.long`
229
+ - result.value 是 JavaScript 中原生的 `bigint` (long 的精度不能用 JavaScript 的 number 准确表示,但可以用 bigint 表示)
230
+
231
+ 只要 WebSocket 连接不断开,在后续的会话中 `foo` 这个自定义函数会一直存在,可复用,比如后续通过 `await ddb.call<DdbInt>('foo', [new DdbInt(1), new DdbInt(1)])` 调用这个自定义函数
232
+
233
+ #### eval 方法声明
234
+ ```ts
235
+ async eval <T extends DdbObj> (
236
+ /** 执行的脚本 */
237
+ script: string,
238
+
239
+ /** 执行选项 */
240
+ options: {
241
+ /** 紧急 flag,使用 urgent worker 处理,防止被其它作业阻塞 */
242
+ urgent?: boolean
243
+ } = { }
244
+ ): Promise<T>
245
+ ```
246
+
247
+
248
+ ### 3. 上传变量
249
+ #### 例子
250
+ ```ts
251
+ import { DdbVectorDouble } from 'dolphindb'
252
+
253
+ let a = new Array(10000)
254
+ a.fill(1.0)
255
+
256
+ ddb.upload(['bar1', 'bar2'], [new DdbVectorDouble(a), new DdbVectorDouble(a)])
257
+ ```
258
+
259
+ 上面的例子中,上传了 `bar1`, `bar2` 两个变量,变量值是长度为 10000 的 double 向量
260
+
261
+ 只要 WebSocket 连接不断开,在后续的会话中 `bar1`, `bar2` 这些变量会一直存在,可复用
262
+
263
+ #### upload 方法声明
264
+ ```ts
265
+ async upload (
266
+ /** 上传的变量名 */
267
+ vars: string[],
268
+
269
+ /** 上传的变量值 */
270
+ args: (DdbObj | string | boolean)[]
271
+ ): Promise<void>
272
+ ```
273
+
package/browser.d.ts ADDED
@@ -0,0 +1,249 @@
1
+ import 'xshell/prototype.browser';
2
+ export declare enum DdbForm {
3
+ scalar = 0,
4
+ vector = 1,
5
+ pair = 2,
6
+ matrix = 3,
7
+ set = 4,
8
+ dict = 5,
9
+ table = 6,
10
+ chart = 7,
11
+ chunk = 8
12
+ }
13
+ export declare enum DdbType {
14
+ void = 0,
15
+ bool = 1,
16
+ char = 2,
17
+ short = 3,
18
+ int = 4,
19
+ long = 5,
20
+ date = 6,
21
+ month = 7,
22
+ time = 8,
23
+ minute = 9,
24
+ second = 10,
25
+ datetime = 11,
26
+ timestamp = 12,
27
+ nanotime = 13,
28
+ nanotimestamp = 14,
29
+ float = 15,
30
+ double = 16,
31
+ symbol = 17,
32
+ string = 18,
33
+ uuid = 19,
34
+ functiondef = 20,
35
+ handle = 21,
36
+ code = 22,
37
+ datasource = 23,
38
+ resource = 24,
39
+ any = 25,
40
+ compress = 26,
41
+ dict = 27,
42
+ datehour = 28,
43
+ ipaddr = 30,
44
+ int128 = 31,
45
+ blob = 32,
46
+ complex = 34,
47
+ point = 35,
48
+ duration = 36,
49
+ symbol_extended = 145
50
+ }
51
+ export declare enum DdbFunctionType {
52
+ SystemFunc = 0,
53
+ SystemProc = 1,
54
+ OperatorFunc = 2,
55
+ UserDefinedFunc = 3,
56
+ PartialFunc = 4,
57
+ DynamicFunc = 5,
58
+ PiecewiseFunc = 6,
59
+ JitFunc = 7,
60
+ JitPartialFunc = 8
61
+ }
62
+ export interface DdbFunctionDefValue {
63
+ type: DdbFunctionType;
64
+ name: string;
65
+ }
66
+ export interface DdbSymbolExtendedValue {
67
+ base_id: number;
68
+ base: string[];
69
+ value: Uint32Array;
70
+ }
71
+ export declare type DdbValue = null | boolean | number | [number, number] | bigint | string | string[] | Uint8Array | Int16Array | Int32Array | Float32Array | Float64Array | BigInt64Array | Uint8Array[] | DdbObj[] | DdbFunctionDefValue | DdbSymbolExtendedValue;
72
+ export declare type DdbVectorValue = string | string[] | Uint8Array | Int16Array | Int32Array | Float32Array | Float64Array | BigInt64Array | Uint8Array[] | DdbObj[] | DdbSymbolExtendedValue;
73
+ export declare const nulls: {
74
+ readonly char: "€";
75
+ readonly int16: -32768;
76
+ readonly int32: -2147483648;
77
+ readonly int64: -9223372036854775808n;
78
+ };
79
+ export declare class DdbObj<T extends DdbValue = DdbValue> {
80
+ static dec: TextDecoder;
81
+ static enc: TextEncoder;
82
+ /** 维护已解析的 symbol base,比如流数据中后续的 symbol 向量可能只发送一个 base.id, base.size == 0, 依赖之前发送的 symbol base */
83
+ static symbol_bases: Record<number, string[]>;
84
+ /** little endian (client) */
85
+ static le_client: boolean;
86
+ /** 是否为小端 (little endian) */
87
+ le: boolean;
88
+ /** 数据形式 https://www.dolphindb.cn/cn/help/DataTypesandStructures/DataForms/index.html */
89
+ form: DdbForm;
90
+ /** 数据类型 https://www.dolphindb.cn/cn/help/DataTypesandStructures/DataTypes/index.html */
91
+ type: DdbType;
92
+ /** 占用 parse 时传入的 buf 的长度 */
93
+ length: number;
94
+ /** table name / column name */
95
+ name?: string;
96
+ /**
97
+ 最低维、第 1 维
98
+ - vector: rows = n, cols = 1
99
+ - pair: rows = 2, cols = 1
100
+ - matrix: rows = n, cols = m
101
+ - set: 同 vector
102
+ - dict: 包含 keys, values 向量
103
+ - table: 同 matrix
104
+ */
105
+ rows?: number;
106
+ /** 第 2 维 */
107
+ cols?: number;
108
+ /** matrix 中值的类型,仅 matrix 才有 */
109
+ datatype?: DdbType;
110
+ /** 实际数据。不同的 DdbForm, DdbType 使用 DdbValue 中不同的类型来表示实际数据 */
111
+ value: T;
112
+ constructor(data: Partial<DdbObj> & {
113
+ form: DdbForm;
114
+ type: DdbType;
115
+ length: number;
116
+ });
117
+ static parse(buf: Uint8Array, le: boolean): DdbObj<DdbValue>;
118
+ static parse_scalar(buf: Uint8Array, le: boolean, type: DdbType): [number, DdbValue];
119
+ /** parse: rows, cols, items
120
+ 返回的 ddbobj.length 不包括 vector 的 type 和 form
121
+ */
122
+ static parse_vector(buf: Uint8Array, le: boolean, type: DdbType): DdbObj;
123
+ /** 有可能没有字节对齐,不能直接使用原有 message 的 arraybuffer, 统一复制出来,让原有 arraybuffer 被回收掉比较好 */
124
+ static parse_vector_items(buf: Uint8Array, le: boolean, type: DdbType, length: number): [
125
+ number,
126
+ DdbVectorValue
127
+ ];
128
+ pack(): Uint8Array;
129
+ static pack_vector_body(value: DdbVectorValue, type: DdbType, length: number): ArrayBufferView[];
130
+ toString(): string;
131
+ to_cols(): any[];
132
+ to_rows<T extends Record<string, any> = Record<string, any>>(): T[];
133
+ }
134
+ export declare class DdbBool extends DdbObj<boolean> {
135
+ constructor(value: boolean);
136
+ }
137
+ export declare class DdbInt extends DdbObj<number> {
138
+ constructor(value: number);
139
+ }
140
+ export declare class DdbString extends DdbObj<string> {
141
+ constructor(value: string);
142
+ }
143
+ export declare class DdbLong extends DdbObj<bigint> {
144
+ constructor(value: bigint);
145
+ }
146
+ export declare class DdbDouble extends DdbObj<number> {
147
+ constructor(value: number);
148
+ }
149
+ export declare class DdbVectorInt extends DdbObj<Int32Array> {
150
+ constructor(value: number[]);
151
+ }
152
+ export declare class DdbVectorString extends DdbObj<string[]> {
153
+ constructor(value: string[]);
154
+ }
155
+ export declare class DdbVectorDouble extends DdbObj<Float64Array> {
156
+ constructor(value: number[]);
157
+ }
158
+ export declare class DdbVectorAny extends DdbObj {
159
+ constructor(value: DdbObj<DdbValue>[]);
160
+ }
161
+ export declare class DdbPair extends DdbObj<Int32Array> {
162
+ constructor(l: number, r?: number);
163
+ }
164
+ export declare class DdbFunction extends DdbObj<DdbFunctionDefValue> {
165
+ constructor(name: string, type: DdbFunctionType);
166
+ }
167
+ export declare class DDB {
168
+ /** 当前的 session id (http 或 tcp) */
169
+ sid: string;
170
+ /** utf-8 text decoder */
171
+ dec: TextDecoder;
172
+ enc: TextEncoder;
173
+ ws_url: string;
174
+ ws: WebSocket;
175
+ /** little endian (server) */
176
+ le: boolean;
177
+ /** little endian (client) */
178
+ static le_client: boolean;
179
+ /** resolver, rejector, promise of last rpc */
180
+ presolver(buf: Uint8Array): void;
181
+ prejector(error: Error): void;
182
+ presult: Promise<Uint8Array>;
183
+ constructor(ws_url?: string);
184
+ /** 连接到 DolphinDB Server */
185
+ connect({ ws_url, login, username, password, }?: {
186
+ /** 默认使用实例初始化时传入的 WebSocket 链接地址 */
187
+ ws_url?: string;
188
+ /** 是否在建立连接后自动登录,默认 true */
189
+ login?: boolean;
190
+ /** DolphinDB 登录用户名 */
191
+ username?: string;
192
+ /** DolphinDB 登录密码 */
193
+ password?: string;
194
+ }): Promise<void>;
195
+ disconnect(): void;
196
+ /** rpc through websocket (function command)
197
+ - type: API 类型: 'script' | 'function' | 'variable'
198
+ - options:
199
+ - urgent?: 决定 `行为标识` 那一行字符串的取值(只适用于 script 和 function)
200
+ - vars?: type === 'variable' 时必传,variable 指令中待上传的变量名
201
+ */
202
+ rpc<T extends DdbObj = DdbObj>(type: 'script' | 'function' | 'variable', { script, func, args, vars, urgent, }: {
203
+ script?: string;
204
+ func?: string;
205
+ args?: (DdbObj | string | boolean)[];
206
+ vars?: string[];
207
+ urgent?: boolean;
208
+ }): Promise<T>;
209
+ /** eval script through websocket (script command) */
210
+ eval<T extends DdbObj>(
211
+ /** 执行的脚本 */
212
+ script: string,
213
+ /** 执行选项 */
214
+ { urgent }?: {
215
+ /** 紧急 flag,使用 urgent worker 处理,防止被其它作业阻塞 */
216
+ urgent?: boolean;
217
+ }): Promise<T>;
218
+ /** call function through websocket (function command)
219
+ - func: 函数名
220
+ - args?: `[ ]` 调用参数 (传入的原生 string 和 boolean 会被自动转换为 DdbObj<string> 和 DdbObj<boolean>)
221
+ - options?: 调用选项
222
+ - urgent?: 紧急 flag,使用 urgent worker 处理,防止被其它作业阻塞
223
+ - node?: 设置结点 alias 时发送到集群中对应的结点执行 (使用 DolphinDB 中的 rpc 方法)
224
+ - nodes?: 设置多个结点 alias 时发送到集群中对应的多个结点执行 (使用 DolphinDB 中的 pnodeRun 方法)
225
+ - func_type?: 设置 node 参数时必传,需指定函数类型,其它情况下不传
226
+ - add_node_alias?: 设置 nodes 参数时选传,其它情况不传
227
+ */
228
+ call<T extends DdbObj>(func: string, args?: (DdbObj | string | boolean)[], { urgent, node, nodes, func_type, add_node_alias }?: {
229
+ urgent?: boolean;
230
+ node?: string;
231
+ nodes?: string[];
232
+ func_type?: DdbFunctionType;
233
+ add_node_alias?: boolean;
234
+ }): Promise<T>;
235
+ /** upload variable through websocket (variable command) */
236
+ upload(
237
+ /** 上传的变量名 */
238
+ vars: string[],
239
+ /** 上传的变量值 */
240
+ args: (DdbObj | string | boolean)[]): Promise<DdbObj<DdbValue>>;
241
+ /** 解析服务端响应报文,返回去掉 header 的 data buf */
242
+ parse_message(buf: Uint8Array): Uint8Array;
243
+ /** 自动转换 js string, boolean 为 DdbObj */
244
+ to_ddbobj(value: DdbObj | string | boolean): DdbObj;
245
+ /** 转换 js 数组为 DdbObj[] (in place, 会修改原数组) */
246
+ to_ddbobjs(values: any[]): DdbObj<DdbValue>[];
247
+ }
248
+ export declare let ddb: DDB;
249
+ export default ddb;