sqlfsync

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Feb 5, 2022 License: MIT Imports: 5 Imported by: 0

README

Sqlfsync

Automatically syncronize your SQL database and filesystem. Built using fsnotify and gorm.

Installation

go get github.com/mattcarp12/sqlfsync

Usage

package main

import (
	"github.com/mattcarp12/sqlfsync"
	"gorm.io/driver/sqlite"
	"gorm.io/gorm"
	"time"
)

type MyFile struct {
	ID        uint
	Path      string `sqlfsync:"path"`
	CreatedAt time.Time
}

var done chan bool

func main() {
	db, _ := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{})

	sfs := sqlfsync.New(db)
	sfs.AddWatch("/my/files", &MyFile{})
	defer sfs.Close()

	done = make(chan bool)
	<-done
}

Contributing

Pull requests are encouraged.

License

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type SqlFSync

type SqlFSync struct {
	// pointer to database connection
	DB *gorm.DB

	// list of directories to watch
	Watches []WatchEntry
}

SqlFSync watches a set of directories for created or deleted files, and inserts or deletes from the database, respectively.

func New

func New(db *gorm.DB) *SqlFSync

Create a new instance of SqlFSync

func (*SqlFSync) AddWatch

func (sfs *SqlFSync) AddWatch(path string, model interface{}) error

Start watching the specified path for create or delete events. The model argument must be a pointer to a struct. It must have a field tagged with sqlfsync:"path".

func (*SqlFSync) Close

func (sfs *SqlFSync) Close()

Stop watching all directories

type WatchEntry

type WatchEntry struct {
	// full path to directory
	Path string

	// Pointer to struct that represents
	// the entity in the database
	Model interface{}

	// Watches Path for create or delete events
	FSWatcher *fsnotify.Watcher
}

WatchEntry represents a single directory

Jump to

Keyboard shortcuts

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