# Quickstart

### Init Project

First, use `go mod init` to initialize the project and add GoooQo by:

```
go get -u github.com/doytowin/goooqo/rdb
```

Then, initialize the database connection and transaction manager as follows:

```go
package main

import (
	"database/sql"
	"github.com/doytowin/goooqo/rdb"
	_ "github.com/mattn/go-sqlite3"
)

func main() {
	db := rdb.Connect("app.properties")
	defer rdb.Disconnect(db)
	tm := rdb.NewTransactionManager(db)

	//...
}
```

### Build DataAccess

Suppose we have the following user table in `test.db`:

<table><thead><tr><th>id</th><th>name</th><th data-type="number">score</th><th>memo</th></tr></thead><tbody><tr><td>1</td><td>Alley</td><td>80</td><td>Good</td></tr><tr><td>2</td><td>Dave</td><td>75</td><td>Well</td></tr><tr><td>3</td><td>Bob</td><td>60</td><td></td></tr><tr><td>4</td><td>Tim</td><td>92</td><td>Great</td></tr><tr><td>5</td><td>Emy</td><td>100</td><td>Great</td></tr></tbody></table>

We define an entity object and a query object for the table:

{% code title="user.go" %}

```go
package main

import (
	. "github.com/doytowin/goooqo/core"
)

type UserEntity struct {
	Int64Id
	Name  *string `json:"name"`
	Score *int    `json:"score"`
	Memo  *string `json:"memo"`
}

func (u UserEntity) GetTableName() string {
	return "t_user"
}

type UserQuery struct {
	PageQuery
	ScoreLt   *int
	MemoStart *string
	// ...
}
```

{% endcode %}

The fields of the entity object correspond to the columns of the table, and the fields of the query object are based on the query conditions of the requirements.

Then we define a `userDataAccess` to access the table:

```go
userDataAccess := rdb.NewTxDataAccess[UserEntity](tm)

userQuery := UserQuery{PageQuery: PageQuery{Page: 5, Size: 10}, ScoreLt: P(80)}
userEntities, err := userDataAccess.Query(ctx, userQuery)
```

This will generate and execute the following SQL:

```sql
SELECT id, name, score, memo FROM t_user WHERE score < ? LIMIT 10 OFFSET 40
```

### Related Documents

<table data-view="cards"><thead><tr><th></th><th></th><th></th><th data-hidden data-card-target data-type="content-ref"></th></tr></thead><tbody><tr><td></td><td>Define an entity object</td><td></td><td><a href="entity-mapping/entity-object">entity-object</a></td></tr><tr><td></td><td>Define a query object</td><td></td><td><a href="query-mapping/query-object">query-object</a></td></tr><tr><td></td><td>Create DataAccess</td><td></td><td><a href="api/crud">crud</a></td></tr><tr><td></td><td>Database Connection</td><td></td><td><a href="api/connection">connection</a></td></tr><tr><td></td><td>Transaction</td><td></td><td><a href="api/transaction">transaction</a></td></tr></tbody></table>
