beanbagdb 0.5.46 → 0.5.51

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,74 @@
1
+ // this is a pouch db instance of beanbagdb used for testing.
2
+ import Ajv from 'ajv';
3
+ import PouchDB from 'pouchdb';
4
+ import pouchdbFind from 'pouchdb-find';
5
+ PouchDB.plugin(pouchdbFind)
6
+ import { scryptSync, randomBytes, createCipheriv, createDecipheriv } from 'crypto';
7
+
8
+
9
+ export const get_pdb_doc = (dbname,secret)=>{
10
+ const pdb = new PouchDB(dbname);
11
+ const doc_obj = {
12
+ name: dbname,
13
+ db_name:"pouchdb",
14
+ encryption_key: secret,
15
+ api: {
16
+ insert: async (doc) => {
17
+ const result = await pdb.post(doc);
18
+ return result;
19
+ },
20
+ // delete: ()=>{db1.destroy},
21
+ update: async (doc) => {
22
+ const result = await pdb.put(doc);
23
+ return result;
24
+ },
25
+ search: async (query) => {
26
+ const results = await pdb.find(query);
27
+ return results; // of the form {docs:[],...}
28
+ },
29
+ get: async (id) => {
30
+ const data = await pdb.get(id);
31
+ return data;
32
+ },
33
+ delete: async (id) => {
34
+ const doc = await pdb.get(id);
35
+ const resp = await pdb.remove(doc);
36
+ return resp;
37
+ },
38
+ createIndex: async (filter) => {
39
+ const data = await pdb.createIndex(filter);
40
+ return data;
41
+ },
42
+ },
43
+ utils: {
44
+ encrypt: (text, encryptionKey) => {
45
+ const key = scryptSync(encryptionKey, "salt", 32); // Derive a 256-bit key
46
+ const iv = randomBytes(16); // Initialization vector
47
+ const cipher = createCipheriv("aes-256-cbc", key, iv);
48
+ let encrypted = cipher.update(text, "utf8", "hex");
49
+ encrypted += cipher.final("hex");
50
+ return iv.toString("hex") + ":" + encrypted; // Prepend the IV for later use
51
+ },
52
+ decrypt: (encryptedText, encryptionKey) => {
53
+ 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"));
57
+ const decipher = createDecipheriv("aes-256-cbc", key, iv);
58
+ let decrypted = decipher.update(encrypted, "hex", "utf8");
59
+ decrypted += decipher.final("utf8");
60
+ return decrypted;
61
+ },
62
+ ping: () => {
63
+ // @TODO ping the database to check connectivity when class is ready to use
64
+ },
65
+ validate_schema: (schema_obj, data_obj)=>{
66
+ const ajv = new Ajv({code: {esm: true}}) // options can be passed, e.g. {allErrors: true}
67
+ const validate = ajv.compile(schema_obj);
68
+ const valid = validate(data_obj);
69
+ return {valid,validate}
70
+ }
71
+ },
72
+ }
73
+ return doc_obj
74
+ }
package/test/test1.js CHANGED
@@ -154,4 +154,33 @@
154
154
  // // let db = new cbbdb(process.env.cdburl, process.env.cdbname, "sample_key");
155
155
  // // await db.ready();
156
156
  // // db.load_plugin("sample",pl1)
157
- // // })();
157
+ // // })();
158
+
159
+ import { get_pdb_doc } from './pouchdb.js';
160
+ import { throws, strictEqual } from "assert";
161
+ import {BeanBagDB} from '../src/index.js';
162
+
163
+ (async()=>{
164
+
165
+ let schema_docs_invalid = [
166
+ {
167
+ name: "contact",
168
+ description: "This can be left blank",
169
+ schema: {
170
+ "type":"object",
171
+ "properties":{"name":{"type":"string"},"address":{type:"object"},"secret":{"type":"string"}},
172
+ "additionalProperties":true
173
+ },
174
+ settings: {
175
+ primary_keys:["name"],
176
+ non_editable_fields:["address"],
177
+ single_record:false,
178
+ encrypted_fields:["name"]
179
+ },
180
+ }
181
+ ]
182
+ let doc_obj = get_pdb_doc("test_database_26","qwertyuiopaqwsde1254")
183
+ let database = new BeanBagDB(doc_obj);
184
+ await database.ready()
185
+ let a = await database.insert("schema",schema_docs_invalid[0])
186
+ })()