cradova 1.0.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.
Files changed (60) hide show
  1. package/LICENSE +201 -0
  2. package/cradova.png +0 -0
  3. package/docs/README.md +0 -0
  4. package/index.d.ts +52 -0
  5. package/index.js +342 -0
  6. package/index.ts +366 -0
  7. package/package.json +36 -0
  8. package/scripts/JsonDB.d.ts +298 -0
  9. package/scripts/JsonDB.js +698 -0
  10. package/scripts/JsonDB.ts +794 -0
  11. package/scripts/Metrics.d.ts +51 -0
  12. package/scripts/Metrics.js +56 -0
  13. package/scripts/Metrics.ts +66 -0
  14. package/scripts/Router.d.ts +12 -0
  15. package/scripts/Router.js +95 -0
  16. package/scripts/Router.ts +105 -0
  17. package/scripts/Screen.d.ts +11 -0
  18. package/scripts/Screen.js +62 -0
  19. package/scripts/Screen.ts +62 -0
  20. package/scripts/animate.d.ts +25 -0
  21. package/scripts/animate.js +47 -0
  22. package/scripts/animate.ts +57 -0
  23. package/scripts/css.d.ts +20 -0
  24. package/scripts/css.js +41 -0
  25. package/scripts/css.ts +46 -0
  26. package/scripts/dispatcher.d.ts +1 -0
  27. package/scripts/dispatcher.js +57 -0
  28. package/scripts/dispatcher.ts +58 -0
  29. package/scripts/file-system.d.ts +2 -0
  30. package/scripts/file-system.js +175 -0
  31. package/scripts/file-system.ts +177 -0
  32. package/scripts/fullscreen.d.ts +4 -0
  33. package/scripts/fullscreen.js +29 -0
  34. package/scripts/fullscreen.ts +30 -0
  35. package/scripts/init.d.ts +2 -0
  36. package/scripts/init.js +17 -0
  37. package/scripts/init.ts +18 -0
  38. package/scripts/localStorage.d.ts +9 -0
  39. package/scripts/localStorage.js +26 -0
  40. package/scripts/localStorage.ts +37 -0
  41. package/scripts/media.d.ts +22 -0
  42. package/scripts/media.js +48 -0
  43. package/scripts/media.ts +51 -0
  44. package/scripts/reuse.ts +74 -0
  45. package/scripts/speaker.d.ts +2 -0
  46. package/scripts/speaker.js +16 -0
  47. package/scripts/speaker.ts +25 -0
  48. package/scripts/store.d.ts +10 -0
  49. package/scripts/store.js +56 -0
  50. package/scripts/store.ts +47 -0
  51. package/scripts/swipe.d.ts +9 -0
  52. package/scripts/swipe.js +113 -0
  53. package/scripts/swipe.ts +129 -0
  54. package/scripts/widget.d.ts +2 -0
  55. package/scripts/widget.js +19 -0
  56. package/scripts/widget.ts +23 -0
  57. package/service-worker.d.ts +2 -0
  58. package/service-worker.js +40 -0
  59. package/service-worker.ts +52 -0
  60. package/tsconfig.json +11 -0
@@ -0,0 +1,698 @@
1
+ /**
2
+ * JSON DB DataBase MIT Licence © 2022
3
+ * ************************************
4
+ * Created by Friday Candour @uiedbooker
5
+ * email > fridaymaxtour@gmail.com
6
+ * github > www.github.com/FridayCandour
7
+ * telegram > @uiedbooker
8
+ * JSONDB @version 1.0.0
9
+ * */
10
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
11
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
12
+ return new (P || (P = Promise))(function (resolve, reject) {
13
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
14
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
15
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
16
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
17
+ });
18
+ };
19
+ export const JSONDBversion = "1.0.0";
20
+ let fs, fileURLToPath, isNode = false, _dirname;
21
+ if (!globalThis.localStorage) {
22
+ isNode = true;
23
+ fs = await import("fs");
24
+ const dr = await import("path");
25
+ const fp = await import("url");
26
+ fileURLToPath = fp.fileURLToPath;
27
+ _dirname = dr
28
+ .dirname(fileURLToPath(import.meta.url))
29
+ .split("node_modules")[0];
30
+ }
31
+ const schema = class {
32
+ constructor(schema_configuration_object, validators) {
33
+ // validations
34
+ if (!schema_configuration_object.columns) {
35
+ throw new Error("JSONDB: can't create an empty table should have some columns");
36
+ }
37
+ validators.validateColumns(schema_configuration_object.columns);
38
+ const isEmptyObject = function (obj) {
39
+ // for checking for empty objects
40
+ for (const name in obj) {
41
+ return false;
42
+ }
43
+ return true;
44
+ };
45
+ if (schema_configuration_object.relations &&
46
+ !isEmptyObject(schema_configuration_object.relations)) {
47
+ validators.validateRelations(schema_configuration_object.relations);
48
+ }
49
+ // assignment
50
+ this.base_name = "";
51
+ this.name = schema_configuration_object.name;
52
+ this.last_index = -1;
53
+ this.columns = schema_configuration_object.columns;
54
+ this.relations = schema_configuration_object.relations || null;
55
+ }
56
+ };
57
+ class JSONDBTableWrapper {
58
+ constructor(self, keys) {
59
+ this.put = (name, value) => __awaiter(this, void 0, void 0, function* () {
60
+ function cb(err) {
61
+ if (err) {
62
+ throw new Error("JSONDB: error failed to update entities in database because " + err);
63
+ }
64
+ }
65
+ if (isNode) {
66
+ fs.writeFile(name + ".json", JSON.stringify(value), cb);
67
+ }
68
+ else {
69
+ localStorage.setItem(name, JSON.stringify(value));
70
+ }
71
+ });
72
+ this.get = (name) => __awaiter(this, void 0, void 0, function* () {
73
+ return new Promise(function (res, rej) {
74
+ try {
75
+ if (!isNode) {
76
+ res(JSON.parse(localStorage.getItem(name)));
77
+ return;
78
+ }
79
+ fs.readFile(_dirname + "/" + name + ".json", { encoding: "utf-8" }, function (err, data) {
80
+ if (err) {
81
+ return rej("JSONDB: error failed to retrieve entities from database because " +
82
+ err);
83
+ }
84
+ try {
85
+ res(JSON.parse(data));
86
+ }
87
+ catch (error) {
88
+ try {
89
+ res(JSON.parse(fs.readFileSync(name + ".json", "utf-8")));
90
+ }
91
+ catch (error) { }
92
+ }
93
+ });
94
+ }
95
+ catch (error) { }
96
+ });
97
+ });
98
+ this.validator = (incoming, tables) => {
99
+ // works for type, nulllable and unique validations.
100
+ const outgoing = {};
101
+ for (const prop in this.self.columns) {
102
+ if (this.self.columns[prop].nullable !== true &&
103
+ !Object.hasOwnProperty.call(incoming, prop)) {
104
+ throw new Error("JSONDB: error failed to validate incoming data because " +
105
+ prop +
106
+ " is required for " +
107
+ this.self.name +
108
+ " Schema");
109
+ }
110
+ if (!this.self.columns[prop].nullable &&
111
+ typeof incoming[prop] !== this.self.columns[prop].type) {
112
+ throw new Error("JSONDB: error failed to validate incoming data because " +
113
+ prop +
114
+ "'s value " +
115
+ incoming[prop] +
116
+ " has got a wrong data type of " +
117
+ typeof incoming[prop] +
118
+ " for " +
119
+ this.self.name +
120
+ " should be " +
121
+ this.self.columns[prop].type +
122
+ " type instead");
123
+ }
124
+ if (this.self.columns[prop].unique === true) {
125
+ for (let i = 0; i < tables.length; i++) {
126
+ const element = tables[i];
127
+ if (element[prop] === incoming[prop]) {
128
+ throw new Error("JSONDB: error failed to validate incoming data because " +
129
+ prop +
130
+ " is unique for " +
131
+ this.self.name +
132
+ " Schema can't have more than one instance");
133
+ }
134
+ }
135
+ }
136
+ // cleaning time
137
+ outgoing[prop] = incoming[prop];
138
+ }
139
+ return outgoing;
140
+ };
141
+ this.self = self;
142
+ this.keys = keys;
143
+ }
144
+ /**
145
+ * Save with relations
146
+ * ---------------------
147
+ * @type .saveWithRelations(target table, schema, schema | schema[]) => Promise(object)
148
+ * @example
149
+ * // single relation
150
+ await PollTable.saveWithRelations(MessageTable, Poll, message);
151
+ // arrays of relations
152
+ await PollTable.saveWithRelations(MessageTable, Poll, allMessages);
153
+ */
154
+ saveWithRelations(table, incoming, relations) {
155
+ return __awaiter(this, void 0, void 0, function* () {
156
+ if (!relations) {
157
+ return;
158
+ }
159
+ const db = yield this.get(this.self.base_name);
160
+ db.last_access_time = Date();
161
+ if (incoming && typeof incoming.index !== "number") {
162
+ throw new Error("JsonDB: save before saving with relations");
163
+ }
164
+ db.tables[this.self.name][incoming.index] = incoming;
165
+ if (relations && Array.isArray(relations)) {
166
+ for (let i = 0; i < relations.length; i++) {
167
+ if (db.Entities[this.self.name].relations[table.self.name]) {
168
+ if (db.Entities[this.self.name].relations[table.self.name].type ===
169
+ "many") {
170
+ db.tables[this.self.name][incoming.index].relations[table.self.name] = !db.tables[this.self.name][incoming.index].relations[table.self.name]
171
+ ? [relations[i]]
172
+ : [
173
+ ...db.tables[this.self.name][incoming.index].relations[table.self.name],
174
+ relations[i],
175
+ ];
176
+ }
177
+ else {
178
+ db.tables[this.self.name][incoming.index].relations[table.self.name] = relations[i];
179
+ }
180
+ }
181
+ }
182
+ }
183
+ else {
184
+ if (relations) {
185
+ if (db.Entities[this.self.name].relations[table.self.name]) {
186
+ if (db.Entities[this.self.name].relations[table.self.name].type ===
187
+ "many") {
188
+ db.tables[this.self.name][incoming.index].relations[table.self.name] = !db.tables[this.self.name][incoming.index].relations[table.self.name]
189
+ ? [relations]
190
+ : [
191
+ ...db.tables[this.self.name][incoming.index].relations[table.self.name],
192
+ relations,
193
+ ];
194
+ }
195
+ else {
196
+ db.tables[this.self.name][incoming.index].relations[table.self.name] = relations;
197
+ }
198
+ }
199
+ }
200
+ }
201
+ yield this.put(this.self.base_name, db);
202
+ return db.tables[this.self.name][incoming.index];
203
+ });
204
+ }
205
+ /**
206
+ * Save table into a Jsondb instance
207
+ * -----------------------------
208
+ * @type .save(schema)=> Promise(object)
209
+ * @example
210
+ await PollTable.save(poll)
211
+ */
212
+ save(incoming) {
213
+ return __awaiter(this, void 0, void 0, function* () {
214
+ // db.tables[this.self.name] = db.tables[this.self.name].sort(
215
+ // (a, b) => a.index - b.index
216
+ // );
217
+ const db = yield this.get(this.self.base_name);
218
+ db.last_access_time = Date();
219
+ if (typeof incoming.index !== "number") {
220
+ incoming = this.validator(incoming, db.tables[this.self.name]);
221
+ if (this.self.relations && !incoming.relations) {
222
+ incoming.relations = {};
223
+ }
224
+ db.Entities[this.self.name].last_index += 1;
225
+ incoming.index = db.Entities[this.self.name].last_index;
226
+ db.tables[this.self.name].push(incoming);
227
+ }
228
+ else {
229
+ db.tables[this.self.name][incoming.index] = incoming;
230
+ }
231
+ yield this.put(this.self.base_name, db);
232
+ return incoming;
233
+ });
234
+ }
235
+ /**
236
+ * Save table into a Jsondb instance
237
+ * -----------------------------
238
+ * @type .remove(schema)=> Promise(object)
239
+ * @example
240
+ await PollTable.remove(poll)
241
+ */
242
+ remove(entity) {
243
+ return __awaiter(this, void 0, void 0, function* () {
244
+ const db = yield this.get(this.self.base_name);
245
+ db.last_access_time = Date();
246
+ // db.tables[this.self.name].splice(entity.index, 1);
247
+ db.tables[this.self.name][entity.index] = null;
248
+ yield this.put(this.self.base_name, db);
249
+ });
250
+ }
251
+ /**
252
+ * Save table into a Jsondb instance
253
+ * -----------------------------
254
+ * @type .count(schema)=> Promise(number)
255
+ * @example
256
+ await PollTable.count(poll)
257
+ */
258
+ count() {
259
+ return __awaiter(this, void 0, void 0, function* () {
260
+ const db = yield this.get(this.self.base_name);
261
+ db.last_access_time = Date();
262
+ return db.tables[this.self.name].length;
263
+ });
264
+ }
265
+ /**
266
+ * Save table into a Jsondb instance
267
+ * -----------------------------
268
+ * @type .getAll()=> Promise(object[])
269
+ * @example
270
+ await PollTable.getAll()
271
+ */
272
+ getAll() {
273
+ return __awaiter(this, void 0, void 0, function* () {
274
+ const db = yield this.get(this.self.base_name);
275
+ db.last_access_time = Date();
276
+ return db.tables[this.self.name];
277
+ });
278
+ }
279
+ /**
280
+ * get entities with any of the values specifiled from a Jsondb instance
281
+ * -----------------------------
282
+ * @type .getWhereAny({prop: value}, number | undefind)=> Promise(object)
283
+ * @example
284
+ await PollTable.getWhereAny({name: "friday", age: 121, class: "senior"}) // gets all
285
+ await PollTable.getWhereAny({email: "fridaymaxtour@gmail.com"}, 2) // gets 2 if they are up to two
286
+ */
287
+ getWhereAny(props, number) {
288
+ return __awaiter(this, void 0, void 0, function* () {
289
+ const results = [];
290
+ let all;
291
+ const db = yield this.get(this.self.base_name);
292
+ db.last_access_time = Date();
293
+ all = db.tables[this.self.name];
294
+ for (let i = 0; i < all.length; i++) {
295
+ const element = all[i];
296
+ for (const [k, v] of Object.entries(props)) {
297
+ if (element[k] && element[k] === v) {
298
+ results.push(element);
299
+ if (typeof number === "number" && results.length === number) {
300
+ return results;
301
+ }
302
+ }
303
+ }
304
+ }
305
+ return results;
306
+ });
307
+ }
308
+ /**
309
+ * get entities with the given prop of type "string" where the values specifiled is included
310
+ * -----------------------------
311
+ * @type .getWhereAnyPropsIncludes({prop: value}, number | undefind)=> Promise(object)
312
+ *
313
+ * @example prop must be type string!
314
+ *
315
+ await PollTable.getWhereAnyPropsIncludes({name: "fri"}) // gets all
316
+ await PollTable.getWhereAnyPropsIncludes({name: "fri"}, 2) // gets 2 if they are up to two
317
+ */
318
+ getWhereAnyPropsIncludes(props, number) {
319
+ return __awaiter(this, void 0, void 0, function* () {
320
+ const results = [];
321
+ let all;
322
+ const db = yield this.get(this.self.base_name);
323
+ db.last_access_time = Date();
324
+ all = db.tables[this.self.name];
325
+ for (let i = 0; i < all.length; i++) {
326
+ const element = all[i];
327
+ for (const [k, v] of Object.entries(props)) {
328
+ if (element[k] && typeof v === "string" && element[k].includes(v)) {
329
+ results.push(element);
330
+ if (typeof number === "number" && results.length === number) {
331
+ return results;
332
+ }
333
+ }
334
+ }
335
+ }
336
+ return results;
337
+ });
338
+ }
339
+ /**
340
+ * get an entity with the values specifiled from a Jsondb instance
341
+ * -----------------------------
342
+ * @type .getOne({prop: value})=> Promise(object)
343
+ * @example
344
+
345
+ await PollTable.getOne({email: "fridaymaxtour@gamail.com"}) // gets one
346
+
347
+ */
348
+ getOne(props) {
349
+ return __awaiter(this, void 0, void 0, function* () {
350
+ let results = null;
351
+ const db = yield this.get(this.self.base_name);
352
+ db.last_access_time = Date();
353
+ const all = db.tables[this.self.name];
354
+ for (let i = 0; i < all.length; i++) {
355
+ const element = all[i];
356
+ for (const [k, v] of Object.entries(props)) {
357
+ if (element[k] && element[k] === v) {
358
+ results = element;
359
+ break;
360
+ }
361
+ }
362
+ }
363
+ return results;
364
+ });
365
+ }
366
+ }
367
+ const JSONDBConnection = class {
368
+ constructor(Entities, keys) {
369
+ this.Entities = Entities;
370
+ this.keys = keys;
371
+ }
372
+ /**
373
+ * Get a table from JSONDB
374
+ *------------------------
375
+ * @example
376
+ *
377
+ *
378
+ const details = {
379
+ password: "password",
380
+ username: "jsondb_username",
381
+ };
382
+ // getting connection instance into JSONDB
383
+ const connection = await database.createJSONDBConnection(details);
384
+ // getting a table
385
+ const MessageTable = connection.getTable("Message");
386
+ * */
387
+ getTable(table_name) {
388
+ for (const [tableName, table] of Object.entries(this.Entities)) {
389
+ if (table_name === tableName) {
390
+ return new JSONDBTableWrapper(table, this.keys);
391
+ }
392
+ }
393
+ }
394
+ };
395
+ /**
396
+ * Create a new JSONDB object
397
+ *------------------------
398
+ * @class
399
+
400
+ * const database = new JSONDB()
401
+ *
402
+ * Creates a new JSONDB object
403
+ *
404
+ * .
405
+ * */
406
+ class JSONDB {
407
+ constructor() {
408
+ this.DB_NAME = "";
409
+ this.username = "";
410
+ this.encrypted = false;
411
+ this.initialised = false;
412
+ this.time_created = Date();
413
+ this.version = JSONDBversion;
414
+ this.last_access_time = "";
415
+ this.visuality = "";
416
+ this.Entities = {};
417
+ this.tables = {};
418
+ this.password = "";
419
+ }
420
+ getDB(name) {
421
+ return __awaiter(this, void 0, void 0, function* () {
422
+ return new Promise(function (res, rej) {
423
+ if (!isNode) {
424
+ res(JSON.parse(localStorage.getItem(name)));
425
+ return;
426
+ }
427
+ try {
428
+ fs.readFile(_dirname + "/" + name + ".json", { encoding: "utf-8" }, function (err, data) {
429
+ if (err) {
430
+ return rej(err);
431
+ }
432
+ try {
433
+ res(JSON.parse(data));
434
+ }
435
+ catch (error) {
436
+ try {
437
+ res(JSON.parse(fs.readFileSync(name + ".json", "utf-8")));
438
+ }
439
+ catch (error) { }
440
+ }
441
+ });
442
+ }
443
+ catch (error) { }
444
+ });
445
+ });
446
+ }
447
+ /**
448
+ * Schema constructor for Jsondb
449
+ * -----------------------------
450
+ *
451
+ * name @type string
452
+ *
453
+ * columns @type object {
454
+ *
455
+ * type > @type any of number > string > boolean > blob and must be specified
456
+ *
457
+ * nullable @type bolean true > false default false
458
+ *
459
+ * unique @type bolean true > false default false
460
+ *
461
+ * }
462
+ *
463
+ * relations @type object {
464
+ *
465
+ * target: entity schema @type object,
466
+ *
467
+ * attachment_name: @type string,
468
+ *
469
+ * type : @type string should be "many" or "one"
470
+ *
471
+ * }
472
+ *
473
+ *
474
+ *
475
+ * @example
476
+ *
477
+ * const MessageSchema = database.schema({
478
+ name: "Message",
479
+ columns: {
480
+ vote: {
481
+ type: "number",
482
+ },
483
+ time: {
484
+ type: "string",
485
+ nullable: true,
486
+ },
487
+ value: {
488
+ type: "string",
489
+ },
490
+ },
491
+ });
492
+ *
493
+ * const PollSchema = new JSONDB.schema({
494
+ name: "Poll",
495
+ columns: {
496
+ value: {
497
+ type: "varchar",
498
+ },
499
+ },
500
+ relations: {
501
+ Message: {
502
+ target: Message,
503
+ type: "many-to-one",
504
+ },
505
+ },
506
+ });
507
+ */
508
+ schema(schema_configuration_object) {
509
+ return new schema(schema_configuration_object, {
510
+ validateColumns: this.validateColumns,
511
+ validateRelations: this.validateRelations,
512
+ });
513
+ }
514
+ /**
515
+ * Create a new JSONDB instance
516
+ *------------------------
517
+ * @example
518
+ * // creates a JSONDB object
519
+ * const Database = new JSONDB()
520
+ * // database configuration object
521
+ * const config = {
522
+ DB_NAME: "my db",
523
+ password: "password",
524
+ username: "jsondb_username",
525
+ encrypted: false,
526
+ }
527
+ // Creates a new JSONDB instance
528
+ * Database.init(config)
529
+ * */
530
+ init(config) {
531
+ console.log(`\x1B[32m JSONDB version ${JSONDBversion} \x1B[39m`);
532
+ this.initialised = true;
533
+ this.DB_NAME = config.name;
534
+ this.password = config.password || "";
535
+ this.username = config.username || "";
536
+ this.encrypted = config.encrypted || false;
537
+ this.time_created = Date();
538
+ this.tables = {};
539
+ try {
540
+ let wasThere;
541
+ if (isNode) {
542
+ wasThere = fs.readFileSync(config.name + ".json", "utf-8");
543
+ }
544
+ else {
545
+ wasThere = localStorage.getItem(config.name);
546
+ }
547
+ if (wasThere) {
548
+ return;
549
+ }
550
+ }
551
+ catch (error) { }
552
+ if (!config.password) {
553
+ throw new Error("JSONDB: error password is empty ");
554
+ }
555
+ if (!config.username) {
556
+ throw new Error("JSONDB: error username is empty ");
557
+ }
558
+ function cb(err) {
559
+ if (err) {
560
+ throw new Error("JSONDB: error failed to create database because " + err);
561
+ }
562
+ }
563
+ if (isNode) {
564
+ fs.writeFile(config.name + ".json", JSON.stringify(this), cb);
565
+ }
566
+ else {
567
+ let db = JSON.stringify(this);
568
+ localStorage.setItem(config.name, db);
569
+ }
570
+ }
571
+ /**
572
+ * Create secure connection a Jsondb instance
573
+ * -----------------------------
574
+ * @example
575
+ *
576
+ * const details = {
577
+ password: "password",
578
+ username: "jsondb_username",
579
+ };
580
+ const connection = await database.createJSONDBConnection(details);
581
+ */
582
+ createJSONDBConnection(details) {
583
+ return __awaiter(this, void 0, void 0, function* () {
584
+ if (!this.initialised) {
585
+ throw new Error("JSONDB: you haven't create a JSONDB instance yet");
586
+ }
587
+ if (details.username !== this.username ||
588
+ details.password !== this.password) {
589
+ throw new Error("JSONDB: Access Denied");
590
+ }
591
+ const connection = yield this.getDB(this.DB_NAME);
592
+ connection.last_access_time = Date();
593
+ return new JSONDBConnection(connection.Entities);
594
+ });
595
+ }
596
+ validateRelations(relations) {
597
+ const types = ["many", "one"];
598
+ for (const [relation, value] of Object.entries(relations)) {
599
+ if (typeof value.target !== "object") {
600
+ throw new Error("JSONDB: wrong relationship target type given " +
601
+ value.target +
602
+ " should be object only");
603
+ }
604
+ if (!types.includes(value.type)) {
605
+ throw new Error("JSONDB: wrong relationship type given " +
606
+ value.type +
607
+ " should be many or one");
608
+ }
609
+ if (value.cascade && typeof value.cascade !== "boolean") {
610
+ throw new Error("JSONDB: wrong cascade value given " +
611
+ value.cascade +
612
+ " should be true or false");
613
+ }
614
+ }
615
+ }
616
+ validateColumns(columns) {
617
+ const types = ["number", "string", "boolean", "blob"];
618
+ for (const [column, value] of Object.entries(columns)) {
619
+ if (column) {
620
+ if (!types.includes(value.type)) {
621
+ throw new Error("JSONDB: wrong data type given " +
622
+ value.type +
623
+ " only number, string, boolean and blob are accepted");
624
+ }
625
+ if (value.unique && typeof value.unique !== "boolean") {
626
+ throw new Error("JSONDB: wrong unique value given " +
627
+ value.unique +
628
+ " should be true or false");
629
+ }
630
+ if (value.nullable && typeof value.nullable !== "boolean") {
631
+ throw new Error("JSONDB: wrong nullable value given " +
632
+ value.nullable +
633
+ " should be true or false");
634
+ }
635
+ }
636
+ }
637
+ }
638
+ /**
639
+ * Assemble Entities into Jsondb
640
+ * -----------------------------
641
+ * @example
642
+ *
643
+ * const MessageSchema = database.schema({
644
+ name: "Message",
645
+ columns: {
646
+ vote: {
647
+ type: "number",
648
+ },
649
+ time: {
650
+ type: "string",
651
+ nullable: true,
652
+ },
653
+ value: {
654
+ type: "string",
655
+ },
656
+ },
657
+ });
658
+
659
+ database.assemble([MessageSchema]);
660
+ *
661
+ */
662
+ assemble(allEntities) {
663
+ if (!this.initialised) {
664
+ throw new Error("JSONDB: you haven't create a JSONDB instance yet");
665
+ }
666
+ try {
667
+ const wasThere = fs.readFileSync(this.DB_NAME + ".json", "utf-8");
668
+ if (wasThere) {
669
+ return;
670
+ }
671
+ }
672
+ catch (error) { }
673
+ if (!Array.isArray(allEntities) || typeof allEntities[0] !== "object") {
674
+ throw new Error("JSONDB: invalid entity array list, can't be assembled");
675
+ }
676
+ for (let i = 0; i < allEntities.length; i++) {
677
+ this.Entities[allEntities[i].name] = allEntities[i];
678
+ this.Entities[allEntities[i].name].base_name = this.DB_NAME;
679
+ this.tables[allEntities[i].name] = [];
680
+ }
681
+ function cb(err) {
682
+ if (err) {
683
+ throw new Error("JSONDB: error failed to assemble entities into database because " +
684
+ err);
685
+ }
686
+ }
687
+ if (isNode) {
688
+ fs.writeFile(this.DB_NAME + ".json", JSON.stringify(this), cb);
689
+ }
690
+ else {
691
+ localStorage.setItem(this.DB_NAME, JSON.stringify(this));
692
+ }
693
+ }
694
+ }
695
+ /**
696
+ * @exports
697
+ */
698
+ export default JSONDB;