simplejsondb

package module
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 5, 2024 License: MIT Imports: 8 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

View Source
var (
	Ext            string = ".json"
	GZipExt        string = ".json.gz"
	ErrNoDirectory error  = errors.New("not a directory")
)

Functions

func Gzip added in v1.1.0

func Gzip(data []byte) (result []byte, err error)

func UnGzip added in v1.0.1

func UnGzip(record []byte) (result []byte, err error)

Types

type Collection

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

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, error)

New - a database instance

type Options

type Options struct {
	UseGzip bool
}

Options - extra configuration

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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