golobby / orm Goto Github PK
View Code? Open in Web Editor NEWA lightweight yet powerful, fast, customizable, type-safe object-relational mapper for the Go programming language.
License: MIT License
A lightweight yet powerful, fast, customizable, type-safe object-relational mapper for the Go programming language.
License: MIT License
Initilize method could be renamed to New and orm returns object instead using global instnce
Hi folks,
I wonder why you do not organize this project and do not create some directories to separate it; it would help the developer see this repository clearly. This facility event can help the developer in another language to get information better.
Example like this: https://github.com/elixir-ecto/ecto/tree/master/lib
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.
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"!
As I see we have to run
orm.Initialize
in order to connect
my question is how do I connect to more then 1 db at the same time
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.
In readmy I see that postgres is supported, but just insert row
is returned like this: LastInsertId is not supported by this driver
(used by pq)
A simple google search for me: https://stackoverflow.com/questions/33382981/go-how-to-get-last-insert-id-on-postgresql-with-namedexec
I think for postgres the insert query needs to be changed for the used PK return... thoughts?
Hi!
How i can work with encrypted SQLite (SQLcipher)
With CGO Free
Thanks 🙏
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 "\"
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,
})
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:
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.