beanbagdb 0.5.76 → 0.5.80
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 +1 -1
- package/src/index.js +39 -18
- package/src/system_schema.js +37 -10
- package/test/edges.test.js +3 -0
- package/test/operations.test.js +38 -0
- package/test/pouchdb.js +5 -3
- package/test/test1.js +106 -147
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -269,7 +269,7 @@ export class BeanBagDB {
|
|
|
269
269
|
let app_doc = { ... app_data.meta, version: latest_version}
|
|
270
270
|
try {
|
|
271
271
|
// modify the app setting doc
|
|
272
|
-
await this.modify_setting(app_data.meta.name,
|
|
272
|
+
await this.modify_setting(app_data.meta.name,app_doc,"update")
|
|
273
273
|
|
|
274
274
|
// add a new log
|
|
275
275
|
let new_log_doc = this._get_blank_doc("system_log")
|
|
@@ -451,6 +451,11 @@ export class BeanBagDB {
|
|
|
451
451
|
// }
|
|
452
452
|
// }
|
|
453
453
|
|
|
454
|
+
// system generated schemas cannot be edited
|
|
455
|
+
if(full_doc.schema=="schema"&&full_doc.data.system_generated==true){
|
|
456
|
+
throw new DocUpdateError("System schemas cannot be updated using this API");
|
|
457
|
+
}
|
|
458
|
+
|
|
454
459
|
// update new value depending on settings.non_editable_fields (if does not exists, all fields are editable)
|
|
455
460
|
let all_fields = Object.keys(schema.schema.properties);
|
|
456
461
|
let unedit_fields = schema.settings["non_editable_fields"];
|
|
@@ -462,7 +467,7 @@ export class BeanBagDB {
|
|
|
462
467
|
// todo : what if additionalField are allowed ??
|
|
463
468
|
let updated_data = { ...full_doc.data, ...allowed_updates };
|
|
464
469
|
|
|
465
|
-
this.util_validate_data(schema.schema, updated_data);
|
|
470
|
+
updated_data = this.util_validate_data(schema.schema, updated_data);
|
|
466
471
|
|
|
467
472
|
// primary key check if multiple records can be created
|
|
468
473
|
if (schema.settings["primary_keys"].length > 0) {
|
|
@@ -489,7 +494,7 @@ export class BeanBagDB {
|
|
|
489
494
|
let m_sch = sys_sch.editable_metadata_schema;
|
|
490
495
|
let editable_fields = Object.keys(m_sch["properties"]);
|
|
491
496
|
let allowed_meta = this.util_filter_object(updates.meta, editable_fields);
|
|
492
|
-
this.util_validate_data(m_sch, allowed_meta);
|
|
497
|
+
allowed_meta = this.util_validate_data(m_sch, allowed_meta);
|
|
493
498
|
// if update has a link ,then check if it already exists
|
|
494
499
|
if (allowed_meta.link){
|
|
495
500
|
let search = await this.search({ selector: {"meta.link":allowed_meta.link} })
|
|
@@ -652,6 +657,7 @@ export class BeanBagDB {
|
|
|
652
657
|
system_defined : doc.data.system_generated,
|
|
653
658
|
description: doc.data.description,
|
|
654
659
|
link: doc.meta.link,
|
|
660
|
+
title:doc.data.title,
|
|
655
661
|
_id:doc._id
|
|
656
662
|
})
|
|
657
663
|
})
|
|
@@ -951,9 +957,9 @@ async _upgrade_schema_in_bulk(schemas,log_upgrade=false,log_message="Schema Upgr
|
|
|
951
957
|
* @returns {Object}
|
|
952
958
|
*/
|
|
953
959
|
_get_blank_schema_doc(schema_name, schema_object, data) {
|
|
954
|
-
this.util_validate_data(schema_object, data);
|
|
960
|
+
let new_data = this.util_validate_data(schema_object, data);
|
|
955
961
|
let obj = this._get_blank_doc(schema_name);
|
|
956
|
-
obj["data"] =
|
|
962
|
+
obj["data"] = new_data;
|
|
957
963
|
return obj;
|
|
958
964
|
}
|
|
959
965
|
|
|
@@ -1024,11 +1030,13 @@ async _upgrade_schema_in_bulk(schemas,log_upgrade=false,log_message="Schema Upgr
|
|
|
1024
1030
|
if (sch_search.docs.length == 0) {throw new DocCreationError(`The schema "${schema}" does not exists`)}
|
|
1025
1031
|
let schemaDoc = sch_search.docs[0]["data"];
|
|
1026
1032
|
// validate data
|
|
1027
|
-
|
|
1033
|
+
if(!schemaDoc.active){throw new DocCreationError(`The schema "${schema}" is not active`)}
|
|
1034
|
+
|
|
1035
|
+
let new_data = this.util_validate_data(schemaDoc.schema, data);
|
|
1028
1036
|
|
|
1029
1037
|
// validate meta
|
|
1030
|
-
if(Object.keys.length>0){
|
|
1031
|
-
this.util_validate_data(sys_sch.editable_metadata_schema, meta)
|
|
1038
|
+
if(Object.keys(meta).length>0){
|
|
1039
|
+
meta = this.util_validate_data(sys_sch.editable_metadata_schema, meta)
|
|
1032
1040
|
}
|
|
1033
1041
|
|
|
1034
1042
|
|
|
@@ -1042,25 +1050,25 @@ async _upgrade_schema_in_bulk(schemas,log_upgrade=false,log_message="Schema Upgr
|
|
|
1042
1050
|
// @TODO : for schema dos: settings fields must be in schema field
|
|
1043
1051
|
if (schema == "schema") {
|
|
1044
1052
|
//more checks are required
|
|
1045
|
-
this.util_validate_schema_object(
|
|
1053
|
+
this.util_validate_schema_object(new_data);
|
|
1046
1054
|
}
|
|
1047
1055
|
// @TODO : check if single record setting is set to true
|
|
1048
1056
|
//console.log(schemaDoc)
|
|
1049
1057
|
// duplicate check
|
|
1050
1058
|
if (schemaDoc.settings["primary_keys"].length > 0) {
|
|
1051
1059
|
let primary_obj = { schema: schema };
|
|
1052
|
-
schemaDoc.settings["primary_keys"].map((ky) => {primary_obj["data." + ky] =
|
|
1060
|
+
schemaDoc.settings["primary_keys"].map((ky) => {primary_obj["data." + ky] = new_data[ky];});
|
|
1053
1061
|
let prim_search = await this.search({ selector: primary_obj });
|
|
1054
1062
|
if (prim_search.docs.length > 0) {
|
|
1055
1063
|
throw new DocCreationError(`Document with the given primary key (${schemaDoc.settings["primary_keys"].join(",")}) already exists in the schema "${schema}"`);
|
|
1056
1064
|
}
|
|
1057
1065
|
}
|
|
1058
1066
|
// encrypt if required
|
|
1059
|
-
|
|
1067
|
+
|
|
1060
1068
|
if (schemaDoc.settings["encrypted_fields"].length > 0) {
|
|
1061
1069
|
// todo test if encryption is successful
|
|
1062
1070
|
for (let itm of schemaDoc.settings["encrypted_fields"]) {
|
|
1063
|
-
new_data[itm] = await this.utils.encrypt(
|
|
1071
|
+
new_data[itm] = await this.utils.encrypt(new_data[itm], this.encryption_key);
|
|
1064
1072
|
}
|
|
1065
1073
|
}
|
|
1066
1074
|
|
|
@@ -1124,16 +1132,18 @@ async _upgrade_schema_in_bulk(schemas,log_upgrade=false,log_message="Schema Upgr
|
|
|
1124
1132
|
|
|
1125
1133
|
|
|
1126
1134
|
/**
|
|
1127
|
-
* Validates a data object against a provided JSON schema
|
|
1135
|
+
* Validates a data object against a provided JSON schema and returns a valid data object (with default value for missing field for which default values are defined in the schema )
|
|
1128
1136
|
* It relies on the external API provided by the user
|
|
1129
1137
|
* @param {Object} schema_obj - The JSON schema object to validate against
|
|
1130
1138
|
* @param {Object} data_obj - The data object to validate
|
|
1131
1139
|
* @throws {Error} If the data object does not conform to the schema
|
|
1132
1140
|
*/
|
|
1133
1141
|
util_validate_data(schema_obj, data_obj) {
|
|
1134
|
-
const { valid, validate } = this.utils.validate_schema(schema_obj,data_obj)
|
|
1142
|
+
const { valid, validate , data} = this.utils.validate_schema(schema_obj,data_obj)
|
|
1135
1143
|
if (!valid) {
|
|
1136
1144
|
throw new ValidationError(validate.errors);
|
|
1145
|
+
}else{
|
|
1146
|
+
return data
|
|
1137
1147
|
}
|
|
1138
1148
|
}
|
|
1139
1149
|
|
|
@@ -1248,10 +1258,21 @@ async _upgrade_schema_in_bulk(schemas,log_upgrade=false,log_message="Schema Upgr
|
|
|
1248
1258
|
* "banana-earth-rain".
|
|
1249
1259
|
*
|
|
1250
1260
|
*/
|
|
1251
|
-
util_generate_random_link() {
|
|
1252
|
-
|
|
1253
|
-
|
|
1254
|
-
|
|
1261
|
+
util_generate_random_link(type=1) {
|
|
1262
|
+
const options = {
|
|
1263
|
+
0:()=>{
|
|
1264
|
+
// prettier-ignore
|
|
1265
|
+
const dictionary = ['rain', 'mars', 'banana', 'earth', 'kiwi', 'mercury', 'fuji', 'hurricane', 'matterhorn', 'snow', 'saturn', 'jupiter', 'peach', 'wind', 'pluto', 'apple', 'k2', 'storm', 'venus', 'denali', 'cloud', 'sunshine', 'mango', 'drizzle', 'pineapple', 'aconcagua', 'gasherbrum', 'apricot', 'neptune', 'fog', 'orange', 'blueberry', 'kilimanjaro', 'uranus', 'grape', 'storm', 'montblanc', 'lemon', 'chooyu', 'raspberry', 'cherry', 'thunder', 'vinson', 'breeze', 'elbrus', 'everest', 'parbat', 'makalu', 'nanga', 'kangchenjunga', 'lightning', 'cyclone', 'comet', 'asteroid', 'pomegranate', 'nectarine', 'clementine', 'strawberry', 'tornado', 'avalanche', 'andes', 'rockies', 'himalayas', 'pyrenees', 'carpathians', 'cascade', 'etna', 'vesuvius', 'volcano', 'tundra', 'whirlwind', 'iceberg', 'eclipse', 'zephyr', 'tropic', 'monsoon', 'aurora'];
|
|
1266
|
+
return Array.from({ length: 3 },() => dictionary[Math.floor(Math.random() * dictionary.length)]).join("-");
|
|
1267
|
+
},
|
|
1268
|
+
1:()=>{
|
|
1269
|
+
const length = Math.floor(Math.random() * 3) + 6; // Random length: 6, 7, or 8
|
|
1270
|
+
const hexNumber = Math.floor(Math.random() * Math.pow(16, length)).toString(16);
|
|
1271
|
+
return hexNumber.padStart(length, '0'); // Ensure it has the desired length
|
|
1272
|
+
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
return options[type]()
|
|
1255
1276
|
}
|
|
1256
1277
|
}
|
|
1257
1278
|
|
package/src/system_schema.js
CHANGED
|
@@ -6,9 +6,11 @@ export const default_app = {
|
|
|
6
6
|
schemas:[
|
|
7
7
|
{
|
|
8
8
|
name: "schema",
|
|
9
|
+
active:true,
|
|
9
10
|
description:"Meta-schema or the schema for defining other schemas",
|
|
10
11
|
system_generated:true,
|
|
11
|
-
version:0.
|
|
12
|
+
version:0.87,
|
|
13
|
+
title:"Schema document",
|
|
12
14
|
schema: {
|
|
13
15
|
type: "object",
|
|
14
16
|
additionalProperties: false,
|
|
@@ -18,6 +20,11 @@ export const default_app = {
|
|
|
18
20
|
type:"boolean",
|
|
19
21
|
default:false
|
|
20
22
|
},
|
|
23
|
+
active:{
|
|
24
|
+
title:"This indicates where new documents can be created using this schema or not. Old documents can still be edited",
|
|
25
|
+
type:"boolean",
|
|
26
|
+
default:false
|
|
27
|
+
},
|
|
21
28
|
version: {
|
|
22
29
|
type: "number",
|
|
23
30
|
title:"Version",
|
|
@@ -33,6 +40,13 @@ export const default_app = {
|
|
|
33
40
|
pattern: "^[a-zA-Z][a-zA-Z0-9_]*$",
|
|
34
41
|
description:"This is the name of the schema.It cannot be changed later"
|
|
35
42
|
},
|
|
43
|
+
title:{
|
|
44
|
+
type:"string",
|
|
45
|
+
title:"Title",
|
|
46
|
+
minLength:0,
|
|
47
|
+
maxLength:1000,
|
|
48
|
+
description:"A title to display with records."
|
|
49
|
+
},
|
|
36
50
|
description:{
|
|
37
51
|
type:"string",
|
|
38
52
|
title:"About",
|
|
@@ -47,6 +61,7 @@ export const default_app = {
|
|
|
47
61
|
minProperties: 1,
|
|
48
62
|
maxProperties: 50,
|
|
49
63
|
description:"This must be a valid JSON Schema which will be used to validate documents created with this schema.See this https://tour.json-schema.org/",
|
|
64
|
+
default:{}
|
|
50
65
|
},
|
|
51
66
|
settings: {
|
|
52
67
|
type: "object",
|
|
@@ -98,20 +113,22 @@ export const default_app = {
|
|
|
98
113
|
required :["primary_keys","non_editable_fields","encrypted_fields"]
|
|
99
114
|
},
|
|
100
115
|
},
|
|
101
|
-
required: ["name","description","schema", "settings"],
|
|
116
|
+
required: ["name","description","schema", "settings","title"],
|
|
102
117
|
},
|
|
103
118
|
settings: {
|
|
104
119
|
primary_keys: ["name"],
|
|
105
|
-
non_editable_fields:[],
|
|
120
|
+
non_editable_fields:["system_generated"],
|
|
106
121
|
encrypted_fields:[],
|
|
107
|
-
display_fields:["name","version","description"]
|
|
122
|
+
display_fields:["name","version","description","title","active"]
|
|
108
123
|
},
|
|
109
124
|
},
|
|
110
125
|
{
|
|
111
126
|
system_generated:true,
|
|
112
|
-
version:0.
|
|
127
|
+
version:0.63,
|
|
113
128
|
description:"To store user defined key. this can include anything like API tokens etc. There is a special method to fetch this. The values are encrypted",
|
|
114
129
|
name: "system_key",
|
|
130
|
+
title:"System key",
|
|
131
|
+
active:true,
|
|
115
132
|
schema: {
|
|
116
133
|
type: "object",
|
|
117
134
|
additionalProperties: true,
|
|
@@ -144,10 +161,12 @@ export const default_app = {
|
|
|
144
161
|
},
|
|
145
162
|
},
|
|
146
163
|
{
|
|
147
|
-
version:0.
|
|
164
|
+
version:0.67,
|
|
148
165
|
system_generated:true,
|
|
149
166
|
description:"The system relies on these settings for proper functioning or enabling optional features.",
|
|
150
167
|
name: "system_setting",
|
|
168
|
+
title:"System Setting",
|
|
169
|
+
active:true,
|
|
151
170
|
schema: {
|
|
152
171
|
required:["name","value"],
|
|
153
172
|
type: "object",
|
|
@@ -172,8 +191,10 @@ export const default_app = {
|
|
|
172
191
|
},
|
|
173
192
|
{
|
|
174
193
|
name:"system_edge_constraint",
|
|
194
|
+
title:"Edge constraint",
|
|
175
195
|
system_generated:true,
|
|
176
|
-
|
|
196
|
+
active:true,
|
|
197
|
+
version:0.52,
|
|
177
198
|
description: "To define edge constraints for simple directed graph of records.",
|
|
178
199
|
schema:{
|
|
179
200
|
type: "object",
|
|
@@ -224,8 +245,10 @@ export const default_app = {
|
|
|
224
245
|
},
|
|
225
246
|
{
|
|
226
247
|
name:"system_edge",
|
|
248
|
+
title:"Edge in the system graph",
|
|
249
|
+
active:true,
|
|
227
250
|
system_generated:true,
|
|
228
|
-
version:0.
|
|
251
|
+
version:0.52,
|
|
229
252
|
description: "To define edges in the simple directed graph of records.",
|
|
230
253
|
schema:{
|
|
231
254
|
type: "object",
|
|
@@ -251,8 +274,10 @@ export const default_app = {
|
|
|
251
274
|
},
|
|
252
275
|
{
|
|
253
276
|
name:"system_media",
|
|
277
|
+
title:"Media content",
|
|
278
|
+
active:true,
|
|
254
279
|
system_generated:true,
|
|
255
|
-
version:0.
|
|
280
|
+
version:0.62,
|
|
256
281
|
description: "To store images as Base64",
|
|
257
282
|
schema:{
|
|
258
283
|
type: "object",
|
|
@@ -282,7 +307,9 @@ export const default_app = {
|
|
|
282
307
|
{
|
|
283
308
|
name:"system_log",
|
|
284
309
|
system_generated:true,
|
|
285
|
-
|
|
310
|
+
title:"System log",
|
|
311
|
+
active:true,
|
|
312
|
+
version:0.52,
|
|
286
313
|
description: "To define edges in the simple directed graph of records.",
|
|
287
314
|
schema:{
|
|
288
315
|
type: "object",
|
package/test/edges.test.js
CHANGED
|
@@ -180,6 +180,7 @@ describe("Edge insertion test", async () => {
|
|
|
180
180
|
{
|
|
181
181
|
name: "player",
|
|
182
182
|
description:"Player",
|
|
183
|
+
title:"Player",
|
|
183
184
|
schema: {
|
|
184
185
|
additionalProperties:true,
|
|
185
186
|
type:"object",
|
|
@@ -198,6 +199,7 @@ describe("Edge insertion test", async () => {
|
|
|
198
199
|
{
|
|
199
200
|
name: "team",
|
|
200
201
|
description:"Team",
|
|
202
|
+
title:"Team",
|
|
201
203
|
schema: {
|
|
202
204
|
additionalProperties:true,
|
|
203
205
|
type:"object",
|
|
@@ -216,6 +218,7 @@ describe("Edge insertion test", async () => {
|
|
|
216
218
|
{
|
|
217
219
|
name: "match",
|
|
218
220
|
description:"Match",
|
|
221
|
+
title:"Match",
|
|
219
222
|
schema: {
|
|
220
223
|
type:"object",
|
|
221
224
|
additionalProperties:true,
|
package/test/operations.test.js
CHANGED
|
@@ -35,6 +35,7 @@ describe("Schema doc insertion gives errors when", async () => {
|
|
|
35
35
|
{
|
|
36
36
|
name: "",
|
|
37
37
|
description: "",
|
|
38
|
+
title:"",
|
|
38
39
|
schema: {},
|
|
39
40
|
settings: {},
|
|
40
41
|
},
|
|
@@ -45,6 +46,7 @@ describe("Schema doc insertion gives errors when", async () => {
|
|
|
45
46
|
name: "nos",
|
|
46
47
|
description: "",
|
|
47
48
|
schema: {},
|
|
49
|
+
title:"",
|
|
48
50
|
settings: {},
|
|
49
51
|
},
|
|
50
52
|
],
|
|
@@ -54,6 +56,7 @@ describe("Schema doc insertion gives errors when", async () => {
|
|
|
54
56
|
name: "contact",
|
|
55
57
|
description: "",
|
|
56
58
|
schema: {},
|
|
59
|
+
title:"",
|
|
57
60
|
settings: {},
|
|
58
61
|
},
|
|
59
62
|
],
|
|
@@ -62,6 +65,7 @@ describe("Schema doc insertion gives errors when", async () => {
|
|
|
62
65
|
{
|
|
63
66
|
name: "contact",
|
|
64
67
|
description: "This can be left blank",
|
|
68
|
+
title:"something",
|
|
65
69
|
settings: {},
|
|
66
70
|
},
|
|
67
71
|
],
|
|
@@ -370,6 +374,28 @@ describe("Schema doc insertion gives errors when", async () => {
|
|
|
370
374
|
},
|
|
371
375
|
},
|
|
372
376
|
],
|
|
377
|
+
[
|
|
378
|
+
"title missing",
|
|
379
|
+
{
|
|
380
|
+
name: "contact",
|
|
381
|
+
description: "This can be left blank",
|
|
382
|
+
schema: {
|
|
383
|
+
type: "object",
|
|
384
|
+
properties: {
|
|
385
|
+
name: { type: "string" },
|
|
386
|
+
address: { type: "object" },
|
|
387
|
+
secret: { type: "string" },
|
|
388
|
+
},
|
|
389
|
+
additionalProperties: true,
|
|
390
|
+
},
|
|
391
|
+
settings: {
|
|
392
|
+
primary_keys: ["name"],
|
|
393
|
+
non_editable_fields: ["mobile"],
|
|
394
|
+
single_record: false,
|
|
395
|
+
encrypted_fields: ["name"],
|
|
396
|
+
},
|
|
397
|
+
},
|
|
398
|
+
],
|
|
373
399
|
];
|
|
374
400
|
|
|
375
401
|
before(async () => {
|
|
@@ -537,7 +563,9 @@ describe("Doc insertion tests", async () => {
|
|
|
537
563
|
|
|
538
564
|
const test_schema = {
|
|
539
565
|
name:"book",
|
|
566
|
+
active:true,
|
|
540
567
|
description:"Test schema 1",
|
|
568
|
+
title:"Book",
|
|
541
569
|
schema: {
|
|
542
570
|
$schema: "http://json-schema.org/draft-07/schema#",
|
|
543
571
|
type: "object",
|
|
@@ -733,6 +761,8 @@ describe("Doc insertion tests", async () => {
|
|
|
733
761
|
describe("Doc insertion tests with encryption", async () => {
|
|
734
762
|
const test_schema = {
|
|
735
763
|
name:"book",
|
|
764
|
+
title:"Book",
|
|
765
|
+
active:true,
|
|
736
766
|
description:"Test schema 1",
|
|
737
767
|
schema: {
|
|
738
768
|
$schema: "http://json-schema.org/draft-07/schema#",
|
|
@@ -930,6 +960,8 @@ describe("Doc read tests", async () => {
|
|
|
930
960
|
const test_schema = {
|
|
931
961
|
name:"book",
|
|
932
962
|
description:"Test schema 1",
|
|
963
|
+
title:"Book",
|
|
964
|
+
active:true,
|
|
933
965
|
schema: {
|
|
934
966
|
$schema: "http://json-schema.org/draft-07/schema#",
|
|
935
967
|
type: "object",
|
|
@@ -1119,6 +1151,8 @@ describe("Doc update tests", async () => {
|
|
|
1119
1151
|
const test_schema = {
|
|
1120
1152
|
name:"book",
|
|
1121
1153
|
description:"Test schema 1",
|
|
1154
|
+
title:"Book",
|
|
1155
|
+
active:true,
|
|
1122
1156
|
schema: {
|
|
1123
1157
|
$schema: "http://json-schema.org/draft-07/schema#",
|
|
1124
1158
|
type: "object",
|
|
@@ -1361,7 +1395,9 @@ describe("Doc delete tests", async () => {
|
|
|
1361
1395
|
|
|
1362
1396
|
const test_schema = {
|
|
1363
1397
|
name:"book",
|
|
1398
|
+
active:true,
|
|
1364
1399
|
description:"Test schema 1",
|
|
1400
|
+
title:"Book",
|
|
1365
1401
|
schema: {
|
|
1366
1402
|
$schema: "http://json-schema.org/draft-07/schema#",
|
|
1367
1403
|
type: "object",
|
|
@@ -1528,6 +1564,8 @@ describe("Doc search tests", async () => {
|
|
|
1528
1564
|
|
|
1529
1565
|
const test_schema = {
|
|
1530
1566
|
name:"book",
|
|
1567
|
+
title:"Book",
|
|
1568
|
+
active:true,
|
|
1531
1569
|
description:"Test schema 1",
|
|
1532
1570
|
schema: {
|
|
1533
1571
|
$schema: "http://json-schema.org/draft-07/schema#",
|
package/test/pouchdb.js
CHANGED
|
@@ -69,10 +69,12 @@ const pdb = new PouchDB(dbname);
|
|
|
69
69
|
// @TODO ping the database to check connectivity when class is ready to use
|
|
70
70
|
},
|
|
71
71
|
validate_schema: (schema_obj, data_obj)=>{
|
|
72
|
-
const ajv = new Ajv({code: {esm: true}}) // options can be passed, e.g. {allErrors: true}
|
|
72
|
+
const ajv = new Ajv({code: {esm: true},strict:false,useDefaults:true}) // options can be passed, e.g. {allErrors: true}
|
|
73
|
+
const data_copy = {...data_obj}
|
|
73
74
|
const validate = ajv.compile(schema_obj);
|
|
74
|
-
const valid = validate(
|
|
75
|
-
|
|
75
|
+
const valid = validate(data_copy);
|
|
76
|
+
|
|
77
|
+
return {valid,validate,data:data_copy}
|
|
76
78
|
}
|
|
77
79
|
},
|
|
78
80
|
}
|
package/test/test1.js
CHANGED
|
@@ -5,157 +5,116 @@ import {BeanBagDB} from '../src/index.js';
|
|
|
5
5
|
import {text_command} from "../src/plugins/text_command.js"
|
|
6
6
|
|
|
7
7
|
(async()=>{
|
|
8
|
-
|
|
9
|
-
// let schema_docs_invalid = [
|
|
10
|
-
// {
|
|
11
|
-
// name: "",
|
|
12
|
-
// description: "",
|
|
13
|
-
// schema: {},
|
|
14
|
-
// settings: {},
|
|
15
|
-
// }
|
|
16
|
-
// ]
|
|
17
|
-
// let doc_obj = get_pdb_doc("test_database_27","qwertyuiopaqwsde1254")
|
|
18
|
-
// let database1 = new BeanBagDB(doc_obj);
|
|
19
|
-
// // await database.ready()
|
|
20
|
-
// // let a = await database.create("schema",schema_docs_invalid[0])
|
|
21
|
-
|
|
22
|
-
// const test_schema = {
|
|
23
|
-
// name:"book",
|
|
24
|
-
// description:"Test schema 1",
|
|
25
|
-
// schema: {
|
|
26
|
-
// $schema: "http://json-schema.org/draft-07/schema#",
|
|
27
|
-
// type: "object",
|
|
28
|
-
// properties: {
|
|
29
|
-
// title: {
|
|
30
|
-
// type: "string",
|
|
31
|
-
// minLength: 1,
|
|
32
|
-
// description: "The title of the book",
|
|
33
|
-
// },
|
|
34
|
-
// author: {
|
|
35
|
-
// type: "string",
|
|
36
|
-
// minLength: 1,
|
|
37
|
-
// description: "The author of the book",
|
|
38
|
-
// },
|
|
39
|
-
// isbn: {
|
|
40
|
-
// type: "string",
|
|
41
|
-
// pattern: "^(97(8|9))?\\d{9}(\\d|X)$",
|
|
42
|
-
// description: "The ISBN of the book, can be 10 or 13 digits",
|
|
43
|
-
// },
|
|
44
|
-
// publicationYear: {
|
|
45
|
-
// type: "integer",
|
|
46
|
-
// minimum: 1450,
|
|
47
|
-
// maximum: 2024,
|
|
48
|
-
// description:
|
|
49
|
-
// "The year the book was published (between 1450 and 2024)",
|
|
50
|
-
// },
|
|
51
|
-
// genre: {
|
|
52
|
-
// type: "string",
|
|
53
|
-
// enum: [
|
|
54
|
-
// "Fiction",
|
|
55
|
-
// "Non-Fiction",
|
|
56
|
-
// "Science",
|
|
57
|
-
// "History",
|
|
58
|
-
// "Fantasy",
|
|
59
|
-
// "Biography",
|
|
60
|
-
// "Children",
|
|
61
|
-
// "Mystery",
|
|
62
|
-
// "Horror",
|
|
63
|
-
// ],
|
|
64
|
-
// description: "The genre of the book",
|
|
65
|
-
// },
|
|
66
|
-
// language: {
|
|
67
|
-
// type: "string",
|
|
68
|
-
// description: "The language of the book",
|
|
69
|
-
// default: "English",
|
|
70
|
-
// },
|
|
71
|
-
// publisher: {
|
|
72
|
-
// type: "string",
|
|
73
|
-
// description: "The publisher of the book",
|
|
74
|
-
// minLength: 1,
|
|
75
|
-
// },
|
|
76
|
-
// pages: {
|
|
77
|
-
// type: "integer",
|
|
78
|
-
// minimum: 1,
|
|
79
|
-
// description: "The number of pages in the book",
|
|
80
|
-
// },
|
|
81
|
-
// secret: {
|
|
82
|
-
// type: "string",
|
|
83
|
-
// description: "Super secret related to the book",
|
|
84
|
-
// minLength: 1,
|
|
85
|
-
// },
|
|
86
|
-
// },
|
|
87
|
-
// required: ["title", "author", "isbn", "publicationYear", "genre"],
|
|
88
|
-
// additionalProperties: false,
|
|
89
|
-
// },
|
|
90
|
-
// settings : {
|
|
91
|
-
// primary_keys:['title','author'],
|
|
92
|
-
// encrypted_fields:['secret'],
|
|
93
|
-
// non_editable_fields:[],
|
|
94
|
-
// single_record:false
|
|
95
|
-
// }
|
|
96
|
-
// };
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
// await database1.ready(); // Ensure the database is ready before running tests
|
|
100
|
-
|
|
101
|
-
// try {
|
|
102
|
-
// //console.log(test_schema)
|
|
103
|
-
// let a = await database1.create("schema",test_schema)
|
|
104
|
-
|
|
105
|
-
// } catch (error) {
|
|
106
|
-
// console.log("error in before")
|
|
107
|
-
// console.log(error)
|
|
108
|
-
// }
|
|
109
|
-
|
|
110
|
-
// const book1 = {
|
|
111
|
-
// title: "Harry Potter",
|
|
112
|
-
// author: "J.K. Rowling",
|
|
113
|
-
// isbn: "9780439139601",
|
|
114
|
-
// publicationYear: 1999,
|
|
115
|
-
// genre: "Fantasy",
|
|
116
|
-
// publisher: "ABC DEF",
|
|
117
|
-
// secret: "Super secret 1"
|
|
118
|
-
// };
|
|
119
|
-
|
|
120
|
-
// let d
|
|
121
|
-
// try {
|
|
122
|
-
// d = await database1.create("book", book1,{link:"sample1"});
|
|
123
|
-
// console.log(d)
|
|
124
|
-
// let rec = await database1.read({"_id":d._id});
|
|
125
|
-
// console.log(rec)
|
|
126
|
-
|
|
127
|
-
// let e = await database1.create("book", {...book1,title:"Something"},{link:"sample2"});
|
|
128
|
-
// console.log(e)
|
|
129
|
-
|
|
130
|
-
// } catch (error) {
|
|
131
|
-
// console.log(error)
|
|
132
|
-
// throw error
|
|
133
|
-
// }
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
// try {
|
|
137
|
-
// let s = await database1.search({selector:{}})
|
|
138
|
-
// console.log(s.docs.length)
|
|
139
|
-
// //console.log( JSON.stringify(s,null,2))
|
|
140
|
-
// } catch (error) {
|
|
141
|
-
// console.log(error)
|
|
142
|
-
// }
|
|
143
|
-
|
|
144
|
-
|
|
145
8
|
let database; // this is the global db object
|
|
146
9
|
let doc_obj = get_pdb_doc("test_database_40", "qwertyuiopaqwsde1254");
|
|
147
10
|
database = new BeanBagDB(doc_obj);
|
|
148
11
|
await database.ready(); // Ensure the database is ready before running tests
|
|
149
|
-
await database.load_plugin("txtcmd",text_command)
|
|
150
|
-
console.log()
|
|
151
|
-
let command = await database.plugins["txtcmd"].parse_and_run("new/schema")
|
|
152
|
-
console.log(command)
|
|
153
|
-
let command2 = await database.plugins["txtcmd"].parse_and_run("new")
|
|
154
|
-
console.log(command2)
|
|
155
|
-
let command3 = await database.plugins["txtcmd"].parse_and_run("open/link/thunder-kangchenjunga-mango")
|
|
156
|
-
console.log(command3)
|
|
157
|
-
let command4 = await database.plugins["txtcmd"].parse_and_run("tool/info")
|
|
158
|
-
console.log(command4)
|
|
12
|
+
// await database.load_plugin("txtcmd",text_command)
|
|
13
|
+
// console.log()
|
|
14
|
+
// let command = await database.plugins["txtcmd"].parse_and_run("new/schema")
|
|
15
|
+
// console.log(command)
|
|
16
|
+
// let command2 = await database.plugins["txtcmd"].parse_and_run("new")
|
|
17
|
+
// console.log(command2)
|
|
18
|
+
// let command3 = await database.plugins["txtcmd"].parse_and_run("open/link/thunder-kangchenjunga-mango")
|
|
19
|
+
// console.log(command3)
|
|
20
|
+
// let command4 = await database.plugins["txtcmd"].parse_and_run("tool/info")
|
|
21
|
+
// console.log(command4)
|
|
22
|
+
const test_schema = {
|
|
23
|
+
name:"book",
|
|
24
|
+
title:"Book",
|
|
25
|
+
description:"Test schema 1",
|
|
26
|
+
schema: {
|
|
27
|
+
$schema: "http://json-schema.org/draft-07/schema#",
|
|
28
|
+
type: "object",
|
|
29
|
+
properties: {
|
|
30
|
+
title: {
|
|
31
|
+
type: "string",
|
|
32
|
+
minLength: 1,
|
|
33
|
+
description: "The title of the book",
|
|
34
|
+
},
|
|
35
|
+
author: {
|
|
36
|
+
type: "string",
|
|
37
|
+
minLength: 1,
|
|
38
|
+
description: "The author of the book",
|
|
39
|
+
},
|
|
40
|
+
isbn: {
|
|
41
|
+
type: "string",
|
|
42
|
+
pattern: "^(97(8|9))?\\d{9}(\\d|X)$",
|
|
43
|
+
description: "The ISBN of the book, can be 10 or 13 digits",
|
|
44
|
+
},
|
|
45
|
+
publicationYear: {
|
|
46
|
+
type: "integer",
|
|
47
|
+
minimum: 1450,
|
|
48
|
+
maximum: 2024,
|
|
49
|
+
description:
|
|
50
|
+
"The year the book was published (between 1450 and 2024)",
|
|
51
|
+
},
|
|
52
|
+
genre: {
|
|
53
|
+
type: "string",
|
|
54
|
+
enum: [
|
|
55
|
+
"Fiction",
|
|
56
|
+
"Non-Fiction",
|
|
57
|
+
"Science",
|
|
58
|
+
"History",
|
|
59
|
+
"Fantasy",
|
|
60
|
+
"Biography",
|
|
61
|
+
"Children",
|
|
62
|
+
"Mystery",
|
|
63
|
+
"Horror",
|
|
64
|
+
],
|
|
65
|
+
description: "The genre of the book",
|
|
66
|
+
},
|
|
67
|
+
language: {
|
|
68
|
+
type: "string",
|
|
69
|
+
description: "The language of the book",
|
|
70
|
+
default: "English",
|
|
71
|
+
},
|
|
72
|
+
publisher: {
|
|
73
|
+
type: "string",
|
|
74
|
+
description: "The publisher of the book",
|
|
75
|
+
minLength: 1,
|
|
76
|
+
},
|
|
77
|
+
pages: {
|
|
78
|
+
type: "integer",
|
|
79
|
+
minimum: 1,
|
|
80
|
+
description: "The number of pages in the book",
|
|
81
|
+
},
|
|
82
|
+
secret: {
|
|
83
|
+
type: "string",
|
|
84
|
+
description: "Super secret related to the book",
|
|
85
|
+
minLength: 1,
|
|
86
|
+
},
|
|
87
|
+
},
|
|
88
|
+
required: ["title", "author", "isbn", "publicationYear", "genre"],
|
|
89
|
+
additionalProperties: false,
|
|
90
|
+
},
|
|
91
|
+
settings : {
|
|
92
|
+
primary_keys:['title','author'],
|
|
93
|
+
encrypted_fields:['secret'],
|
|
94
|
+
non_editable_fields:[],
|
|
95
|
+
single_record:false
|
|
96
|
+
}
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
try {
|
|
100
|
+
//console.log(test_schema)
|
|
101
|
+
let a = await database.create("schema",test_schema)
|
|
102
|
+
console.log(a)
|
|
103
|
+
} catch (error) {
|
|
104
|
+
console.log("error in before")
|
|
105
|
+
console.log(error)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
// try {
|
|
110
|
+
// //console.log(test_schema)
|
|
111
|
+
// let a = await database.update({link:"7f82b8"},{meta:{tags:["sample"]}})
|
|
112
|
+
// console.log(a)
|
|
113
|
+
// } catch (error) {
|
|
114
|
+
// console.log("error in before")
|
|
115
|
+
// console.log(error)
|
|
116
|
+
// }
|
|
117
|
+
|
|
159
118
|
})()
|
|
160
119
|
|
|
161
120
|
|