Skip to content

Commit

Permalink
readme
Browse files Browse the repository at this point in the history
  • Loading branch information
amirrezaask committed Nov 19, 2021
1 parent 6c20f6c commit 20ae22e
Showing 1 changed file with 123 additions and 2 deletions.
125 changes: 123 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,130 @@ To install this package run the following command in the root of your project.
go get github.com/golobby/orm
```

# Quick Start
### Quick Start
#### Creating repository
first step to use golobby/orm is creating
a new repository.
```go
package main

### Benchmarks
import (
"github.com/golobby/orm"
"database/sql"
)
type Model struct {
ID int
Name string
}
func main() {
db, _ := sql.Open("postgres", "")
modelRepository := orm.NewRepository(db, orm.PostgreSQLDialect, &Model{})
}
```
### Inserting a new record
```go
package main

import (
"github.com/golobby/orm"
"database/sql"
"fmt"
)
type Model struct {
ID int
Name string
}
func main() {
db, _ := sql.Open("postgres", "")
modelRepository := orm.NewRepository(db, orm.PostgreSQLDialect, &Model{})
amirreza := &Model{
Name: "Amirreza",
}
err := modelRepository.Save(amirreza)
if err != nil {
panic(err)
}
fmt.Println(amirreza.ID) // primary key is now set to last inserted id
}
```
### Fetching a record from database
```go
package main

import (
"github.com/golobby/orm"
"database/sql"
"fmt"
)
type Model struct {
ID int
Name string
}
func main() {
db, _ := sql.Open("postgres", "")
modelRepository := orm.NewRepository(db, orm.PostgreSQLDialect, &Model{})
amirreza := &Model{
ID: 1,
}
err := modelRepository.Fill(amirreza)
if err != nil {
panic(err)
}
fmt.Println(amirreza.Name) // primary key is now set to last inserted id
}
```
### Updating a record
```go
package main

import (
"github.com/golobby/orm"
"database/sql"
)
type Model struct {
ID int
Name string
}
func main() {
db, _ := sql.Open("postgres", "")
modelRepository := orm.NewRepository(db, orm.PostgreSQLDialect, &Model{})
amirreza := &Model{
ID: 1,

}
amirreza.Name = "comrade"
err := modelRepository.Update(amirreza)
if err != nil {
panic(err)
}
}
```
### Deleting a record
```go
package main

import (
"github.com/golobby/orm"
"database/sql"
)
type Model struct {
ID int
Name string
}
func main() {
db, _ := sql.Open("postgres", "")
modelRepository := orm.NewRepository(db, orm.PostgreSQLDialect, &Model{})
amirreza := &Model{
ID: 1,

}
err := modelRepository.Delete(amirreza)
if err != nil {
panic(err)
}
}
```
# Benchmarks
for CRUD operations on 10000 records
- Create
- Read
Expand Down

0 comments on commit 20ae22e

Please sign in to comment.