beanbagdb 0.0.1 → 0.0.5

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,38 @@
1
+ name: Release npm Package
2
+
3
+ # Trigger the workflow on push to the "main" branch
4
+ on:
5
+ push:
6
+ branches:
7
+ - main
8
+
9
+ jobs:
10
+ build:
11
+ runs-on: ubuntu-latest
12
+
13
+ steps:
14
+ # Step 1: Check out the code from the repository
15
+ - name: Checkout code
16
+ uses: actions/checkout@v3
17
+
18
+ # Step 2: Set up Node.js environment
19
+ - name: Setup Node.js
20
+ uses: actions/setup-node@v3
21
+ with:
22
+ node-version: '20.17.0' # Specify the Node.js version you want to use
23
+ cache: 'npm' # Cache npm modules for faster builds
24
+
25
+ # Step 3: Install dependencies
26
+ - name: Install dependencies
27
+ run: npm install
28
+
29
+ # Step 4: Run tests
30
+ - name: Run tests
31
+ run: npm test
32
+
33
+ # Step 5: Publish to npm if tests pass
34
+ - name: Publish to npm
35
+ run: npm publish
36
+ env:
37
+ NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}
38
+
package/README.md CHANGED
@@ -7,19 +7,131 @@
7
7
  v 0.0.0
8
8
  </div>
9
9
 
10
- ## Intro
10
+ ## The Database architecture
11
+ A BeanbagDB is a collection of JSON documents. In a NoSQL database, documents don't need to follow a specific schema. Typically, the developer defines a schema based on the specific problem the app is intended to solve.
11
12
 
13
+ While, BeanBagDB allows users to create their own schemas for documents, all document internally share a consistent structure that accommodates the user defined schema.
12
14
 
13
- ## Installation
15
+ Each document in the database follows this structure :
16
+ ```
17
+ {
18
+ _id, _rev
19
+ data : {
20
+ user data
21
+ },
22
+ schema: "name_of_the_schema",
23
+ meta : {},
24
+ }
25
+ ```
14
26
 
15
- ### Option 1 : To use it with CouchDB
27
+ Here, the `data` object contains user's information, while the `schema` defines it's associated structure. The `meta` field holds metadata such as creation and last updated on date and user defined tags.
16
28
 
17
- ### Option 2 : To use it with PouchDB
29
+ Even schema documents follow this structure.
18
30
 
19
- ### Option 3 : Web app
31
+ ### Examples :
20
32
 
21
- ## The Database architecture
33
+ A system defined schema :
34
+ ```
35
+ {
36
+ schema:"schema"
37
+ data : {
38
+ name: "system_tags",
39
+ .....
40
+ },
41
+ ...
42
+ }
43
+ ```
44
+ (In this example, the schema of this doc is `schema` because it stores the schema document, which will be use to validate documents that follows this schema)
45
+
46
+ The document adhering to this schema might look like :
47
+ ```
48
+ {
49
+ schema:"system_tags",
50
+ data:{
51
+ tags:["tag1","tag2"]
52
+ }
53
+ ....
54
+ }
55
+ ```
56
+ User Defined schemas :
57
+ ```
58
+ {
59
+ "schema":"schema",
60
+ "data":{
61
+ "name":"contacts",
62
+ ...
63
+ }
64
+ }
65
+ ```
66
+ And a sample document based on this user defined schema might look like :
67
+ ```
68
+ {
69
+ schema:"contacts",
70
+ "data":{
71
+ "name":"Human 1",
72
+ "email":"human@on.earth"
73
+ }
74
+ }
75
+ ```
76
+
77
+ ### System defined schemas
78
+ To ensure the system functions smoothly, each DB is initialized with a set of system defined schemas and seed documents. These are automatically added when the database is first created. These system defined are typically named with the prefix `system_`.
79
+
80
+ List of system schemas :
81
+ - `logs` : to log system and user actions
82
+ - `settings` : System level settings (`name`,`value`,`json` for more details)
83
+ - `keys` : user level settings , these are encrypted and stored in the DB
84
+ - `secrets` : encrypted text (TODO)
85
+ - `relations` : To create a network of relations (TODO)
86
+ - `index` : (TODO)
87
+ - `scripts` : (TODO)
88
+
89
+ ## The BeanBagDB class
90
+
91
+ The `BeanBagDB` class contains the the logical part of how to interact with a local database. However, it does not itself makes any changes in the database. To make it (somewhat) compatible with multiple databases (e.g. CouchDB on the server, PouchDB in the browser), the class takes an db_instance object where the actual CRUD actions on the database can be performed.
92
+
93
+ To add support for a database, we extend the `BeanBagDB` class.
94
+
95
+ ### API
96
+ - `initialize_db()`
97
+ - `log(stuff={whatever})`
98
+ - `get_setting_doc(setting_name)`
99
+ - `get_schema_doc()`
100
+ - `validate_data()`
101
+ - `insert_pre_check()`
102
+ - `insert()`
103
+ - `get()`
104
+ - `load_doc()`
105
+ - `update_data()`
106
+ - ...
107
+
108
+
109
+ ## JSON Schema (in progress)
110
+ [Source](https://json-schema.org/) and [another](https://www.learnjsonschema.com/2020-12/)
111
+
112
+ ### Basic schema structure
113
+ ```
114
+ {
115
+ "name":"",
116
+ "description":"",
117
+ "properties":{
118
+
119
+ },
120
+ "settings":{
22
121
 
23
- ## API
122
+ }
123
+ }
124
+ ```
125
+ #### Properties:
126
+ - Settings :
127
+ - `additionalProperties:true`
128
+ - `required:[]`
129
+ -
130
+ - Defining custom fields :
131
+ - `type:""` Valid values: ``
24
132
 
25
133
 
134
+ #### Settings :
135
+ - `primary_key:[]`
136
+ - `editable_fields:[]`
137
+ - `single_record : true`
package/package.json CHANGED
@@ -1,30 +1,38 @@
1
1
  {
2
2
  "name": "beanbagdb",
3
- "version": "0.0.1",
4
- "description": "The core JS library to work with schema with NoSQL DBs",
3
+ "version": "0.0.5",
4
+ "description": "A JS library to introduce a schema layer to a No-SQL local database",
5
5
  "main": "index.js",
6
6
  "scripts": {
7
- "test": "echo \"Error: no test specified\" && exit 1"
7
+ "test": "mocha"
8
8
  },
9
9
  "repository": {
10
10
  "type": "git",
11
- "url": "git+https://github.com/shubhvjain/infodb-core.git"
11
+ "url": "git+https://github.com/shubhvjain/beanbagdb.git"
12
12
  },
13
13
  "keywords": [
14
14
  "couchdb",
15
15
  "pouchdb",
16
16
  "local",
17
- "database"
17
+ "database",
18
+ "schema"
18
19
  ],
19
20
  "author": "svj",
20
21
  "license": "ISC",
21
22
  "bugs": {
22
- "url": "https://github.com/shubhvjain/infodb-core/issues"
23
+ "url": "https://github.com/shubhvjain/beanbagdb/issues"
23
24
  },
24
- "homepage": "https://github.com/shubhvjain/infodb-core#readme",
25
+ "homepage": "https://github.com/shubhvjain/beanbagdb#readme",
25
26
  "dependencies": {
26
27
  "ajv": "^8.12.0",
27
28
  "dotenv": "^16.4.5",
28
- "nano": "^10.1.3"
29
+ "nano": "^10.1.3",
30
+ "pouchdb": "^9.0.0",
31
+ "pouchdb-adapter-memory": "^9.0.0",
32
+ "pouchdb-find": "^9.0.0"
33
+ },
34
+ "devDependencies": {
35
+ "chai": "^5.1.1",
36
+ "mocha": "^10.7.3"
29
37
  }
30
38
  }
package/src/couchdb.js ADDED
@@ -0,0 +1,63 @@
1
+ const crypto = require('crypto');
2
+ const SDB = require("./index.js")
3
+
4
+ class BeanBagDB_CouchDB extends SDB {
5
+ constructor(db_url,db_name,encryption_key){
6
+ const cdb = require("nano")(db_url)
7
+ const doc_obj = {
8
+ name: db_name,
9
+ encryption_key: encryption_key,
10
+ api:{
11
+ insert: async (doc)=>{
12
+ const result = await cdb.insert(doc)
13
+ return result
14
+ },
15
+ // delete: ()=>{db1.destroy},
16
+ update: async (doc)=>{
17
+ const result = await cdb.insert(doc)
18
+ return result
19
+ },
20
+ search: async (query)=>{
21
+ const results = await cdb.find(query)
22
+ return results // of the form {docs:[],...}
23
+ },
24
+ get: async (id)=>{
25
+ const data = await cdb.get(id)
26
+ return data
27
+ },
28
+ createIndex: async (filter)=>{
29
+ const data = await cdb.createIndex(filter)
30
+ return data
31
+ },
32
+ delete: async (doc_id)=>{
33
+ const data = await cdb.get(id)
34
+ await cdb.destroy(data._id,data._rev)
35
+ }
36
+ },
37
+ utils:{
38
+ encrypt: (text,encryptionKey)=>{
39
+ const key = crypto.scryptSync(encryptionKey, 'salt', 32); // Derive a 256-bit key
40
+ const iv = crypto.randomBytes(16); // Initialization vector
41
+ const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
42
+ let encrypted = cipher.update(text, 'utf8', 'hex');
43
+ encrypted += cipher.final('hex');
44
+ return iv.toString('hex') + ':' + encrypted; // Prepend the IV for later use
45
+ },
46
+ decrypt : (encryptedText, encryptionKey)=>{
47
+ const key = crypto.scryptSync(encryptionKey, 'salt', 32); // Derive a 256-bit key
48
+ const [iv, encrypted] = encryptedText.split(':').map(part => Buffer.from(part, 'hex'));
49
+ const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
50
+ let decrypted = decipher.update(encrypted, 'hex', 'utf8');
51
+ decrypted += decipher.final('utf8');
52
+ return decrypted;
53
+ },
54
+ ping : ()=>{
55
+ // @TODO ping the database to check connectivity when class is ready to use
56
+ }
57
+ }
58
+ }
59
+ super(doc_obj)
60
+ }
61
+ }
62
+
63
+ module.exports = BeanBagDB_CouchDB