Code Monkey home page Code Monkey logo

Comments (7)

jinzhu avatar jinzhu commented on May 6, 2024

相对而言,gorp的应该还好一点,因为可以注册一次,以后永久使用,

beedb 是不是每次都需要设定table name?

其实现在gorm也支持设定table,不过是有点类似beedb,每次都要设定,可以看看这里

https://github.com/jinzhu/gorm#specify-table-name

我想一下永久性方案吧,做起来应该挺简单的,只是我这里没有遗留项目要转gorm,所以没有太关注这个功能。。。 ;)

from gorm.

janckerchen avatar janckerchen commented on May 6, 2024

可以考虑在struct中定义meta字段, 专门用于保存表相关数据, 内部处理时忽略该字段, 例如

type User struct{
       table_name int8  "my_users"
}

定义为int8是为了节省空间. 如果为了将来扩展, 也可以更深入一些

type User struct{
       _meta int8  "gorm:table_name=my_users;otherdata=xxxxx"
}

缺点显而易见, 每个对象都要浪费一个字节的空间, 当对象数量非常庞大时, 也可能是个问题. 不过在我自己的应用场景中倒不算什么.

from gorm.

jinzhu avatar jinzhu commented on May 6, 2024

我不太喜欢tag的方式,因为感觉他污染了struct... ;(

并且每次都处理下tag, 不知道会影响多少速度。。。

from gorm.

janckerchen avatar janckerchen commented on May 6, 2024

是的, tag非结构化数据会让人感觉代码既脆弱又不够光滑:)

from gorm.

jinzhu avatar jinzhu commented on May 6, 2024

嗯,所以我尽量不使用tag。。。 等感觉到找不到其它办法描述的时候再用它

from gorm.

jinzhu avatar jinzhu commented on May 6, 2024

我增加了几个方式,可以通过 db.SingularTable(true) 来关闭复数表名,

struct 的 TableName() 方法来设定表名,参考

https://github.com/jinzhu/gorm#specify-table-name-for-struct

这样子,你可以通过这个方法来设定任何表名了,例如可以根据用户的角色不同设定不同的表名。

from gorm.

janckerchen avatar janckerchen commented on May 6, 2024

nice, clean and readable

from gorm.

Related Issues (20)

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.