1.0.0 • Published 1 year ago

branchstorage v1.0.0

Weekly downloads
-
License
-
Repository
-
Last release
1 year ago

npm treestorage 1.0.2 🌳

A fully open source database building tool for node.js

install :

npm i treestorage

example 1 - branch with leaf 🌿

let treeStorage = require("treestorage")

let database = treeStorage(__dirname+"/database")

database.users = {
  name:{type:String,require:true,maxSize:30},
  surname:{type:String,require:true,maxSize:30},
  email:{unique:true,type:String,require:true,maxSize:100,lowerCase:true,match:/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,solid:true},
  password:{type:String,require:true,maxSize:35,minSize:8,private:true},
}

let __db = database();

__db.create({
  name:"Damian",
  surname:"Mostert",
  email:"damian@example.com",
  password:"password1"
},result=>{
  console.log(result)
})

console.log(__db.search({name:"d"}))
output
let treeStorage = require("treestorage")

let database = treeStorage(__dirname+"/database")

database.users = {
  name:{type:String,require:true,maxSize:30},
  surname:{type:String,require:true,maxSize:30},
  email:{unique:true,type:String,require:true,maxSize:100,lowerCase:true,match:/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,solid:true},
  password:{type:String,require:true,maxSize:35,minSize:8,private:true},
}

let __db = database();

__db.create({
  name:"Damian",
  surname:"Mostert",
  email:"damian@example.com",
  password:"password1"
},result=>{
  console.log(result)
})

console.log(__db.search({name:"d"}))

example 2 - leaf 🍃

let treeStorage = require("treestorage")

let users = treeStorage.leaf(__dirname+"/users",{
  name:{type:String,require:true,maxSize:30},
  surname:{type:String,require:true,maxSize:30},
  email:{unique:true,type:String,require:true,maxSize:100,lowerCase:true,match:/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,solid:true},
  password:{type:String,require:true,maxSize:35,minSize:8,private:true},
})

users.create({
  name:"Damian",
  surname:"Mostert",
  email:"damian@example.com",
  password:"password1"
},result=>{
  console.log(result)
})

users.searchOne({name:"d"},user=>{
  if(user){
    console.log(user.change({name:"Bob",surname:"Lukas"}))
  }
})

console.log(users.getAll())

console.log(users.deleteAll())
output
{ errors: [], success: 'item made' }
{ errors: [], success: 'item updated' }
[
  {
    name: 'Bob',
    surname: 'Lukas',
    email: 'damian@example.com',
    password: 'password1',
    __v: 1,
    __id: '4GW5VxQSIDeRlykS39S9eyEd90dIsnNDeK11OaR0eGqSnXyQ1rZdkMVoOW4o68meG8QBFnvuOzU',
    __dir: '/home/damian/Desktop/d/users/4GW5VxQSIDeRlykS39S9eyEd90dIsnNDeK11OaR0eGqSnXyQ1rZdkMVoOW4o68meG8QBFnvuOzU',
    delete: [Function],
    update: [Function],
    change: [Function]
  }
]
[ { success: 'item removed' } ]

example 3 - branch 🌿

let treeStorage = require("treestorage")

let userTemplate = {
  name:{type:String,require:true,maxSize:30},
  surname:{type:String,require:true,maxSize:30},
  email:{unique:true,type:String,require:true,maxSize:100,lowerCase:true,match:/^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/,solid:true},
  password:{type:String,require:true,maxSize:35,minSize:8,private:true},
}

let users = treeStorage.leaf(__dirname+"/users",userTemplate)

let database2 = treeStorage(__dirname+"/database")

database2.users = userTemplate

let __db = database2();

let fullDB = treeStorage.branch({
  backup:__db,
  main:users
})

fullDB.create({
  name:"Damian",
  surname:"Mostert",
  email:"damian@example.com",
  password:"password1"
},result=>{
  console.log(result)
})

console.log(fullDB.getAll())

console.log(fullDB.deleteAll())
output
{
  backup: { users: { errors: [], success: 'item made' } },
  main: { errors: [], success: 'item made' }
}
{
  backup: { users: [ [Object] ] },
  main: [
    {
      name: 'Damian',
      surname: 'Mostert',
      email: 'damian@example.com',
      password: 'password1',
      __v: 0,
      __id: 'iP173bwqih5tjPz4o4xKCouL8yB2uMhnIdQOpFp1GDtq1DfoVrr2xiecA4zQ7Rb7bCVQKVltPcy',
      __dir: '/home/damian/Desktop/d/users/iP173bwqih5tjPz4o4xKCouL8yB2uMhnIdQOpFp1GDtq1DfoVrr2xiecA4zQ7Rb7bCVQKVltPcy',
      delete: [Function],
      update: [Function],
      change: [Function]
    }
  ]
}
{
  backup: { users: [ [Object] ] },
  main: [ { success: 'item removed' } ]
}

leaf template options

option nameexplenationsantax usage
uniquemakes shure there wont be a duplicate inside leaf object{type:Array,}
privatewill disable all finding and searching by this object{private:true,}
typewont create item if it is the incorect type{type:Object,} {type:String,} {type:Array,} {type:Number,}
matchsame as using String.match(/a-x/) match is good to check emails and passwords{match:/a-z/,}
requirewon't create item without this data{require:true,}
sizewont create item if String.length or Number not == to size{size:5,}
minSizewont create item if String.length or Number not >= to size{minSize:2,}
maxSizewont create item if String.length or Number not <= to size{maxSize:10,}
lowerCasewill automaticly save as lowerCase if String{lowerCase:true,}
upperCasewill automaticly save as upperCase if String{upperCase:true,}
solidif a storage item on leaf set to solid, it canot be changed{solid:true,}

leaf and branch tool list

tool nameexplenationsantaxsantax with callback example
getAllused to get all items in a leaf or branch objectAll=object.getAll()object.getAll(All=>{})
createused make a item in a leaf or items in branch objectAll=object.getAll()object.getAll(All=>{})
findused to find multiple objects with exact match to filterresults=object.find({/*filter_data*/})object.find({/*filter_data*/,results=>{})
findOneused to get all items in a leaf or branch objectAll=object.getAll()object.getAll(All=>{})
findByIdused to get all items in a leaf or branch objectAll=object.getAll()object.getAll(All=>{})
searchused to get all items in a leaf or branch objectAll=object.getAll()object.getAll(All=>{})
searchOneused to get all items in a leaf or branch objectAll=object.getAll()object.getAll(All=>{})
searchOneUpdateused to get all items in a leaf or branch objectAll=object.getAll()object.getAll(All=>{})
searchByIdused to get all items in a leaf or branch objectAll=object.getAll()object.getAll(All=>{})
updateused to get all items in a leaf or branch objectAll=object.getAll()object.getAll(All=>{})
updateMultused to update multipe items in leaf or branch objectAll=object.getAll()object.getAll(All=>{})
updateByIdused to update items in a leaf object by there .__id valueAll=object.getAll()object.getAll(All=>{})
updateAllused to update all items in a leaf or branch objectAll=object.getAll()object.getAll(All=>{})
deleteused to get all items in a leaf or branch objectAll=object.getAll()object.getAll(All=>{})
searchOneDeleteused to get all items in a leaf or branch objectAll=object.getAll()object.getAll(All=>{})
searchDeleteused to get all items in a leaf or branch objectAll=object.getAll()object.getAll(All=>{})
deleteByIdused to delete a object by its .__id valueresult=object.getAll()object.getAll(All=>{})
deleteMultused to delete multiple items in database with find resultsresults=object.deleteMult({/*find_filter*/})object.deleteMult({/*find_filter*/},results=>{ })
deleteAlldeletes all in leaf or branchresult=object.deleteAll()object.deleteAll(result=>{})

treeStorage database object structure example

branch {
  path: { users: '/home/damian/Desktop/d/database/users' },
  rules: {
    users: {
      name: [Object],
      surname: [Object],
      email: [Object],
      password: [Object]
    }
  },
  getAll: [Function: getAll],
  create: [Function: create],
  find: [Function: find],
  findOne: [Function: findOne],
  findById: [Function: findById],
  search: [Function: search],
  searchOne: [Function: searchOne],
  searchUpdate: [Function: searchUpdate],
  searchOneUpdate: [Function: searchOneUpdate],
  searchById: [Function: searchById],
  update: [Function: update],
  updateMult: [Function: updateMult],
  updateById: [Function: updateById],
  updateAll: [Function: updateAll],
  delete: [Function: deleteOne],
  deleteById: [Function: deleteById],
  searchOneDelete: [Function: searchOneDelete],
  searchDelete: [Function: searchDelete],
  deleteMult: [Function: deleteMult],
  deleteAll: [Function: deleteAll],
  users: leaf {
    path: '/home/damian/Desktop/d/database/users',
    rules: {
      name: [Object],
      surname: [Object],
      email: [Object],
      password: [Object]
    },
    getAll: [Function: getAll],
    create: [Function: create],
    find: [Function: find],
    findOne: [Function: findOne],
    findById: [Function: findById],
    search: [Function: search],
    searchOne: [Function: searchOne],
    searchOneUpdate: [Function: searchOneUpdate],
    searchUpdate: [Function: searchUpdate],
    searchById: [Function: searchById],
    update: [Function: update],
    updateMult: [Function: updateMult],
    updateById: [Function: updateById],
    updateAll: [Function: updateAll],
    delete: [Function: deleteOne],
    searchOneDelete: [Function: searchOneDelete],
    searchDelete: [Function: searchDelete],
    deleteById: [Function: deleteById],
    deleteMult: [Function: deleteMult],
    deleteAll: [Function: deleteAll]
  }
}
AutherDamian Mostert
Emaildamianmostert86@gmail.com
Time it took to build this project till this point24h00 (one day)