How to use beedb ORM
Last updated
Was this helpful?
Last updated
Was this helpful?
( Project beedb is no longer maintained, but the code s still there )
beedb is an ORM ( object-relational mapper ) developed in Go, by me. It uses idiomatic Go to operate on databases, implementing struct-to-database mapping and acts as a lightweight Go ORM framework. The purpose of developing this ORM is not only to help people learn how to write an ORM, but also to find a good balance between functionality and performance when it comes to data persistence.
beedb is an open source project that supports basic ORM functionality, but doesn't support association queries.
Because beedb supports database/sql
interface standards, any driver that implements this interface can be used with beedb. I've tested the following drivers:
Mysql:
PostgreSQL:
SQLite:
Mysql:
MS ADODB:
Oracle:
ODBC:
You can use go get
to install beedb locally.
First, you have to import all the necessary packages:
Then you need to open a database connection and create a beedb object (MySQL in this example):
beedb.New()
actually has two arguments. The first is the database object, and the second is for indicating which database engine you're using. If you're using MySQL/SQLite, you can just skip the second argument.
Otherwise, this argument must be supplied. For instance, in the case of SQLServer:
PostgreSQL:
beedb supports debugging. Use the following code to enable it:
Next, we have a struct for the Userinfo
database table that we used in previous sections.
Be aware that beedb auto-converts camelcase names to lower snake case. For example, if we have UserInfo
as the struct name, beedb will convert it to user_info
in the database. The same rule applies to struct field names.
The following example shows you how to use beedb to save a struct, instead of using raw SQL commands. We use the beedb Save method to apply the change.
You can check saveone.Uid
after the record is inserted; its value is a self-incremented ID, which the Save method takes care of for you.
beedb provides another way of inserting data; this is via Go's map type.
Insert multiple data:
The method shown above is similar to a chained query, which you should be familiar with if you've ever used jquery. It returns the original ORM object after calls, then continues doing other jobs.
The method SetTable
tells the ORM we want to insert our data into the userinfo
table.
Let's continue working with the above example to see how to update data. Now that we have the primary key of saveone(Uid), beedb executes an update operation instead of inserting a new record.
Like before, you can also use map for updating data:
Let me explain some of the methods used above:
.SetPK()
tells the ORM that uid
is the primary key records in the userinfo
table.
.Where()
sets conditions and supports multiple arguments. If the first argument is an integer, it's a short form for Where("<primary key>=?", <value>)
.
.Update()
method accepts a map and updates the database.
The beedb query interface is very flexible. Let's see some examples:
Example 1, query by primary key:
Example 2:
Example 3, other query conditions:
Example 4, more complex conditions:
Examples to get multiple records:
Example 1, gets 10 records with id>3
that starts with position 20:
Example 2, omits the second argument of limit, so it starts with 0 and gets 10 records:
Example 3, gets all records:
As you can see, the Limit method is for limiting the number of results.
.Limit()
supports two arguments: the number of results and the starting position. 0 is the default value of the starting position.
.OrderBy()
is for ordering results. The argument is the order condition.
All the examples here are simply mapping records to structs. You can also just put the data into a map as follows:
.Select()
tells beedb how many fields you want to get from the database table. If unspecified, all fields are returned by default.
.FindMap()
returns the []map[string][]byte
type, so you need to convert to other types yourself.
beedb provides rich methods to delete data.
Example 1, delete a single record:
Example 2, delete multiple records:
Example 3, delete records by SQL:
beedb doesn't support joining between structs. However, since some applications need this feature, here is an implementation:
We see a new method called .Join()
that has three arguments:
The first argument: Type of Join; INNER, LEFT, OUTER, CROSS, etc.
The second argument: the table you want to join with.
The third argument: join condition.
beedb also has an implementation of group by
and having
.
.GroupBy()
indicates the field that is for group by.
.Having()
indicates conditions of having.
I have received a lot of feedback on beedb from many people all around the world, and I'm thinking about reconfiguring the following aspects:
Implement an interface design similar to database/sql/driver
in order to facilitate CRUD operations.
Implement relational database associations like one to one, one to many and many to many. Here's a sample:
Auto-create tables and indexes.
Implement a connection pool using goroutines.
Previous section:
Next section: