dolphindb 0.0.5 → 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 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
- # Install npm packages in your project
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<DdbInt>('add', [new DdbInt(1), new DdbInt(1)])
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
- import type { DdbLong } from 'dolphindb'
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
 
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
- # 在项目中安装 npm 包
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<DdbInt>('add', [new DdbInt(1), new DdbInt(1)])
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
- import type { DdbLong } from 'dolphindb'
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
 
package/browser.d.ts CHANGED
@@ -136,7 +136,11 @@ export declare class DdbObj<T extends DdbValue = DdbValue> {
136
136
  pack(): Uint8Array;
137
137
  static pack_vector_body(value: DdbVectorValue, type: DdbType, length: number): ArrayBufferView[];
138
138
  toString(): string;
139
- to_cols(): any[];
139
+ to_cols(): {
140
+ title: string;
141
+ dataIndex: string;
142
+ render?: any;
143
+ }[];
140
144
  to_rows<T extends Record<string, any> = Record<string, any>>(): T[];
141
145
  to_dict<T = Record<string, any>>(): T;
142
146
  }
@@ -190,6 +194,8 @@ export declare class DDB {
190
194
  le: boolean;
191
195
  /** little endian (client) */
192
196
  static le_client: boolean;
197
+ /** print message handler */
198
+ printer(message: string): void;
193
199
  /** resolver, rejector, promise of last rpc */
194
200
  presolver(buf: Uint8Array): void;
195
201
  prejector(error: Error): void;
@@ -197,7 +203,7 @@ export declare class DDB {
197
203
  constructor(ws_url?: string);
198
204
  /** 连接到 DolphinDB Server */
199
205
  connect({ ws_url, login, username, password, }?: {
200
- /** 默认使用实例初始化时传入的 WebSocket 链接 */
206
+ /** 默认使用实例初始化时传入的 WebSocket URL */
201
207
  ws_url?: string;
202
208
  /** 是否在建立连接后自动登录,默认 true */
203
209
  login?: boolean;
@@ -253,7 +259,13 @@ export declare class DDB {
253
259
  /** 上传的变量值 */
254
260
  args: (DdbObj | string | boolean)[]): Promise<DdbObj<DdbValue>>;
255
261
  /** 解析服务端响应报文,返回去掉 header 的 data buf */
256
- parse_message(buf: Uint8Array): Uint8Array;
262
+ parse_message(buf: Uint8Array): {
263
+ type: "message";
264
+ data: string;
265
+ } | {
266
+ type: "object";
267
+ data: Uint8Array;
268
+ };
257
269
  /** 自动转换 js string, boolean 为 DdbObj */
258
270
  to_ddbobj(value: DdbObj | string | boolean): DdbObj;
259
271
  /** 转换 js 数组为 DdbObj[] (in place, 会修改原数组) */
package/browser.js CHANGED
@@ -1032,6 +1032,10 @@ class DDB {
1032
1032
  }
1033
1033
  this.ws_url = ws_url;
1034
1034
  }
1035
+ /** print message handler */
1036
+ printer(message) {
1037
+ console.log(message);
1038
+ }
1035
1039
  /** resolver, rejector, promise of last rpc */
1036
1040
  presolver(buf) { }
1037
1041
  prejector(error) { }
@@ -1062,7 +1066,12 @@ class DDB {
1062
1066
  });
1063
1067
  ws.addEventListener('message', ({ data: buf }) => {
1064
1068
  try {
1065
- this.presolver(this.parse_message(new Uint8Array(buf)));
1069
+ const { type, data } = this.parse_message(new Uint8Array(buf));
1070
+ if (type === 'message') {
1071
+ this.printer(data);
1072
+ return;
1073
+ }
1074
+ this.presolver(data);
1066
1075
  }
1067
1076
  catch (error) {
1068
1077
  this.prejector(error);
@@ -1142,7 +1151,7 @@ class DDB {
1142
1151
  }
1143
1152
  })());
1144
1153
  const message = (0, utils_browser_1.concat)([
1145
- this.enc.encode(`API ${this.sid} ${command.length}${urgent ? ` / 1_1_8_8` : ''}\n`),
1154
+ this.enc.encode(`API2 ${this.sid} ${command.length}${urgent ? ` / 1_1_8_8` : ''}\n`),
1146
1155
  command,
1147
1156
  ...args.map((arg) => arg.pack())
1148
1157
  ]);
@@ -1206,6 +1215,14 @@ class DDB {
1206
1215
  }
1207
1216
  /** 解析服务端响应报文,返回去掉 header 的 data buf */
1208
1217
  parse_message(buf) {
1218
+ // MSG\n
1219
+ // <message>\0
1220
+ // 'M'.codePointAt(0).to_hex_str()
1221
+ if (buf[0] === 0x4d && buf[1] === 0x53 && buf[2] === 0x47 && buf[3] === 0x0a)
1222
+ return {
1223
+ type: 'message',
1224
+ data: this.dec.decode(buf.subarray(4))
1225
+ };
1209
1226
  // '1166953221 1 1\n'
1210
1227
  // 'OK\n'
1211
1228
  // '\x04\x00\x02\x00\x00\x00'
@@ -1228,7 +1245,10 @@ class DDB {
1228
1245
  const message = this.dec.decode(buf.subarray(i_ls_1, i_lf_1));
1229
1246
  if (message !== 'OK')
1230
1247
  throw new Error(message);
1231
- return buf.subarray(i_lf_1 + 1);
1248
+ return {
1249
+ type: 'object',
1250
+ data: buf.subarray(i_lf_1 + 1)
1251
+ };
1232
1252
  }
1233
1253
  /** 自动转换 js string, boolean 为 DdbObj */
1234
1254
  to_ddbobj(value) {