dolphindb 2.0.800 → 2.0.900

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 CHANGED
@@ -21,7 +21,7 @@ DolphinDB JavaScript API is a JavaScript library that encapsulates the ability t
21
21
  https://www.npmjs.com/package/dolphindb
22
22
 
23
23
  ## Features
24
- - Communicate with DolphinDB database using WebSocket, exchange data in binary format
24
+ - Use WebSocket to communicate with DolphinDB database, exchange data in binary format, and support real-time push of streaming data
25
25
  - Support running in browser environment and Node.js environment
26
26
  - Use TypedArray such as Int32Array in JavaScript to process binary data, with high performance
27
27
  - A single call supports serialized upload of up to 2GB of data, and the amount of downloaded data is not limited
@@ -48,10 +48,13 @@ import { DDB } from 'dolphindb'
48
48
  // The import method for existing projects using CommonJS modules is const { DDB } = require('dolphindb')
49
49
  // Use in browser: import { DDB } form 'dolphindb/browser.js'
50
50
 
51
- // Create a database object and initialize the WebSocket URL
51
+ // Initially connect to an instance of DolphinDB using the WebSocket URL (without establishing an actual network connection)
52
52
  let ddb = new DDB('ws://127.0.0.1:8848')
53
53
 
54
- // Establish a WebSocket connection to DolphinDB (requires DolphinDB database version at least 1.30.16 or 2.00.4)
54
+ // Encrypt with HTTPS
55
+ // let ddb = new DDB('wss://dolphindb.com')
56
+
57
+ // Establish a connection to DolphinDB (requires DolphinDB database version at least 1.30.16 or 2.00.4)
55
58
  await ddb.connect()
56
59
  ```
57
60
 
@@ -71,7 +74,10 @@ let ddbsecure = new DDB('wss://dolphindb.com', {
71
74
  password: '123456',
72
75
 
73
76
  // set python session flag, default `false`
74
- python: false
77
+ python: false,
78
+
79
+ // After setting this option, the database connection is only used for streaming data. For details, see `5. Streaming Data`
80
+ streaming: undefined
75
81
  })
76
82
  ```
77
83
 
@@ -336,7 +342,7 @@ async upload (
336
342
  ```
337
343
 
338
344
 
339
- ### Some Examples
345
+ ### 4. Some Examples
340
346
  ```ts
341
347
  import { nulls, DdbInt, timestamp2str, DdbVectorSymbol, DdbTable, DdbVectorDouble } from 'dolphindb'
342
348
 
@@ -371,3 +377,72 @@ new DdbTable(
371
377
  )
372
378
  ```
373
379
 
380
+ ### 5. Streaming Data
381
+ ```ts
382
+ // New Streaming Data Connection Configuration
383
+ let sddb = new DDB('ws://192.168.0.43:8800', {
384
+ autologin: true,
385
+ username: 'admin',
386
+ password: '123456',
387
+ streaming: {
388
+ table: 'Streaming table name to subscribe to',
389
+
390
+ // Streaming data processing callback, the type of message is StreamingData
391
+ handler (message) {
392
+ console.log(message)
393
+ }
394
+ }
395
+ })
396
+
397
+ // Establish connection
398
+ await sddb.connect()
399
+ ```
400
+
401
+ The streaming data received after the connection is established will be used as the message parameter of the handler. The type of the message is StreamingData, as follows:
402
+
403
+ ```ts
404
+ export interface StreamingParams {
405
+ table: string
406
+ action?: string
407
+
408
+ handler (message: StreamingData): any
409
+ }
410
+
411
+ export interface StreamingData extends StreamingParams {
412
+ /**
413
+ The time the server sent the message (nano seconds since epoch)
414
+ std::chrono::system_clock::now().time_since_epoch() / std::chrono::nanoseconds(1)
415
+ */
416
+ time: bigint
417
+
418
+ /** message id */
419
+ id: bigint
420
+
421
+ colnames: string[]
422
+
423
+ /** Subscription topic, which is the name of a subscription.
424
+ It is a string consisting of the alias of the node where the subscription table is located, the stream data table name, and the subscription task name (if actionName is specified), separated by `/`
425
+ */
426
+ topic: string
427
+
428
+ /** Streaming data, the type is any vector, each element of which corresponds to a column (without name) of the subscribed table, and the content in the column (DdbObj<DdbVectorValue>) is the new data value */
429
+ data: DdbObj<DdbVectorObj[]>
430
+
431
+ /** Number of new streaming data rows */
432
+ rows: number
433
+
434
+ window: {
435
+ /** The establishment of the connection starts offset = 0, and gradually increases as the window moves */
436
+ offset: number
437
+
438
+ /** sum of segment.row in segments */
439
+ rows: number
440
+
441
+ /** An array of data received each time */
442
+ segments: DdbObj<DdbVectorObj[]>[]
443
+ }
444
+
445
+ /** After successfully subscribed, if the subsequently pushed message is parsed incorrectly, the error will be set and the handler will be called. */
446
+ error?: Error
447
+ }
448
+ ```
package/README.zh.md CHANGED
@@ -21,7 +21,7 @@ DolphinDB JavaScript API 是一个 JavaScript 库,封装了操作 DolphinDB
21
21
  https://www.npmjs.com/package/dolphindb
22
22
 
23
23
  ## 特性
24
- - 使用 WebSocket 与 DolphinDB 数据库通信,用二进制格式进行数据交换
24
+ - 使用 WebSocket 与 DolphinDB 数据库通信,用二进制格式进行数据交换,支持流数据实时推送
25
25
  - 支持在浏览器环境和 Node.js 环境中运行
26
26
  - 使用了 JavaScript 中的 Int32Array 等 TypedArray 处理二进制数据,性能较高
27
27
  - 单次调用支持最大 2 GB 数据的序列化上传,下载数据量不受限制
@@ -48,19 +48,19 @@ import { DDB } from 'dolphindb'
48
48
  // 已有的使用 CommonJS 模块的项目的导入方法为 const { DDB } = require('dolphindb')
49
49
  // 在浏览器中使用: import { DDB } form 'dolphindb/browser.js'
50
50
 
51
- // 创建数据库对象,初始化 WebSocket 连接地址
51
+ // 使用 WebSocket URL 初始化连接到 DolphinDB 的实例(不建立实际的网络连接)
52
52
  let ddb = new DDB('ws://127.0.0.1:8848')
53
53
 
54
- // 建立到 DolphinDB 的 WebSocket 连接(要求 DolphinDB 数据库版本不低于 1.30.16 或 2.00.4)
54
+ // 使用 HTTPS 加密
55
+ // let ddb = new DDB('wss://dolphindb.com')
56
+
57
+ // 建立到 DolphinDB 的连接(要求 DolphinDB 数据库版本不低于 1.30.16 或 2.00.4)
55
58
  await ddb.connect()
56
59
  ```
57
60
 
58
61
  #### DDB 选项
59
62
  ```ts
60
- let ddb = new DDB('ws://127.0.0.1:8848')
61
-
62
- // 使用 HTTPS 加密
63
- let ddbsecure = new DDB('wss://dolphindb.com', {
63
+ let ddb = new DDB('ws://127.0.0.1:8848', {
64
64
  // 是否在建立连接后自动登录,默认 `true`
65
65
  autologin: true,
66
66
 
@@ -71,7 +71,10 @@ let ddbsecure = new DDB('wss://dolphindb.com', {
71
71
  password: '123456',
72
72
 
73
73
  // 设置 python session flag,默认 `false`
74
- python: false
74
+ python: false,
75
+
76
+ // 设置该选项后,该数据库连接只用于流数据,详细用法见后文 `5. 流数据`
77
+ streaming: undefined
75
78
  })
76
79
  ```
77
80
 
@@ -336,7 +339,7 @@ async upload (
336
339
  ```
337
340
 
338
341
 
339
- ### 一些例子
342
+ ### 4. 一些例子
340
343
  ```ts
341
344
  import { nulls, DdbInt, timestamp2str, DdbVectorSymbol, DdbTable, DdbVectorDouble } from 'dolphindb'
342
345
 
@@ -370,3 +373,74 @@ new DdbTable(
370
373
  'mytable'
371
374
  )
372
375
  ```
376
+
377
+
378
+ ### 5. 流数据
379
+ ```ts
380
+ // 新建流数据连接配置
381
+ let sddb = new DDB('ws://192.168.0.43:8800', {
382
+ autologin: true,
383
+ username: 'admin',
384
+ password: '123456',
385
+ streaming: {
386
+ table: '要订阅的流表名称',
387
+
388
+ // 流数据处理回调, message 的类型是 StreamingData
389
+ handler (message) {
390
+ console.log(message)
391
+ }
392
+ }
393
+ })
394
+
395
+ // 建立连接
396
+ await sddb.connect()
397
+ ```
398
+
399
+ 连接建立后接收到的流数据会作为 message 参数调用 handler, message 的类型是 StreamingData, 如下:
400
+
401
+ ```ts
402
+ export interface StreamingParams {
403
+ table: string
404
+ action?: string
405
+
406
+ handler (message: StreamingData): any
407
+ }
408
+
409
+ export interface StreamingData extends StreamingParams {
410
+ /**
411
+ server 发送消息的时间 (nano seconds since epoch)
412
+ std::chrono::system_clock::now().time_since_epoch() / std::chrono::nanoseconds(1)
413
+ */
414
+ time: bigint
415
+
416
+ /** message id */
417
+ id: bigint
418
+
419
+ colnames: string[]
420
+
421
+ /** 订阅主题,即一个订阅的名称。
422
+ 它是一个字符串,由订阅表所在节点的别名、流数据表名称和订阅任务名称(如果指定了 actionName)组合而成,使用 `/` 分隔
423
+ */
424
+ topic: string
425
+
426
+ /** 流数据,类型是 any vector, 其中的每一个元素对应被订阅表的一个列 (没有 name),列 (DdbObj<DdbVectorValue>) 中的内容是新增的数据值 */
427
+ data: DdbObj<DdbVectorObj[]>
428
+
429
+ /** 新增的流数据行数 */
430
+ rows: number
431
+
432
+ window: {
433
+ /** 建立连接开始 offset = 0, 随着 window 的移动逐渐增加 */
434
+ offset: number
435
+
436
+ /** segments 中 segment.row 的总和 */
437
+ rows: number
438
+
439
+ /** 每次接收到的 data 组成的数组 */
440
+ segments: DdbObj<DdbVectorObj[]>[]
441
+ }
442
+
443
+ /** 成功订阅后,后续推送过来的 message 解析错误,则会设置 error 并调用 handler */
444
+ error?: Error
445
+ }
446
+ ```
package/browser.d.ts CHANGED
@@ -124,12 +124,15 @@ export interface DdbArrayVectorBlock {
124
124
  lengths: Uint8Array | Uint16Array | Uint32Array;
125
125
  data: Int8Array | Int16Array | Int32Array | Float32Array | Float64Array | BigInt64Array;
126
126
  }
127
+ export type DdbArrayVectorValue = DdbArrayVectorBlock[] & /* decimal32, decimal64 会有这个属性 */ {
128
+ scale?: number;
129
+ };
127
130
  export interface DdbMatrixValue {
128
131
  rows: DdbVectorObj;
129
132
  cols: DdbVectorObj;
130
133
  data: DdbVectorValue;
131
134
  }
132
- export declare type DdbDictValue = [DdbVectorObj, DdbVectorObj];
135
+ export type DdbDictValue = [DdbVectorObj, DdbVectorObj];
133
136
  export declare enum DdbChartType {
134
137
  area = 0,
135
138
  bar = 1,
@@ -164,24 +167,24 @@ export interface DdbChartValue {
164
167
  };
165
168
  data: DdbMatrixObj;
166
169
  }
167
- export declare type DdbScalarValue = null | boolean | number | bigint | string | Uint8Array | // uuid, ipaddr, int128, blob
170
+ export type DdbScalarValue = null | boolean | number | bigint | string | Uint8Array | // uuid, ipaddr, int128, blob
168
171
  [
169
172
  number,
170
173
  number
171
174
  ] | // complex, point
172
175
  DdbFunctionDefValue | DdbDurationValue | DdbDecimal32Value | DdbDecimal64Value;
173
- export declare type DdbVectorValue = Uint8Array | Int8Array | Int16Array | Int32Array | Float32Array | Float64Array | BigInt64Array | string[] | // string[]
176
+ export type DdbVectorValue = Uint8Array | Int8Array | Int16Array | Int32Array | Float32Array | Float64Array | BigInt64Array | string[] | // string[]
174
177
  Uint8Array[] | // blob
175
178
  DdbObj[] | // any
176
- DdbSymbolExtendedValue | DdbArrayVectorBlock[] | DdbDecimal32VectorValue | DdbDecimal64VectorValue;
177
- export declare type DdbValue = DdbScalarValue | DdbVectorValue | DdbMatrixValue | DdbDictValue | DdbChartValue;
178
- export declare type DdbVectorObj = DdbObj<DdbVectorValue>;
179
- export declare type DdbVectorAnyObj = DdbObj<DdbObj[]>;
180
- export declare type DdbVectorStringObj = DdbObj<string[]>;
181
- export declare type DdbTableObj = DdbObj<DdbVectorObj[]>;
182
- export declare type DdbDictObj = DdbObj<[DdbVectorObj, DdbVectorObj]>;
183
- export declare type DdbMatrixObj = DdbObj<DdbMatrixValue>;
184
- export declare type DdbChartObj = DdbObj<DdbChartValue>;
179
+ DdbSymbolExtendedValue | DdbArrayVectorValue | DdbDecimal32VectorValue | DdbDecimal64VectorValue;
180
+ export type DdbValue = DdbScalarValue | DdbVectorValue | DdbMatrixValue | DdbDictValue | DdbChartValue;
181
+ export type DdbVectorObj = DdbObj<DdbVectorValue>;
182
+ export type DdbVectorAnyObj = DdbObj<DdbObj[]>;
183
+ export type DdbVectorStringObj = DdbObj<string[]>;
184
+ export type DdbTableObj = DdbObj<DdbVectorObj[]>;
185
+ export type DdbDictObj = DdbObj<[DdbVectorObj, DdbVectorObj]>;
186
+ export type DdbMatrixObj = DdbObj<DdbMatrixValue>;
187
+ export type DdbChartObj = DdbObj<DdbChartValue>;
185
188
  export declare const nulls: {
186
189
  readonly int8: -128;
187
190
  readonly int16: -32768;
@@ -192,14 +195,6 @@ export declare const nulls: {
192
195
  readonly double: number;
193
196
  readonly bytes16: Uint8Array;
194
197
  };
195
- export declare const timezone_offset: number;
196
- export declare enum StreamingStatusCode {
197
- ok = 0,
198
- connection_existed = 1,
199
- not_leader = 2,
200
- other = 3,
201
- error = 4
202
- }
203
198
  /** 可以表示所有 DolphinDB 数据库中的数据类型 */
204
199
  export declare class DdbObj<TValue extends DdbValue = DdbValue> {
205
200
  static dec: TextDecoder;
@@ -262,7 +257,7 @@ export declare class DdbObj<TValue extends DdbValue = DdbValue> {
262
257
  to_cols(): {
263
258
  title: string;
264
259
  dataIndex: string;
265
- render?: any;
260
+ render: (value: any) => string;
266
261
  }[];
267
262
  to_rows<T extends Record<string, any> = Record<string, any>>(): T[];
268
263
  /** Automatically convert dict<string, any> to js object (Record<string, any>)
@@ -283,10 +278,14 @@ export interface InspectOptions {
283
278
  colors?: boolean;
284
279
  /** decimal places */
285
280
  decimals?: number;
281
+ /** `false` 决定 null 值如何返回. nullstr ? 'null' : '' */
282
+ nullstr?: boolean;
283
+ /** `false` 决定 string, symbol, char 类型是否加引号 */
284
+ quote?: boolean;
286
285
  }
287
286
  /** Formats a single element (value) as a string according to DdbType, null returns a 'null' string */
288
287
  export declare function format(type: DdbType, value: DdbValue, le: boolean, options?: InspectOptions): string;
289
- /** formatted vector, the index-th item in the collection is a string, a null value returns a 'null' string */
288
+ /** formatted vector, the index-th item in the collection is a string */
290
289
  export declare function formati(obj: DdbVectorObj, index: number, options?: InspectOptions): string;
291
290
  export declare class DdbVoid extends DdbObj<undefined> {
292
291
  constructor();
@@ -428,10 +427,10 @@ export interface StreamingData extends StreamingParams {
428
427
  It is a string consisting of the alias of the node where the subscription table is located, the stream data table name, and the subscription task name (if actionName is specified), separated by `/`
429
428
  */
430
429
  topic: string;
431
- /** The schema of the flow table, the type is table, there is no data in the column vector (rows === 0), only the column name and type */
432
- schema: DdbTableObj;
433
430
  /** Stream data, the type is any vector, each element of which corresponds to a column (without name) of the subscribed table, and the content in the column (DdbObj<DdbVectorValue>) is the new data value */
434
431
  data: DdbObj<DdbVectorObj[]>;
432
+ /** Number of new streaming data rows */
433
+ rows: number;
435
434
  window: {
436
435
  /** The establishment of the connection starts offset = 0, and gradually increases as the window moves */
437
436
  offset: number;
@@ -440,8 +439,14 @@ export interface StreamingData extends StreamingParams {
440
439
  /** An array of data received each time */
441
440
  segments: DdbObj<DdbVectorObj[]>[];
442
441
  };
442
+ /** After successfully subscribed, if the subsequently pushed message is parsed incorrectly, the error will be set and the handler will be called. */
443
+ error?: Error;
444
+ }
445
+ export declare const winsize: 100000;
446
+ export declare class ConnectionError extends Error {
447
+ ddb: DDB;
448
+ constructor(ddb: DDB);
443
449
  }
444
- export declare const winsize: 40;
445
450
  export declare class DDB {
446
451
  /** 当前的 session id (http 或 tcp) */
447
452
  sid: string;
@@ -508,23 +513,10 @@ export declare class DDB {
508
513
  streaming?: StreamingParams;
509
514
  });
510
515
  private on_message;
511
- /** Establish the actual WebSocket connection to the DolphinDB corresponding to the URL
512
- - options?:
513
- - url?: DolphinDB WebSocket URL. By default, the WebSocket URL passed in when the instance is initialized is used
514
- - autologin?: Whether to log in automatically after establishing a connection, default `true`
515
- - username?: DolphinDB username, default `'admin'`
516
- - password?: DolphinDB password, default `'123456'`
517
- - python?: set python session flag, default `false`
518
- - streaming?: When this option is set, the WebSocket connection is only used for streaming data
519
- */
520
- connect({ url, autologin, username, password, python, streaming }?: {
521
- url?: string;
522
- autologin?: boolean;
523
- username?: string;
524
- password?: string;
525
- python?: boolean;
526
- streaming?: StreamingParams;
527
- }): Promise<void>;
516
+ /** Establish a actual websocket connection to the DolphindB corresponding to the URL
517
+ After calling, it will ensure that it has been connected to the database (ensure that websocket.ReadyState is open), otherwise an error will be reported
518
+ this.autologin automatically log in when it is true */
519
+ connect(): Promise<void>;
528
520
  get_rpc_options({ urgent, secondary, async: _async, pickle, clear, api, compress, cancellable, priority, parallelism, root_id, limit, }?: {
529
521
  urgent?: boolean;
530
522
  /** API 提交的任务, secondary 必须为 false */
@@ -550,6 +542,7 @@ export declare class DDB {
550
542
  }): string;
551
543
  disconnect(): void;
552
544
  /** rpc through websocket (function/script/variable command)
545
+ When the DDB is not connected, the call will be automatically connected. When the connection is disconnected, the call will throw the Connectionerror
553
546
  - type: API 类型: 'script' | 'function' | 'variable'
554
547
  - options:
555
548
  - urgent?: 决定 `行为标识` 那一行字符串的取值(只适用于 script 和 function)
@@ -618,7 +611,7 @@ export declare class DDB {
618
611
  /** 解析服务端响应报文,返回去掉 header 的 data buf */
619
612
  parse_message(buf: Uint8Array, parse_object?: boolean): DdbMessage;
620
613
  /** Internal stream subscription method */
621
- subscribe(): Promise<DdbTableObj>;
614
+ subscribe(): Promise<StreamingData>;
622
615
  }
623
616
  export interface DdbMessageListener {
624
617
  (message: DdbMessage, _this: DDB): any;
@@ -635,5 +628,5 @@ export interface DdbErrorMessage {
635
628
  type: 'error';
636
629
  data: Error;
637
630
  }
638
- export declare type DdbMessage = DdbPrintMessage | DdbObjectMessage | DdbErrorMessage;
631
+ export type DdbMessage = DdbPrintMessage | DdbObjectMessage | DdbErrorMessage;
639
632
  export declare let ddb: DDB;