Skip to main content

collection

ready for the juices!, here is where the orm and schema is

pouchlite stores data in collections just like: mongodb

a collection requires schema pouchlite uses valibot for its schema validation

schema must be a valid object

     //create collection
import {object,string} from 'valibot';

//check valibot for docs schema

//init
let db = lite.useDb('blog');

//schema
let schema = object({
name:string(),
title:string(),
text:string()
})
//create collection
db.createCollection('comments',schema);



Remove a collection

   db.removeCollection('comments')
//danger removes data and the collection itself

Use collection

use() this method initializes a created collection and the orm

   let comments =  db.use('comments');//returns orm methods

console.log(comments)

//put _id will be autogen
//always remember your schema
let comment = {
name:'pouchlite',
title:'how to use pouchlite',
text: 'its very awesome'
}

Put data in a collection requires opts: userid,data, data is not optional



comments.put({data:comment},(res)=>{
console.log(res)

res.iserror,
res.msg,
res.error,
res.doc

}) //returns created data

//for logged in user
comments.put({userid:'joe doe',data:comment},(res)=> {
console.log(res)
})

//bulkput
comments.bulkPut({data:[{text:'text one'},{text:'text two'}]},(res)=>{
console.log(res)
})//add (userid) for logged in user



Get data from collection

 
comments.get({id:'hhhjdd'},(res)=>{
console.log(res)
})

//bulk get many
comments.get({id:['hgjjgkg','jjfjjf']},(res)=>{
console.log(res)
})//or id:[{id:'kkkkkk'},{id:'kkfhfjhf'}]


//for logged in user
comments.getOneByuser({userid:'joe doe',id:'comment id'},(res)=>{
res.iserror,
res.msg,
res.error,
res.doc
})

//get many for user
comments.getAllByuser('joe doe',(res)=>{
res.iserror,
res.msg,
res.error,
res.doc
})


Update data in collection


let newdata = {
text:'update text'
}
comments.update({id:'jjjjjj',data:newdata},(res)=>{
console.log(res)
})


Remove data from a colletion

//single
comments.remove('id to remove',(res)=>{
console.log(res)
})

//bulkRemove
comments.bulkRemove(['hhggggg','jjjhggh'],(res)=>{
console.log(res)
})//or [{id:'hhhhhh'},{id:'jjgggffh'}]

Events

initialized collection exports two event listeners default event is change

//tip on should be first

comments.on('event',(data)=>{
console.log(data)
//your logic
})

//emit event
comments.emit('event',comment)

//default
comments.on('change',(data)=>{
console.log(data)
})