mirror of
https://github.com/Dvorinka/excalidraw-full.git
synced 2026-06-03 22:02:57 +00:00
Add different data providers and improve docs
This adds support for switching between in-memory, local filesystem, s3 or sqlite.
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
package sqlite
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"excalidraw-complete/core"
|
||||
"fmt"
|
||||
|
||||
"database/sql"
|
||||
"log"
|
||||
|
||||
_ "github.com/mattn/go-sqlite3"
|
||||
"github.com/oklog/ulid/v2"
|
||||
)
|
||||
|
||||
var savedDocuments = make(map[string]core.Document)
|
||||
|
||||
type documentStore struct {
|
||||
db *sql.DB
|
||||
}
|
||||
|
||||
func NewDocumentStore(dataSourceName string) core.DocumentStore {
|
||||
// db, err := sql.Open("sqlite3", ":memory:")
|
||||
db, err := sql.Open("sqlite3", dataSourceName)
|
||||
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
sts := `CREATE TABLE IF NOT EXISTS documents (id TEXT PRIMARY KEY, data BLOB);`
|
||||
_, err = db.Exec(sts)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
return &documentStore{db}
|
||||
}
|
||||
|
||||
func (s *documentStore) FindID(ctx context.Context, id string) (*core.Document, error) {
|
||||
var data []byte
|
||||
err := s.db.QueryRowContext(ctx, "SELECT data FROM documents WHERE id = ?", id).Scan(&data)
|
||||
if err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, fmt.Errorf("document with id %s not found", id)
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
document := core.Document{
|
||||
Data: *bytes.NewBuffer(data),
|
||||
}
|
||||
return &document, nil
|
||||
}
|
||||
|
||||
func (s *documentStore) Create(ctx context.Context, document *core.Document) (string, error) {
|
||||
id := ulid.Make().String()
|
||||
data := document.Data.Bytes()
|
||||
_, err := s.db.ExecContext(ctx, "INSERT INTO documents (id, data) VALUES (?, ?)", id, data)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return id, nil
|
||||
}
|
||||
Reference in New Issue
Block a user