dolphindb 3.0.0 → 3.0.2

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
@@ -26,57 +26,81 @@ https://www.npmjs.com/package/dolphindb
26
26
  - Use TypedArray such as Int32Array in JavaScript to process binary data, with high performance
27
27
  - Support serialized upload of up to 2GB of data with a single call, and the amount of downloaded data is not limited
28
28
 
29
- ## Installation
29
+ ## Usage
30
+ ### Initialize and connect to DolphinDB
30
31
 
31
- 1. Install the latest version of Node.js and browser.
32
- 2. (Optional) Create a new project (skip this step if there's an existing project):
32
+ #### Method 1: Use the built CDN version directly in the browser
33
33
 
34
- ```bash
35
- mkdir dolphindb-example
36
- cd dolphindb-example
37
- npm init --yes
38
- ```
39
- 3. Open the package.json file with an editor, and add the line `"type": "module"`, below `"main": "./index.js"`. This enables the ECMAScript modules. In the following code, you can use `import { DDB } from 'dolphindb'` to import npm packages.
40
- 4. Install npm packages in your project.
34
+ Save the following content to the `example.html` file, open it with a browser and run it. F12 opens the debugging console to see the log.
41
35
 
42
- ```bash
43
- npm install dolphindb
44
- ```
36
+ ```html
37
+ <!doctype html>
38
+ <html>
39
+ <head>
40
+ <title>DolphinDB</title>
41
+ <meta charset='utf-8' />
42
+ </head>
43
+ <body>
44
+ <script type="module">
45
+ import { DDB } from 'https://cdn.dolphindb.cn/assets/api.js'
46
+
47
+ let ddb = new DDB('ws://127.0.0.1:8848')
48
+
49
+ await ddb.connect()
50
+
51
+ console.log(
52
+ await ddb.eval('1 + 1')
53
+ )
54
+ </script>
55
+ </body>
56
+ </html>
57
+ ```
58
+
59
+ #### Method 2: Install the npm package in the project and import it
60
+
61
+ ##### 1. Installation
62
+
63
+ 1.1. Install the latest version of Node.js and browser on the machine.
64
+ - windows: https://nodejs.org/en/download/current/
65
+ - linux: https://github.com/nodesource/distributions?tab=readme-ov-file#debian-and-ubuntu-based-distributions
66
+ 1.2. (Optional) Create a new project using the following command. If you already have a project, you can skip this step.
67
+ ```bash
68
+ mkdir dolphindb-example
69
+ cd dolphindb-example
70
+ npm init --yes
71
+ ```
72
+ 1.3. Open the package.json file with an editor and add a line "type": "module" below `"main": "./index.js"`. This will enable ECMAScript modules. You can use `import { DDB } from 'dolphindb'` to import npm package.
73
+ 1.4. Install the npm package in the project.
74
+ ```bash
75
+ npm install dolphindb
76
+ ```
77
+
78
+ ##### 2. Use
45
79
 
46
- ## Usage
47
- ### Initializing and connecting to DolphinDB
48
- #### NPM
49
80
  ```ts
50
- import { DDB } from 'dolphindb'
81
+ // 2.1 Use the following method to import in the browser environment
82
+ import { DDB } from 'dolphindb/browser.js'
83
+
84
+ // 2.1 Use the following method to import in Node.js environment
85
+ // import { DDB } from 'dolphindb'
51
86
  // The import method for existing projects using CommonJS modules is const { DDB } = await import('dolphindb')
52
- // Use in browser: import { DDB } from 'dolphindb/browser.js'
53
87
 
54
- // Initially connect to an instance of DolphinDB using the WebSocket URL (without establishing an actual network connection)
88
+ // 2.2 Use the WebSocket URL to initialize the connection to the DolphinDB instance (without establishing an actual network connection)
55
89
  let ddb = new DDB('ws://127.0.0.1:8848')
56
90
 
57
- // Encrypt with HTTPS
91
+ // Use HTTPS encryption
58
92
  // let ddb = new DDB('wss://dolphindb.com')
59
93
 
60
- // Establish a connection to DolphinDB (requires DolphinDB database version at least 1.30.16 or 2.00.4)
94
+ // 2.3 Establish a connection to DolphinDB (requires DolphinDB database version to be no less than 1.30.16 or 2.00.4)
61
95
  await ddb.connect()
62
96
  ```
63
97
 
64
- #### CDN
65
- ```html
66
- <script type="module">
67
- import { DDB } from 'https://cdn.dolphindb.cn/assets/api.js'
68
-
69
- let ddb = new DDB('ws://127.0.0.1:8848')
70
-
71
- await ddb.connect()
72
- </script>
73
- ```
74
-
75
- Data for code completion and function prompts:
76
- - https://cdn.dolphindb.cn/assets/docs.zh.json
98
+ #### Code completion, function prompt data
99
+ - https://cdn.dolphindb.cn/assets/docs.zh.json
77
100
  - https://cdn.dolphindb.cn/assets/docs.en.json
78
101
 
79
- #### DDB options
102
+
103
+ #### DDB Connection Options
80
104
  ```ts
81
105
  let ddb = new DDB('ws://127.0.0.1:8848')
82
106
 
package/README.zh.md CHANGED
@@ -26,58 +26,81 @@ https://www.npmjs.com/package/dolphindb
26
26
  - 使用了 JavaScript 中的 Int32Array 等 TypedArray 处理二进制数据,性能较高
27
27
  - 单次调用支持最大 2 GB 数据的序列化上传,下载数据量不受限制
28
28
 
29
- ## 安装
29
+ ## 用法
30
+ ### 初始化并连接到 DolphinDB
31
+
32
+ #### 方法一:在浏览器中直接使用构建好的 CDN 版本
33
+
34
+ 保存以下内容到 `example.html` 文件,用浏览器打开即可运行,F12 打开调试控制台可以看到日志
35
+
36
+ ```html
37
+ <!doctype html>
38
+ <html>
39
+ <head>
40
+ <title>DolphinDB</title>
41
+ <meta charset='utf-8' />
42
+ </head>
43
+ <body>
44
+ <script type="module">
45
+ import { DDB } from 'https://cdn.dolphindb.cn/assets/api.js'
46
+
47
+ let ddb = new DDB('ws://127.0.0.1:8848')
48
+
49
+ await ddb.connect()
50
+
51
+ console.log(
52
+ await ddb.eval('1 + 1')
53
+ )
54
+ </script>
55
+ </body>
56
+ </html>
57
+ ```
58
+
59
+ #### 方法二:在项目中安装 npm 包并导入
60
+
61
+ ##### 1. 安装
30
62
 
31
- 1. 在机器上安装最新版的 Node.js 及浏览器。
32
- 2. (可选)使用以下命令创建新项目。如果已有项目,可跳过此步。
63
+ 1.1. 在机器上安装最新版的 Node.js 及浏览器。
64
+ - windows: https://nodejs.org/en/download/current/
65
+ - linux: https://github.com/nodesource/distributions?tab=readme-ov-file#debian-and-ubuntu-based-distributions
66
+ 1.2. (可选)使用以下命令创建新项目。如果已有项目,可跳过此步。
33
67
  ```bash
34
68
  mkdir dolphindb-example
35
69
  cd dolphindb-example
36
70
  npm init --yes
37
71
  ```
38
- 3. 用编辑器打开 package.json 文件,在 `"main": "./index.js"` 下方加入一行 "type": "module", 这样能够启用 ECMAScript modules,在后面代码中可以使用 `import { DDB } from 'dolphindb'` 导入 npm 包。
39
- 4. 在项目中安装 npm 包。
72
+ 1.3. 用编辑器打开 package.json 文件,在 `"main": "./index.js"` 下方加入一行 "type": "module", 这样能够启用 ECMAScript modules,在后面代码中可以使用 `import { DDB } from 'dolphindb'` 导入 npm 包。
73
+ 1.4. 在项目中安装 npm 包。
40
74
  ```bash
41
75
  npm install dolphindb
42
76
  ```
43
77
 
44
- ## 用法
45
- ### 初始化并连接到 DolphinDB
46
- #### NPM
78
+ ##### 2. 使用
47
79
 
48
80
  ```ts
49
- import { DDB } from 'dolphindb'
81
+ // 2.1 在浏览器环境中用下面的方法导入
82
+ import { DDB } from 'dolphindb/browser.js'
83
+
84
+ // 2.1 在 Node.js 环境中用下面的方法导入
85
+ // import { DDB } from 'dolphindb'
50
86
  // 已有的使用 CommonJS 模块的项目的导入方法为 const { DDB } = await import('dolphindb')
51
- // 在浏览器中使用: import { DDB } from 'dolphindb/browser.js'
52
87
 
53
- // 使用 WebSocket URL 初始化连接到 DolphinDB 的实例(不建立实际的网络连接)
88
+ // 2.2 使用 WebSocket URL 初始化连接到 DolphinDB 的实例(不建立实际的网络连接)
54
89
  let ddb = new DDB('ws://127.0.0.1:8848')
55
90
 
56
91
  // 使用 HTTPS 加密
57
92
  // let ddb = new DDB('wss://dolphindb.com')
58
93
 
59
- // 建立到 DolphinDB 的连接(要求 DolphinDB 数据库版本不低于 1.30.16 或 2.00.4)
94
+ // 2.3 建立到 DolphinDB 的连接(要求 DolphinDB 数据库版本不低于 1.30.16 或 2.00.4)
60
95
  await ddb.connect()
61
96
  ```
62
97
 
63
- #### CDN
64
-
65
- ```html
66
- <script type="module">
67
- import { DDB } from 'https://cdn.dolphindb.cn/assets/api.js'
68
-
69
- let ddb = new DDB('ws://127.0.0.1:8848')
70
-
71
- await ddb.connect()
72
- </script>
73
- ```
74
-
75
- 代码补全、函数提示数据:
98
+ #### 代码补全、函数提示数据
76
99
  - https://cdn.dolphindb.cn/assets/docs.zh.json
77
100
  - https://cdn.dolphindb.cn/assets/docs.en.json
78
101
 
79
102
 
80
- #### DDB 选项
103
+ #### DDB 连接选项
81
104
 
82
105
  ```ts
83
106
  let ddb = new DDB('ws://127.0.0.1:8848', {
@@ -486,8 +509,7 @@ export interface StreamingData extends StreamingParams {
486
509
  ### 开发方法
487
510
 
488
511
  ```shell
489
- # 安装最新版的 nodejs
490
- # https://nodejs.org/en/download/current/
512
+ # 安装最新版的 nodejs (见上文)
491
513
 
492
514
  # 安装 pnpm 包管理器
493
515
  corepack enable
package/browser.d.ts CHANGED
@@ -167,8 +167,7 @@ export declare class DdbObj<TValue extends DdbValue = DdbValue> {
167
167
  to_rows<T extends Record<string, any> = Record<string, any>>(): T[];
168
168
  /** 将 dict<string, any> 自动转换为 js object (Record<string, any>) Automatically convert dict<string, any> to js object (Record<string, any>)
169
169
  - options?:
170
- - strip?: `false` 是否将 DdbObj 中的 value 直接提取、剥离出来作为 js object 的 value (丢弃 DdbObj 中的其余信息,只保留 value)
171
- Whether to directly extract and strip the value in DdbObj as the value of js object (discard the rest of the information in DdbObj, only keep the value)
170
+ - strip?: `false` 是否将 dict<string, any> 中的 value 直接提取、剥离出来作为 js object 的 value (丢弃 DdbObj 中的其余信息,只保留 value)
172
171
  - deep?: `false` 是否递归转换
173
172
  Whether to convert recursively
174
173
  */
@@ -182,6 +181,7 @@ export declare class DdbObj<TValue extends DdbValue = DdbValue> {
182
181
  }): T;
183
182
  }
184
183
  export interface InspectOptions {
184
+ /** `false` */
185
185
  colors?: boolean;
186
186
  /** `null` decimal places 小数位数 */
187
187
  decimals?: number;
@@ -192,7 +192,7 @@ export interface InspectOptions {
192
192
  /** `true` 决定格式化后的数据是否有千分位 */
193
193
  grouping?: boolean;
194
194
  }
195
- /** 根据 DdbType 格式化单个元素 (value) 为字符串 Formats a single element (value) as a string according to DdbType, null returns a 'null' string */
195
+ /** 根据 DdbType 格式化单个元素 (value) 为字符串 */
196
196
  export declare function format(type: DdbType, value: DdbValue, le: boolean, options?: InspectOptions): string;
197
197
  /** 格式化向量、集合中的第 index 项为字符串,空值返回 'null' 字符串 formatted vector, the index-th item in the collection is a string, a null value returns a 'null' string */
198
198
  export declare function formati(obj: DdbVectorObj, index: number, options?: InspectOptions): string;
package/browser.js CHANGED
@@ -10,7 +10,7 @@ import { connect_websocket } from 'xshell/net.browser.js';
10
10
  import { t } from './i18n/index.js';
11
11
  import { DdbDecimal128Serializor } from './data-types/decimal-128.js';
12
12
  import { is_decimal_type, is_decimal_null_value, get_duration_unit } from './shared/utils.js';
13
- import { nulls, DdbChartType, DdbDurationUnit, DdbForm, DdbFunctionType, DdbType, DdbVoidType } from './shared/constants.js';
13
+ import { nulls, DdbChartType, DdbDurationUnit, DdbForm, DdbFunctionType, DdbType, DdbVoidType, dictables } from './shared/constants.js';
14
14
  export * from './shared/constants.js';
15
15
  /** 可以表示所有 DolphinDB 数据库中的数据类型 Can represent data types in all DolphinDB databases */
16
16
  export class DdbObj {
@@ -120,6 +120,8 @@ export class DdbObj {
120
120
  let i_start = i_items_start + len_items;
121
121
  for (let i = 0; i < cols; i++) {
122
122
  const type = buf_data[i_start];
123
+ if (type === DdbType.compress)
124
+ throw new Error(t('{{form}}<{{type}}> 暂时不支持解析', { form: 'table', type: 'compress' }));
123
125
  let col = this.parse_vector(buf_data.subarray(i_start + 2), le, type);
124
126
  col.length += 2;
125
127
  col.name = colnames[i];
@@ -701,8 +703,16 @@ export class DdbObj {
701
703
  });
702
704
  return [8 * length, durations];
703
705
  }
706
+ case DdbType.compress:
707
+ return [
708
+ length,
709
+ new Uint8Array(buf.buffer.slice(buf.byteOffset, buf.byteOffset + length))
710
+ ];
704
711
  default:
705
- throw new Error(t('vector<{{type}}> 暂时不支持解析', { type: String(DdbType[type] || type) }));
712
+ throw new Error(t('{{form}}<{{type}}> 暂时不支持解析', {
713
+ form: 'vector',
714
+ type: String(DdbType[type] || type)
715
+ }));
706
716
  }
707
717
  }
708
718
  pack() {
@@ -933,6 +943,7 @@ export class DdbObj {
933
943
  case DdbType.uuid:
934
944
  case DdbType.ipaddr:
935
945
  case DdbType.int128:
946
+ case DdbType.compress:
936
947
  return [value];
937
948
  case DdbType.blob: {
938
949
  let bufs = new Array(length * 2);
@@ -1111,7 +1122,7 @@ export class DdbObj {
1111
1122
  return format_array(items, data.length > limit);
1112
1123
  }
1113
1124
  default: {
1114
- const limit = 50;
1125
+ const limit = this.type === DdbType.compress ? 5 : 50;
1115
1126
  let items = new Array(Math.min(limit, this.value.length));
1116
1127
  for (let i = 0; i < items.length; i++)
1117
1128
  items[i] = format(this.type, this.value[i], this.le, options);
@@ -1255,28 +1266,33 @@ export class DdbObj {
1255
1266
  to_dict({ strip, deep, } = {}) {
1256
1267
  assert(this.form === DdbForm.dict, t('this.form 必须是 DdbForm.dict, 否则不能调用 to_dict 转换为 js object'));
1257
1268
  const [{ value: keys, type: key_type }, { value: values, type: value_type }] = this.value;
1258
- assert(key_type === DdbType.string && value_type === DdbType.any, t('当前只支持自动转换 dict<string, any> 为 js object'));
1269
+ assert(key_type === DdbType.string && dictables.has(value_type), t('当前只支持自动转换 dict<string, any | ...dictables> 为 js object'));
1259
1270
  assert(!(deep && !strip), t('deep = true 时必须设置 strip = true'));
1260
1271
  let obj = {};
1261
1272
  for (let i = 0; i < this.rows; i++) {
1262
- let value = values[i];
1263
- if (deep && value.form === DdbForm.dict)
1264
- obj[keys[i]] = value.to_dict({ strip, deep });
1273
+ const key = keys[i];
1274
+ if (value_type === DdbType.any) {
1275
+ let value = values[i];
1276
+ if (deep && value.form === DdbForm.dict)
1277
+ obj[key] = value.to_dict({ strip, deep });
1278
+ else
1279
+ obj[key] = strip ? value.value : value;
1280
+ }
1265
1281
  else
1266
- obj[keys[i]] = strip ? value.value : value;
1282
+ obj[key] = values[i];
1267
1283
  }
1268
1284
  return obj;
1269
1285
  }
1270
1286
  }
1271
- /** 整数一定用这个 number formatter, InspectOptions.decimals 不传也用这个 Integer must use this number formatter, InspectOptions.decimals also use this if not passed */
1287
+ /** 整数一定用这个 number formatter, InspectOptions.decimals 不传也用这个 */
1272
1288
  let default_formatter = new Intl.NumberFormat('en-US', { maximumFractionDigits: 20 });
1273
1289
  let _decimals = 20;
1274
1290
  let _grouping = true;
1275
- /** 缓存,为了优化性能,通常 options.decimals 都是不变的 Cache, in order to optimize performance, usually options.decimals are unchanged */
1291
+ /** 缓存,为了优化性能,通常 options.decimals 都是不变的 */
1276
1292
  let _formatter = new Intl.NumberFormat('en-US', { maximumFractionDigits: 20 });
1277
1293
  /** 用来处理时差 To deal with jet lag */
1278
1294
  let _datetime_formatter = new Intl.DateTimeFormat('zh-CN', { dateStyle: 'short', timeStyle: 'medium', timeZone: 'UTC', hour12: false });
1279
- /** 根据 DdbType 格式化单个元素 (value) 为字符串 Formats a single element (value) as a string according to DdbType, null returns a 'null' string */
1295
+ /** 根据 DdbType 格式化单个元素 (value) 为字符串 */
1280
1296
  export function format(type, value, le, options = {}) {
1281
1297
  const { nullstr = false, colors = false, quote = false, grouping = true } = options;
1282
1298
  const formatter = (() => {
@@ -2510,7 +2526,7 @@ export class DDB {
2510
2526
  ]));
2511
2527
  });
2512
2528
  if (this.verbose)
2513
- console.log(result.toString() + rpc_id);
2529
+ console.log(result.toString({ quote: true, nullstr: true }) + rpc_id);
2514
2530
  return result;
2515
2531
  });
2516
2532
  }