# memory/file

> Package file is a memory.Store that keeps notes as markdown files under a directory, one file per note, which a person can read and edit with any editor.

<!-- Generated from the Go source of github.com/barevoidco/zelk/core/memory/file (cmd/zelk/internal/reference TestReferenceGoAPI). Don't edit: change the code, then run make reference. -->

```go
import "github.com/barevoidco/zelk/core/memory/file"
```

Package file is a [memory.Store](/reference/go/memory/#store) that keeps notes as markdown files under a directory, one file per note, which a person can read and edit with any editor. An edit by hand is a new revision, so a session holding the old one gets a conflict instead of overwriting it.

Writes take an exclusive file lock per scope, so Stores in several processes over one directory serialize, and replace a note's file by renaming a synced temporary file over it, so a crash leaves the old note or the new one.

See docs/design/durability.md for the Windows NTFS process-crash contract.

**Example**

```go
dir, err := os.MkdirTemp("", "memory")
if err != nil {
	fmt.Println(err)
	return
}
defer func() { _ = os.RemoveAll(dir) }()
s, err := file.New(dir)
if err != nil {
	fmt.Println(err)
	return
}
ctx := context.Background()
scope := memory.Project("/work/app")

n, err := s.Put(ctx, memory.Note{Scope: scope, Name: "table-tests", Description: "Write Go tests table-driven",
	Type: memory.TypeFeedback, Author: memory.AuthorUser, Body: "Use tables of named cases."}, "")
if err != nil {
	fmt.Println(err)
	return
}
// Correcting needs the revision read; a stale one is a conflict.
_, err = s.Put(ctx, memory.Note{Scope: scope, Name: "table-tests", Description: "Write Go tests table-driven",
	Type: memory.TypeFeedback, Author: memory.AuthorUser, Body: "Use tables of named cases run with t.Run."}, n.Revision)
fmt.Println(err)
if err != nil {
	return
}
_, err = s.Put(ctx, n, n.Revision)
fmt.Println(err != nil)

notes, err := s.List(ctx, scope)
if err != nil {
	fmt.Println(err)
	return
}
for _, note := range notes {
	fmt.Println(note.Body)
}
// Output:
// <nil>
// true
// Use tables of named cases run with t.Run.
```

Output:

```text
<nil>
true
Use tables of named cases run with t.Run.
```


## Option

```go
type Option func(*Store)
```

Option configures a Store.


### WithClock

```go
func WithClock(now func() time.Time) Option
```

WithClock sets the clock that stamps notes; the default is time.Now.


## Store

```go
type Store struct {
	// contains filtered or unexported fields
}
```

Store keeps notes as markdown files under a directory: the user scope in user/, each project in projects/\<digest of its ID>/, one \<name>.md per note. A Store is safe for concurrent use, and Stores in several processes over one directory serialize their writes through a lock file per scope.


### New

```go
func New(dir string, opts ...Option) (*Store, error)
```

New returns a Store over dir, creating it if it doesn't exist.


### Store.Delete

```go
func (s *Store) Delete(ctx context.Context, scope memory.Scope, name string, expect memory.Revision) error
```

Delete removes scope's note name, as memory.Store.Delete says.


### Store.Dir

```go
func (s *Store) Dir(scope memory.Scope) string
```

Dir returns the directory scope's notes are kept in, which may not exist yet. Hosts show it, so the user knows where their notes live.


### Store.List

```go
func (s *Store) List(ctx context.Context, scope memory.Scope) ([]memory.Note, error)
```

List returns scope's notes, ordered by name. Files whose names can't name a note are not notes and are left out; a note's file that is a link, or doesn't parse, is listed with its Problem.


### Store.Put

```go
func (s *Store) Put(ctx context.Context, n memory.Note, expect memory.Revision) (memory.Note, error)
```

Put saves n, as memory.Store.Put says. It returns once the note's file and its directory entry are on disk.
