Bearcat-dao — a SQL mapping dao framework
bearcat-dao is a DAO (domain access objects) framework for node.js. It implements SQL mapping as its main concept compared to O/R mapping, therefore SQL is still the main concern using with bearcat-dao, and bearcat-dao will map the datebase resultset into bearcat model object.
SQL mapping vs O/R mapping
Structured Query Language (SQL) has been around for a long time, relational database and SQL have been claimed to have stood the test of time. Moreover, we have experiences whereby the database and even the SQL itself have outlived the application source code, and even mulitiple versions of the source code.
SQL mapping is on the idea that there is value in relational database and SQL, developers write SQL and maps data resultsets into objects. Therefore, it is easy for enterprise application to optimize, reuse SQL, maintain.
In another way, O/R mapping enables developers to write mapping object to database table, ORM framework then generates the specific SQL to execute on the database. So, as we can see, developers have to take great knowledge of the ORM framework in order to use the database well, especially when optimization is needed.
model definition is using bearcat model
therefore it is easy to be mapped into table and setup constraint, relation
for example, if we have a test table with single primary id
the we can define the following model
in the TestModel, we use $table attribute to setup the mapping table name, in id attribute we use primary to mark it as a primary key, then we add with a type constraint
Tables in relational database can have relations, there are one-to-one relation, one-to-many relation, many-to-many relation
One-to-one relation means in two models, one model has the reference of the other model
for example, if we have a test1 table with primary id and reference id of the test2 table
then we can define the following two models
as we can see, in Test1Model.rid attribute we use ref:test2Model to set the reference to test2Model
One-to-many relation means one model refer to the array of other model. In the real world, for example, we can have a blog, and a blog have many commnets, so blog and comment are one-to-many relation.
therefore, in the above example, we simply modify the rid attribute type to Array, it becomes a one-to-many relation
many-to-many relation can be spilted into two one-many relation through middle table
When writing complex sql, it is not quite well writing as a String, the better way is using SQL template.
write SQL tempalte is easy
for example, we can define SQL template with id testResultSql
then we can use this sql in dao, like this
in domainDaoSupport.getList api, the first argument can be SQL tempalte id, the second argument is the SQL arguments, the third argument is the SQL result mapping model id, then in the callback function, we can get the results which are already mapped with testModel array
Moreover, SQL template can include other SQL template
then testResultSql template is equal to the above
ResultSet is an array of field/value objects, therefore the process of mapping resultSet is like filling objects with specific key/value pairs, to make the key match with the resultSet, we can use prefix in model magic attribute value or use prefix in model attribute to mark all attributes in this model will be prefixed
for example, if you query for a resultSet like this
then mapping model can be like this
if your resultSet is prefixed with blog_ like this
then mapping model will be like this
just add this.$prefix model attribute
DAO is short for domain access object, we can use DAO objects to manage database
bearcat-dao provides domainDaoSupport wrapping basic sql and cache operations
add it with properties dependency injection, and init it by invoking initConfig method
then you can use domainDaoSupport convenient methods to wrap your own daos
api reference for domainDaoSupport
add bearcat-dao to your project
modify context.json used by your project
placeholds can be nicely used to switch between contexts
if you do not use redis, you can remove redisConnectionManager definition
bearcat-dao provides transaction support based on bearcat AOP. The aspect is transactionAspect which provides around advice, when target transaction method calls cb function with err, rollback will be emited, otherwise it will commit the operations.
The pointcut defined is:
Therefore, any POJO method match this pointcut can a transcation method
Since transaction must be within the same connection, in Bearcat-dao it is transactionStatus, daos under the transaction method must hold the same transactionStatus
Enable Debug Mode
run with BEARCAT_DEBUG flag true