GithubHelp home page GithubHelp logo

golobby / orm Goto Github PK

View Code? Open in Web Editor NEW
156.0 4.0 5.0 402 KB

A lightweight yet powerful, fast, customizable, type-safe object-relational mapper for the Go programming language.

License: MIT License

Go 99.86% Makefile 0.14%
orm orm-framework golang object-relational-mapper database generics query-builder sql go

orm's Issues

New

Initilize method could be renamed to New and orm returns object instead using global instnce

My Comments

No need to Connection method in EntityConfgure since in intializing orm, you have to pass related entities to each connection you add.

in
go user, err := orm.Query[User]().Where("id", 1).One()
One() shoud be renamed to First() since all other ORMs use that name convestion.

in
go user, err := orm.Query[User]().WherePK(1).One()
Are you sure PK is always int?

in
users, err := orm.QueryRaw[User](`SELECT * FROM users`)
are you sure raw queries always have response and entity related?

Merge ConfigureRelations and ConfigureEntity into one method.

Rename to ORM

I think it's an ORM package, a data mapper one!
It maps data from database to structs and it can also extended to persist data from structs into databases.
So it should be renamed to "orm"!
Or if change o (object) to s (struct), it could have a funny name, "srm"!

New Design

A good API to me :D

db1 := sql.Open("sqlite", "path/to/hell")
db2 := sql.Open("mysql", "hell:666")

m := orm.New(orm.Config{
    Connections: []orm.Connecion{}
    DefaultConnection: "sqlite"
    Entities: []interface{}{User, Post, Comment}
});

type User struct {
    ID       int      `dbPrimaryKey:"true", dbField:"id"`
    Email string `dbField:"email"`

    // Address is one-to-one, address_id is here in users table
    AddressID  int          `dbField:"address_id"`
    Address     Address `dbRelation:"belongsTo,addresses,address_id"`

    // Meat is one-to-one, user_id is there in meta table, nothing here is users table
    Meta Meta `dbRelation:"hasOne,meta,user_id"`

    // City is one-to-many, the many is user!
    CityID int      `dbField:"city_id"`
    City    []City `dbRelation:"belongsTo,cities,city_id"`

    // Posts is one-to-many, the one is user!
    Posts []Post `dbRelation:"hasMany,posts,user_id"`

    // Roles is many-to-many
    Roles []Role `dbRelation:"belongsToMany,role_user,user_id,role_id"`
}

var u User
var us []User{}

err = m.Entity(&us).Get()
err = m.Connection("mysql").Entity(&us).Get()
err = m.Entity(&us).Limit(10).Get()

err = m.Entity(&u).Find(13)
for _, post := u.Posts { 
  //...
}

err = m.Entity(&User).Where("email", "=", "@").First()
err = m.Entity(&User).Where("email", "=", "@").OrderByDesc("id").First()

err = m.Entity(&Users).Where(func(q Query) {
              q.where("age", "<", 18)
              q.orWhere("age", ">", 50)
         }).Get()

err = m.Entity.

SQLite cipher

Hi!
How i can work with encrypted SQLite (SQLcipher)
With CGO Free

Thanks 🙏

DatabaseValidations: true gives not informative error

if err := orm.SetupConnections(orm.ConnectionConfig{
	DB:      db,
	Dialect: orm.Dialects.PostgreSQL,
	Entities: []orm.Entity{
		//...
	},
	DatabaseValidations: true,
}); err != nil {
	log.Fatalf("orm.SetupConnections: %v", err)
}
2023/10/04 14:52:44 orm.SetupConnections: pq: syntax error at or near "\"

Update documentation

In Readme we have:

 err := orm.Initialize(orm.ConnectionConfig{
    Driver:           "sqlite3",
    ConnectionString: ":memory:",
    DatabaseValidations: true,
  })

But this is the new way

err := orm.SetupConnections(orm.ConnectionConfig{
    DB: ,
    DatabaseValidations: true, 
})

Using time.Time fields results panic

panic: reflect: call of reflect.Value.Type on zero Value

goroutine 1 [running]:
reflect.Value.typeSlow({0x0?, 0x0?, 0x1400021e5b8?})
        /usr/local/go/src/reflect/value.go:2634 +0x134
reflect.Value.Type(...)
        /usr/local/go/src/reflect/value.go:2629
github.com/golobby/orm.(*binder).makeNewPointersOf(0x1400021f2d8, {0x140001461c0?, 0x1400013a0c0?, 0x3?})
        /Users/alex/go/pkg/mod/github.com/golobby/[email protected]/binder.go:16 +0xa0
github.com/golobby/orm.(*binder).makeNewPointersOf(0x1400021f2d8, {0x1027d3040?, 0x1400013a0b0?, 0x4?})
        /Users/alex/go/pkg/mod/github.com/golobby/[email protected]/binder.go:24 +0x3c0
github.com/golobby/orm.(*binder).makeNewPointersOf(0x1400021f2d8, {0x1027bda80?, 0x1400013a070?, 0x10230164c?})
        /Users/alex/go/pkg/mod/github.com/golobby/[email protected]/binder.go:24 +0x3c0
github.com/golobby/orm.(*binder).ptrsFor(0x1400013c090?, {0x1027bda80?, 0x1400013a070?, 0x1022fb534?}, {0x14000146000, 0x8, 0x102751f40?})
        /Users/alex/go/pkg/mod/github.com/golobby/[email protected]/binder.go:48 +0x30
github.com/golobby/orm.(*binder).bind(0x140003382a0?, 0x1400042c040?, {0x10276b2a0, 0x1400013a070})
        /Users/alex/go/pkg/mod/github.com/golobby/[email protected]/binder.go:114 +0x1d0
github.com/golobby/orm.(*QueryBuilder[...]).Get(0x1027e2880)
        /Users/alex/go/pkg/mod/github.com/golobby/[email protected]/query.go:78 +0xcc

I noticed that orm.Schematic() shows extra fields look related to time.Time and not exist in my structs:
Снимок экрана 2023-10-04 в 15 14 48
Снимок экрана 2023-10-04 в 15 12 22

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.