Currently I'm using following commands at the beginning of each function that accesses the database.
o := orm.NewOrm()
o.Using("default") // Using default, you can use other database
It feels like I should do that only once at router initialization. Can that be a security issue?
In part 1, we made a good start, understanding and working with Beego by installing Beego and the command line tool Bee, creating a basic project, adding a controller action, creating a basic view template, adding a custom route and finished up learning how to work with request parameters.
An example of beego/orm is set out below. All the code samples in this section are based on this example unless otherwise stated. The ORM instance should be stateless, so it’s now thread safe. ORM supports three popular databases. Here are the tested drivers, you need to import them: ORM must register a database with alias default.
The best practice is to have a single models.go file and register in it’s init function. RegisterModel can register multiple models at the same time: You may want Beego to automatically create your database tables. One way to do this is by using the method described in the cli documentation. in your main.go file in your main block.
One way to do this is by writing models in your application and have these models transform the database into the state it needs. Beego allows you to do this with the orm.RunSyncdb function. Another way to keep the models and database in sync (this is the way I prefer) is by having the DB be the source of truth.
You are doing it correctly already. If you are using the default named database you can even omit the second statement. orm.NewOrm sets up a new relationship, not necessarily a new database connection. This relationship is necessary in the controller so that your data can be mapped to the correct database calls eventually when another command is issued. As far as I have seen this does not pose any security issues.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With