Database Access Strategy

Since CabloyJS has three built-in running environments, we need to configure different MySQL parameters for different running environments. Before introducing MySQL parameter configuration, we also need to introduce the concept of database access strategy

In order to further optimize the daily development experience and improve the development efficiency, CabloyJS defines a database access strategy concept, as follows:

1. Unit-test Environment

It is not necessary to specify the database name in the unit-test environment, but the system automatically creates the test database. Whenever unit-test executed, the old test database is always deleted and a new test database is created

  1. 1# recreate database + unit-test
  2. 2$ npm run test:backend
  3. 3# or
  4. 4# recreate database only
  5. 5$ npm run db:reset

For example, the name of the project is cabloy-demo. When unit-test executed, the system will automatically create a test database which name is egg-born-test-calloy-demo-20210703-110520

  • The advantage of this design: During the development iteration of the project, it is inevitable to frequently modify the database schema. The general approach is to open the database management tool for manual modification. In CabloyJS, you only need to write the change logics in the bean component version.manager of the module, and then execute the unit-test once to take effect. In other words, database management tools are rarely used in CabloyJS development

  • Someone will ask a question: If the unit-test will recreate a new database, do you need to manually import the old test data again?

    • To solve this problem, CabloyJS also proposed a corresponding solution: The initialization logics of test data is written directly in the bean component version.manager. Thus, these test data are automatically generated whenever a new test database is created

About the bean component version.manager of the module, see also: Module Data Version & Modification

2. Development Environment

It is also unnecessary to specify the database name in the development environment. Whenever you start the backend development service, always find the available test database. If it is not found (for example, no unit test has been performed), automatically create a new test database

  1. 1# start backend development service
  2. 2$ npm run dev:backend

3. Production Environment

In the production environment, you need to manually create a database and specify the database name in the file of config.prod.js

  1. 1# start service
  2. 2$ npm run start:backend
  3. 3# start service (background running)
  4. 4$ npm run start:backend-daemon
  5. 5# stop service
  6. 6$ npm run stop:backend

MySQL Config

Take notice: The following are the default configurations, which generally do not need to be changed. Just ensure that the host, port, user and password conform to the actual values

1. Unit-test Environment

src/backend/config/config.unittest.js

  1. 1// mysql
  2. 2 config.mysql = {
  3. 3 clients: {
  4. 4 // donnot change the name
  5. 5 __ebdb: {
  6. 6 host: '127.0.0.1',
  7. 7 port: '3306',
  8. 8 user: 'root',
  9. 9 password: '',
  10. 10 database: 'mysql', // donnot change the name
  11. 11 },
  12. 12 },
  13. 13 };
Name Description
__ebdb This is the built-in MySQL node name of the system and cannot be changed
database It is specified as mysql. When the unit-test is executed, the old test database will be deleted automatically, and then a new test database will be created

2. Development Environment

src/backend/config/config.local.js

  1. 1// mysql
  2. 2 config.mysql = {
  3. 3 clients: {
  4. 4 // donnot change the name
  5. 5 __ebdb: {
  6. 6 host: '127.0.0.1',
  7. 7 port: '3306',
  8. 8 user: 'root',
  9. 9 password: '',
  10. 10 database: 'mysql', // recommended
  11. 11 },
  12. 12 },
  13. 13 };
Name Description
database It is recommended to specify mysql. When starting backend development service, search for available test database. If not, create a new test database. If other specific database name are specified, this database will be used directly

3. Production Environment

src/backend/config/config.prod.js

  1. 1// mysql
  2. 2 config.mysql = {
  3. 3 clients: {
  4. 4 // donnot change the name
  5. 5 __ebdb: {
  6. 6 host: '127.0.0.1',
  7. 7 port: '3306',
  8. 8 user: 'root',
  9. 9 password: '',
  10. 10 database: '{{name}}',
  11. 11 },
  12. 12 },
  13. 13 };
Name Description
database This database needs to be created manually

SQL Output

Only one parameter needs to be specified in CabloyJS to output SQL statements, so that we can easily troubleshoot SQL problems

src/backend/config/config.{env}.js

  1. 1// mysql
  2. 2 config.mysql = {
  3. 3 clients: {
  4. 4 // donnot change the name
  5. 5 __ebdb: {
  6. 6 ...
  7. 7 hook: {
  8. 8 meta: {
  9. 9 long_query_time: 200,
  10. 10 },
  11. 11 },
  12. 12 },
  13. 13 },
  14. 14 };
Name Unit Description
long_query_time ms When the SQL statement execution time exceeds this value, it will be output to the console. If set to 0, it is equivalent to outputting all SQL statements