simplejsondb

package module
v1.0.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 1, 2023 License: MIT Imports: 7 Imported by: 0

README

A simple JSON database

Provides a quick file based database which helps to store the json file based data along with key based search.

sample structure will be generated by below code

database1

    collection1
        key1.json  `{"key": "val", ...}`
        ...

    collection2
        key1.json   `{"key": "val", ...}`
        ...


HOW TO USE


package main

import (
	"fmt"

	simplejsondb "github.com/pnkj-kmr/simple-json-db"
)

func main() {
	// db instance
	db, err := simplejsondb.New("database1", nil)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println("database created")

	// collection1 creation
	t, err := db.Collection("collection1")
	if err != nil {
		fmt.Println("table err", err)
		return
	}
	fmt.Println("collection1 created")

	// collection1 - inserting a record
	data := "{\"key\": 123}"
	err = t.Create("key1", []byte(data))
	if err != nil {
		fmt.Println(err)
		return
	}
	// collection1 - inserting  a record 2
	err = t.Create("key2", []byte(data))
	if err != nil {
		fmt.Println(err)
		return
	}

	// collection2 creation
	t2, err := db.Collection("collection2")
	if err != nil {
		fmt.Println("table err", err)
		return
	}
	fmt.Println("collection2 created")

	// collection2 - inserting a record
	data2 := "{\"key\": 124}"
	err = t2.Create("key1", []byte(data2))
	if err != nil {
		fmt.Println(err)
		return
	}

	// fetching all records from collection1
	records := t.GetAll()
	for _, r := range records {
		fmt.Println(string(r))
	}

	// getting one record from collection1
	_, err = t.Get("key3")
	if err != nil {
		fmt.Println("record get-", err)
	}
	record, err := t.Get("key2")
	if err != nil {
		fmt.Println("record get-", err)
	}
	fmt.Println("record -- ", string(record))

	// deleting record from collection1
	err = t.Delete("key2")
	if err != nil {
		fmt.Println("record delete--", err)
	}

	// fecthing all records from collection1
	fmt.Println("after delete of record with key2..")
	records = t.GetAll()
	for _, r := range records {
		fmt.Println(string(r))
	}
}

DESCRIPTION


A simple JSON database helps to store the json file based data into your current working directory, you can define N number of databases and One database can contains N number of collections(tables) and One collection can contains N number of records(entries).

To install:

go install github.com/pnkj-kmr/simple-json-db

FAQ?

Why, we need simple-json-db?

This database helps to maintain the small dataset to keep into files by avoiding extra affects to maintain a seperate database along the connection libraries

:)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Collection

type Collection interface {
	Get(string) ([]byte, error)
	GetAll() [][]byte
	Create(string, []byte) error
	Delete(string) error
}

Collection - it's like a table name

type DB

type DB interface {
	Collection(string) (Collection, error)
}

DB - a database

func New

func New(dbname string, options *Options) (db DB, err error)

New - a database instance

type Logger

type Logger interface {
	Error(string, ...zapcore.Field)
	Warn(string, ...zapcore.Field)
	Info(string, ...zapcore.Field)
	Debug(string, ...zapcore.Field)
}

Logger - logging interface

type Options

type Options struct {
	Logger
}

Options - extra configuration

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL