@k3000/store 1.3.0 → 1.4.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/architect.mjs +58 -3
- package/generator.mjs +142 -4
- package/package.json +1 -1
- package/test/1/data +0 -0
- package/test/1/index +0 -0
- package/test/2/data +0 -0
- package/test/2/index +0 -0
- package/test/3/index +0 -0
- package/test/4/index +0 -0
- package/test/5/index +0 -0
- package/test/6/index +0 -0
- package/test/7/index +0 -0
- package/test/7/index.mjs +6 -1
- package/test/7/type.ts +1 -0
- package/test/8/data +0 -0
- package/test/8/index +0 -0
- package/test/8/index.mjs +373 -0
- package/test/8/type.ts +172 -0
- package/test/index.mjs +3 -3
- package/test.mjs +43 -11
- package/type.ts +2 -1
package/architect.mjs
CHANGED
|
@@ -108,7 +108,7 @@ export const TypeLen = {
|
|
|
108
108
|
* @param {Buffer} b
|
|
109
109
|
* @returns {-1 | 0 | 1}
|
|
110
110
|
*/
|
|
111
|
-
const compareBuffer = (a, b) => {
|
|
111
|
+
export const compareBuffer = (a, b) => {
|
|
112
112
|
|
|
113
113
|
if (a.length > b.length) return 1
|
|
114
114
|
if (a.length < b.length) return -1
|
|
@@ -761,8 +761,8 @@ export class Entities extends Array {
|
|
|
761
761
|
|
|
762
762
|
let m = Math.floor((s + e) / 2), buffer = this.#storage.getValue(index[m], length)
|
|
763
763
|
|
|
764
|
-
if (compareBuffer(before, buffer)) return this.#findByTime(index, after, before, length, s, m - 1)
|
|
765
|
-
if (compareBuffer(after, buffer)) return this.#findByTime(index, after, before, length, m + 1, e)
|
|
764
|
+
if (compareBuffer(before, buffer) < 0) return this.#findByTime(index, after, before, length, s, m - 1)
|
|
765
|
+
if (compareBuffer(after, buffer) > 0) return this.#findByTime(index, after, before, length, m + 1, e)
|
|
766
766
|
|
|
767
767
|
let result = [index[m]], i
|
|
768
768
|
|
|
@@ -811,6 +811,56 @@ export class Entities extends Array {
|
|
|
811
811
|
|
|
812
812
|
return s2b(value, this.#lengthSet[name])
|
|
813
813
|
}
|
|
814
|
+
|
|
815
|
+
intToBuffer(value, name) {
|
|
816
|
+
|
|
817
|
+
const size = this.#lengthSet[name]
|
|
818
|
+
|
|
819
|
+
const buffer = Buffer.alloc(size)
|
|
820
|
+
|
|
821
|
+
switch (size) {
|
|
822
|
+
|
|
823
|
+
case 1:
|
|
824
|
+
buffer.writeInt8(value)
|
|
825
|
+
break
|
|
826
|
+
case 2:
|
|
827
|
+
buffer.writeInt16BE(value)
|
|
828
|
+
break
|
|
829
|
+
case 3:
|
|
830
|
+
writeInt24BE(buffer, value)
|
|
831
|
+
break
|
|
832
|
+
case 4:
|
|
833
|
+
buffer.writeInt32BE(value)
|
|
834
|
+
break
|
|
835
|
+
}
|
|
836
|
+
|
|
837
|
+
return buffer
|
|
838
|
+
}
|
|
839
|
+
|
|
840
|
+
uintToBuffer(value, name) {
|
|
841
|
+
|
|
842
|
+
const size = this.#lengthSet[name]
|
|
843
|
+
|
|
844
|
+
const buffer = Buffer.alloc(size)
|
|
845
|
+
|
|
846
|
+
switch (size) {
|
|
847
|
+
|
|
848
|
+
case 1:
|
|
849
|
+
buffer.writeUint8(value)
|
|
850
|
+
break
|
|
851
|
+
case 2:
|
|
852
|
+
buffer.writeUint16BE(value)
|
|
853
|
+
break
|
|
854
|
+
case 3:
|
|
855
|
+
writeUint24BE(buffer, value)
|
|
856
|
+
break
|
|
857
|
+
case 4:
|
|
858
|
+
buffer.writeUint32BE(value)
|
|
859
|
+
break
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
return buffer
|
|
863
|
+
}
|
|
814
864
|
}
|
|
815
865
|
|
|
816
866
|
const u2o = (url = '') => {
|
|
@@ -1113,6 +1163,11 @@ export class Storage {
|
|
|
1113
1163
|
}
|
|
1114
1164
|
}
|
|
1115
1165
|
|
|
1166
|
+
/**
|
|
1167
|
+
* @param {Number} position
|
|
1168
|
+
* @param {Number} length
|
|
1169
|
+
* @returns {Buffer}
|
|
1170
|
+
*/
|
|
1116
1171
|
getValue(position, length) {
|
|
1117
1172
|
|
|
1118
1173
|
if (this.#values.has(position)) {
|
package/generator.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import {copyFileSync, existsSync, mkdirSync, readdirSync, statSync, writeFileSync} from "node:fs";
|
|
2
|
-
import {buffer, string, Storage, TypeLen} from './architect.mjs'
|
|
2
|
+
import {buffer, string, Storage, TypeLen, readInt24BE, readUint24BE, compareBuffer} from './architect.mjs'
|
|
3
3
|
import {resolve} from 'node:path'
|
|
4
4
|
|
|
5
5
|
const modulePath = import.meta.url.endsWith('?test=true') ? '../../architect.mjs' : '@k3000/store/architect.mjs'
|
|
@@ -127,7 +127,7 @@ class Type {
|
|
|
127
127
|
|
|
128
128
|
if (this.#index) {
|
|
129
129
|
|
|
130
|
-
obj.index =
|
|
130
|
+
obj.index = true
|
|
131
131
|
}
|
|
132
132
|
|
|
133
133
|
if (this.#value !== undefined) {
|
|
@@ -582,13 +582,15 @@ function appendSet(name, set, remark = '') {
|
|
|
582
582
|
|
|
583
583
|
} else if (typeof value.type === "string" || value.type instanceof String) {
|
|
584
584
|
|
|
585
|
+
if (!Reflect.has(typeMap, value.type)) continue
|
|
586
|
+
|
|
585
587
|
value.length = value.length || TypeLen[value.type]
|
|
586
588
|
|
|
587
589
|
value.type = typeMap[value.type]
|
|
588
590
|
|
|
589
591
|
if (value.index) {
|
|
590
592
|
|
|
591
|
-
value.index =
|
|
593
|
+
value.index = true
|
|
592
594
|
|
|
593
595
|
} else if (Reflect.has(value, 'index')) {
|
|
594
596
|
|
|
@@ -633,13 +635,15 @@ function updateCol(name, set) {
|
|
|
633
635
|
|
|
634
636
|
} else if (typeof value.type === "string" || value.type instanceof String) {
|
|
635
637
|
|
|
638
|
+
if (!Reflect.has(typeMap, value.type)) continue
|
|
639
|
+
|
|
636
640
|
value.length = value.length || TypeLen[value.type]
|
|
637
641
|
|
|
638
642
|
value.type = typeMap[value.type]
|
|
639
643
|
|
|
640
644
|
if (value.index) {
|
|
641
645
|
|
|
642
|
-
value.index =
|
|
646
|
+
value.index = true
|
|
643
647
|
|
|
644
648
|
} else if (Reflect.has(value, 'index')) {
|
|
645
649
|
|
|
@@ -787,6 +791,128 @@ function updateSetRemark(name, remark) {
|
|
|
787
791
|
}
|
|
788
792
|
}
|
|
789
793
|
|
|
794
|
+
const updateIndex2 = function (store, record, struct) {
|
|
795
|
+
|
|
796
|
+
record = record.map(position => position + struct.position)
|
|
797
|
+
|
|
798
|
+
switch (struct.type) {
|
|
799
|
+
|
|
800
|
+
case typeMap.id:
|
|
801
|
+
|
|
802
|
+
return record.sort((a, b) => store.getValue(a, struct.length).readUInt32BE()
|
|
803
|
+
- store.getValue(b, struct.length).readUInt32BE())
|
|
804
|
+
|
|
805
|
+
case typeMap.uint:
|
|
806
|
+
|
|
807
|
+
switch (struct.length) {
|
|
808
|
+
|
|
809
|
+
case 1:
|
|
810
|
+
|
|
811
|
+
return record.sort((a, b) => store.getValue(a, struct.length).readUInt8()
|
|
812
|
+
- store.getValue(b, struct.length).readUInt8())
|
|
813
|
+
|
|
814
|
+
case 2:
|
|
815
|
+
|
|
816
|
+
return record.sort((a, b) => store.getValue(a, struct.length).readUInt16BE()
|
|
817
|
+
- store.getValue(b, struct.length).readUInt16BE())
|
|
818
|
+
|
|
819
|
+
case 3:
|
|
820
|
+
|
|
821
|
+
return record.sort((a, b) => readUint24BE(store.getValue(a, struct.length))
|
|
822
|
+
- readUint24BE(store.getValue(b, struct.length)))
|
|
823
|
+
|
|
824
|
+
case 4:
|
|
825
|
+
|
|
826
|
+
return record.sort((a, b) => store.getValue(a, struct.length).readUInt32BE()
|
|
827
|
+
- store.getValue(b, struct.length).readUInt32BE())
|
|
828
|
+
}
|
|
829
|
+
|
|
830
|
+
case typeMap.int:
|
|
831
|
+
|
|
832
|
+
switch (struct.length) {
|
|
833
|
+
|
|
834
|
+
case 1:
|
|
835
|
+
|
|
836
|
+
return record.sort((a, b) => store.getValue(a, struct.length).readInt8()
|
|
837
|
+
- store.getValue(b, struct.length).readInt8())
|
|
838
|
+
|
|
839
|
+
case 2:
|
|
840
|
+
|
|
841
|
+
return record.sort((a, b) => store.getValue(a, struct.length).readInt16BE()
|
|
842
|
+
- store.getValue(b, struct.length).readInt16BE())
|
|
843
|
+
|
|
844
|
+
case 3:
|
|
845
|
+
|
|
846
|
+
return record.sort((a, b) => readInt24BE(store.getValue(a, struct.length))
|
|
847
|
+
- readInt24BE(store.getValue(b, struct.length)))
|
|
848
|
+
|
|
849
|
+
case 4:
|
|
850
|
+
|
|
851
|
+
return record.sort((a, b) => store.getValue(a, struct.length).readInt32BE()
|
|
852
|
+
- store.getValue(b, struct.length).readInt32BE())
|
|
853
|
+
}
|
|
854
|
+
|
|
855
|
+
case typeMap.bigUint:
|
|
856
|
+
|
|
857
|
+
return record.sort((a, b) => store.getValue(a, struct.length).readBigUInt64BE()
|
|
858
|
+
- store.getValue(b, struct.length).readBigUInt64BE())
|
|
859
|
+
|
|
860
|
+
case typeMap.bigint:
|
|
861
|
+
|
|
862
|
+
return record.sort((a, b) => store.getValue(a, struct.length).readBigInt64BE()
|
|
863
|
+
- store.getValue(b, struct.length).readBigInt64BE())
|
|
864
|
+
|
|
865
|
+
case typeMap.time:
|
|
866
|
+
|
|
867
|
+
return record.sort((a, b) => b2d(store.getValue(a, struct.length))
|
|
868
|
+
- b2d(store.getValue(b, struct.length)))
|
|
869
|
+
|
|
870
|
+
case typeMap.float:
|
|
871
|
+
|
|
872
|
+
return record.sort((a, b) => store.getValue(a, struct.length).readDoubleBE()
|
|
873
|
+
- store.getValue(b, struct.length).readDoubleBE())
|
|
874
|
+
|
|
875
|
+
case typeMap.string:
|
|
876
|
+
|
|
877
|
+
return record.sort((a, b) => compareBuffer(store.getValue(a, struct.length)
|
|
878
|
+
, store.getValue(b, struct.length)))
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
const updateIndex = function (name, set) {
|
|
883
|
+
|
|
884
|
+
console.log(this.curr.record.index)
|
|
885
|
+
|
|
886
|
+
const struct = this.curr.struct.base[name]
|
|
887
|
+
const index = this.curr.record.index[name]
|
|
888
|
+
|
|
889
|
+
if (index === undefined) return
|
|
890
|
+
|
|
891
|
+
for (let [key, value] of Object.entries(set)) {
|
|
892
|
+
|
|
893
|
+
if (!Reflect.has(struct, key)) continue
|
|
894
|
+
|
|
895
|
+
if (value) {
|
|
896
|
+
|
|
897
|
+
if (!Reflect.has(index, key)) {
|
|
898
|
+
|
|
899
|
+
const indexList = updateIndex2(this.curr, [...this.curr.record.record[name]], struct[key])
|
|
900
|
+
|
|
901
|
+
if (indexList) {
|
|
902
|
+
|
|
903
|
+
struct[key].index = true
|
|
904
|
+
index[key] = indexList
|
|
905
|
+
}
|
|
906
|
+
}
|
|
907
|
+
|
|
908
|
+
} else if (Reflect.has(struct, key)) {
|
|
909
|
+
|
|
910
|
+
delete struct[key].index
|
|
911
|
+
delete index[key]
|
|
912
|
+
}
|
|
913
|
+
}
|
|
914
|
+
}
|
|
915
|
+
|
|
790
916
|
const commentRemark = (remark = '') => remark.replace(/\n/g, '\n\t * ')
|
|
791
917
|
|
|
792
918
|
function submit() {
|
|
@@ -1426,6 +1552,18 @@ export default function upgrade(name, {
|
|
|
1426
1552
|
* updateSetRemark(name, remark)
|
|
1427
1553
|
*/
|
|
1428
1554
|
updateSetRemark: updateSetRemark.bind(scope),
|
|
1555
|
+
/**
|
|
1556
|
+
* 更新索引
|
|
1557
|
+
* @type {function}
|
|
1558
|
+
* @param {string} name
|
|
1559
|
+
* @param {Object} set
|
|
1560
|
+
* @example
|
|
1561
|
+
* updateIndex(name, {
|
|
1562
|
+
* uid: true,
|
|
1563
|
+
* pwd: false,
|
|
1564
|
+
* })
|
|
1565
|
+
*/
|
|
1566
|
+
updateIndex: updateIndex.bind(scope),
|
|
1429
1567
|
/**
|
|
1430
1568
|
* 提交跟新
|
|
1431
1569
|
* @type {function}
|
package/package.json
CHANGED
package/test/1/data
CHANGED
|
Binary file
|
package/test/1/index
CHANGED
|
Binary file
|
package/test/2/data
CHANGED
|
Binary file
|
package/test/2/index
CHANGED
|
Binary file
|
package/test/3/index
CHANGED
|
Binary file
|
package/test/4/index
CHANGED
|
Binary file
|
package/test/5/index
CHANGED
|
Binary file
|
package/test/6/index
CHANGED
|
Binary file
|
package/test/7/index
CHANGED
|
Binary file
|
package/test/7/index.mjs
CHANGED
|
@@ -132,7 +132,7 @@ class Log extends Entity {
|
|
|
132
132
|
enumerable: true,
|
|
133
133
|
configurable: false,
|
|
134
134
|
get: () => super.get($type).readUint16BE(0),
|
|
135
|
-
set: value => super.
|
|
135
|
+
set: value => super.set2($type, super.uintToBuffer(value, $type)),
|
|
136
136
|
},
|
|
137
137
|
uid: {
|
|
138
138
|
enumerable: true,
|
|
@@ -301,6 +301,11 @@ class LogSet extends Entities {
|
|
|
301
301
|
return super.findByValue($uid, super.s2b(uid, $uid))
|
|
302
302
|
}
|
|
303
303
|
|
|
304
|
+
indexByType(type) {
|
|
305
|
+
|
|
306
|
+
return super.findByValue($type, super.uintToBuffer(type, $type))
|
|
307
|
+
}
|
|
308
|
+
|
|
304
309
|
indexByTime({after, before} = {}) {
|
|
305
310
|
|
|
306
311
|
return super.findByTime($time, {after, before})
|
package/test/7/type.ts
CHANGED
package/test/8/data
ADDED
|
Binary file
|
package/test/8/index
ADDED
|
Binary file
|
package/test/8/index.mjs
ADDED
|
@@ -0,0 +1,373 @@
|
|
|
1
|
+
|
|
2
|
+
import {Entities, Entity, Storage, b2d, d2b, b2s, position, uInt32BEToBuffer, int32BEToBuffer,
|
|
3
|
+
doubleBEToBuffer, bigint32BEToBuffer, bigUint32BEToBuffer, readInt24BE, readUint24BE, Store} from '../../architect.mjs'
|
|
4
|
+
import {struct} from '../../generator.mjs'
|
|
5
|
+
|
|
6
|
+
const storage = new Storage(import.meta.url)
|
|
7
|
+
|
|
8
|
+
const $id = 'id'
|
|
9
|
+
const $uid = 'uid'
|
|
10
|
+
const $pwd = 'pwd'
|
|
11
|
+
const $valid = 'valid'
|
|
12
|
+
const $time = 'time'
|
|
13
|
+
const $type = 'type'
|
|
14
|
+
const $content = 'content'
|
|
15
|
+
const $test = 'test'
|
|
16
|
+
const $str = 'str'
|
|
17
|
+
const $test2 = 'test2'
|
|
18
|
+
|
|
19
|
+
class Admin extends Entity {
|
|
20
|
+
|
|
21
|
+
static name = 'admin'
|
|
22
|
+
|
|
23
|
+
static create(_) {
|
|
24
|
+
_.id = storage.updateValue(_.id, Admin.name, $id)
|
|
25
|
+
_.pwd = _.pwd || '123456'
|
|
26
|
+
_.time = _.time || new Date()
|
|
27
|
+
_.valid = _.valid || 0
|
|
28
|
+
return _
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
constructor(...arg) {
|
|
32
|
+
|
|
33
|
+
super(storage, ...arg)
|
|
34
|
+
|
|
35
|
+
return Object.defineProperties({}, {
|
|
36
|
+
[position]: {
|
|
37
|
+
enumerable: false,
|
|
38
|
+
configurable: false,
|
|
39
|
+
get: () => this[position],
|
|
40
|
+
},
|
|
41
|
+
id: {
|
|
42
|
+
enumerable: true,
|
|
43
|
+
configurable: false,
|
|
44
|
+
get: () => super.get($id).readUInt32BE(0),
|
|
45
|
+
set: value => {
|
|
46
|
+
super.set2($id, uInt32BEToBuffer(value))
|
|
47
|
+
this.#update()
|
|
48
|
+
},
|
|
49
|
+
},
|
|
50
|
+
pwd: {
|
|
51
|
+
enumerable: true,
|
|
52
|
+
configurable: false,
|
|
53
|
+
get: () => b2s(super.get($pwd)),
|
|
54
|
+
set: value => {
|
|
55
|
+
super.set($pwd, super.s2b(value, $pwd))
|
|
56
|
+
this.#update()
|
|
57
|
+
},
|
|
58
|
+
},
|
|
59
|
+
time: {
|
|
60
|
+
enumerable: true,
|
|
61
|
+
configurable: false,
|
|
62
|
+
get: () => b2d(super.get($time)),
|
|
63
|
+
set: value => super.set2($time, d2b(value)),
|
|
64
|
+
},
|
|
65
|
+
uid: {
|
|
66
|
+
enumerable: true,
|
|
67
|
+
configurable: false,
|
|
68
|
+
get: () => b2s(super.get($uid)),
|
|
69
|
+
set: value => {
|
|
70
|
+
super.set2($uid, super.s2b(value, $uid))
|
|
71
|
+
this.#update()
|
|
72
|
+
},
|
|
73
|
+
},
|
|
74
|
+
valid: {
|
|
75
|
+
enumerable: true,
|
|
76
|
+
configurable: false,
|
|
77
|
+
get: () => super.get($valid).readUint8(0),
|
|
78
|
+
set: value => {
|
|
79
|
+
super.set2($valid, super.uintToBuffer(value, $valid))
|
|
80
|
+
this.#update()
|
|
81
|
+
},
|
|
82
|
+
},
|
|
83
|
+
})
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
#update() {
|
|
88
|
+
|
|
89
|
+
this.time = Date.now()
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
class Log extends Entity {
|
|
94
|
+
|
|
95
|
+
static name = 'log'
|
|
96
|
+
|
|
97
|
+
static create(_) {
|
|
98
|
+
_.id = storage.updateValue(_.id, Log.name, $id)
|
|
99
|
+
_.time = _.time || new Date()
|
|
100
|
+
return _
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
constructor(...arg) {
|
|
104
|
+
|
|
105
|
+
super(storage, ...arg)
|
|
106
|
+
|
|
107
|
+
return Object.defineProperties({}, {
|
|
108
|
+
[position]: {
|
|
109
|
+
enumerable: false,
|
|
110
|
+
configurable: false,
|
|
111
|
+
get: () => this[position],
|
|
112
|
+
},
|
|
113
|
+
content: {
|
|
114
|
+
enumerable: true,
|
|
115
|
+
configurable: false,
|
|
116
|
+
get: () => b2s(super.get($content)),
|
|
117
|
+
set: value => super.set($content, super.s2b(value, $content)),
|
|
118
|
+
},
|
|
119
|
+
id: {
|
|
120
|
+
enumerable: true,
|
|
121
|
+
configurable: false,
|
|
122
|
+
get: () => super.get($id).readUInt32BE(0),
|
|
123
|
+
set: value => super.set2($id, uInt32BEToBuffer(value)),
|
|
124
|
+
},
|
|
125
|
+
time: {
|
|
126
|
+
enumerable: true,
|
|
127
|
+
configurable: false,
|
|
128
|
+
get: () => b2d(super.get($time)),
|
|
129
|
+
set: value => super.set2($time, d2b(value)),
|
|
130
|
+
},
|
|
131
|
+
type: {
|
|
132
|
+
enumerable: true,
|
|
133
|
+
configurable: false,
|
|
134
|
+
get: () => super.get($type).readUint16BE(0),
|
|
135
|
+
set: value => super.set2($type, super.uintToBuffer(value, $type)),
|
|
136
|
+
},
|
|
137
|
+
uid: {
|
|
138
|
+
enumerable: true,
|
|
139
|
+
configurable: false,
|
|
140
|
+
get: () => b2s(super.get($uid)),
|
|
141
|
+
set: value => super.set2($uid, super.s2b(value, $uid)),
|
|
142
|
+
},
|
|
143
|
+
})
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
class Test3 extends Entity {
|
|
149
|
+
|
|
150
|
+
static name = 'test3'
|
|
151
|
+
|
|
152
|
+
static create(_) {
|
|
153
|
+
|
|
154
|
+
return _
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
constructor(...arg) {
|
|
158
|
+
|
|
159
|
+
super(storage, ...arg)
|
|
160
|
+
|
|
161
|
+
return Object.defineProperties({}, {
|
|
162
|
+
[position]: {
|
|
163
|
+
enumerable: false,
|
|
164
|
+
configurable: false,
|
|
165
|
+
get: () => this[position],
|
|
166
|
+
},
|
|
167
|
+
pwd: {
|
|
168
|
+
enumerable: true,
|
|
169
|
+
configurable: false,
|
|
170
|
+
get: () => b2s(super.get($pwd)),
|
|
171
|
+
set: value => super.set($pwd, super.s2b(value, $pwd)),
|
|
172
|
+
},
|
|
173
|
+
str: {
|
|
174
|
+
enumerable: true,
|
|
175
|
+
configurable: false,
|
|
176
|
+
get: () => b2s(super.get($str)),
|
|
177
|
+
set: value => super.set($str, super.s2b(value, $str)),
|
|
178
|
+
},
|
|
179
|
+
test: {
|
|
180
|
+
enumerable: true,
|
|
181
|
+
configurable: false,
|
|
182
|
+
get: () => b2s(super.get($test)),
|
|
183
|
+
set: value => super.set($test, super.s2b(value, $test)),
|
|
184
|
+
},
|
|
185
|
+
test2: {
|
|
186
|
+
enumerable: true,
|
|
187
|
+
configurable: false,
|
|
188
|
+
get: () => b2s(super.get($test2)),
|
|
189
|
+
set: value => super.set($test2, super.s2b(value, $test2)),
|
|
190
|
+
},
|
|
191
|
+
uid: {
|
|
192
|
+
enumerable: true,
|
|
193
|
+
configurable: false,
|
|
194
|
+
get: () => b2s(super.get($uid)),
|
|
195
|
+
set: value => super.set($uid, super.s2b(value, $uid)),
|
|
196
|
+
},
|
|
197
|
+
})
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
|
|
203
|
+
class AdminSet extends Entities {
|
|
204
|
+
|
|
205
|
+
constructor() {
|
|
206
|
+
|
|
207
|
+
super(storage, Admin)
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
* 内部方法
|
|
211
|
+
*/
|
|
212
|
+
s2b() {
|
|
213
|
+
|
|
214
|
+
throw new TypeError('s2b is not a function')
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* 内部方法
|
|
218
|
+
*/
|
|
219
|
+
findByValue() {
|
|
220
|
+
|
|
221
|
+
throw new TypeError('findByValue is not a function')
|
|
222
|
+
}
|
|
223
|
+
/**
|
|
224
|
+
* 内部方法
|
|
225
|
+
*/
|
|
226
|
+
findByTime() {
|
|
227
|
+
|
|
228
|
+
throw new TypeError('findByTime is not a function')
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* 内部方法
|
|
232
|
+
*/
|
|
233
|
+
stringToBuffer() {
|
|
234
|
+
|
|
235
|
+
throw new TypeError('stringToBuffer is not a function')
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
indexById(id) {
|
|
239
|
+
|
|
240
|
+
return super.findByValue($id, uInt32BEToBuffer(id, $id))
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
indexByUid(uid) {
|
|
244
|
+
|
|
245
|
+
return super.findByValue($uid, super.s2b(uid, $uid))
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
indexByValid(valid) {
|
|
249
|
+
|
|
250
|
+
return super.findByValue($valid, super.uintToBuffer(valid, $valid))
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
indexByTime({after, before} = {}) {
|
|
254
|
+
|
|
255
|
+
return super.findByTime($time, {after, before})
|
|
256
|
+
}
|
|
257
|
+
}
|
|
258
|
+
|
|
259
|
+
class LogSet extends Entities {
|
|
260
|
+
|
|
261
|
+
constructor() {
|
|
262
|
+
|
|
263
|
+
super(storage, Log)
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* 内部方法
|
|
267
|
+
*/
|
|
268
|
+
s2b() {
|
|
269
|
+
|
|
270
|
+
throw new TypeError('s2b is not a function')
|
|
271
|
+
}
|
|
272
|
+
/**
|
|
273
|
+
* 内部方法
|
|
274
|
+
*/
|
|
275
|
+
findByValue() {
|
|
276
|
+
|
|
277
|
+
throw new TypeError('findByValue is not a function')
|
|
278
|
+
}
|
|
279
|
+
/**
|
|
280
|
+
* 内部方法
|
|
281
|
+
*/
|
|
282
|
+
findByTime() {
|
|
283
|
+
|
|
284
|
+
throw new TypeError('findByTime is not a function')
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* 内部方法
|
|
288
|
+
*/
|
|
289
|
+
stringToBuffer() {
|
|
290
|
+
|
|
291
|
+
throw new TypeError('stringToBuffer is not a function')
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
indexById(id) {
|
|
295
|
+
|
|
296
|
+
return super.findByValue($id, uInt32BEToBuffer(id, $id))
|
|
297
|
+
}
|
|
298
|
+
|
|
299
|
+
indexByUid(uid) {
|
|
300
|
+
|
|
301
|
+
return super.findByValue($uid, super.s2b(uid, $uid))
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
indexByType(type) {
|
|
305
|
+
|
|
306
|
+
return super.findByValue($type, super.uintToBuffer(type, $type))
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
indexByTime({after, before} = {}) {
|
|
310
|
+
|
|
311
|
+
return super.findByTime($time, {after, before})
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
class Test3Set extends Entities {
|
|
316
|
+
|
|
317
|
+
constructor() {
|
|
318
|
+
|
|
319
|
+
super(storage, Test3)
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* 内部方法
|
|
323
|
+
*/
|
|
324
|
+
s2b() {
|
|
325
|
+
|
|
326
|
+
throw new TypeError('s2b is not a function')
|
|
327
|
+
}
|
|
328
|
+
/**
|
|
329
|
+
* 内部方法
|
|
330
|
+
*/
|
|
331
|
+
findByValue() {
|
|
332
|
+
|
|
333
|
+
throw new TypeError('findByValue is not a function')
|
|
334
|
+
}
|
|
335
|
+
/**
|
|
336
|
+
* 内部方法
|
|
337
|
+
*/
|
|
338
|
+
findByTime() {
|
|
339
|
+
|
|
340
|
+
throw new TypeError('findByTime is not a function')
|
|
341
|
+
}
|
|
342
|
+
/**
|
|
343
|
+
* 内部方法
|
|
344
|
+
*/
|
|
345
|
+
stringToBuffer() {
|
|
346
|
+
|
|
347
|
+
throw new TypeError('stringToBuffer is not a function')
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
export const remark = Symbol('remark')
|
|
353
|
+
|
|
354
|
+
export const close = () => storage.close()
|
|
355
|
+
|
|
356
|
+
export const getStruct = name => struct(storage, remark, name)
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* @type {import('./type').Storage}
|
|
360
|
+
*/
|
|
361
|
+
const store = Object.freeze({
|
|
362
|
+
admin: new Proxy([], {
|
|
363
|
+
get: new AdminSet()
|
|
364
|
+
}),
|
|
365
|
+
log: new Proxy([], {
|
|
366
|
+
get: new LogSet()
|
|
367
|
+
}),
|
|
368
|
+
test3: new Proxy([], {
|
|
369
|
+
get: new Test3Set()
|
|
370
|
+
}),
|
|
371
|
+
})
|
|
372
|
+
|
|
373
|
+
export default store
|
package/test/8/type.ts
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
|
|
2
|
+
interface Admin {
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
*
|
|
6
|
+
*/
|
|
7
|
+
id: Number
|
|
8
|
+
/**
|
|
9
|
+
* 账号
|
|
10
|
+
*/
|
|
11
|
+
uid: String
|
|
12
|
+
/**
|
|
13
|
+
* 密码
|
|
14
|
+
*/
|
|
15
|
+
pwd: String
|
|
16
|
+
/**
|
|
17
|
+
* 是否有效
|
|
18
|
+
*/
|
|
19
|
+
valid: Number
|
|
20
|
+
/**
|
|
21
|
+
* 登录时间
|
|
22
|
+
*/
|
|
23
|
+
time: Number | Date
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
interface AdminSet extends Array<Admin> {
|
|
27
|
+
|
|
28
|
+
indexById(id: Number): Array<Admin>
|
|
29
|
+
indexByUid(uid: String): Array<Admin>
|
|
30
|
+
indexByValid(valid: Number): Array<Admin>
|
|
31
|
+
indexByTime({after, before}): Array<Admin>
|
|
32
|
+
/**
|
|
33
|
+
* 重写push方法
|
|
34
|
+
*/
|
|
35
|
+
push(...item: Array<Admin>): Array<Admin>
|
|
36
|
+
/**
|
|
37
|
+
* 重写unshift方法
|
|
38
|
+
*/
|
|
39
|
+
unshift(...item: Array<Admin>): Array<Admin>
|
|
40
|
+
/**
|
|
41
|
+
* 分页查询
|
|
42
|
+
*/
|
|
43
|
+
page(predicate: Function, index: Number | String, size: Number | String, params: Object): Array<Admin>
|
|
44
|
+
/**
|
|
45
|
+
* 移除数据
|
|
46
|
+
*/
|
|
47
|
+
remove(...items: Array<Admin>): void
|
|
48
|
+
/**
|
|
49
|
+
* 联合查询,类似LEFT JOIN
|
|
50
|
+
*/
|
|
51
|
+
eachFlat(array: Array<Object>, predicate: Function | String): Array<Object>
|
|
52
|
+
/**
|
|
53
|
+
* 联合查询,类似INNER JOIN
|
|
54
|
+
*/
|
|
55
|
+
filterFlat(array: Array<Object>, predicate: Function | String): Array<Object>
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
interface Log {
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
*
|
|
62
|
+
*/
|
|
63
|
+
id: Number
|
|
64
|
+
/**
|
|
65
|
+
* 账号
|
|
66
|
+
*/
|
|
67
|
+
uid: String
|
|
68
|
+
/**
|
|
69
|
+
* 类型
|
|
70
|
+
*/
|
|
71
|
+
type: Number
|
|
72
|
+
/**
|
|
73
|
+
* 创建时间
|
|
74
|
+
*/
|
|
75
|
+
time: Number | Date
|
|
76
|
+
/**
|
|
77
|
+
* 内容
|
|
78
|
+
*/
|
|
79
|
+
content: String
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
interface LogSet extends Array<Log> {
|
|
83
|
+
|
|
84
|
+
indexById(id: Number): Array<Log>
|
|
85
|
+
indexByUid(uid: String): Array<Log>
|
|
86
|
+
indexByType(type: Number): Array<Log>
|
|
87
|
+
indexByTime({after, before}): Array<Log>
|
|
88
|
+
/**
|
|
89
|
+
* 重写push方法
|
|
90
|
+
*/
|
|
91
|
+
push(...item: Array<Log>): Array<Log>
|
|
92
|
+
/**
|
|
93
|
+
* 重写unshift方法
|
|
94
|
+
*/
|
|
95
|
+
unshift(...item: Array<Log>): Array<Log>
|
|
96
|
+
/**
|
|
97
|
+
* 分页查询
|
|
98
|
+
*/
|
|
99
|
+
page(predicate: Function, index: Number | String, size: Number | String, params: Object): Array<Log>
|
|
100
|
+
/**
|
|
101
|
+
* 移除数据
|
|
102
|
+
*/
|
|
103
|
+
remove(...items: Array<Log>): void
|
|
104
|
+
/**
|
|
105
|
+
* 联合查询,类似LEFT JOIN
|
|
106
|
+
*/
|
|
107
|
+
eachFlat(array: Array<Object>, predicate: Function | String): Array<Object>
|
|
108
|
+
/**
|
|
109
|
+
* 联合查询,类似INNER JOIN
|
|
110
|
+
*/
|
|
111
|
+
filterFlat(array: Array<Object>, predicate: Function | String): Array<Object>
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
interface Test3 {
|
|
115
|
+
|
|
116
|
+
/**
|
|
117
|
+
* 账
|
|
118
|
+
* 户
|
|
119
|
+
*/
|
|
120
|
+
uid: String
|
|
121
|
+
/**
|
|
122
|
+
*
|
|
123
|
+
*/
|
|
124
|
+
pwd: String
|
|
125
|
+
/**
|
|
126
|
+
*
|
|
127
|
+
*/
|
|
128
|
+
test: String
|
|
129
|
+
/**
|
|
130
|
+
*
|
|
131
|
+
*/
|
|
132
|
+
str: String
|
|
133
|
+
/**
|
|
134
|
+
*
|
|
135
|
+
*/
|
|
136
|
+
test2: String
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
interface Test3Set extends Array<Test3> {
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* 重写push方法
|
|
143
|
+
*/
|
|
144
|
+
push(...item: Array<Test3>): Array<Test3>
|
|
145
|
+
/**
|
|
146
|
+
* 重写unshift方法
|
|
147
|
+
*/
|
|
148
|
+
unshift(...item: Array<Test3>): Array<Test3>
|
|
149
|
+
/**
|
|
150
|
+
* 分页查询
|
|
151
|
+
*/
|
|
152
|
+
page(predicate: Function, index: Number | String, size: Number | String, params: Object): Array<Test3>
|
|
153
|
+
/**
|
|
154
|
+
* 移除数据
|
|
155
|
+
*/
|
|
156
|
+
remove(...items: Array<Test3>): void
|
|
157
|
+
/**
|
|
158
|
+
* 联合查询,类似LEFT JOIN
|
|
159
|
+
*/
|
|
160
|
+
eachFlat(array: Array<Object>, predicate: Function | String): Array<Object>
|
|
161
|
+
/**
|
|
162
|
+
* 联合查询,类似INNER JOIN
|
|
163
|
+
*/
|
|
164
|
+
filterFlat(array: Array<Object>, predicate: Function | String): Array<Object>
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
export interface Storage {
|
|
169
|
+
admin: AdminSet,
|
|
170
|
+
log: LogSet,
|
|
171
|
+
test3: Test3Set
|
|
172
|
+
}
|
package/test/index.mjs
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
|
|
2
2
|
const index = import.meta.url.indexOf('?')
|
|
3
3
|
|
|
4
|
-
export const modules = await import(`./
|
|
4
|
+
export const modules = await import(`./8/index.mjs${index > -1 ? import.meta.url.substring(index) : ''}`)
|
|
5
5
|
|
|
6
6
|
export const close = modules.close
|
|
7
7
|
export const remark = modules.remark
|
|
8
|
-
export const version =
|
|
8
|
+
export const version = 8
|
|
9
9
|
export const getStruct = modules.getStruct
|
|
10
10
|
|
|
11
11
|
/**
|
|
12
|
-
* @type {import('./
|
|
12
|
+
* @type {import('./8/type').Storage}
|
|
13
13
|
*/
|
|
14
14
|
export const storage = modules.default
|
|
15
15
|
|
package/test.mjs
CHANGED
|
@@ -15,7 +15,6 @@ import upgrade, {
|
|
|
15
15
|
} from './generator.mjs?test=true'
|
|
16
16
|
// } from '@k3000/store/generator.mjs'
|
|
17
17
|
import {existsSync, readdirSync, rmdirSync, statSync, unlinkSync} from "node:fs";
|
|
18
|
-
import {json} from "node:stream/consumers";
|
|
19
18
|
|
|
20
19
|
test('测试类型类', async t => {
|
|
21
20
|
|
|
@@ -23,7 +22,7 @@ test('测试类型类', async t => {
|
|
|
23
22
|
|
|
24
23
|
assert.strictEqual(
|
|
25
24
|
Id('备注').toString(),
|
|
26
|
-
'{"type":0,"remark":"备注","index":
|
|
25
|
+
'{"type":0,"remark":"备注","index":true,"value":1,"length":4,"step":1}')
|
|
27
26
|
})
|
|
28
27
|
|
|
29
28
|
await t.test('测试Id类型完整配置', _ => {
|
|
@@ -66,7 +65,7 @@ test('测试类型类', async t => {
|
|
|
66
65
|
.step()
|
|
67
66
|
.remark()
|
|
68
67
|
.toString(),
|
|
69
|
-
'{"type":1,"remark":"","index":
|
|
68
|
+
'{"type":1,"remark":"","index":true,"value":0,"length":4,"step":1}')
|
|
70
69
|
})
|
|
71
70
|
|
|
72
71
|
await t.test('测试Int类型缺省配置', _ => {
|
|
@@ -97,7 +96,7 @@ test('测试类型类', async t => {
|
|
|
97
96
|
.step()
|
|
98
97
|
.remark()
|
|
99
98
|
.toString(),
|
|
100
|
-
'{"type":2,"remark":"","index":
|
|
99
|
+
'{"type":2,"remark":"","index":true,"value":0,"length":4,"step":1}')
|
|
101
100
|
})
|
|
102
101
|
|
|
103
102
|
await t.test('测试BigUint类型缺省配置', _ => {
|
|
@@ -128,7 +127,7 @@ test('测试类型类', async t => {
|
|
|
128
127
|
.step()
|
|
129
128
|
.remark()
|
|
130
129
|
.toString(),
|
|
131
|
-
'{"type":3,"remark":"","index":
|
|
130
|
+
'{"type":3,"remark":"","index":true,"value":"0","length":8,"step":1}')
|
|
132
131
|
})
|
|
133
132
|
|
|
134
133
|
await t.test('测试Bigint类型缺省配置', _ => {
|
|
@@ -159,7 +158,7 @@ test('测试类型类', async t => {
|
|
|
159
158
|
.step()
|
|
160
159
|
.remark()
|
|
161
160
|
.toString(),
|
|
162
|
-
'{"type":4,"remark":"","index":
|
|
161
|
+
'{"type":4,"remark":"","index":true,"value":"0","length":8,"step":1}')
|
|
163
162
|
})
|
|
164
163
|
|
|
165
164
|
await t.test('测试Time类型缺省配置', _ => {
|
|
@@ -189,7 +188,7 @@ test('测试类型类', async t => {
|
|
|
189
188
|
.step()
|
|
190
189
|
.remark()
|
|
191
190
|
.toString(),
|
|
192
|
-
'{"type":5,"remark":"","index":
|
|
191
|
+
'{"type":5,"remark":"","index":true,"value":0,"length":6}')
|
|
193
192
|
})
|
|
194
193
|
|
|
195
194
|
await t.test('测试Float类型缺省配置', _ => {
|
|
@@ -220,7 +219,7 @@ test('测试类型类', async t => {
|
|
|
220
219
|
.step()
|
|
221
220
|
.remark()
|
|
222
221
|
.toString(),
|
|
223
|
-
'{"type":6,"remark":"","index":
|
|
222
|
+
'{"type":6,"remark":"","index":true,"value":0,"length":8,"step":1}')
|
|
224
223
|
})
|
|
225
224
|
|
|
226
225
|
await t.test('测试String类型缺省配置', _ => {
|
|
@@ -251,7 +250,7 @@ test('测试类型类', async t => {
|
|
|
251
250
|
.step()
|
|
252
251
|
.remark()
|
|
253
252
|
.toString(),
|
|
254
|
-
'{"type":7,"remark":"","index":
|
|
253
|
+
'{"type":7,"remark":"","index":true,"value":"","length":12}')
|
|
255
254
|
})
|
|
256
255
|
|
|
257
256
|
await t.test('测试Buffer类型缺省配置', _ => {
|
|
@@ -595,6 +594,11 @@ test('测试结构操作', async t => {
|
|
|
595
594
|
deleteDir('test/7')
|
|
596
595
|
}
|
|
597
596
|
|
|
597
|
+
if (existsSync('test/8')) {
|
|
598
|
+
|
|
599
|
+
deleteDir('test/8')
|
|
600
|
+
}
|
|
601
|
+
|
|
598
602
|
await new Promise(resolve => {
|
|
599
603
|
|
|
600
604
|
t.test('测试添加操作(appendSet)', _ => {
|
|
@@ -744,18 +748,46 @@ test('测试结构操作', async t => {
|
|
|
744
748
|
})
|
|
745
749
|
})
|
|
746
750
|
|
|
751
|
+
await new Promise(resolve => {
|
|
752
|
+
|
|
753
|
+
t.test('测试更新索引操作(updateIndex)', _ => {
|
|
754
|
+
|
|
755
|
+
const {updateIndex, submit} = upgrade('test', {version: 7, password: '123123'})
|
|
756
|
+
|
|
757
|
+
updateIndex('log', {
|
|
758
|
+
type: true,
|
|
759
|
+
})
|
|
760
|
+
|
|
761
|
+
submit().then(() => import('./test/7/index.mjs?password=123123')).then(m => {
|
|
762
|
+
|
|
763
|
+
const storage = m.default
|
|
764
|
+
|
|
765
|
+
const result = storage.log.indexByType(0)
|
|
766
|
+
|
|
767
|
+
assert.strictEqual(
|
|
768
|
+
JSON.stringify(result),
|
|
769
|
+
'[{"content":"login","id":2,"time":1640966400000,"type":0,"uid":"test"},{"content":"test","id":1,"time":1641052800000,"type":0,"uid":"test"}]')
|
|
770
|
+
|
|
771
|
+
m.close()
|
|
772
|
+
|
|
773
|
+
resolve()
|
|
774
|
+
|
|
775
|
+
}).catch(e => console.error(e))
|
|
776
|
+
})
|
|
777
|
+
})
|
|
778
|
+
|
|
747
779
|
await new Promise(resolve => {
|
|
748
780
|
|
|
749
781
|
t.test('测试压缩操作', _ => {
|
|
750
782
|
|
|
751
783
|
const {submit} = upgrade('test', {
|
|
752
|
-
version:
|
|
784
|
+
version: 8,
|
|
753
785
|
prod: true,
|
|
754
786
|
password: '123123',
|
|
755
787
|
newPwd: '111111'
|
|
756
788
|
})
|
|
757
789
|
|
|
758
|
-
submit().then(() => import('./test/
|
|
790
|
+
submit().then(() => import('./test/8/index.mjs?password=111111')).then(m => {
|
|
759
791
|
|
|
760
792
|
const storage = m.default
|
|
761
793
|
|
package/type.ts
CHANGED
|
@@ -12,8 +12,9 @@ export interface Gen {
|
|
|
12
12
|
deleteSet(name: String): void
|
|
13
13
|
deleteCol(name: String, colName: String): void
|
|
14
14
|
updateSetRemark(name: String, remark: String): void
|
|
15
|
+
updateIndex(name: String, set: Object): void
|
|
15
16
|
/**
|
|
16
17
|
* 提交修改
|
|
17
18
|
*/
|
|
18
19
|
submit(): void
|
|
19
|
-
}
|
|
20
|
+
}
|