baja-lite 1.1.13 → 1.2.1

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/code.js CHANGED
@@ -96,6 +96,7 @@ console.log(`
96
96
  className, eventMainInfo
97
97
  ClassName, EventMainInfo
98
98
  vueName, event-main-info
99
+ splitVueName, event/main-info
99
100
  splitName, event/main/info
100
101
  SplitName, event/mainInfo
101
102
 
@@ -118,7 +119,7 @@ console.log(`
118
119
  idNames: [], 主键字段数组
119
120
  idNames_join: []
120
121
 
121
- modelName: 模块名称,可能为空字符串
122
+ modelName: 模块名称,可能为空字符串 event
122
123
  modelPath: 模块名称实际就影响访问路径,所以这里会直接拼好controller的模块访问路径,如果模块为空,则该属性就是空字符串,否则是 /模块名称/
123
124
 
124
125
  logicDelete: 逻辑删除的查询条件,可以附加在sql条件的末尾,可能是空的
@@ -239,6 +240,8 @@ try {
239
240
  const className = tableName.replace(/_(\w)/g, (a, b) => b.toUpperCase());
240
241
  const ClassName = className.replace(/\w/, (v) => v.toUpperCase());
241
242
  const vueName = tableName.replace(/_/g, '-');
243
+ const splitVueName = tableName.replace(/_/, '/').replace(/_/g, '-');
244
+ const modelName = tableName.substring(0, tableName.indexOf('_'));
242
245
  const splitName = tableName.replace(/_/g, '/');
243
246
  const SplitName = tableName.replace(/_/, '/').replace(/_(\w)/g, (a, b) => b.toUpperCase());
244
247
  const data = {
@@ -247,6 +250,7 @@ try {
247
250
  className,
248
251
  ClassName,
249
252
  vueName,
253
+ splitVueName,
250
254
  splitName,
251
255
  SplitName,
252
256
  columns,
package/index.d.ts CHANGED
@@ -6,6 +6,7 @@ export * from './list.js';
6
6
  export * from './math.js';
7
7
  export * from './object.js';
8
8
  export * from './set-ex.js';
9
+ export * from './snowflake.js';
9
10
  export * from './sql.js';
10
11
  export * from './sqlite.js';
11
12
  export * from './string.js';
package/index.js CHANGED
@@ -6,6 +6,7 @@ export * from './list.js';
6
6
  export * from './math.js';
7
7
  export * from './object.js';
8
8
  export * from './set-ex.js';
9
+ export * from './snowflake.js';
9
10
  export * from './sql.js';
10
11
  export * from './sqlite.js';
11
12
  export * from './string.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "baja-lite",
3
- "version": "1.1.13",
3
+ "version": "1.2.1",
4
4
  "description": "some util for self",
5
5
  "homepage": "https://github.com/void-soul/util-man",
6
6
  "repository": {
@@ -31,7 +31,9 @@
31
31
  "postgres": "bun --inspect ./src/test-postgresql.ts",
32
32
  "postgres2": "node inspect ./dist/cjs/test-postgresql.js",
33
33
  "sqlite": "node inspect ./dist/cjs/test-sqlite.js",
34
- "xml": "bun --inspect ./src/test-xml.ts"
34
+ "xml": "bun --inspect ./src/test-xml.ts",
35
+ "test": "bun --inspect ./src/test.ts",
36
+ "test2": "node inspect ./dist/cjs/tes.js"
35
37
  },
36
38
  "dependencies": {
37
39
  "@msgpack/msgpack": "3.1.0",
package/snowflake.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ export declare class Snowflake {
2
+ private seq;
3
+ private mid;
4
+ private offset;
5
+ private lastTime;
6
+ constructor(options?: {
7
+ mid?: number;
8
+ offset?: number;
9
+ });
10
+ generate(): string | null;
11
+ }
12
+ export declare const snowflake: Snowflake;
package/snowflake.js ADDED
@@ -0,0 +1,108 @@
1
+ function hexToDec(hexStr) {
2
+ if (hexStr.substring(0, 2) === "0x")
3
+ hexStr = hexStr.substring(2);
4
+ hexStr = hexStr.toLowerCase();
5
+ return convertBase(hexStr, 16, 10);
6
+ }
7
+ function add(x, y, base) {
8
+ let z = [];
9
+ let n = Math.max(x.length, y.length);
10
+ let carry = 0;
11
+ let i = 0;
12
+ while (i < n || carry) {
13
+ let xi = i < x.length ? x[i] : 0;
14
+ let yi = i < y.length ? y[i] : 0;
15
+ let zi = carry + xi + yi;
16
+ z.push(zi % base);
17
+ carry = Math.floor(zi / base);
18
+ i++;
19
+ }
20
+ return z;
21
+ }
22
+ function multiplyByNumber(num, x, base) {
23
+ if (num < 0)
24
+ return null;
25
+ if (num == 0)
26
+ return [];
27
+ let result = [];
28
+ let power = x;
29
+ while (true) {
30
+ if (num & 1) {
31
+ result = add(result, power, base);
32
+ }
33
+ num = num >> 1;
34
+ if (num === 0)
35
+ break;
36
+ power = add(power, power, base);
37
+ }
38
+ return result;
39
+ }
40
+ function parseToDigitsArray(str, base) {
41
+ let digits = str.split("");
42
+ let ary = [];
43
+ for (let i = digits.length - 1; i >= 0; i--) {
44
+ let n = parseInt(digits[i], base);
45
+ if (isNaN(n))
46
+ return null;
47
+ ary.push(n);
48
+ }
49
+ return ary;
50
+ }
51
+ function convertBase(str, fromBase, toBase) {
52
+ let digits = parseToDigitsArray(str, fromBase);
53
+ if (digits === null)
54
+ return null;
55
+ let outArray = [];
56
+ let power = [1];
57
+ for (let i = 0; i < digits.length; i++) {
58
+ if (digits[i]) {
59
+ outArray = add(outArray, multiplyByNumber(digits[i], power, toBase), toBase);
60
+ }
61
+ power = multiplyByNumber(fromBase, power, toBase);
62
+ }
63
+ let out = "";
64
+ for (let i = outArray.length - 1; i >= 0; i--) {
65
+ out += outArray[i].toString(toBase);
66
+ }
67
+ return out;
68
+ }
69
+ export class Snowflake {
70
+ constructor(options) {
71
+ options = options || {};
72
+ this.seq = 0;
73
+ this.mid = (options.mid || 1) % 1023;
74
+ this.offset = options.offset || 0;
75
+ this.lastTime = 0;
76
+ }
77
+ generate() {
78
+ let time = Date.now(), bTime = (time - this.offset).toString(2);
79
+ if (this.lastTime == time) {
80
+ this.seq++;
81
+ if (this.seq > 4095) {
82
+ this.seq = 0;
83
+ while (Date.now() <= time) { }
84
+ }
85
+ }
86
+ else {
87
+ this.seq = 0;
88
+ }
89
+ this.lastTime = time;
90
+ let bSeq = this.seq.toString(2), bMid = this.mid.toString(2);
91
+ while (bSeq.length < 12) {
92
+ bSeq = "0" + bSeq;
93
+ }
94
+ while (bMid.length < 10) {
95
+ bMid = "0" + bMid;
96
+ }
97
+ let bid = bTime + bMid + bSeq;
98
+ let id = "";
99
+ for (let i = bid.length; i > 0; i -= 4) {
100
+ id = parseInt(bid.substring(i - 4, i), 2).toString(16) + id;
101
+ }
102
+ return hexToDec(id);
103
+ }
104
+ }
105
+ export const snowflake = new Snowflake({
106
+ mid: 66,
107
+ offset: (2024 - 1970) * 31536000 * 1000,
108
+ });
package/sql.d.ts CHANGED
@@ -1,7 +1,7 @@
1
+ import { ExtensionCodec } from "@msgpack/msgpack";
1
2
  import { XML } from './convert-xml.js';
2
- import { ArrayList } from './list.js';
3
3
  import { EnumMap } from './enum.js';
4
- import { ExtensionCodec } from "@msgpack/msgpack";
4
+ import { ArrayList } from './list.js';
5
5
  export declare const extensionCodec: ExtensionCodec<undefined>;
6
6
  declare const _daoDBName: unique symbol;
7
7
  declare const _tableName: unique symbol;
package/sql.js CHANGED
@@ -9,21 +9,21 @@ var __metadata = (this && this.__metadata) || function (k, v) {
9
9
  };
10
10
  var _b, _c, _d, _e;
11
11
  var _f, _g, _h, _j;
12
- import { Throw } from './error.js';
13
- import tslib from 'tslib';
12
+ import { decode, DecodeError, encode, ExtensionCodec } from "@msgpack/msgpack";
13
+ import HTML from 'html-parse-stringify';
14
14
  import * as ite from 'iterare';
15
- import { emptyString } from './string.js';
15
+ import LGet from 'lodash.get';
16
+ import mustache from 'mustache';
16
17
  import pino from 'pino';
18
+ import { formatDialect, mysql, postgresql, sqlite } from 'sql-formatter';
19
+ import tslib from 'tslib';
20
+ import { convert } from './convert-xml.js';
21
+ import { Throw } from './error.js';
17
22
  import { excuteSplit, ExcuteSplitMode, sleep } from './fn.js';
23
+ import { ArrayList } from './list.js';
18
24
  import { add, calc, ten2Any } from './math.js';
19
- import mustache from 'mustache';
20
25
  import { C2P, C2P2, P2C } from './object.js';
21
- import { formatDialect, sqlite, mysql, postgresql } from 'sql-formatter';
22
- import HTML from 'html-parse-stringify';
23
- import { convert } from './convert-xml.js';
24
- import { ArrayList } from './list.js';
25
- import LGet from 'lodash.get';
26
- import { encode, decode, ExtensionCodec, DecodeError } from "@msgpack/msgpack";
26
+ import { emptyString } from './string.js';
27
27
  const iterate = ite.iterate;
28
28
  BigInt.prototype.toJSON = function () { return this.toString(); };
29
29
  const BIGINT_EXT_TYPE = 0;
@@ -2607,6 +2607,8 @@ export class SqlService {
2607
2607
  option.mode ?? (option.mode = InsertMode.Insert);
2608
2608
  const isArray = option.data instanceof Array;
2609
2609
  const datas = option.data instanceof Array ? option.data : [option.data];
2610
+ if (datas.length === 0)
2611
+ return 0n;
2610
2612
  if (option.sync === SyncMode.Sync) {
2611
2613
  const fn = () => {
2612
2614
  const result = excuteSplit(ExcuteSplitMode.SyncTrust, datas, _data => {
@@ -2713,6 +2715,8 @@ export class SqlService {
2713
2715
  update(option) {
2714
2716
  Throw.if(!this[_ids] || this[_ids].length === 0, 'not found id');
2715
2717
  const datas = option.data instanceof Array ? option.data : [option.data];
2718
+ if (datas.length === 0)
2719
+ return 0;
2716
2720
  if (option.sync === SyncMode.Sync) {
2717
2721
  const fn = () => {
2718
2722
  const result = excuteSplit(ExcuteSplitMode.SyncTrust, datas, _data => {
@@ -2774,6 +2778,9 @@ export class SqlService {
2774
2778
  option.where = ids.map(i => ({ [idName]: i }));
2775
2779
  }
2776
2780
  const wheres = option.where instanceof Array ? option.where : [option.where];
2781
+ if (wheres.length === 0) {
2782
+ return 0;
2783
+ }
2777
2784
  const sqls = [];
2778
2785
  if (option.mode === DeleteMode.Common) {
2779
2786
  const params = new Array();
package/test.js CHANGED
@@ -1,2 +1,2 @@
1
- import { format } from 'mysql2';
2
- console.log(format('select 1 from a where ?', [['1', '2', '3']]));
1
+ import { snowflake } from "snowflake";
2
+ console.log(snowflake.generate());