Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Beego: Creating a new orm before every request?

Tags:

orm

go

beego

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?

like image 668
Michael Avatar asked Sep 29 '15 11:09

Michael


People also ask

How do I work with beego?

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.

Is beego/Orm thread safe?

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.

How do I register multiple models in beego?

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.

How do I sync a database with a beego application?

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.


1 Answers

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.

like image 136
Diablojoe Avatar answered Sep 28 '22 03:09

Diablojoe