postdb

package module
v0.8.0 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2023 License: MIT Imports: 12 Imported by: 1

README

PostDB

GoDoc GoReport MIT

A database to store posts in Go with BoltDB, noSQL.

Installation

go get github.com/ije/postdb

Usage

import (
	"github.com/ije/postdb"
	"github.com/ije/postdb/q"
)

// opening a database
db, err := postdb.Open("post.db", 0666, false)
if err != nil {
    return err
}
defer db.Close()

// get all posts in the database
db.List()

// get posts with query
db.List(q.Tags("foo", "bar"), q.Range(1, 100), q.Order(q.DESC), q.Select("title", "date", "content"))

// get post without kv
db.Get(q.ID(id))

// get post with specified kv
db.Get(q.ID(id), q.Select("title", "date"))

// get post with prefixed kv
db.Get(q.ID(id), q.Select("title_*")) // match key in title_en,title_zh...

// get post with full kv
db.Get(q.ID(id), q.Select("*"))

// add a new post
db.Put(q.Alias(name), q.Status(1), q.Tags("foo", "bar"), q.KV{"foo": []byte("bar")})

// update the existing post
db.Update(q.ID(id), q.KV{"foo": []byte("cool")})

// delete the existing kv of the post
db.DeleteKV(q.ID(id), q.Select("foo"))

// delete the existing posts
db.Delete(q.ID(id))

// using namespace
ns := db.Namespace("name")
ns.List()
ns.Get(q.ID(id))
...

// backup the entire database
db.WriteTo(w)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotFound       = errors.New("not found")
	ErrDuplicateAlias = errors.New("duplicate alias")
)

Functions

This section is empty.

Types

type DB

type DB struct {
	// contains filtered or unexported fields
}

A DB to store posts

func Open

func Open(path string, mode os.FileMode, readonly bool) (db *DB, err error)

Open opens a database at the given path.

func (*DB) Begin

func (db *DB) Begin(writable bool) (*Tx, error)

Begin starts a new transaction.

func (*DB) Close

func (db *DB) Close() error

Close releases all database resources.

func (*DB) Delete

func (db *DB) Delete(qs ...q.Query) (int, error)

Delete deletes the post

func (*DB) DeleteKV

func (db *DB) DeleteKV(qs ...q.Query) error

DeleteKV deletes the post kv

func (*DB) Get

func (db *DB) Get(qs ...q.Query) (*post.Post, error)

Get returns the post

func (*DB) List

func (db *DB) List(qs ...q.Query) ([]post.Post, error)

List returns some posts

func (*DB) NS

func (db *DB) NS(name string) *NS

NS is a shortcut for Namespace

func (*DB) Namespace

func (db *DB) Namespace(name string) *NS

Namespace returns the namepace.

func (*DB) Put

func (db *DB) Put(qs ...q.Query) (*post.Post, error)

Put puts a new post

func (*DB) Update

func (db *DB) Update(qs ...q.Query) (ok bool, err error)

Update updates the post

func (*DB) WriteTo

func (db *DB) WriteTo(w io.Writer) (int64, error)

WriteTo writes the entire database to a writer.

type NS

type NS struct {
	// contains filtered or unexported fields
}

A NS for the DB

func (*NS) Begin

func (ns *NS) Begin(writable bool) (*Tx, error)

Begin starts a new transaction.

func (*NS) Delete

func (ns *NS) Delete(qs ...q.Query) (int, error)

Delete deletes the post

func (*NS) DeleteKV

func (ns *NS) DeleteKV(qs ...q.Query) error

DeleteKV deletes the post kv

func (*NS) Get

func (ns *NS) Get(qs ...q.Query) (*post.Post, error)

Get returns the post

func (*NS) List

func (ns *NS) List(qs ...q.Query) ([]post.Post, error)

List returns some posts

func (*NS) Put

func (ns *NS) Put(qs ...q.Query) (*post.Post, error)

Put puts a new post

func (*NS) Update

func (ns *NS) Update(qs ...q.Query) (ok bool, err error)

Update updates the post

type Tx

type Tx struct {
	// contains filtered or unexported fields
}

Tx represents a transaction on the database.

func (*Tx) Commit

func (tx *Tx) Commit() error

Commit writes all changes to disk and updates the meta page. Returns an error if a disk write error occurs, or if Commit is called on a read-only transaction.

func (*Tx) Delete

func (tx *Tx) Delete(qs ...q.Query) (n int, err error)

Delete deletes the post

func (*Tx) DeleteKV

func (tx *Tx) DeleteKV(qs ...q.Query) error

DeleteKV deletes the post kv

func (*Tx) Get

func (tx *Tx) Get(qs ...q.Query) (*post.Post, error)

Get returns the post

func (*Tx) List

func (tx *Tx) List(qs ...q.Query) (posts []post.Post)

List returns some posts

func (*Tx) Put

func (tx *Tx) Put(qs ...q.Query) (*post.Post, error)

Put puts a new post

func (*Tx) PutPost

func (tx *Tx) PutPost(post *post.Post) (err error)

PutPost puts a new post

func (*Tx) Rollback

func (tx *Tx) Rollback() error

Rollback closes the transaction and ignores all previous updates. Read-only transactions must be rolled back and not committed.

func (*Tx) Update

func (tx *Tx) Update(qs ...q.Query) (ok bool, err error)

Update updates the post

func (*Tx) WriteTo

func (tx *Tx) WriteTo(w io.Writer) (int64, error)

WriteTo writes the entire database to a writer. If err == nil then exactly tx.Size() bytes will be written into the writer.

Directories

Path Synopsis
internal
q

Jump to

Keyboard shortcuts

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