-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rewrite(driver): changed model creation through the use of bingo.Docu…
…ment
- Loading branch information
Noku
committed
Oct 19, 2023
1 parent
2751cda
commit 79c237c
Showing
9 changed files
with
264 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,6 +38,7 @@ const ( | |
) | ||
|
||
type Function struct { | ||
bingo.Document | ||
Name string `json:"name,omitempty"` | ||
Category string `json:"category,omitempty"` | ||
Args []string `json:"args,omitempty"` | ||
|
@@ -47,10 +48,6 @@ type Function struct { | |
Platform Platform `json:"platform,omitempty"` | ||
} | ||
|
||
func (f Function) Key() []byte { | ||
return []byte(fmt.Sprintf("%s-%s", f.Name, f.Platform)) | ||
} | ||
|
||
func main() { | ||
driver, err := bingo.NewDriver(bingo.DriverConfig{ | ||
Database: "clippy.db", | ||
|
@@ -120,15 +117,12 @@ You can specify an autoincrement ID by returning nil in the `Key` method. | |
|
||
```go | ||
type User struct { | ||
bingo.Document | ||
Username string `json:"username,omitempty" validate:"required,min=3,max=64"` | ||
Email string `json:"email,omitempty" validate:"required,email"` | ||
Password string `json:"password,omitempty" preprocessor:"password-prep" validate:"required,min=6,max=64"` | ||
} | ||
|
||
func (u User) Key() []byte { | ||
return []byte(u.Username) | ||
} | ||
|
||
func (u *User) CheckPassword(password string) bool { | ||
current := u.Password | ||
if strings.HasPrefix(current, "hash:") { | ||
|
@@ -152,6 +146,7 @@ func (u *User) EnsureHashedPassword() error { | |
``` | ||
|
||
### 3. Create a collection for your document type | ||
Note: Your document type must have bingo.Document as it's first field or implement the `bingo.DocumentSpec` interface. | ||
|
||
```go | ||
users := bingo.CollectionFrom[User](driver, "users") | ||
|
@@ -182,6 +177,25 @@ if err != nil { | |
fmt.Println("Inserted user with ID:", id) | ||
``` | ||
|
||
**Inserting documents with a custom ID:** | ||
|
||
```go | ||
id, err := users.Insert(User{ | ||
Document: bingo.Document{ | ||
ID: []byte("custom-id"), | ||
}, | ||
Username: "test", | ||
Password: "test123", | ||
Email: "[email protected]", | ||
}) | ||
if err != nil { | ||
panic(err) | ||
} | ||
|
||
fmt.Println("Inserted user with ID:", id) | ||
``` | ||
|
||
|
||
**Querying documents:** | ||
|
||
```go | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package bingo | ||
|
||
type Document struct { | ||
ID string `json:"_id" bingo_json:"_id"` | ||
} | ||
|
||
func (d Document) Key() []byte { | ||
return []byte(d.ID) | ||
} |
Oops, something went wrong.