Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
miladrahimi committed Mar 16, 2022
1 parent 13da2cd commit 65ced97
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 18 deletions.
18 changes: 5 additions & 13 deletions orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ type ConnectionConfig struct {
Name string
// Which driver to be used for your connection, you can name [mysql, sqlite3, postgres]
Driver string
// Which connection string to be passed as second argument to sql.Open(driver, connectionString)
ConnectionString string
// Which connection string to be passed as second argument to sql.Open(driver, DSN)
DSN string
// If you already have an active database connection configured pass it in this value and
// do not pass Driver and ConnectionString fields.
// do not pass Driver and DSN fields.
DB *sql.DB
// Which dialect of sql to generate queries for, you don't need it most of the times when you are using
// traditional databases such as mysql, sqlite3, postgres.
Expand All @@ -57,6 +57,7 @@ type ConnectionConfig struct {
// SetupConnection declares a new connection for ORM.
func SetupConnection(conf ConnectionConfig) error {
// configure logger
// TODO: remove logger
var err error
globalLogger, err = newZapLogger(LogLevelDev)
if err != nil {
Expand All @@ -75,7 +76,7 @@ func SetupConnection(conf ConnectionConfig) error {
if err != nil {
return err
}
db, err = getDB(conf.Driver, conf.ConnectionString)
db, err = sql.Open(conf.Driver, conf.DSN)
if err != nil {
return err
}
Expand Down Expand Up @@ -124,10 +125,6 @@ type Entity interface {
ConfigureEntity(e *EntityConfigurator)
}

func getDB(driver string, connectionString string) (*sql.DB, error) {
return sql.Open(driver, connectionString)
}

func getDialect(driver string) (*Dialect, error) {
switch driver {
case "mysql":
Expand Down Expand Up @@ -533,11 +530,6 @@ func addProperty(to Entity, items ...Entity) error {

}

// addBelongsToMany(Post, Category)
func addBelongsToMany(to Entity, items ...Entity) error {
return nil
}

// Query creates a new QueryBuilder for given type parameter, sets dialect and table as well.
func Query[E Entity]() *QueryBuilder[E] {
q := NewQueryBuilder[E]()
Expand Down
6 changes: 3 additions & 3 deletions orm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,9 @@ func (c Category) Posts() ([]Post, error) {

func setup(t *testing.T) {
err := orm.SetupConnection(orm.ConnectionConfig{
Driver: "sqlite3",
ConnectionString: ":memory:",
Entities: []orm.Entity{&Post{}, &Comment{}, &Category{}, &HeaderPicture{}},
Driver: "sqlite3",
DSN: ":memory:",
Entities: []orm.Entity{&Post{}, &Comment{}, &Category{}, &HeaderPicture{}},
})
_, err = orm.GetConnection("default").Connection.Exec(`CREATE TABLE IF NOT EXISTS posts (id INTEGER PRIMARY KEY, body text, created_at TIMESTAMP, updated_at TIMESTAMP, deleted_at TIMESTAMP)`)
_, err = orm.GetConnection("default").Connection.Exec(`CREATE TABLE IF NOT EXISTS emails (id INTEGER PRIMARY KEY, post_id INTEGER, email text)`)
Expand Down
4 changes: 2 additions & 2 deletions schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ import (

func setup(t *testing.T) {
err := SetupConnection(ConnectionConfig{
Driver: "sqlite3",
ConnectionString: ":memory:",
Driver: "sqlite3",
DSN: ":memory:",
})
// orm.Schematic()
_, err = GetConnection("default").Connection.Exec(`CREATE TABLE IF NOT EXISTS posts (id INTEGER PRIMARY KEY, body text, created_at TIMESTAMP, updated_at TIMESTAMP, deleted_at TIMESTAMP)`)
Expand Down

0 comments on commit 65ced97

Please sign in to comment.