beanbagdb 0.0.5 → 0.5.2
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/.github/workflows/release.yml +9 -6
- package/dist/beanbagdb.cjs.js +153242 -0
- package/dist/beanbagdb.cjs.js.map +1 -0
- package/dist/beanbagdb.esm.js +153237 -0
- package/dist/beanbagdb.esm.js.map +1 -0
- package/package.json +14 -4
- package/rollup.config.js +47 -0
- package/src/beanbagdb.js +580 -0
- package/src/couchdb.js +11 -10
- package/src/index.js +4 -577
- package/src/pouchdb.js +13 -11
- package/src/system_schema.js +4 -8
- package/src/utils.js +40 -0
- package/test/init.test.js +19 -19
- package/test/test1.js +26 -9
- package/test/helper.js +0 -12
package/src/system_schema.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const schema_schema = {
|
|
1
|
+
export const schema_schema = {
|
|
2
2
|
name: "schema",
|
|
3
3
|
description:"Meta-schema or schema for defining other schemas",
|
|
4
4
|
system_generated:true,
|
|
@@ -72,7 +72,7 @@ const schema_schema = {
|
|
|
72
72
|
},
|
|
73
73
|
};
|
|
74
74
|
|
|
75
|
-
const system_schemas = {
|
|
75
|
+
export const system_schemas = {
|
|
76
76
|
logs: {
|
|
77
77
|
system_generated:true,
|
|
78
78
|
description:"Schema for the log doc. Single log doc for the whole DB to log stuff about the DB",
|
|
@@ -167,7 +167,7 @@ const system_schemas = {
|
|
|
167
167
|
};
|
|
168
168
|
|
|
169
169
|
// this is not stored in the DB. only for validating the metadata during doc update
|
|
170
|
-
const editable_metadata_schema = {
|
|
170
|
+
export const editable_metadata_schema = {
|
|
171
171
|
additionalProperties: false,
|
|
172
172
|
properties:{
|
|
173
173
|
tags:{
|
|
@@ -177,8 +177,4 @@ const editable_metadata_schema = {
|
|
|
177
177
|
maxItems: 40,
|
|
178
178
|
}
|
|
179
179
|
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
module.exports.system_schemas = system_schemas;
|
|
183
|
-
module.exports.schema_schema = schema_schema;
|
|
184
|
-
module.exports.editable_metadata_schema = editable_metadata_schema;
|
|
180
|
+
}
|
package/src/utils.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import { readFile } from 'fs/promises';
|
|
2
|
+
import { fileURLToPath } from 'url';
|
|
3
|
+
import { dirname, join } from 'path';
|
|
4
|
+
|
|
5
|
+
// Get the current directory
|
|
6
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
7
|
+
const __dirname = dirname(__filename);
|
|
8
|
+
|
|
9
|
+
// Adjust path to point to the correct location of package.json (move up from /src)
|
|
10
|
+
const packageJsonPath = join(__dirname, '../package.json'); // Adjust to point to the correct folder
|
|
11
|
+
|
|
12
|
+
function isNode() {
|
|
13
|
+
return typeof process !== 'undefined' && process.versions != null && process.versions.node != null;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
// Function to read package.json and get the version
|
|
18
|
+
async function getPackageVersionNode() {
|
|
19
|
+
try {
|
|
20
|
+
const data = await readFile(packageJsonPath, 'utf-8');
|
|
21
|
+
const packageJson = JSON.parse(data);
|
|
22
|
+
return packageJson.version;
|
|
23
|
+
} catch (error) {
|
|
24
|
+
console.error('Error reading package.json:', error);
|
|
25
|
+
throw error;
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
export async function getPackageVersion() {
|
|
32
|
+
if (isNode()) {
|
|
33
|
+
return await getPackageVersionNode(); // Node.js environment
|
|
34
|
+
} else {
|
|
35
|
+
return process.env.PACKAGE_VERSION; // Browser environment
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
// getPackageVersion().then(version => console.log('Package version:', version));
|
|
40
|
+
|
package/test/init.test.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
// to test initialization of the BeanBagDB class. using in memory pouch db for testing to avoid additional setup.
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
import PouchDB from 'pouchdb';
|
|
3
|
+
import pouchdbFind from 'pouchdb-find';
|
|
4
|
+
PouchDB.plugin(pouchdbFind)
|
|
5
|
+
import { scryptSync, randomBytes, createCipheriv, createDecipheriv } from 'crypto';
|
|
5
6
|
const db_name = "test_database_24"
|
|
6
7
|
const pdb = new PouchDB(db_name);
|
|
7
8
|
const doc_obj = {
|
|
@@ -37,19 +38,19 @@ const doc_obj = {
|
|
|
37
38
|
},
|
|
38
39
|
utils: {
|
|
39
40
|
encrypt: (text, encryptionKey) => {
|
|
40
|
-
const key =
|
|
41
|
-
const iv =
|
|
42
|
-
const cipher =
|
|
41
|
+
const key = scryptSync(encryptionKey, "salt", 32); // Derive a 256-bit key
|
|
42
|
+
const iv = randomBytes(16); // Initialization vector
|
|
43
|
+
const cipher = createCipheriv("aes-256-cbc", key, iv);
|
|
43
44
|
let encrypted = cipher.update(text, "utf8", "hex");
|
|
44
45
|
encrypted += cipher.final("hex");
|
|
45
46
|
return iv.toString("hex") + ":" + encrypted; // Prepend the IV for later use
|
|
46
47
|
},
|
|
47
48
|
decrypt: (encryptedText, encryptionKey) => {
|
|
48
|
-
const key =
|
|
49
|
+
const key = scryptSync(encryptionKey, "salt", 32); // Derive a 256-bit key
|
|
49
50
|
const [iv, encrypted] = encryptedText
|
|
50
51
|
.split(":")
|
|
51
52
|
.map((part) => Buffer.from(part, "hex"));
|
|
52
|
-
const decipher =
|
|
53
|
+
const decipher = createDecipheriv("aes-256-cbc", key, iv);
|
|
53
54
|
let decrypted = decipher.update(encrypted, "hex", "utf8");
|
|
54
55
|
decrypted += decipher.final("utf8");
|
|
55
56
|
return decrypted;
|
|
@@ -60,12 +61,11 @@ const doc_obj = {
|
|
|
60
61
|
},
|
|
61
62
|
}
|
|
62
63
|
|
|
63
|
-
the_correct_object = {};
|
|
64
|
+
let the_correct_object = {};
|
|
64
65
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
const BeanBagDB = require("../src/index");
|
|
66
|
+
import { throws, strictEqual } from "assert";
|
|
68
67
|
|
|
68
|
+
import BeanBagDB from '../src/beanbagdb.js';
|
|
69
69
|
/**
|
|
70
70
|
* Initial setup
|
|
71
71
|
* database is the global var where the beanbag class is initialized
|
|
@@ -150,7 +150,7 @@ let database;
|
|
|
150
150
|
|
|
151
151
|
describe("Tests initialization of the BeanBagDB class without no init object", async () => {
|
|
152
152
|
it("Throws error", () => {
|
|
153
|
-
|
|
153
|
+
throws(() => {
|
|
154
154
|
database = new BeanBagDB();
|
|
155
155
|
}, Error);
|
|
156
156
|
});
|
|
@@ -161,18 +161,18 @@ describe("Tests initialization of the BeanBagDB class with incomplete init objec
|
|
|
161
161
|
* Proper object : {name,encryption_key,api:{insert,updated,delete,search,get,createIndex},utils:{encrypt,decrypt,ping}}
|
|
162
162
|
*/
|
|
163
163
|
it("Blank object throw error", () => {
|
|
164
|
-
|
|
164
|
+
throws(() => {
|
|
165
165
|
database = new BeanBagDB({});
|
|
166
166
|
}, Error);
|
|
167
167
|
});
|
|
168
168
|
it("some invalid field throws error", () => {
|
|
169
|
-
|
|
169
|
+
throws(() => {
|
|
170
170
|
database = new BeanBagDB({ dbname: "sample" });
|
|
171
171
|
}, Error);
|
|
172
172
|
});
|
|
173
173
|
test_set1.forEach((item) => {
|
|
174
174
|
it(`only ${item[0]} throws error`, () => {
|
|
175
|
-
|
|
175
|
+
throws(() => {
|
|
176
176
|
let key = item[0];
|
|
177
177
|
database = new BeanBagDB({ key: item[1] });
|
|
178
178
|
}, Error);
|
|
@@ -180,7 +180,7 @@ describe("Tests initialization of the BeanBagDB class with incomplete init objec
|
|
|
180
180
|
});
|
|
181
181
|
test_set_api.forEach((item) => {
|
|
182
182
|
it(`${item[0]} throws error`, () => {
|
|
183
|
-
|
|
183
|
+
throws(() => {
|
|
184
184
|
let obj = { ...item[1] };
|
|
185
185
|
database = new BeanBagDB(obj);
|
|
186
186
|
}, Error);
|
|
@@ -190,7 +190,7 @@ describe("Tests initialization of the BeanBagDB class with incomplete init objec
|
|
|
190
190
|
|
|
191
191
|
describe("Successful database class init", async () => {
|
|
192
192
|
it("global database variable not yet initialized", () => {
|
|
193
|
-
|
|
193
|
+
strictEqual(
|
|
194
194
|
database instanceof BeanBagDB,
|
|
195
195
|
false,
|
|
196
196
|
"The variable is not yet initialized"
|
|
@@ -199,7 +199,7 @@ describe("Successful database class init", async () => {
|
|
|
199
199
|
|
|
200
200
|
it("DB init successful", () => {
|
|
201
201
|
database = new BeanBagDB(doc_obj);
|
|
202
|
-
|
|
202
|
+
strictEqual(
|
|
203
203
|
database instanceof BeanBagDB,
|
|
204
204
|
true,
|
|
205
205
|
"The variable is initialized successfully"
|
package/test/test1.js
CHANGED
|
@@ -1,7 +1,24 @@
|
|
|
1
|
-
require("dotenv").config();
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const
|
|
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
|
+
|
|
5
22
|
async function main1() {
|
|
6
23
|
let db = new cbbdb(process.env.cdburl, process.env.cdbname, "sample_key");
|
|
7
24
|
|
|
@@ -133,8 +150,8 @@ async function main6(){
|
|
|
133
150
|
// main6().then(() => {console.log("Bye.");}).catch();
|
|
134
151
|
|
|
135
152
|
|
|
136
|
-
(async ()=>{
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
})();
|
|
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
|
+
// })();
|