@k3000/store 0.1.0

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/package.json ADDED
@@ -0,0 +1,15 @@
1
+ {
2
+ "name": "@k3000/store",
3
+ "version": "0.1.0",
4
+ "description": "storage dmb3",
5
+ "main": "generator.mjs",
6
+ "scripts": {
7
+ "test": "node test.mjs"
8
+ },
9
+ "keywords": [
10
+ "store",
11
+ "storage"
12
+ ],
13
+ "author": "qshfu",
14
+ "license": "ISC"
15
+ }
package/test/0/data ADDED
File without changes
package/test/0/index ADDED
Binary file
@@ -0,0 +1,19 @@
1
+
2
+ import {Storage, Store} from '../../architect.mjs'
3
+ import {struct} from '../../generator.mjs'
4
+
5
+ const storage = new Storage(import.meta.url)
6
+
7
+ export const remark = Symbol('remark')
8
+
9
+ export function getStruct() {
10
+
11
+ return struct(storage, remark)
12
+ }
13
+
14
+ export default new class extends Store {
15
+
16
+ close() {
17
+ storage.close()
18
+ }
19
+ }
package/test/1/data ADDED
Binary file
package/test/1/index ADDED
Binary file
@@ -0,0 +1,453 @@
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 $uint = 'uint'
16
+ const $int = 'int'
17
+ const $bigint = 'bigint'
18
+ const $bigUint = 'bigUint'
19
+ const $float = 'float'
20
+ const $string = 'string'
21
+ const $buffer = 'buffer'
22
+ const $text = 'text'
23
+
24
+ class Admin extends Entity {
25
+
26
+ static name = 'admin'
27
+
28
+ static create({
29
+ id,
30
+ pwd,
31
+ time,
32
+ uid,
33
+ valid
34
+ }) {
35
+
36
+ return {
37
+ id: storage.updateValue(id, Admin.name, $id),
38
+ pwd: pwd || '123456',
39
+ time: time || new Date(),
40
+ uid,
41
+ valid: valid || 0,
42
+ }
43
+ }
44
+
45
+ constructor(...arg) {
46
+
47
+ super(storage, ...arg)
48
+
49
+ return Object.defineProperties({}, {
50
+ [position]: {
51
+ enumerable: false,
52
+ configurable: false,
53
+ get: () => this[position],
54
+ },
55
+ id: {
56
+ enumerable: true,
57
+ configurable: false,
58
+ get: () => super.get($id).readUInt32BE(0),
59
+ set: id => {
60
+ super.set2($id, uInt32BEToBuffer(id))
61
+ this.#update()
62
+ },
63
+ },
64
+ pwd: {
65
+ enumerable: true,
66
+ configurable: false,
67
+ get: () => b2s(super.get($pwd)),
68
+ set: pwd => {
69
+ super.set($pwd, super.s2b(pwd, $pwd))
70
+ this.#update()
71
+ },
72
+ },
73
+ time: {
74
+ enumerable: true,
75
+ configurable: false,
76
+ get: () => b2d(super.get($time)),
77
+ set: time => super.set2($time, d2b(time)),
78
+ },
79
+ uid: {
80
+ enumerable: true,
81
+ configurable: false,
82
+ get: () => b2s(super.get($uid)),
83
+ set: uid => {
84
+ super.set2($uid, super.s2b(uid, $uid))
85
+ this.#update()
86
+ },
87
+ },
88
+ valid: {
89
+ enumerable: true,
90
+ configurable: false,
91
+ get: () => super.get($valid).readUint8(0),
92
+ set: valid => {
93
+ super.set2($valid, super.uintToBuffer(valid, $valid))
94
+ this.#update()
95
+ },
96
+ },
97
+ })
98
+ }
99
+
100
+
101
+ #update() {
102
+
103
+ this.time = Date.now()
104
+ }
105
+ }
106
+
107
+ class Log extends Entity {
108
+
109
+ static name = 'log'
110
+
111
+ static create({
112
+ content,
113
+ id,
114
+ time,
115
+ type,
116
+ uid
117
+ }) {
118
+
119
+ return {
120
+ content,
121
+ id: storage.updateValue(id, Log.name, $id),
122
+ time: time || new Date(),
123
+ type,
124
+ uid,
125
+ }
126
+ }
127
+
128
+ constructor(...arg) {
129
+
130
+ super(storage, ...arg)
131
+
132
+ return Object.defineProperties({}, {
133
+ [position]: {
134
+ enumerable: false,
135
+ configurable: false,
136
+ get: () => this[position],
137
+ },
138
+ content: {
139
+ enumerable: true,
140
+ configurable: false,
141
+ get: () => b2s(super.get($content)),
142
+ set: content => super.set($content, super.s2b(content, $content)),
143
+ },
144
+ id: {
145
+ enumerable: true,
146
+ configurable: false,
147
+ get: () => super.get($id).readUInt32BE(0),
148
+ set: id => super.set2($id, uInt32BEToBuffer(id)),
149
+ },
150
+ time: {
151
+ enumerable: true,
152
+ configurable: false,
153
+ get: () => b2d(super.get($time)),
154
+ set: time => super.set2($time, d2b(time)),
155
+ },
156
+ type: {
157
+ enumerable: true,
158
+ configurable: false,
159
+ get: () => super.get($type).readUint16BE(0),
160
+ set: type => super.set($type, super.uintToBuffer(type, $type)),
161
+ },
162
+ uid: {
163
+ enumerable: true,
164
+ configurable: false,
165
+ get: () => b2s(super.get($uid)),
166
+ set: uid => super.set2($uid, super.s2b(uid, $uid)),
167
+ },
168
+ })
169
+ }
170
+
171
+ }
172
+
173
+ class Test extends Entity {
174
+
175
+ static name = 'test'
176
+
177
+ static create({
178
+ bigUint,
179
+ bigint,
180
+ buffer,
181
+ float,
182
+ id,
183
+ int,
184
+ string,
185
+ text,
186
+ time,
187
+ uint
188
+ }) {
189
+
190
+ return {
191
+ bigUint,
192
+ bigint,
193
+ buffer,
194
+ float,
195
+ id: storage.updateValue(id, Test.name, $id),
196
+ int,
197
+ string,
198
+ text,
199
+ time,
200
+ uint,
201
+ }
202
+ }
203
+
204
+ constructor(...arg) {
205
+
206
+ super(storage, ...arg)
207
+
208
+ return Object.defineProperties({}, {
209
+ [position]: {
210
+ enumerable: false,
211
+ configurable: false,
212
+ get: () => this[position],
213
+ },
214
+ bigUint: {
215
+ enumerable: true,
216
+ configurable: false,
217
+ get: () => super.get($bigUint).readBigUInt64BE(0),
218
+ set: bigUint => super.set($bigUint, bigUint32BEToBuffer(bigUint)),
219
+ },
220
+ bigint: {
221
+ enumerable: true,
222
+ configurable: false,
223
+ get: () => super.get($bigint).readBigInt64BE(0),
224
+ set: bigint => super.set($bigint, bigint32BEToBuffer(bigint)),
225
+ },
226
+ buffer: {
227
+ enumerable: true,
228
+ configurable: false,
229
+ get: () => super.get($buffer)[0] ? super.readFileSync($buffer) : null,
230
+ set: buffer => super.set($buffer, super.buffer(buffer, $buffer)),
231
+ },
232
+ float: {
233
+ enumerable: true,
234
+ configurable: false,
235
+ get: () => super.get($float).readDoubleBE(0),
236
+ set: float => super.set($float, doubleBEToBuffer(float)),
237
+ },
238
+ id: {
239
+ enumerable: true,
240
+ configurable: false,
241
+ get: () => super.get($id).readUInt32BE(0),
242
+ set: id => super.set2($id, uInt32BEToBuffer(id)),
243
+ },
244
+ int: {
245
+ enumerable: true,
246
+ configurable: false,
247
+ get: () => readInt24BE(super.get($int)),
248
+ set: int => super.set($int, super.intToBuffer(int, $int)),
249
+ },
250
+ string: {
251
+ enumerable: true,
252
+ configurable: false,
253
+ get: () => b2s(super.get($string)),
254
+ set: string => super.set($string, super.s2b(string, $string)),
255
+ },
256
+ text: {
257
+ enumerable: true,
258
+ configurable: false,
259
+ get: () => super.get($text)[0] ? super.readFileSync($text).toString() : '',
260
+ set: text => super.set($text, super.textToBuffer(text, $text)),
261
+ },
262
+ time: {
263
+ enumerable: true,
264
+ configurable: false,
265
+ get: () => b2d(super.get($time)),
266
+ set: time => super.set($time, d2b(time)),
267
+ },
268
+ uint: {
269
+ enumerable: true,
270
+ configurable: false,
271
+ get: () => super.get($uint).readUInt32BE(0),
272
+ set: uint => super.set($uint, super.uintToBuffer(uint, $uint)),
273
+ },
274
+ })
275
+ }
276
+
277
+ }
278
+
279
+
280
+ class AdminSet extends Entities {
281
+
282
+ constructor() {
283
+
284
+ super(storage, Admin)
285
+ }
286
+ /**
287
+ * 内部方法
288
+ */
289
+ s2b() {
290
+
291
+ throw new TypeError('s2b is not a function')
292
+ }
293
+ /**
294
+ * 内部方法
295
+ */
296
+ findByValue() {
297
+
298
+ throw new TypeError('findByValue is not a function')
299
+ }
300
+ /**
301
+ * 内部方法
302
+ */
303
+ findByTime() {
304
+
305
+ throw new TypeError('findByTime is not a function')
306
+ }
307
+ /**
308
+ * 内部方法
309
+ */
310
+ stringToBuffer() {
311
+
312
+ throw new TypeError('stringToBuffer is not a function')
313
+ }
314
+
315
+ indexById(id) {
316
+
317
+ return super.findByValue($id, uInt32BEToBuffer(id, $id))
318
+ }
319
+
320
+ indexByUid(uid) {
321
+
322
+ return super.findByValue($uid, super.s2b(uid, $uid))
323
+ }
324
+
325
+ indexByValid(valid) {
326
+
327
+ return super.findByValue($valid, super.uintToBuffer(valid, $valid))
328
+ }
329
+
330
+ indexByTime({after, before} = {}) {
331
+
332
+ return super.findByTime($time, {after, before})
333
+ }
334
+ }
335
+
336
+ class LogSet extends Entities {
337
+
338
+ constructor() {
339
+
340
+ super(storage, Log)
341
+ }
342
+ /**
343
+ * 内部方法
344
+ */
345
+ s2b() {
346
+
347
+ throw new TypeError('s2b is not a function')
348
+ }
349
+ /**
350
+ * 内部方法
351
+ */
352
+ findByValue() {
353
+
354
+ throw new TypeError('findByValue is not a function')
355
+ }
356
+ /**
357
+ * 内部方法
358
+ */
359
+ findByTime() {
360
+
361
+ throw new TypeError('findByTime is not a function')
362
+ }
363
+ /**
364
+ * 内部方法
365
+ */
366
+ stringToBuffer() {
367
+
368
+ throw new TypeError('stringToBuffer is not a function')
369
+ }
370
+
371
+ indexById(id) {
372
+
373
+ return super.findByValue($id, uInt32BEToBuffer(id, $id))
374
+ }
375
+
376
+ indexByUid(uid) {
377
+
378
+ return super.findByValue($uid, super.s2b(uid, $uid))
379
+ }
380
+
381
+ indexByTime({after, before} = {}) {
382
+
383
+ return super.findByTime($time, {after, before})
384
+ }
385
+ }
386
+
387
+ class TestSet extends Entities {
388
+
389
+ constructor() {
390
+
391
+ super(storage, Test)
392
+ }
393
+ /**
394
+ * 内部方法
395
+ */
396
+ s2b() {
397
+
398
+ throw new TypeError('s2b is not a function')
399
+ }
400
+ /**
401
+ * 内部方法
402
+ */
403
+ findByValue() {
404
+
405
+ throw new TypeError('findByValue is not a function')
406
+ }
407
+ /**
408
+ * 内部方法
409
+ */
410
+ findByTime() {
411
+
412
+ throw new TypeError('findByTime is not a function')
413
+ }
414
+ /**
415
+ * 内部方法
416
+ */
417
+ stringToBuffer() {
418
+
419
+ throw new TypeError('stringToBuffer is not a function')
420
+ }
421
+
422
+ indexById(id) {
423
+
424
+ return super.findByValue($id, uInt32BEToBuffer(id, $id))
425
+ }
426
+ }
427
+
428
+ export const remark = Symbol('remark')
429
+
430
+ export function getStruct() {
431
+
432
+ return struct(storage, remark)
433
+ }
434
+
435
+ /**
436
+ * @type {import('./type').Storage}
437
+ */
438
+ const store = Object.freeze({
439
+ close() {
440
+ storage.close()
441
+ },
442
+ admin: new Proxy([], {
443
+ get: new AdminSet()
444
+ }),
445
+ log: new Proxy([], {
446
+ get: new LogSet()
447
+ }),
448
+ test: new Proxy([], {
449
+ get: new TestSet()
450
+ }),
451
+ })
452
+
453
+ export default store
package/test/1/type.ts ADDED
@@ -0,0 +1,170 @@
1
+ interface BigInt {}
2
+ interface Buffer {}
3
+
4
+ interface Admin {
5
+
6
+ /**
7
+ *
8
+ */
9
+ id: Number
10
+ /**
11
+ * 账号
12
+ */
13
+ uid: String
14
+ /**
15
+ * 密码
16
+ */
17
+ pwd: String
18
+ /**
19
+ * 是否有效
20
+ */
21
+ valid: Number
22
+ /**
23
+ * 登录时间
24
+ */
25
+ time: Number | Date
26
+ }
27
+
28
+ interface AdminSet extends Array<Admin> {
29
+
30
+ indexById(id: Number): Array<Admin>
31
+ indexByUid(uid: String): Array<Admin>
32
+ indexByValid(valid: Number): Array<Admin>
33
+ indexByTime({after, before}): Array<Admin>
34
+ /**
35
+ * 分页查询
36
+ */
37
+ page(predicate: Function, index: Number | String, size: Number | String, params: Object): Array<Admin>
38
+ /**
39
+ * 移除数据
40
+ */
41
+ remove(...items: Array<Admin>): void
42
+ /**
43
+ * 联合查询,类似LEFT JOIN
44
+ */
45
+ eachFlat(array: Array<Object>, predicate: Function | String): Array<Object>
46
+ /**
47
+ * 联合查询,类似INNER JOIN
48
+ */
49
+ filterFlat(array: Array<Object>, predicate: Function | String): Array<Object>
50
+ }
51
+
52
+ interface Log {
53
+
54
+ /**
55
+ *
56
+ */
57
+ id: Number
58
+ /**
59
+ * 账号
60
+ */
61
+ uid: String
62
+ /**
63
+ * 类型
64
+ */
65
+ type: Number
66
+ /**
67
+ * 创建时间
68
+ */
69
+ time: Number | Date
70
+ /**
71
+ * 内容
72
+ */
73
+ content: String
74
+ }
75
+
76
+ interface LogSet extends Array<Log> {
77
+
78
+ indexById(id: Number): Array<Log>
79
+ indexByUid(uid: String): Array<Log>
80
+ indexByTime({after, before}): Array<Log>
81
+ /**
82
+ * 分页查询
83
+ */
84
+ page(predicate: Function, index: Number | String, size: Number | String, params: Object): Array<Log>
85
+ /**
86
+ * 移除数据
87
+ */
88
+ remove(...items: Array<Log>): void
89
+ /**
90
+ * 联合查询,类似LEFT JOIN
91
+ */
92
+ eachFlat(array: Array<Object>, predicate: Function | String): Array<Object>
93
+ /**
94
+ * 联合查询,类似INNER JOIN
95
+ */
96
+ filterFlat(array: Array<Object>, predicate: Function | String): Array<Object>
97
+ }
98
+
99
+ interface Test {
100
+
101
+ /**
102
+ *
103
+ */
104
+ id: Number
105
+ /**
106
+ *
107
+ */
108
+ uint: Number
109
+ /**
110
+ *
111
+ */
112
+ int: Number
113
+ /**
114
+ *
115
+ */
116
+ bigint: Number | BigInt
117
+ /**
118
+ *
119
+ */
120
+ bigUint: Number | BigInt
121
+ /**
122
+ *
123
+ */
124
+ time: Number | Date
125
+ /**
126
+ *
127
+ */
128
+ float: Number
129
+ /**
130
+ *
131
+ */
132
+ string: String
133
+ /**
134
+ *
135
+ */
136
+ buffer: Buffer
137
+ /**
138
+ *
139
+ */
140
+ text: String
141
+ }
142
+
143
+ interface TestSet extends Array<Test> {
144
+
145
+ indexById(id: Number): Array<Test>
146
+ /**
147
+ * 分页查询
148
+ */
149
+ page(predicate: Function, index: Number | String, size: Number | String, params: Object): Array<Test>
150
+ /**
151
+ * 移除数据
152
+ */
153
+ remove(...items: Array<Test>): void
154
+ /**
155
+ * 联合查询,类似LEFT JOIN
156
+ */
157
+ eachFlat(array: Array<Object>, predicate: Function | String): Array<Object>
158
+ /**
159
+ * 联合查询,类似INNER JOIN
160
+ */
161
+ filterFlat(array: Array<Object>, predicate: Function | String): Array<Object>
162
+ }
163
+
164
+
165
+ export interface Storage {
166
+ close(): void
167
+ admin: AdminSet,
168
+ log: LogSet,
169
+ test: TestSet
170
+ }
package/test/2/data ADDED
Binary file
package/test/2/index ADDED
Binary file