baja-lite 1.5.25 → 1.5.28

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.
Files changed (4) hide show
  1. package/package.json +4 -4
  2. package/sql.d.ts +5 -1
  3. package/sql.js +34 -7
  4. package/sqlite.js +25 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "baja-lite",
3
- "version": "1.5.25",
3
+ "version": "1.5.28",
4
4
  "description": "some util for self",
5
5
  "homepage": "https://github.com/void-soul/baja-lite",
6
6
  "repository": {
@@ -37,7 +37,7 @@
37
37
  "dependencies": {
38
38
  "@msgpack/msgpack": "3.1.2",
39
39
  "@types/request-promise": "4.1.51",
40
- "axios": "1.10.0",
40
+ "axios": "1.11.0",
41
41
  "baja-lite-field": "1.4.13",
42
42
  "decimal.js": "10.6.0",
43
43
  "html-parse-stringify": "3.0.1",
@@ -64,8 +64,8 @@
64
64
  "@typescript-eslint/parser": "8.36.0",
65
65
  "better-sqlite3": "12.2.0",
66
66
  "ioredis": "5.6.1",
67
- "mongodb": "6.17.0",
68
- "mysql2": "3.14.1",
67
+ "mongodb": "6.18.0",
68
+ "mysql2": "3.14.2",
69
69
  "pg": "8.16.3",
70
70
  "pg-pool": "3.10.1",
71
71
  "redlock": "5.0.0-beta.2",
package/sql.d.ts CHANGED
@@ -57,10 +57,14 @@ export declare enum InsertMode {
57
57
  2. 临时表的结构复制正式表
58
58
  */
59
59
  InsertWithTempTable = 1,
60
+ /**
61
+ * 如果不存在则插入
62
+ * 来源是数据库,根据ID或者指定字段查询
63
+ */
60
64
  InsertIfNotExists = 2,
61
65
  /**
62
66
  # 插入或者更新
63
- 1. 判断依据是主键
67
+ 1. 判断依据是主键,来源是从数据库查询
64
68
  */
65
69
  Replace = 3
66
70
  }
package/sql.js CHANGED
@@ -23,6 +23,7 @@ import { Throw } from './error.js';
23
23
  import { excuteSplit, ExcuteSplitMode, sleep } from './fn.js';
24
24
  import { add, calc, ten2Any } from './math.js';
25
25
  import { C2P, C2P2, P2C } from './object.js';
26
+ import { snowflake } from './snowflake.js';
26
27
  import { emptyString } from './string.js';
27
28
  const iterate = ite.iterate;
28
29
  BigInt.prototype.toJSON = function () { return this.toString(); };
@@ -129,10 +130,14 @@ export var InsertMode;
129
130
  2. 临时表的结构复制正式表
130
131
  */
131
132
  InsertMode[InsertMode["InsertWithTempTable"] = 1] = "InsertWithTempTable";
133
+ /**
134
+ * 如果不存在则插入
135
+ * 来源是数据库,根据ID或者指定字段查询
136
+ */
132
137
  InsertMode[InsertMode["InsertIfNotExists"] = 2] = "InsertIfNotExists";
133
138
  /**
134
139
  # 插入或者更新
135
- 1. 判断依据是主键
140
+ 1. 判断依据是主键,来源是从数据库查询
136
141
  */
137
142
  InsertMode[InsertMode["Replace"] = 3] = "Replace";
138
143
  })(InsertMode || (InsertMode = {}));
@@ -944,6 +949,28 @@ export class Sqlite {
944
949
  PRIMARY KEY ( ______tableName )
945
950
  );
946
951
  `);
952
+ this[_daoDB].function('UUID_SHORT', { deterministic: false }, () => snowflake.generate());
953
+ this[_daoDB].function('UUID', { deterministic: false }, () => snowflake.generate());
954
+ this[_daoDB].function('TIME_TO_SEC', { deterministic: true }, (time) => time.split(':').map((v, i) => parseInt(v) * (i > 0 ? 1 : 60)).reduce((a, b) => a + b, 0));
955
+ this[_daoDB].function('IF', { deterministic: true }, (condition, v1, v2) => condition ? v1 : v2);
956
+ this[_daoDB].function('RIGHT', { deterministic: true }, (src, p) => src.slice(p * -1));
957
+ this[_daoDB].function('LEFT', { deterministic: true }, (str, len) => str?.substring(0, len) || null);
958
+ this[_daoDB].function('NOW', { deterministic: false }, () => new Date().toISOString().slice(0, 19).replace('T', ' '));
959
+ this[_daoDB].function('CURDATE', { deterministic: false }, () => new Date().toISOString().split('T')[0]);
960
+ this[_daoDB].function('DATE_FORMAT', { deterministic: true }, (dateStr, format) => {
961
+ const date = new Date(dateStr);
962
+ return format
963
+ .replace('%Y', date.getFullYear().toString())
964
+ .replace('%m', (date.getMonth() + 1).toString().padStart(2, '0'))
965
+ .replace('%d', date.getDate().toString().padStart(2, '0'))
966
+ .replace('%H', date.getHours().toString().padStart(2, '0'))
967
+ .replace('%i', date.getMinutes().toString().padStart(2, '0'))
968
+ .replace('%s', date.getSeconds().toString().padStart(2, '0'));
969
+ });
970
+ this[_daoDB].function('RAND', { deterministic: false }, () => Math.random());
971
+ this[_daoDB].function('UNIX_TIMESTAMP', { deterministic: false }, (dateStr) => dateStr
972
+ ? Math.floor(new Date(dateStr).getTime() / 1000)
973
+ : Math.floor(Date.now() / 1000));
947
974
  }
948
975
  createConnection(sync) {
949
976
  if (sync === SyncMode.Async) {
@@ -2862,13 +2889,13 @@ export class SqlService {
2862
2889
  const { sql, params } = this._generSql(option.dbType, option.sql, _params);
2863
2890
  if (option.sync === SyncMode.Sync) {
2864
2891
  const result = option.conn.query(SyncMode.Sync, sql, params);
2865
- return result.map(item => this._select(option.selectResult, result, null, undefined, option.hump, option.mapper, option.mapperIfUndefined, option.dataConvert));
2892
+ return result.map(item => this._select(option.selectResult, item, null, undefined, option.hump, option.mapper, option.mapperIfUndefined, option.dataConvert));
2866
2893
  }
2867
2894
  else {
2868
2895
  return new Promise(async (resolve, reject) => {
2869
2896
  try {
2870
2897
  const result = await option.conn.query(SyncMode.Async, sql, params);
2871
- resolve(result.map(item => this._select(option.selectResult, result, null, undefined, option.hump, option.mapper, option.mapperIfUndefined, option.dataConvert)));
2898
+ resolve(result.map(item => this._select(option.selectResult, item, null, undefined, option.hump, option.mapper, option.mapperIfUndefined, option.dataConvert)));
2872
2899
  }
2873
2900
  catch (error) {
2874
2901
  reject(error);
@@ -3975,10 +4002,10 @@ class StreamQuery {
3975
4002
  }
3976
4003
  }
3977
4004
  if (sets.length > 0) {
3978
- const sql = `UPDATE ${option.tableName ?? this._service[_tableName]} t SET ${sets.join(',')}
4005
+ const sql = `UPDATE ${option.tableName ?? this._service[_tableName]} SET ${sets.join(',')}
3979
4006
  ${where ? ' WHERE ' : ''}
3980
4007
  ${where}
3981
- `;
4008
+ `.replace(/t\./g, '');
3982
4009
  if (option.sync === SyncMode.Async) {
3983
4010
  return this._service.excute({ ...option, sync: SyncMode.Async, sql, params });
3984
4011
  }
@@ -3994,10 +4021,10 @@ class StreamQuery {
3994
4021
  option ?? (option = {});
3995
4022
  option.sync ?? (option.sync = SyncMode.Async);
3996
4023
  const { where, params } = this._where();
3997
- const sql = `DELETE FROM ${option.tableName ?? this._service[_tableName]} t
4024
+ const sql = `DELETE FROM ${option.tableName ?? this._service[_tableName]}
3998
4025
  ${where ? ' WHERE ' : ''}
3999
4026
  ${where}
4000
- `;
4027
+ `.replace(/t\./g, '');
4001
4028
  // if (option.sync === SyncMode.Async) {
4002
4029
  // return this._service.delete({ ...option, sync: SyncMode.Async, whereSql: where, whereParams: params });
4003
4030
  // } else {
package/sqlite.js CHANGED
@@ -1,6 +1,7 @@
1
- import { extensionCodec, logger } from './sql.js';
1
+ import { decode, encode } from "@msgpack/msgpack";
2
2
  import Sqlstring from 'sqlstring';
3
- import { encode, decode } from "@msgpack/msgpack";
3
+ import { snowflake } from './snowflake.js';
4
+ import { extensionCodec, logger } from './sql.js';
4
5
  export class SqliteRemoteClass {
5
6
  constructor() {
6
7
  this.dbList = {};
@@ -131,6 +132,28 @@ export class SqliteRemoteClass {
131
132
  PRIMARY KEY ( ______tableName )
132
133
  );
133
134
  `);
135
+ this.dbList[dbName].function('UUID_SHORT', { deterministic: false }, () => snowflake.generate());
136
+ this.dbList[dbName].function('UUID', { deterministic: false }, () => snowflake.generate());
137
+ this.dbList[dbName].function('TIME_TO_SEC', { deterministic: true }, (time) => time.split(':').map((v, i) => parseInt(v) * (i > 0 ? 1 : 60)).reduce((a, b) => a + b, 0));
138
+ this.dbList[dbName].function('IF', { deterministic: true }, (condition, v1, v2) => condition ? v1 : v2);
139
+ this.dbList[dbName].function('RIGHT', { deterministic: true }, (src, p) => src.slice(p * -1));
140
+ this.dbList[dbName].function('LEFT', { deterministic: true }, (str, len) => str?.substring(0, len) || null);
141
+ this.dbList[dbName].function('NOW', { deterministic: false }, () => new Date().toISOString().slice(0, 19).replace('T', ' '));
142
+ this.dbList[dbName].function('CURDATE', { deterministic: false }, () => new Date().toISOString().split('T')[0]);
143
+ this.dbList[dbName].function('DATE_FORMAT', { deterministic: true }, (dateStr, format) => {
144
+ const date = new Date(dateStr);
145
+ return format
146
+ .replace('%Y', date.getFullYear().toString())
147
+ .replace('%m', (date.getMonth() + 1).toString().padStart(2, '0'))
148
+ .replace('%d', date.getDate().toString().padStart(2, '0'))
149
+ .replace('%H', date.getHours().toString().padStart(2, '0'))
150
+ .replace('%i', date.getMinutes().toString().padStart(2, '0'))
151
+ .replace('%s', date.getSeconds().toString().padStart(2, '0'));
152
+ });
153
+ this.dbList[dbName].function('RAND', { deterministic: false }, () => Math.random());
154
+ this.dbList[dbName].function('UNIX_TIMESTAMP', { deterministic: false }, (dateStr) => dateStr
155
+ ? Math.floor(new Date(dateStr).getTime() / 1000)
156
+ : Math.floor(Date.now() / 1000));
134
157
  }
135
158
  }
136
159
  async export(dbName, exportPath) {