beanbagdb 0.5.52 → 0.5.54

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,55 @@
1
+ // to test database operations. assuming the class is initialized successfully
2
+ // to test initialization of the BeanBagDB class
3
+ import { get_pdb_doc } from "./pouchdb.js";
4
+ import assert, { throws, strictEqual, rejects } from "assert";
5
+ import { BeanBagDB, DocCreationError, EncryptionError, ValidationError,DocNotFoundError, DocUpdateError } from "../src/index.js";
6
+
7
+ import {text_command} from "../src/plugins/text_command.js"
8
+
9
+ import * as chai from 'chai';
10
+ import chaiAsPromised from 'chai-as-promised';
11
+
12
+ chai.use(chaiAsPromised);
13
+
14
+ // Then either:
15
+ const expect = chai.expect;
16
+
17
+ let database; // this is the global db object
18
+
19
+
20
+ describe("Testing plugin load", async () => {
21
+
22
+
23
+ before(async () => {
24
+ let doc_obj = get_pdb_doc("test_database_30", "qwertyuiopaqwsde1254");
25
+ database = new BeanBagDB(doc_obj);
26
+ await database.ready(); // Ensure the database is ready before running tests
27
+ console.log("Ready for more tests...");
28
+ });
29
+
30
+
31
+
32
+ it('successfully loads the plugin', async () => {
33
+ try {
34
+ await database.load_plugin("txtcmd",text_command)
35
+ chai.expect(database.plugins).to.not.be.empty
36
+ } catch (error) {
37
+ console.log(error)
38
+ throw error
39
+ }
40
+ })
41
+
42
+ it('successfully runs the loaded the plugin method', async () => {
43
+ try {
44
+
45
+ let command = await database["txtcmd"].parse("new/system_keys")
46
+ console.log(command)
47
+ assert (1 ==2)
48
+ } catch (error) {
49
+ console.log(error)
50
+ throw error
51
+ }
52
+ })
53
+ });
54
+
55
+
package/test/pouchdb.js CHANGED
@@ -41,19 +41,25 @@ const pdb = new PouchDB(dbname);
41
41
  },
42
42
  },
43
43
  utils: {
44
- encrypt: (text, encryptionKey) => {
44
+ encrypt: async (text, encryptionKey) => {
45
+ //console.log(encryptionKey)
45
46
  const key = scryptSync(encryptionKey, "salt", 32); // Derive a 256-bit key
46
47
  const iv = randomBytes(16); // Initialization vector
47
48
  const cipher = createCipheriv("aes-256-cbc", key, iv);
48
49
  let encrypted = cipher.update(text, "utf8", "hex");
49
50
  encrypted += cipher.final("hex");
50
- return iv.toString("hex") + ":" + encrypted; // Prepend the IV for later use
51
+ //console.log("IV:", iv.toString("hex"));
52
+ //console.log("Encrypted:", encrypted);
53
+ return iv.toString("hex") + ":" + encrypted; // Prepend the IV for decryption
51
54
  },
52
- decrypt: (encryptedText, encryptionKey) => {
55
+ decrypt: async (encryptedText, encryptionKey) => {
56
+ //console.log(encryptedText, encryptionKey)
53
57
  const key = scryptSync(encryptionKey, "salt", 32); // Derive a 256-bit key
54
- const [iv, encrypted] = encryptedText
55
- .split(":")
56
- .map((part) => Buffer.from(part, "hex"));
58
+ const [ivHex, encryptedHex] = encryptedText.split(":");
59
+ const iv = Buffer.from(ivHex, "hex");
60
+ const encrypted = Buffer.from(encryptedHex, "hex");
61
+ //console.log("IV:", iv.toString("hex"));
62
+ //console.log("Encrypted:", encrypted.toString("hex"));
57
63
  const decipher = createDecipheriv("aes-256-cbc", key, iv);
58
64
  let decrypted = decipher.update(encrypted, "hex", "utf8");
59
65
  decrypted += decipher.final("utf8");
package/test/test1.js CHANGED
@@ -1,177 +1,162 @@
1
- // // require("dotenv").config();
2
- // import 'dotenv/config'
3
- // // import * as cbbdb from "../src/couchdb.js"
4
- // // const cbbdb = require("../src/couchdb.js");
5
- // // const pbbdb = require("../src/pouchdb.js");
6
- // import BeanBagDB_PouchDB from "../src/pouchdb.js";
7
- // import BeanBagDB_CouchDB from "../src/couchdb.js";
8
- // // const pl1 = require("./helper.js")
9
-
10
- // (async()=>{
11
- // // console.log(process.env.cdburl)
12
- // let db = new BeanBagDB_CouchDB(process.env.cdburl, process.env.cdbname, "sample_key");
13
- // try {
14
- // await db.ready();
15
- // await db.initialize_db();
16
- // await db.update_indexes()
17
- // } catch (error) {
18
- // console.log(error);
19
- // }
20
- // })()
21
1
 
22
- // async function main1() {
23
- // let db = new cbbdb(process.env.cdburl, process.env.cdbname, "sample_key");
24
-
25
- // try {
26
- // await db.ready();
27
- // await db.initialize_db();
28
- // await db.update_indexes()
29
- // } catch (error) {
30
- // console.log(error);
31
- // }
32
- // }
33
-
34
- // async function main2() {
35
- // let db = new cbbdb(process.env.cdburl, process.env.cdbname, "sample_key");
36
- // await db.ready();
37
- // // await db.update_indexes()
38
- // sample_setting_docs = [
39
- // {
40
- // name: "sample1",
41
- // value: "sample thing here",
42
- // },
43
- // {
44
- // name: "no_value",
45
- // },
46
- // {
47
- // value: "incorrect name",
48
- // },
49
- // {
50
- // name: "sample1",
51
- // value: "sample thing here",
52
- // },
53
- // {
54
- // name: "sample1",
55
- // value: "primary key check",
56
- // },
57
- // {
58
- // name: "Sample2",
59
- // value: "normal insert",
60
- // },
61
- // {
62
- // value: "No name provided",
63
- // },
64
- // ];
65
- // let t = sample_setting_docs.length
66
- // for (let i = 0; i < t; i++) {
67
- // try {
68
- // let newid = await db.insert("system_settings",sample_setting_docs[i])
69
- // console.log(newid)
70
- // } catch (error) {
71
- // console.log("Error "+i)
72
- // console.error(error)
73
- // continue
74
- // }
75
- // }
76
- // }
77
-
78
- // async function main3() {
79
- // let db = new cbbdb(process.env.cdburl, process.env.cdbname, "sample_key");
80
- // await db.ready();
81
- // // await db.update_indexes()
82
- // sample_setting_docs = [
83
- // {
84
- // name: "sample1",
85
- // value: "sample thing here",
86
- // },
87
- // {
88
- // name: "sample2",
89
- // value: "sample thing here again",
90
- // },
91
-
92
- // {
93
- // name: "sample1",
94
- // value: "sample thing here again",
95
- // },
96
- // ];
97
- // let t = sample_setting_docs.length
98
- // for (let i = 0; i < t; i++) {
99
- // try {
100
- // let newid = await db.insert("system_keys",sample_setting_docs[i])
101
- // console.log(newid)
102
- // } catch (error) {
103
- // console.log("Error "+i)
104
- // console.error(error)
105
- // continue
106
- // }
107
- // }
108
- // }
2
+ import { get_pdb_doc } from './pouchdb.js';
3
+ import { throws, strictEqual } from "assert";
4
+ import {BeanBagDB} from '../src/index.js';
5
+ import {text_command} from "../src/plugins/text_command.js"
109
6
 
7
+ (async()=>{
110
8
 
111
- // async function main4(){
112
- // let db = new cbbdb(process.env.cdburl, process.env.cdbname, "sample_key");
113
- // await db.ready();
114
- // let doc1 = await db.get("e94b5eebe6b3c6dab8e2508d5908717c")
115
- // console.log(doc1)
116
- // let doc2 = await db.get_doc("system_keys",{"name":"sample2"})
117
- // console.log(doc2)
118
- // let doc3 = await db.get_doc("system_logs")
119
- // console.log(doc3)
120
- // }
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
+ // };
121
97
 
122
- // async function main5(){
123
- // let db = new cbbdb(process.env.cdburl, process.env.cdbname, "sample_key");
124
- // await db.ready();
125
- // let id = "e94b5eebe6b3c6dab8e2508d5908717c"
126
- // let rec_1 = await db.get(id)
127
- // let rec1 = rec_1.doc
128
- // console.log(rec1)
129
- // let rec1u = await db.update(id,"",{data:{"value":"secret key updated"},meta:{tags:["testing1","testing2","money"]}})
130
- // console.log(rec1u)
131
- // let r1 = await db.get(id)
132
- // console.log(r1)
133
- // }
134
98
 
135
- // async function main6(){
136
- // let db = new pbbdb(process.env.cdburl, process.env.cdbname, "sample_key");
137
- // await db.ready()
138
- // //await db.initialize_db()
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)
139
104
 
140
- // }
105
+ // } catch (error) {
106
+ // console.log("error in before")
107
+ // console.log(error)
108
+ // }
141
109
 
142
- // // main1().then(() => {console.log("Bye.");}).catch();
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)
143
126
 
144
- // // main3().then(() => {console.log("Bye.");}).catch();
127
+ // let e = await database1.create("book", {...book1,title:"Something"},{link:"sample2"});
128
+ // console.log(e)
145
129
 
146
- // // main4().then(() => {console.log("Bye.");}).catch();
130
+ // } catch (error) {
131
+ // console.log(error)
132
+ // throw error
133
+ // }
147
134
 
148
- // // main5().then(() => {console.log("Bye.");}).catch();
149
135
 
150
- // // main6().then(() => {console.log("Bye.");}).catch();
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
+ // }
151
143
 
152
144
 
153
- // // (async ()=>{
154
- // // let db = new cbbdb(process.env.cdburl, process.env.cdbname, "sample_key");
155
- // // await db.ready();
156
- // // db.load_plugin("sample",pl1)
157
- // // })();
145
+ let database; // this is the global db object
146
+ let doc_obj = get_pdb_doc("test_database_40", "qwertyuiopaqwsde1254");
147
+ database = new BeanBagDB(doc_obj);
148
+ 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)
159
+ })()
158
160
 
159
- import { get_pdb_doc } from './pouchdb.js';
160
- import { throws, strictEqual } from "assert";
161
- import {BeanBagDB} from '../src/index.js';
162
161
 
163
- (async()=>{
164
162
 
165
- let schema_docs_invalid = [
166
- {
167
- name: "",
168
- description: "",
169
- schema: {},
170
- settings: {},
171
- }
172
- ]
173
- let doc_obj = get_pdb_doc("test_database_26","qwertyuiopaqwsde1254")
174
- let database = new BeanBagDB(doc_obj);
175
- await database.ready()
176
- let a = await database.create("schema",schema_docs_invalid[0])
177
- })()