Database Support

SonicJs uses TypeORM (https://typeorm.io/) for data access. By default, when you first install SonicJs, you will typically want to use the SQLite file based database. 

The default SQLite database that you get after cloning master is the same database used for http://sonicjs.com and is therefore a good starter for learning how to use SonicJs to bring your big idea to life!

SQLite is even suitable for production in many cases and is the most widely used database in the world. 

However, should you need to use another database, SonicJs supports the many databases below:

  • MySQL
  • PostgreSQL
  • SQLite (local file based database)
  • MariaDB
  • Microsoft SQL Server
  • Oracle
  • WebSQL
  • MongoDB
  • CockroachDB
  • SAP Hana
  • sql.js
  • Others (Any Database Supported by TypeORM)

Database Connection

The .env file at the root of the project is where you will configure your database connection. DO NOT MANUALLY CREATE THIS FILE. It is automatically generated the first time that you start up the application. Please see the samples below and check out the TypeORM connection docs for full details.

SQLite (Default Database)

Open the project environments file to set your database connection options:

[path to sonicjs]/.env:

# The path your entity files, do not modify this
TYPEORM_ENTITIES=server/data/entity/*.js

# The type of database that you want to connect to
TYPEORM_CONNECTION=sqlite

# The path to your SQLite database file
TYPEORM_DATABASE=server/data/data.sqlite

# Set synchronize to true so that your tables are created in your 
# destination database if they do not exist
TYPEORM_SYNCHRONIZE=true

# Set this to true if you are troubleshooting connection 
# issues, other leave it off
TYPEORM_LOGGING=false

MySQL

TYPEORM_CONNECTION = mysql
TYPEORM_HOST = localhost
TYPEORM_USERNAME = root
TYPEORM_PASSWORD = admin
TYPEORM_DATABASE = test
TYPEORM_PORT = 3000
TYPEORM_SYNCHRONIZE = true
TYPEORM_LOGGING = true
TYPEORM_ENTITIES = server/data/entity/*.js

Other Databases

To use a database that is different from the 2 examples above, please see the full list of environment variables below:

  • TYPEORM_CACHE
  • TYPEORM_CACHE_ALWAYS_ENABLED
  • TYPEORM_CACHE_DURATION
  • TYPEORM_CACHE_OPTIONS
  • TYPEORM_CONNECTION
  • TYPEORM_DATABASE
  • TYPEORM_DEBUG
  • TYPEORM_DRIVER_EXTRA
  • TYPEORM_DROP_SCHEMA
  • TYPEORM_ENTITIES
  • TYPEORM_ENTITIES_DIR
  • TYPEORM_ENTITY_PREFIX
  • TYPEORM_HOST
  • TYPEORM_LOGGER
  • TYPEORM_LOGGING
  • TYPEORM_MAX_QUERY_EXECUTION_TIME
  • TYPEORM_MIGRATIONS
  • TYPEORM_MIGRATIONS_DIR
  • TYPEORM_MIGRATIONS_RUN
  • TYPEORM_MIGRATIONS_TABLE_NAME
  • TYPEORM_PASSWORD
  • TYPEORM_PORT
  • TYPEORM_SCHEMA
  • TYPEORM_SID
  • TYPEORM_SUBSCRIBERS
  • TYPEORM_SUBSCRIBERS_DIR
  • TYPEORM_SYNCHRONIZE
  • TYPEORM_URL
  • TYPEORM_USERNAME
  • TYPEORM_UUID_EXTENSION

Please refer to the TypeORM connection documentation here for details.

Database FAQs

Its possible that you can. Google "typeorm some-database" and see if a connector has been built for the database that you want to use.