@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.
@@ -0,0 +1,562 @@
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
+ const $test = 'test'
24
+ const $test3 = 'test3'
25
+ const $test2 = 'test2'
26
+
27
+ class Admin extends Entity {
28
+
29
+ static name = 'admin'
30
+
31
+ static create({
32
+ id,
33
+ pwd,
34
+ time,
35
+ uid,
36
+ valid
37
+ }) {
38
+
39
+ return {
40
+ id: storage.updateValue(id, Admin.name, $id),
41
+ pwd: pwd || '123456',
42
+ time: time || new Date(),
43
+ uid,
44
+ valid: valid || 0,
45
+ }
46
+ }
47
+
48
+ constructor(...arg) {
49
+
50
+ super(storage, ...arg)
51
+
52
+ return Object.defineProperties({}, {
53
+ [position]: {
54
+ enumerable: false,
55
+ configurable: false,
56
+ get: () => this[position],
57
+ },
58
+ id: {
59
+ enumerable: true,
60
+ configurable: false,
61
+ get: () => super.get($id).readUInt32BE(0),
62
+ set: id => {
63
+ super.set2($id, uInt32BEToBuffer(id))
64
+ this.#update()
65
+ },
66
+ },
67
+ pwd: {
68
+ enumerable: true,
69
+ configurable: false,
70
+ get: () => b2s(super.get($pwd)),
71
+ set: pwd => {
72
+ super.set($pwd, super.s2b(pwd, $pwd))
73
+ this.#update()
74
+ },
75
+ },
76
+ time: {
77
+ enumerable: true,
78
+ configurable: false,
79
+ get: () => b2d(super.get($time)),
80
+ set: time => super.set2($time, d2b(time)),
81
+ },
82
+ uid: {
83
+ enumerable: true,
84
+ configurable: false,
85
+ get: () => b2s(super.get($uid)),
86
+ set: uid => {
87
+ super.set2($uid, super.s2b(uid, $uid))
88
+ this.#update()
89
+ },
90
+ },
91
+ valid: {
92
+ enumerable: true,
93
+ configurable: false,
94
+ get: () => super.get($valid).readUint8(0),
95
+ set: valid => {
96
+ super.set2($valid, super.uintToBuffer(valid, $valid))
97
+ this.#update()
98
+ },
99
+ },
100
+ })
101
+ }
102
+
103
+
104
+ #update() {
105
+
106
+ this.time = Date.now()
107
+ }
108
+ }
109
+
110
+ class Log extends Entity {
111
+
112
+ static name = 'log'
113
+
114
+ static create({
115
+ content,
116
+ id,
117
+ time,
118
+ type,
119
+ uid
120
+ }) {
121
+
122
+ return {
123
+ content,
124
+ id: storage.updateValue(id, Log.name, $id),
125
+ time: time || new Date(),
126
+ type,
127
+ uid,
128
+ }
129
+ }
130
+
131
+ constructor(...arg) {
132
+
133
+ super(storage, ...arg)
134
+
135
+ return Object.defineProperties({}, {
136
+ [position]: {
137
+ enumerable: false,
138
+ configurable: false,
139
+ get: () => this[position],
140
+ },
141
+ content: {
142
+ enumerable: true,
143
+ configurable: false,
144
+ get: () => b2s(super.get($content)),
145
+ set: content => super.set($content, super.s2b(content, $content)),
146
+ },
147
+ id: {
148
+ enumerable: true,
149
+ configurable: false,
150
+ get: () => super.get($id).readUInt32BE(0),
151
+ set: id => super.set2($id, uInt32BEToBuffer(id)),
152
+ },
153
+ time: {
154
+ enumerable: true,
155
+ configurable: false,
156
+ get: () => b2d(super.get($time)),
157
+ set: time => super.set2($time, d2b(time)),
158
+ },
159
+ type: {
160
+ enumerable: true,
161
+ configurable: false,
162
+ get: () => super.get($type).readUint16BE(0),
163
+ set: type => super.set($type, super.uintToBuffer(type, $type)),
164
+ },
165
+ uid: {
166
+ enumerable: true,
167
+ configurable: false,
168
+ get: () => b2s(super.get($uid)),
169
+ set: uid => super.set2($uid, super.s2b(uid, $uid)),
170
+ },
171
+ })
172
+ }
173
+
174
+ }
175
+
176
+ class Test extends Entity {
177
+
178
+ static name = 'test'
179
+
180
+ static create({
181
+ bigUint,
182
+ bigint,
183
+ buffer,
184
+ float,
185
+ id,
186
+ int,
187
+ string,
188
+ text,
189
+ time,
190
+ uint
191
+ }) {
192
+
193
+ return {
194
+ bigUint,
195
+ bigint,
196
+ buffer,
197
+ float,
198
+ id: storage.updateValue(id, Test.name, $id),
199
+ int,
200
+ string,
201
+ text,
202
+ time,
203
+ uint,
204
+ }
205
+ }
206
+
207
+ constructor(...arg) {
208
+
209
+ super(storage, ...arg)
210
+
211
+ return Object.defineProperties({}, {
212
+ [position]: {
213
+ enumerable: false,
214
+ configurable: false,
215
+ get: () => this[position],
216
+ },
217
+ bigUint: {
218
+ enumerable: true,
219
+ configurable: false,
220
+ get: () => super.get($bigUint).readBigUInt64BE(0),
221
+ set: bigUint => super.set($bigUint, bigUint32BEToBuffer(bigUint)),
222
+ },
223
+ bigint: {
224
+ enumerable: true,
225
+ configurable: false,
226
+ get: () => super.get($bigint).readBigInt64BE(0),
227
+ set: bigint => super.set($bigint, bigint32BEToBuffer(bigint)),
228
+ },
229
+ buffer: {
230
+ enumerable: true,
231
+ configurable: false,
232
+ get: () => super.get($buffer)[0] ? super.readFileSync($buffer) : null,
233
+ set: buffer => super.set($buffer, super.buffer(buffer, $buffer)),
234
+ },
235
+ float: {
236
+ enumerable: true,
237
+ configurable: false,
238
+ get: () => super.get($float).readDoubleBE(0),
239
+ set: float => super.set($float, doubleBEToBuffer(float)),
240
+ },
241
+ id: {
242
+ enumerable: true,
243
+ configurable: false,
244
+ get: () => super.get($id).readUInt32BE(0),
245
+ set: id => super.set2($id, uInt32BEToBuffer(id)),
246
+ },
247
+ int: {
248
+ enumerable: true,
249
+ configurable: false,
250
+ get: () => readInt24BE(super.get($int)),
251
+ set: int => super.set($int, super.intToBuffer(int, $int)),
252
+ },
253
+ string: {
254
+ enumerable: true,
255
+ configurable: false,
256
+ get: () => b2s(super.get($string)),
257
+ set: string => super.set($string, super.s2b(string, $string)),
258
+ },
259
+ text: {
260
+ enumerable: true,
261
+ configurable: false,
262
+ get: () => super.get($text)[0] ? super.readFileSync($text).toString() : '',
263
+ set: text => super.set($text, super.textToBuffer(text, $text)),
264
+ },
265
+ time: {
266
+ enumerable: true,
267
+ configurable: false,
268
+ get: () => b2d(super.get($time)),
269
+ set: time => super.set($time, d2b(time)),
270
+ },
271
+ uint: {
272
+ enumerable: true,
273
+ configurable: false,
274
+ get: () => super.get($uint).readUInt32BE(0),
275
+ set: uint => super.set($uint, super.uintToBuffer(uint, $uint)),
276
+ },
277
+ })
278
+ }
279
+
280
+ }
281
+
282
+ class Test3 extends Entity {
283
+
284
+ static name = 'test3'
285
+
286
+ static create({
287
+ pwd,
288
+ test,
289
+ test2,
290
+ test3,
291
+ uid
292
+ }) {
293
+
294
+ return {
295
+ pwd,
296
+ test,
297
+ test2,
298
+ test3,
299
+ uid,
300
+ }
301
+ }
302
+
303
+ constructor(...arg) {
304
+
305
+ super(storage, ...arg)
306
+
307
+ return Object.defineProperties({}, {
308
+ [position]: {
309
+ enumerable: false,
310
+ configurable: false,
311
+ get: () => this[position],
312
+ },
313
+ pwd: {
314
+ enumerable: true,
315
+ configurable: false,
316
+ get: () => b2s(super.get($pwd)),
317
+ set: pwd => super.set($pwd, super.s2b(pwd, $pwd)),
318
+ },
319
+ test: {
320
+ enumerable: true,
321
+ configurable: false,
322
+ get: () => b2s(super.get($test)),
323
+ set: test => super.set($test, super.s2b(test, $test)),
324
+ },
325
+ test2: {
326
+ enumerable: true,
327
+ configurable: false,
328
+ get: () => b2s(super.get($test2)),
329
+ set: test2 => super.set($test2, super.s2b(test2, $test2)),
330
+ },
331
+ test3: {
332
+ enumerable: true,
333
+ configurable: false,
334
+ get: () => readInt24BE(super.get($test3)),
335
+ set: test3 => super.set($test3, super.intToBuffer(test3, $test3)),
336
+ },
337
+ uid: {
338
+ enumerable: true,
339
+ configurable: false,
340
+ get: () => b2s(super.get($uid)),
341
+ set: uid => super.set($uid, super.s2b(uid, $uid)),
342
+ },
343
+ })
344
+ }
345
+
346
+ }
347
+
348
+
349
+ class AdminSet extends Entities {
350
+
351
+ constructor() {
352
+
353
+ super(storage, Admin)
354
+ }
355
+ /**
356
+ * 内部方法
357
+ */
358
+ s2b() {
359
+
360
+ throw new TypeError('s2b is not a function')
361
+ }
362
+ /**
363
+ * 内部方法
364
+ */
365
+ findByValue() {
366
+
367
+ throw new TypeError('findByValue is not a function')
368
+ }
369
+ /**
370
+ * 内部方法
371
+ */
372
+ findByTime() {
373
+
374
+ throw new TypeError('findByTime is not a function')
375
+ }
376
+ /**
377
+ * 内部方法
378
+ */
379
+ stringToBuffer() {
380
+
381
+ throw new TypeError('stringToBuffer is not a function')
382
+ }
383
+
384
+ indexById(id) {
385
+
386
+ return super.findByValue($id, uInt32BEToBuffer(id, $id))
387
+ }
388
+
389
+ indexByUid(uid) {
390
+
391
+ return super.findByValue($uid, super.s2b(uid, $uid))
392
+ }
393
+
394
+ indexByValid(valid) {
395
+
396
+ return super.findByValue($valid, super.uintToBuffer(valid, $valid))
397
+ }
398
+
399
+ indexByTime({after, before} = {}) {
400
+
401
+ return super.findByTime($time, {after, before})
402
+ }
403
+ }
404
+
405
+ class LogSet extends Entities {
406
+
407
+ constructor() {
408
+
409
+ super(storage, Log)
410
+ }
411
+ /**
412
+ * 内部方法
413
+ */
414
+ s2b() {
415
+
416
+ throw new TypeError('s2b is not a function')
417
+ }
418
+ /**
419
+ * 内部方法
420
+ */
421
+ findByValue() {
422
+
423
+ throw new TypeError('findByValue is not a function')
424
+ }
425
+ /**
426
+ * 内部方法
427
+ */
428
+ findByTime() {
429
+
430
+ throw new TypeError('findByTime is not a function')
431
+ }
432
+ /**
433
+ * 内部方法
434
+ */
435
+ stringToBuffer() {
436
+
437
+ throw new TypeError('stringToBuffer is not a function')
438
+ }
439
+
440
+ indexById(id) {
441
+
442
+ return super.findByValue($id, uInt32BEToBuffer(id, $id))
443
+ }
444
+
445
+ indexByUid(uid) {
446
+
447
+ return super.findByValue($uid, super.s2b(uid, $uid))
448
+ }
449
+
450
+ indexByTime({after, before} = {}) {
451
+
452
+ return super.findByTime($time, {after, before})
453
+ }
454
+ }
455
+
456
+ class TestSet extends Entities {
457
+
458
+ constructor() {
459
+
460
+ super(storage, Test)
461
+ }
462
+ /**
463
+ * 内部方法
464
+ */
465
+ s2b() {
466
+
467
+ throw new TypeError('s2b is not a function')
468
+ }
469
+ /**
470
+ * 内部方法
471
+ */
472
+ findByValue() {
473
+
474
+ throw new TypeError('findByValue is not a function')
475
+ }
476
+ /**
477
+ * 内部方法
478
+ */
479
+ findByTime() {
480
+
481
+ throw new TypeError('findByTime is not a function')
482
+ }
483
+ /**
484
+ * 内部方法
485
+ */
486
+ stringToBuffer() {
487
+
488
+ throw new TypeError('stringToBuffer is not a function')
489
+ }
490
+
491
+ indexById(id) {
492
+
493
+ return super.findByValue($id, uInt32BEToBuffer(id, $id))
494
+ }
495
+ }
496
+
497
+ class Test3Set extends Entities {
498
+
499
+ constructor() {
500
+
501
+ super(storage, Test3)
502
+ }
503
+ /**
504
+ * 内部方法
505
+ */
506
+ s2b() {
507
+
508
+ throw new TypeError('s2b is not a function')
509
+ }
510
+ /**
511
+ * 内部方法
512
+ */
513
+ findByValue() {
514
+
515
+ throw new TypeError('findByValue is not a function')
516
+ }
517
+ /**
518
+ * 内部方法
519
+ */
520
+ findByTime() {
521
+
522
+ throw new TypeError('findByTime is not a function')
523
+ }
524
+ /**
525
+ * 内部方法
526
+ */
527
+ stringToBuffer() {
528
+
529
+ throw new TypeError('stringToBuffer is not a function')
530
+ }
531
+
532
+ }
533
+
534
+ export const remark = Symbol('remark')
535
+
536
+ export function getStruct() {
537
+
538
+ return struct(storage, remark)
539
+ }
540
+
541
+ /**
542
+ * @type {import('./type').Storage}
543
+ */
544
+ const store = Object.freeze({
545
+ close() {
546
+ storage.close()
547
+ },
548
+ admin: new Proxy([], {
549
+ get: new AdminSet()
550
+ }),
551
+ log: new Proxy([], {
552
+ get: new LogSet()
553
+ }),
554
+ test: new Proxy([], {
555
+ get: new TestSet()
556
+ }),
557
+ test3: new Proxy([], {
558
+ get: new Test3Set()
559
+ }),
560
+ })
561
+
562
+ export default store