-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dataset.js
1000 lines (887 loc) · 27.9 KB
/
Dataset.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import fs from 'fs';
import {Arreglo} from './Group.js';
import process from 'process';
// Versión inestable...
export class Dataset extends Arreglo {
constructor(datasource) {
super();
if (datasource) {
this.open(datasource);
} else {
this.open([]);
}
this.filename = null;
}
// Cargar de un formato .json clásico
loadFromFile(filename) {
if (filename && !this.filename) {
this.filename = filename;
}
if (this.filename) {
const data = super.readFromFile(filename);
const content = JSON.parse(data);
this._array = content['RECORDS'];
} else {
throw new Error('You must specify a file name at lest once time.');
}
}
writeToFile(fileName) {
if (filename && !this.filename) {
this.filename = filename;
}
if (this.filename) {
super.writeToFile(fileName, `{"RECORDS":${JSON.stringify(this._array)}}`);
} else {
throw new Error('You must specify a file name at lest once time.');
}
}
// Sin ser tan mongo, puedes desarrollar tu propio SGDB.
// Agregar una nueva relación... (no chequea., luego)., foreign es la tabla física con la que se relaciona, también de tipo Table.
get recNo() {
return this._recNo;
}
set recNo(value) {
this._recNo = value;
this.loadBuffer();
}
// @Abstract, @Platform dependent
// Puedes acceder a los campos dal registro anterior sin necesidad de mover el cursor
get priorRecord() {
return this.recNo > 0 ? null : this._array[this.recNo - 1];
}
// @Abstract, @Platform dependent
set priorRecord(value) {
if (this.recNo > 0) {
this._array[this.recNo - 1] = value;
}
}
// Puedes utilizar la propiedad activeRecord para acceder a los campos del registro activo...
get activeRecord() {
return this._array[this.recNo];
}
set activeRecord(value) {
this._array[this.recNo] = value;
}
// Puedes acceder a los campos del siguiente registro sin necesidad de mover el cursor
get nextRecord() {
return this.recNo + 1 < this._array.length ? this._array[this.recNo + 1] : null;
}
set nextRecord(value) {
if (this.recNo + 1 < this._array.length) {
this[this.recNo + 1] = value;
}
}
get active() {
return this._active;
}
set active(value) {
if (value) {
this.open();
} else {
this.close();
}
}
get fields() {
return this.buffer;
}
set fields(value) {
this.buffer = value;
}
// Lo demás son nombres de campos y el de la misma.
addRelation(localKey, foreignName, foreignKey, foreign) {
if (!this._relations) {
this._relations = new Dataset({
localKey: primaryKey,
foreignName: foreignName,
foreignKey: foreignKey,
foreign: foreign,
}); // is an array of object records like {primaryKey:"color_id", foreignTable: this, foreignKey: id}
} else {
this._relations._array.push({
localKey: primaryKey, foreignName: foreignName, foreignKey: foreignKey,
});
}
}
// Realiza una relación sencilla previamente establecida de uno a muchos...
relations(tableName, tableField) {
if (!this._relations.find('foreignName', tableName)) {
throw new Error(`No existe relación con la tabla ${tableName}.`);
}
if (this._relations.activeRecord['foreign'].find(tableField, this.activeRecord[this._relations.localKey])) {
return this._relations.activeRecord.foreign[tableField];
}
}
// Este método debería cargar el datasource completo en la bd.
load() {
return null;
}
// Este método debería guardar el datasource completo en la bd.
save() {
return null;
}
// Cargar o guardar el contenido del dataset en memoria, hacia o desde la posición actual.
loadBuffer() {
this.buffer = this._array[this._recNo];
}
saveBuffer() {
this._array[this._recNo] = this.buffer;
}
// Todos los campos de la tabla
allFields() {
const resultado = [];
for (const field in this.activeRecord) {
if (this.activeRecord.hasOwnProperty(field)) {
resultado.push(field);
}
}
return resultado;
}
// Abre un dataset, un arreglo de objetos
open(datasource) {
if (datasource) {
this._array = datasource;
}
if (!this._array) {
this._array = [];
}
this._recNo = 0;
// Esta es la primera vez que se va a cargar el buffer.
this.loadBuffer();
this._active = true;
}
// Si la tabla está activa, la salva y la cierra; si no querías salvar, primero debiste cancelar la op.
close() {
if (this._active) {
this.saveBuffer();
this.save();
this.open([]);
this._active = false;
}
}
recordCount() {
return this._array.length;
}
// Las funciones first, last, prev y next, cancelan la operación.
first() {
this._recNo = 0;
this.loadBuffer();
}
last() {
this._recNo = this._array.length - 1;
this.loadBuffer();
}
prev() {
if (this._recNo > 0) {
this._recNo = this._recNo - 1;
}
this.loadBuffer();
}
next() {
if (this._recNo < this._array.length - 1) {
this._recNo = this._recNo + 1;
}
this.loadBuffer();
}
// Subir y bajar registros (es útil en el control de capas) // re-con-validado
moveUp() {
[tabla.nextRecord, tabla.activeRecord] = [tabla.activeRecord, tabla.nextRecord];
}
moveDown() {
[tabla.priorRecord, tabla.activeRecord] = [tabla.activeRecord, tabla.priorRecord];
}
// refresh from db
refresh() {
const tmpRec = this.recNo;
this.open();
this.recNo = tmpRec;
this.loadBuffer();
}
// return an array with the fieldNames
fieldNames() {
const resultado = [];
if (this._array.length > 0) {
for (const llave in this._array[0]) {
resultado.push(llave);
}
}
return resultado;
}
// insert
insert(fieldNames, fieldValues) {
const blob = {};
for (let i = 0; i < Math.min(fieldNames.length, fieldValues.length); i++) {
blob[fieldNames[i]] = fieldValues[i];
}
this._array.push(blob);
this.refresh();
}
// el registro actual cumple ese criterio de igualdad
match(fieldNames, fieldValues) {
let prov = 0;
const comparables = Math.min(inFieldNames.length, inFieldValues.length);
for (let j = 0; j < comparables; j++) {
if (this.activeRecord[inFieldNames[j]] === inFieldValues[j]) {
prov++;
}
}
return (prov === comparables);
}
// find === criteria (si devuelve true, actualiza el cursor apuntando al resultado)
// todo select y where condition
find(fieldNames, fieldValues) {
// Flexibility
const inFieldNames = fieldNames instanceof Array ? fieldNames : [fieldNames];
const inFieldValues = fieldValues instanceof Array ? fieldNames : [fieldValues];
// recorre el dataset...
let comparables;
let prov;
comparables = Math.min(inFieldNames.length, inFieldValues.length);
for (let i = 0; i < this._array.length; i++) {
prov = 0;
for (let j = 0; j < comparables; j++) {
if (this._array[i][inFieldNames[j]] === inFieldValues[j]) {
prov++;
}
}
if (prov === comparables) {
// update recno
this.recNo = i;
return true;
}
}
return false;
}
// Delete when
// todo where condition
delete(fieldNames, fieldValues) {
// Flexibility
const inFieldNames = fieldNames instanceof Array ? fieldNames : [fieldNames];
const inFieldValues = fieldValues instanceof Array ? fieldNames : [fieldValues];
// recorre el dataset...
let comparables;
let prov;
let hasDeleted = false;
comparables = Math.min(inFieldNames.length, inFieldValues.length);
for (let i = this._array.length - 1; i >= 0; i--) {
prov = 0;
for (let j = 0; j < comparables; j++) {
if (this._array[i][inFieldNames[j]] === inFieldValues[j]) {
prov++;
}
}
if (prov === comparables) {
// update recno
this._array.splice(i, 1);
hasDeleted = true;
// update the rearguard
if (this.recNo > this._array.length - 1) {
this.recNo = this._array.length - 1;
}
this.loadBuffer();
} // until the end...
}
return hasDeleted;
}
// select
// todo where condition y from varias tablas, que debe hacen un producto cartesiano
//
select(selectedFields, fieldNames, fieldValues) {
// Flexibility, revisar, galileo
const inSelectedFields = !fieldValues instanceof Array ? fieldNames : [fieldValues];
const inFieldNames = !fieldNames instanceof Array ? fieldNames : [fieldNames];
const inFieldValues = !fieldValues instanceof Array ? fieldNames : [fieldValues];
// recorre el dataset...
let comparables;
let prov;
const hasDeleted = false;
const selection = new Dataset();
comparables = Math.min(inFieldNames.length, inFieldValues.length);
for (let i = this._array.length - 1; i >= 0; i--) {
prov = 0;
for (let j = 0; j < comparables; j++) {
if (this._array[i][inFieldNames[j]] === inFieldValues[j]) {
prov++;
}
}
if (prov === comparables) {
const item = {};
var value;
var sValues = [];
for (let k = 0; k < inSelectedFields.length; k++) {
value = this._array[i][inSelectedFields[k]];
sValues.push(value);
item[inSelectedFields[k]] === value;
prov++;
}
}
selection.insert(inSelectedFields, sValues);
} // until the end...
return selection;
}
// Este te genera la tabla html (recuerda que el datasource es un arreglo de objetos o registros)
html(estilo) {
if (!estilo) {
estilo = 'border=1';
}
let popupContent = `<table ${estilo}>`;
if (this._array.length !== 0) { // something in the data set?
popupContent += '<thead>';
// Títulos
for (let llave in this._array[0]) {
popupContent += '<th>';
popupContent += llave.toString().bold();
popupContent += '</th>';
}
popupContent += '</thead>';
popupContent += '<tbody>';
// Contents
for (let i = 0; i < this._array.length; i++) {
popupContent += '<tr>';
for (let llave in this._array[i]) {
let valor = this._array[i][llave];
if (typeof (valor) == 'boolean') {
valor = (valor ? 'Sí' : 'No');
} else {
valor = ((this._array[i][llave]) !== null ? String((this._array[i][llave])) : '');
}
popupContent += '<td>' + valor.toString().italics() + '</td>';
}
popupContent += '</tr>';
}
// Par luego, en caso de que se esté editando u registro, puedes poner cuadros de edición con botones
// de aplicar o cancelar... de igual forma, a la derecha, abrir o eliminar registro,
// esto es super que genérico..., y por supuesto se puede adaptar a los nuevos proyectos...
popupContent += '<tbody>';
} // else if at least you have an schema... just show the caption's
popupContent += '</table>';
return popupContent;
}
// Posted by Solomon Yunana (location Kaduna, Nigeria.) to https://dev.to/solexy on 14 mar 2022...
groupBy(key) {
// tried to figure this out, help!!!!!
return this._array.reduce((previous, current) => {
if (!previous[current[key]]) {
previous[current[key]] = [];
}
previous[current[key]].push(current);
return previous;
}, {});
}
}
// The substantial parts, ported from
// JSONBb (c) 2016 Niccolò Maggioni, under:
// The MIT License (MIT)
export class JSONdb extends Dataset {
/**
* Default configuration values.
* @type {{ jsonSpaces: number}}
*/
static defaultOptions = {
jsonSpaces: 4,
stringify: JSON.stringify,
parse: JSON.parse,
};
/**
* Validates the contents of a JSON file.
* @param {string} fileContent
* @returns {boolean} `true` if content is ok, throws error if not.
*/
validateJSON(fileContent) {
try {
this.options.parse(fileContent);
} catch (e) {
console.error('Given filePath is not empty and its content is not valid JSON.');
throw e;
}
return true;
};
// CAUTION !!! UNDER AUTHOR LICENCE, AUTO-TEST AND ADVANCED.
static iterableTestsCount = 100;
/**
* Generates a file path and name based on current UNIX timestamp.
* @returns {string}
*/
static genFileName() {
return '/tmp/' + Date.now().toString() + '.json';
}
/**
* Makes sure that a unique filename is generated.
*/
static genFilePath(timeOut = 10000) {
let loop = true;
setTimeout(() => loop = false, timeOut);
while (loop) {
try {
this.filePath = JSONdb.genFileName();
fs.statSync(this.filePath);
} catch (err) {
console.error(err.toString());
break;
}
}
};
/**
* Returns a new instance of JSONdb.
* @returns {JSONdb}
*/
static createInstance() {
return new JSONdb(this.filePath);
}
// Priviledge tests (facultades).
// Tests are not being run as root
static checkRunAsRoot() {
try {
let isRoot = process.getuid && process.getuid() === 0;
if (!isRoot) {
console.error('Please do not run tests with root privileges!');
}
} catch (e) {
console.error('Cannot check key instances, cause:', e.toString());
}
}
// Consistency tests (solidez).
// Database cleanup
static checkDatabaseCleanup() {
try {
this.createInstance().deleteAll();
} catch (e) {
console.error('Cannot check database cleanup, cause:', e.toString());
}
}
// Create a new JSONdb instance and test `instanceOf`'
static checkDatabaseInstance() {
try {
const db = this.createInstance();
if (!(db, JSONdb)) {
console.error(`Database is not saved as instance of ${JSONdb.constructor.name}.`);
}
} catch (e) {
console.error('Cannot check database instance, cause:', e.toString());
}
}
// Create a new JSONdb instance and test `instanceOf`'
static checkDatabaseInstance() {
try {
const db = this.createInstance();
if (!(db, JSONdb)) {
console.error(`Database is not saved as instance of ${JSONdb.constructor.name}.`);
}
} catch (e) {
console.error('Cannot check database instance, cause:', e.toString());
}
}
//
// it('Check error handling for paths with no access', function() {
// assert.throws(function() {
// const db = new JSONdb('/' + Date.now().toString() + '.json', { syncOnWrite: true });
// db.set('foo', 'bar');
// });
// });
//
// Check that a non-exhistent key returns `undefined`
static checkNonExhistentKey() {
try {
const db = JSONdb.createInstance();
if (typeof (db.get(Date.now())) !== 'undefined') {
console.error('Unexpected type of initial read.');
}
} catch (e) {
console.error('Cannot check key instances, cause:', e.toString());
}
}
// Mechanic tests (así es la mecánica).
// Check that values can change (deterministic)
static checkVolumeChange() {
try {
const db = JSONdb.createInstance();
for (let i = 0; i < JSONdb.iterableTestsCount; i++) {
db.set('foo', new Date().toISOString());
let firstVal = db.get('foo');
db.set('foo', new Date().toUTCString());
let secondVal = db.get('foo');
if (firstVal !== secondVal) {
console.error('Values do not change');
}
}
} catch (e) {
console.error('Cannot check key instances, cause:', e.toString());
}
}
// Check that keys can be deleted
static checkKeyDeletion() {
try {
const db = JSONdb.createInstance();
for (let i = 0; i < this.witerableTestsCount; i++) {
db.set('foo', Date.now());
let firstVal = db.get('foo');
db.delete('foo');
let secondVal = db.get('foo');
if (firstVal !== secondVal) {
console.error('Values do not change.');
}
if (!secondVal) {
console.error('Key was not deleted.');
}
}
} catch (e) {
console.error('Cannot check key deletion, cause:', e.toString());
}
}
// Check that keys existence can be verified
static checkKeyInstance() {
try {
const db = this.createInstance();
for (let i = 0; i < this.iterableTestsCount; i++) {
db.set('foo', Date.now());
if (!db.has('foo')) {
console.error('Key existence is erroneous (returned False instead of True)');
}
db.delete('foo');
if (db.has('foo')) {
console.error('Key existence is erroneous (returned True instead of False)');
}
}
} catch (e) {
console.error('Cannot check key instances, cause:', e.toString());
}
}
// Verify sync to disk
static diskSynch() {
try {
const db = JSONdb.createInstance();
db.set('foo', Date.now());
} catch (e) {
console.error('Cannot save to disk, cause:', e.toString());
}
}
// Check that the copy of the underlying structure is coherent
static underlyingCoherency() {
try {
const db = JSONdb.createInstance();
const reference = {};
for (let i = 0; i < JSONdb.iterableTestsCount; i++) {
const now = Date.now();
db.set('foo', now);
reference.foo = now;
db.set('bar', now + 1000);
reference.bar = now + 1000;
if (JSON.stringify(db.JSON()) !== JSON.stringify(reference)) {
console.error('Returned copy does not match.');
}
}
} catch (e) {
console.error('Cannot check underlying coherency, cause:', e.toString());
}
}
// Check that the underlying structure can be replaced
static underlyingReplacement() {
try {
const db = JSONdb.createInstance();
const replacement = {};
const now = Date.now();
db.set('foo', now);
db.set('bar', now + 1000);
replacement.foo = now - 1000;
replacement.bar = now - 2000;
if (JSON.stringify(db.JSON()) === JSON.stringify(replacement)) {
console.log('Replacement is equal.');
}
db.JSON(replacement);
if (JSON.stringify(db.JSON()) !== JSON.stringify(replacement)) {
console.log('Replacement is not equal.');
}
} catch (e) {
console.error(e.toString());
}
}
// Persistency
static persitency() {
try {
let db = JSONdb.createInstance();
db.set('foo', Date.now());
let oldVal = db.get('foo');
db = JSONdb.createInstance();
if (db.get('foo') !== oldVal) {
console.error('Reloaded value differs from last written.');
}
} catch (e) {
console.error(e.toString());
}
}
// Temporary file removal
static cleanUp() {
try {
fs.unlinkSync(this.filePath);
} catch (e) {
console.error('Unable to cleanup.');
}
}
/**
* Main constructor, manages existing storage file and parses options against default ones.
* @param {string} filePath The path of the file to use as storage.
* @param {object} [options] Configuration options.
* @param {boolean} [options.syncOnWrite] Makes the storage be written to disk after every modification. Enabled by default.
* @param {boolean} [options.syncOnWrite] Makes the storage be written to disk after every modification. Enabled by default.
* @param {number} [options.jsonSpaces] How many spaces to use for indentation in the output json files. Default = 4
* @constructor
*/
constructor(filePath, options) {
super();
// Mandatory arguments check
if (!filePath || !filePath.length) {
throw new Error('Missing file path argument.');
} else {
this.filePath = filePath;
}
// Options parsing
if (options) {
for (let key in defaultOptions) {
if (!options.hasOwnProperty(key)) options[key] = defaultOptions[key];
}
this.options = JSON(options);
} else {
this.options = JSON(defaultOptions);
}
// Storage initialization
this.storage = {};
// File existence check
let stats;
try {
stats = fs.statSync(filePath);
} catch (err) {
if (err.code === 'ENOENT') {
/* File doesn't exist */
return;
} else if (err.code === 'EACCES') {
throw new Error(`Cannot access path "${filePath}".`);
} else {
// Other error
throw new Error(`Error while checking for existence of path "${filePath}": ${err}`);
}
}
/* File exists */
try {
fs.accessSync(filePath, fs.constants.R_OK | fs.constants.W_OK);
} catch (err) {
throw new Error(`Cannot read & write on path "${filePath}". Check permissions!`);
}
if (stats.size > 0) {
let data;
try {
data = fs.readFileSync(filePath);
} catch (err) {
throw err; // TODO: Do something meaningful
}
if (this.validateJSON.bind(this)(data)) this.storage = this.options.parse(data);
}
}
/**
* Creates or modifies a key in the database.
* @param {string} key The key to create or alter.
* @param {object} value Whatever to store in the key. You name it, just keep it JSON-friendly.
*/
set(key, value) {
this.storage[key] = value;
if (this.options) this.sync();
};
/**
* Extracts the value of a key from the database.
* @param {string} key The key to search for.
* @returns {object|undefined} The value of the key or `undefined` if it doesn't exist.
*/
get(key) {
return this.storage.hasOwnProperty(key) ? this.storage[key] : undefined;
};
/**
* Checks if a key is contained in the database.
* @param {string} key The key to search for.
* @returns {boolean} `True` if it exists, `false` if not.
*/
has(key) {
return this.storage.hasOwnProperty(key);
};
/**
* Deletes a key from the database.
* @param {string} key The key to delete.
* @returns {boolean|undefined} `true` if the deletion succeeded, `false` if there was an error, or `undefined` if the key wasn't found.
*/
delete(key) {
let retVal = this.storage.hasOwnProperty(key) ? delete this.storage[key] : undefined;
if (this.options) this.sync();
return retVal;
};
/**
* Deletes all keys from the database.
* @returns {object} The JSONdb instance itself.
*/
deleteAll() {
for (var key in this.storage) {
//noinspection JSUnfilteredForInLoop
this.delete(key);
}
return this;
};
/**
* Writes the local storage object to disk.
*/
sync() {
if (this.options) {
fs.writeFile(this.filePath, this.options.stringify(this.storage, null, this.options.jsonSpaces), (err) => {
if (err) throw err;
});
}
};
/**
* If no parameter is given, returns **a copy** of the local storage. If an object is given, it is used to replace the local storage.
* @param {object} storage A JSON object to overwrite the local storage with.
* @returns {object} Clone of the internal JSON storage. `Error` if a parameter was given and it was not a valid JSON object.
*/
JSON(storage) {
if (storage) {
try {
JSON.parse(this.options.stringify(storage));
this.storage = storage;
} catch (err) {
throw new Error('Given parameter is not a valid JSON object.');
}
}
return JSON.parse(this.options.stringify(this.storage));
};
// Recuerda hacer la salva antes de actualizar el archivo para evitar pérdidas de datos
// y borrarla después de la operación.
// y en la carga inicial, revisar si este archivo existe... para determinar si es posible
// una pérdida de datos... además, incluir un chequeo de errores.
// Hacerlo compatible con TypeORM o Massive.js
}
/*
function saveState(state) {
window.localStorage.setItem("gameState", JSON.stringify(state));
}
function restoreState() {
var state = window.localStorage.getItem("gameState");
if (state) {
return JSON.parse(state);
} else {
return null;
}
}
// ok after
function saveToStorage(key, val) {
localStorage.setItem(key, JSON.stringify(val))
}
function loadFromStorage(key) {
var val = localStorage.getItem(key)
return JSON.parse(val)
}
function removeFromStorage(key) {
localStorage.removeItem(key)
}
// ok before
// Limpiar el localstorage
localStorage.clear();
localStorage.setItem("usuario", usuario);
sessionStorage.apellidos = apellidos;
sessionStorage.clear();
Mira el html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Web Storage</title>
</head>
<body>
<form name="session">
Nombre: <input type="text" name="nombre" id="nombre"/><br>
Apellidos: <input type="text" name="apellidos" id="apellidos"/><br>
Página web: <input type="text" name="pagina" id="pagina"/><br>
<input type="button" id="guardar_claves_session" value="Guardar claves sessionStorage" onclick="guardar_claves_session()"></input>
</form>
<input type="button" id="borrar_claves_session" value="Borrar claves sessionStorage" onclick="borrar_claves_session()"></input>
<br>
<br>
<form name="local">
Usuario: <input type="text" id="usuario"/><br>
Email: <input type="email" id="email"/><br>
Contraseña: <input type="password" id="contrasena"/><br>
<input type="button" id="guardar_claves_local" value="Guardar claves localStorage" onclick="guardar_claves_local()"></input>
</form>
<input type="button" id="borrar_claves_local" value="Borrar claves localStorage" onclick=" localStorage.clear();obtener_claves_local();"></input>
<script src="js/storage_examples.js"></script>
</body>
</html>
y las funciones:
window.onload = function () {
if (window.sessionStorage != null) {
//Recuperar claves
obtener_claves_session();
}
if (window.localStorage != null) {
//Recuperar claves
obtener_claves_local();
}
};
function borrar_claves_session() {
sessionStorage.clear();
obtener_claves_session();
}
function obtener_claves_session() {
if (window.sessionStorage != null) {
//Recuperar claves y escribirlas en los input correspondientes
var nombre = sessionStorage.getItem("nombre");
var nombre_object = document.getElementById("nombre");
nombre_object.value = nombre;
var apellidos = sessionStorage.apellidos;
var apellidos_object = document.getElementById("apellidos");
apellidos_object.value = apellidos;
var pagina = sessionStorage["pagina"];
var pagina_object = document.getElementById("pagina");
pagina_object.value = pagina;
}
}
function guardar_claves_session() {
if (window.sessionStorage != null) {
//Guardar claves con los valores de los inputs
var nombre_object = document.getElementById("nombre");
var nomber = nombre_object.value;
sessionStorage.setItem("nombre", nombre);
var apellidos_object = document.getElementById("apellidos");
var apellidos = apellidos_object.value;
sessionStorage.apellidos = apellidos;
var pagina_object = document.getElementById("pagina");
var pagina = pagina_object.value;
sessionStorage.setItem("pagina", pagina);
}
}
function borrar_claves_local() {
localStorage.clear();
obtener_claves_local();
}
function obtener_claves_local() {
if (window.localStorage != null) {
//Recuperar claves y escribirlas en los input correspondientes
var nombre = localStorage.getItem("nombre");
var nombre_object = document.getElementById("nombre");
nombre_object.value = nombre;
var apellidos = localStorage.apellidos;
var apellidos_object = document.getElementById("apellidos");
apellidos_object.value = apellidos;
var pagina = localStorage["pagina"];
var pagina_object = document.getElementById("pagina");
pagina_object.value = pagina;
}
}
function guardar_claves_local() {
if (window.localStorage != null) {
//Guardar claves con los valores de los inputs
var nombre_object = document.getElementById("nombre");
var nomber = nombre_object.value;
localStorage.setItem("nombre", nombre);
var apellidos_object = document.getElementById("apellidos");
var apellidos = apellidos_object.value;
localStorage.apellidos = apellidos;
var pagina_object = document.getElementById("pagina");
var pagina = pagina_object.value;
localStorage.setItem("pagina", pagina);
}
}
*/