Skip to content

Commit

Permalink
Merge pull request #14 from shubhvjain/main
Browse files Browse the repository at this point in the history
new version
  • Loading branch information
shubhvjain authored Sep 27, 2024
2 parents f13b4eb + 20dfebb commit a8df8dd
Show file tree
Hide file tree
Showing 9 changed files with 689 additions and 245 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "beanbagdb",
"version": "0.5.50",
"version": "0.5.51",
"description": "A JS library to introduce a schema layer to a No-SQL local database",
"main": "src/index.js",
"module": "src/index.js",
Expand Down Expand Up @@ -34,5 +34,8 @@
"nano": "^10.1.4",
"pouchdb": "^9.0.0",
"pouchdb-find": "^9.0.0"
},
"mocha": {
"spec": "test/**/*.test.js"
}
}
459 changes: 306 additions & 153 deletions src/index.js

Large diffs are not rendered by default.

67 changes: 30 additions & 37 deletions src/system_schema.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
export const schema_schema = {
name: "schema",
description:"Meta-schema or schema for defining other schemas",
description:"Meta-schema or the schema for defining other schemas",
system_generated:true,
version:0.5,
schema: {
type: "object",
additionalProperties: false,
Expand All @@ -10,6 +11,12 @@ export const schema_schema = {
type:"boolean",
default:false
},
version: {
type: "number",
minimum: 0,
default: 1,
description:"This is an optional field.To be used primarily for system schemas"
},
name: {
type: "string",
minLength: 5,
Expand Down Expand Up @@ -41,14 +48,17 @@ export const schema_schema = {
type: "string",
},
maxItems: 10,
description:"Fields that makes each document unique in the schema.Leave it blank if you do not need it. You can still be able to distinguish documents using the link field and the document id."
},
editable_fields: {
non_editable_fields: {
type: "array",
default: [],
items: {
type: "string",
},
maxItems: 50,
minItems:0,
description:"The list of fields whose values are added when the document is created but cannot be edited later in future."
},
encrypted_fields: {
type: "array",
Expand All @@ -57,6 +67,7 @@ export const schema_schema = {
type: "string",
},
maxItems: 50,
description:"Once set, all the data in this field will be encrypted before storing it in the database. Encryption key must be provided during the time of BeanBagDB initialization and must be managed by the user as it is NOT stored in the database"
},
single_record: {
type: "boolean",
Expand All @@ -65,6 +76,7 @@ export const schema_schema = {
"If set, only a single records with this schema will be allowed to insert in the database",
},
},
required :["primary_keys","non_editable_fields","single_record","encrypted_fields"]
},
},
required: ["name","description","schema", "settings"],
Expand All @@ -76,29 +88,9 @@ export const schema_schema = {
};

export const system_schemas = {
logs: {
system_generated:true,
description:"Schema for the log doc. Single log doc for the whole DB to log stuff about the DB",
name: "system_logs",
schema: {
type: "object",
additionalProperties: true,
properties: {
logs: {
type: "array",
items: {
type: "object",
additionalProperties: true,
},
},
},
},
settings: {
single_record: true
},
},
keys: {
system_generated:true,
version:0.5,
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",
name: "system_keys",
schema: {
Expand Down Expand Up @@ -129,10 +121,13 @@ export const system_schemas = {
},
settings: {
primary_keys: ["name"],
encrypted_fields:["value"]
encrypted_fields:["value"],
non_editable_fields:[],
single_record: false
},
},
settings: {
version:0.5,
system_generated:true,
description:"The system relies on these settings for proper functioning or enabling optional features.",
name: "system_settings",
Expand All @@ -144,27 +139,25 @@ export const system_schemas = {
name: {
type: "string",
minLength: 5,
maxLength: 250,
maxLength: 1000,
pattern: "^[a-zA-Z][a-zA-Z0-9_]*$",
},
value: {
type: "string",
minLength: 5,
maxLength: 5000,
pattern: "^[^\n\r]*$",
description:"Must be a single line string"
},
user_editable: {
type: "boolean",
default: true,
description:
"Whether this setting is editable by the user or only by the system",
type: ["string", "number", "boolean", "array"]
},
on_update_array:{
type:"string",
default:"replace",
description:"Special operation only for updating Arrays. Either replace it or append new elements to it. Cannot be edited",
enum:["replace","append"],
}
},
},
settings: {
primary_keys: ["name"],
editable_fields: ["value"],
non_editable_fields: ["name"],
encrypted_fields:[],
single_record:false
},
},
};
Expand Down
29 changes: 29 additions & 0 deletions test/couch_connect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import * as cdb from "./couchdb.js"
import "dotenv/config"





(async()=>{

let db = new cdb.BeanBagDB_CouchDB(process.env.cdburl, process.env.cdbname, process.env.secret)

//db.initialize_db()
await db.ready()
console.log(await db.metadata())
let test_schema = {
"name":"contact_list",
"description":"My contact book",
"schema":{},
"settings":{}
}

//let sch = await db.insert("schema",test_schema,{"link":"test1"})
//console.log(sch)


})();



75 changes: 75 additions & 0 deletions test/couchdb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// const crypto = require('crypto');
// const SDB = require("beanbagdb")

import Ajv from 'ajv';
import nano from "nano";
import BeanBagDB from '../src/index.js';

export class BeanBagDB_CouchDB extends BeanBagDB {
constructor(db_url,db_name,encryption_key){
//const cdb = import("nano")(db_url)
const cdb = nano(db_url)
const doc_obj = {
db_name:"couchdb",
name: db_name,
encryption_key: encryption_key,
api:{
insert: async (doc)=>{
const result = await cdb.insert(doc)
return result
},
// delete: ()=>{db1.destroy},
update: async (doc)=>{
const result = await cdb.insert(doc)
return result
},
search: async (query)=>{
const results = await cdb.find(query)
return results // of the form {docs:[],...}
},
get: async (id)=>{
const data = await cdb.get(id)
return data
},
createIndex: async (filter)=>{
const data = await cdb.createIndex(filter)
return data
},
delete: async (doc_id)=>{
const data = await cdb.get(id)
await cdb.destroy(data._id,data._rev)
}
},
utils:{
encrypt: (text,encryptionKey)=>{
const key = crypto.scryptSync(encryptionKey, 'salt', 32); // Derive a 256-bit key
const iv = crypto.randomBytes(16); // Initialization vector
const cipher = crypto.createCipheriv('aes-256-cbc', key, iv);
let encrypted = cipher.update(text, 'utf8', 'hex');
encrypted += cipher.final('hex');
return iv.toString('hex') + ':' + encrypted; // Prepend the IV for later use
},
decrypt : (encryptedText, encryptionKey)=>{
const key = crypto.scryptSync(encryptionKey, 'salt', 32); // Derive a 256-bit key
const [iv, encrypted] = encryptedText.split(':').map(part => Buffer.from(part, 'hex'));
const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv);
let decrypted = decipher.update(encrypted, 'hex', 'utf8');
decrypted += decipher.final('utf8');
return decrypted;
},
ping : ()=>{
// @TODO ping the database to check connectivity when class is ready to use
},
validate_schema: (schema_obj, data_obj)=>{
const ajv = new Ajv({code: {esm: true}}) // options can be passed, e.g. {allErrors: true}
const validate = ajv.compile(schema_obj);
const valid = validate(data_obj);
return {valid,validate}
}
}
}
super(doc_obj)
}
}


2 changes: 1 addition & 1 deletion test/init.test.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// to test initialization of the BeanBagDB class
import { get_pdb_doc } from './pouchdb.js';
import { throws, strictEqual } from "assert";
import BeanBagDB from '../src/index.js';
import {BeanBagDB} from '../src/index.js';

/**
* Initial setup
Expand Down
Loading

0 comments on commit a8df8dd

Please sign in to comment.