dolphindb 0.0.10 → 0.0.13

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.zh.md CHANGED
@@ -46,6 +46,7 @@ npm install dolphindb
46
46
  ```ts
47
47
  import { DDB } from 'dolphindb'
48
48
  // 已有的使用 CommonJS 模块的项目的导入方法为 const { DDB } = require('dolphindb')
49
+ // 在浏览器中使用: import { DDB } form 'dolphindb/browser.js'
49
50
 
50
51
  // 创建数据库对象,初始化 WebSocket 连接地址
51
52
  let ddb = new DDB('ws://127.0.0.1:8848')
package/browser.d.ts CHANGED
@@ -151,6 +151,9 @@ export declare class DdbVoid extends DdbObj<undefined> {
151
151
  export declare class DdbBool extends DdbObj<boolean> {
152
152
  constructor(value: boolean);
153
153
  }
154
+ export declare class DdbChar extends DdbObj<string> {
155
+ constructor(value: string);
156
+ }
154
157
  export declare class DdbInt extends DdbObj<number> {
155
158
  constructor(value: number);
156
159
  }
@@ -163,6 +166,15 @@ export declare class DdbLong extends DdbObj<bigint> {
163
166
  export declare class DdbDouble extends DdbObj<number> {
164
167
  constructor(value: number);
165
168
  }
169
+ export declare class DdbDateTime extends DdbObj<number> {
170
+ constructor(value: number);
171
+ }
172
+ export declare class DdbTimeStamp extends DdbObj<bigint> {
173
+ constructor(value: bigint);
174
+ }
175
+ export declare class DdbNanoTimeStamp extends DdbObj<bigint> {
176
+ constructor(value: bigint);
177
+ }
166
178
  export declare class DdbVectorInt extends DdbObj<Int32Array> {
167
179
  constructor(value: number[], name?: string);
168
180
  }
@@ -181,8 +193,16 @@ export declare class DdbPair extends DdbObj<Int32Array> {
181
193
  export declare class DdbFunction extends DdbObj<DdbFunctionDefValue> {
182
194
  constructor(name: string, type: DdbFunctionType);
183
195
  }
184
- export declare function date2str(date: number): string;
196
+ export declare function date2str(date: number, format?: string): string;
197
+ export declare function datetime2str(datetime: number, format?: string): string;
185
198
  export declare function timestamp2str(timestamp: bigint): string;
199
+ export declare function nanotimestamp2str(nanotimestamp: bigint, format?: string): string;
200
+ /** parse nano timestamp string to bigint value
201
+ - str: nano timestamp string, 如果为空字符串会返回对应的空值 (nulls.int64)
202
+ - format?: Format string up to and including seconds of the passed string, default is `YYYY.MM.DD HH:mm:ss`
203
+ https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens
204
+ */
205
+ export declare function str2nanotimestamp(str: string, format?: string): bigint;
186
206
  export declare class DDB {
187
207
  /** 当前的 session id (http 或 tcp) */
188
208
  sid: string;
package/browser.js CHANGED
@@ -1,8 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.ddb = exports.DDB = exports.timestamp2str = exports.date2str = exports.DdbFunction = exports.DdbPair = exports.DdbVectorAny = exports.DdbVectorDouble = exports.DdbVectorString = exports.DdbVectorInt = exports.DdbDouble = exports.DdbLong = exports.DdbString = exports.DdbInt = exports.DdbBool = exports.DdbVoid = exports.DdbObj = exports.timezone_offset = exports.nulls = exports.DdbFunctionType = exports.DdbType = exports.DdbForm = void 0;
3
+ exports.ddb = exports.DDB = exports.str2nanotimestamp = exports.nanotimestamp2str = exports.timestamp2str = exports.datetime2str = exports.date2str = exports.DdbFunction = exports.DdbPair = exports.DdbVectorAny = exports.DdbVectorDouble = exports.DdbVectorString = exports.DdbVectorInt = exports.DdbNanoTimeStamp = exports.DdbTimeStamp = exports.DdbDateTime = exports.DdbDouble = exports.DdbLong = exports.DdbString = exports.DdbInt = exports.DdbChar = exports.DdbBool = exports.DdbVoid = exports.DdbObj = exports.timezone_offset = exports.nulls = exports.DdbFunctionType = exports.DdbType = exports.DdbForm = void 0;
4
4
  const tslib_1 = require("tslib");
5
5
  const dayjs_1 = tslib_1.__importDefault(require("dayjs"));
6
+ const customParseFormat_js_1 = tslib_1.__importDefault(require("dayjs/plugin/customParseFormat.js"));
7
+ dayjs_1.default.extend(customParseFormat_js_1.default);
6
8
  require("xshell/prototype.browser");
7
9
  const utils_browser_1 = require("xshell/utils.browser");
8
10
  var DdbForm;
@@ -113,7 +115,7 @@ class DdbObj {
113
115
  le,
114
116
  form,
115
117
  type,
116
- length: 2 + length,
118
+ length: i_data + length,
117
119
  value,
118
120
  });
119
121
  }
@@ -121,7 +123,7 @@ class DdbObj {
121
123
  case DdbForm.pair:
122
124
  case DdbForm.set: {
123
125
  let vector = this.parse_vector(buf_data, le, type);
124
- vector.length += 2;
126
+ vector.length += i_data;
125
127
  vector.form = form;
126
128
  return vector;
127
129
  }
@@ -152,17 +154,17 @@ class DdbObj {
152
154
  let i_start = i_items_start + len_items;
153
155
  for (let i = 0; i < cols; i++) {
154
156
  const type = buf_data[i_start];
155
- const i_vector_head = i_start + 2;
156
- let col = this.parse_vector(buf_data.subarray(i_vector_head), le, type);
157
+ let col = this.parse_vector(buf_data.subarray(i_start + 2), le, type);
158
+ col.length += 2;
157
159
  col.name = colnames[i];
158
160
  value[i] = col;
159
- i_start = i_vector_head + col.length;
161
+ i_start += col.length;
160
162
  }
161
163
  return new this({
162
164
  le,
163
165
  form,
164
166
  type,
165
- length: i_start,
167
+ length: i_data + i_start,
166
168
  name,
167
169
  rows,
168
170
  cols,
@@ -185,7 +187,7 @@ class DdbObj {
185
187
  le,
186
188
  form,
187
189
  type,
188
- length: 2 + keys.length + values.length,
190
+ length: i_data + keys.length + values.length,
189
191
  rows: keys.rows,
190
192
  cols: 2,
191
193
  value: [
@@ -218,7 +220,7 @@ class DdbObj {
218
220
  le,
219
221
  form,
220
222
  type,
221
- length: 11 + len_items,
223
+ length: i_data + 11 + len_items,
222
224
  rows,
223
225
  cols,
224
226
  datatype,
@@ -230,7 +232,7 @@ class DdbObj {
230
232
  le,
231
233
  form,
232
234
  type,
233
- length: buf_data.length,
235
+ length: i_data + buf_data.length,
234
236
  value: buf_data
235
237
  });
236
238
  }
@@ -786,7 +788,7 @@ class DdbObj {
786
788
  case DdbForm.pair:
787
789
  return `pair<${tname}>`;
788
790
  case DdbForm.set:
789
- return `set<${tname}>`;
791
+ return `set<${tname}>[${this.rows}]`;
790
792
  case DdbForm.table:
791
793
  return `table[${this.rows} rows][${this.cols} cols]`;
792
794
  case DdbForm.dict:
@@ -827,16 +829,23 @@ class DdbObj {
827
829
  });
828
830
  }
829
831
  to_rows() {
832
+ if (this.form !== DdbForm.table)
833
+ throw new Error('this.form is not DdbForm.table, cannot call to_rows');
830
834
  let rows = new Array(this.rows);
831
835
  for (let i = 0; i < this.rows; i++) {
832
836
  let row = {};
833
837
  for (let j = 0; j < this.cols; j++) {
834
- const c = this.value[j];
835
- if (c.type === DdbType.ipaddr) {
836
- row[c.name] = c.value.subarray(16 * i, 16 * (i + 1));
837
- continue;
838
+ const { type, name, value } = this.value[j]; // column
839
+ switch (type) {
840
+ case DdbType.bool:
841
+ row[name] = Boolean(value[i]);
842
+ break;
843
+ case DdbType.ipaddr:
844
+ row[name] = value.subarray(16 * i, 16 * (i + 1));
845
+ break;
846
+ default:
847
+ row[name] = value[i];
838
848
  }
839
- row[c.name] = c.value[i];
840
849
  }
841
850
  rows[i] = row;
842
851
  }
@@ -880,6 +889,17 @@ class DdbBool extends DdbObj {
880
889
  }
881
890
  }
882
891
  exports.DdbBool = DdbBool;
892
+ class DdbChar extends DdbObj {
893
+ constructor(value) {
894
+ super({
895
+ form: DdbForm.scalar,
896
+ type: DdbType.char,
897
+ length: 1,
898
+ value,
899
+ });
900
+ }
901
+ }
902
+ exports.DdbChar = DdbChar;
883
903
  class DdbInt extends DdbObj {
884
904
  constructor(value) {
885
905
  super({
@@ -924,6 +944,39 @@ class DdbDouble extends DdbObj {
924
944
  }
925
945
  }
926
946
  exports.DdbDouble = DdbDouble;
947
+ class DdbDateTime extends DdbObj {
948
+ constructor(value) {
949
+ super({
950
+ form: DdbForm.scalar,
951
+ type: DdbType.datetime,
952
+ length: 4,
953
+ value
954
+ });
955
+ }
956
+ }
957
+ exports.DdbDateTime = DdbDateTime;
958
+ class DdbTimeStamp extends DdbObj {
959
+ constructor(value) {
960
+ super({
961
+ form: DdbForm.scalar,
962
+ type: DdbType.timestamp,
963
+ length: 8,
964
+ value
965
+ });
966
+ }
967
+ }
968
+ exports.DdbTimeStamp = DdbTimeStamp;
969
+ class DdbNanoTimeStamp extends DdbObj {
970
+ constructor(value) {
971
+ super({
972
+ form: DdbForm.scalar,
973
+ type: DdbType.nanotimestamp,
974
+ length: 8,
975
+ value
976
+ });
977
+ }
978
+ }
979
+ exports.DdbNanoTimeStamp = DdbNanoTimeStamp;
927
980
  class DdbVectorInt extends DdbObj {
928
981
  constructor(value, name) {
929
982
  super({
@@ -1004,13 +1057,20 @@ class DdbFunction extends DdbObj {
1004
1057
  }
1005
1058
  }
1006
1059
  exports.DdbFunction = DdbFunction;
1007
- function date2str(date) {
1060
+ function date2str(date, format = 'YYYY.MM.DD') {
1008
1061
  return date === exports.nulls.int32 ?
1009
1062
  ''
1010
1063
  :
1011
- (0, dayjs_1.default)(exports.timezone_offset + 1000 * 3600 * 24 * date).format('YYYY.MM.DD');
1064
+ (0, dayjs_1.default)(exports.timezone_offset + 1000 * 3600 * 24 * date).format(format);
1012
1065
  }
1013
1066
  exports.date2str = date2str;
1067
+ function datetime2str(datetime, format = 'YYYY.MM.DD HH:mm:ss') {
1068
+ return datetime === exports.nulls.int32 ?
1069
+ ''
1070
+ :
1071
+ (0, dayjs_1.default)(exports.timezone_offset + 1000 * datetime).format(format);
1072
+ }
1073
+ exports.datetime2str = datetime2str;
1014
1074
  function timestamp2str(timestamp) {
1015
1075
  return timestamp === exports.nulls.int64 ?
1016
1076
  ''
@@ -1018,6 +1078,29 @@ function timestamp2str(timestamp) {
1018
1078
  (0, dayjs_1.default)(exports.timezone_offset + Number(timestamp)).format('YYYY.MM.DD HH:mm:ss.SSS');
1019
1079
  }
1020
1080
  exports.timestamp2str = timestamp2str;
1081
+ function nanotimestamp2str(nanotimestamp, format = 'YYYY.MM.DD HH:mm:ss') {
1082
+ return nanotimestamp === exports.nulls.int64 ?
1083
+ ''
1084
+ :
1085
+ (0, dayjs_1.default)(exports.timezone_offset + (Number(nanotimestamp) / 1000000)).format(format) + `.${nanotimestamp % 1000000000n}`;
1086
+ }
1087
+ exports.nanotimestamp2str = nanotimestamp2str;
1088
+ /** parse nano timestamp string to bigint value
1089
+ - str: nano timestamp string, 如果为空字符串会返回对应的空值 (nulls.int64)
1090
+ - format?: Format string up to and including seconds of the passed string, default is `YYYY.MM.DD HH:mm:ss`
1091
+ https://day.js.org/docs/en/parse/string-format#list-of-all-available-parsing-tokens
1092
+ */
1093
+ function str2nanotimestamp(str, format = 'YYYY.MM.DD HH:mm:ss') {
1094
+ if (!str)
1095
+ return exports.nulls.int64;
1096
+ if (str.length - format.length !== 9)
1097
+ throw new Error('nanotimestamp 字符串长度减格式串长度应该等于 9');
1098
+ return (BigInt(-exports.timezone_offset +
1099
+ (0, dayjs_1.default)(str.slice(0, format.length), format).valueOf()) * 1000000n
1100
+ +
1101
+ BigInt(str.slice(format.length)));
1102
+ }
1103
+ exports.str2nanotimestamp = str2nanotimestamp;
1021
1104
  class DDB {
1022
1105
  /**
1023
1106
  Initialize an instance of DolphinDB Client using the WebSocket URL