Skip to content

Commit

Permalink
Add license and readme
Browse files Browse the repository at this point in the history
  • Loading branch information
krstak committed Oct 28, 2017
1 parent b85c639 commit 4f7e3bf
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 2 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2017 krstak

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
76 changes: 76 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# In-Memory Database
Small in-memory database. It is useful for testing and test environment. Should not be used in production.

## Install
```
go get -u github.com/krstak/memorydb
```

## API

```go
Add func(item interface{}, collection string) string
FindAll func(collection string) ([]interface{}, error)
FindById func(id string, collection string) (interface{}, error)
FindBy func(field, value, collection string) (interface{}, error)
Update func(id string, item interface{}, collection string) error
Remove func(id, collection string) error
```

## Usage

##### Create database
```go
db := memorydb.Create()
```

##### Entity to persist
In order to save some struct in db, it should have the required field `Id` type `string`. Everything else is optional.
```go
type book struct {
Id string
isbn string
name string
author string
}
```

##### Write to database
Important: Reference should be passed in.
```go
memBook := book{isbn: "1234567", name: "In-Memory DB", author: "Marko Krstic"}
id := db.Add(&memBook, "books")
```

##### Find all
Important: slice of interface{} is returned
```go
books, err := db.FindAll("books")
bk := books[0].(*book)
```

##### Find by Id
Important: interface{} is returned
```go
bk, err := db.FindById(id, "books")
b := bk.(*book)
```

##### Find by custom field
Important: interface{} is returned
```go
bk, err := db.FindBy("isbn", "1234567", "books")
b := bk.(*book)
```

##### Update
Important: Reference should be passed in.
```go
bk := book{isbn: "222", name: "In-Memory DB", author: "Marko Krstic"}
err := db.Update(id, &bk, "books")
```

##### Remove
```go
err := db.Remove(id, "books")
```
4 changes: 2 additions & 2 deletions db.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ const (
type Manager struct {
Add func(item interface{}, collection string) string
FindAll func(collection string) ([]interface{}, error)
FindBy func(field, value, collection string) (interface{}, error)
FindById func(id string, collection string) (interface{}, error)
Remove func(id, collection string) error
FindBy func(field, value, collection string) (interface{}, error)
Update func(id string, item interface{}, collection string) error
Remove func(id, collection string) error
}

type col struct {
Expand Down

0 comments on commit 4f7e3bf

Please sign in to comment.