SQL (without ORM)

The SQL data source allows importing tables from SQL databases.

Each table in the database will be mapped to a collection in Forest Admin.

Installation

To make everything work as expected, you need to:

  • install the package @forestadmin/datasource-sql.

  • install the native drivers for the vendors you are aiming to connect to.

Vendor
Install extra package

MariaDB

mariadb

Microsoft SQL Server

tedious

MySQL

mysql2

Oracle

oracledb

PostgreSQL

pg + pg-hstore

SQLite

sqlite3

Usage

Get started by using the following code sample in your agent.

You can then tune the configuration to your needs.

const { createAgent } = require('@forestadmin/agent');
const { createSqlDataSource } = require('@forestadmin/datasource-sql');

// Create agent and import collections from SQL database
const agent = createAgent(options).addDataSource(
  createSqlDataSource({
    uri: 'postgres://user:pass@localhost:5432/myDatabase',
    sslMode: 'preferred',
  }),
);

Automatic schema discovery

By default, when using this data source, there is no need to provide a schema, as the agent will extract the list of tables, columns, and relations when the agent restarts.

However, to be able to work, the credentials which are used must be able to access the information_schema or the database. Using a role that has ownership of the tables you would like to use in ForestAdmin is highly recommended.

The introspection only supports tables from Microsoft SQL Server, PostgreSQL, MySQL, and MariaDB (views and materialized views will be ignored).

If you wish to use a different database, or have your SQL views and materialized views available in Forest Admin, you will need to provide the schema yourself.

Example: Caching the introspection

If your database schema does not change or if you want to skip the introspection step when running in production, you may want to cache it.

To do so, you can call the introspection function yourself and pass the result to the data source constructor.

Note that you will need to delete the cache file if you change the database structure.

Example: Using a different account for the schema introspection and the agent

You may be running forest admin with database credentials that have restricted access to the database. In this case, you will need to provide a different set of credentials to the data source to be able to introspect the database.

Advanced configuration

Configuration can be as simple as passing a URI to the data source constructor, but more options are available:

Note that under the hood, the data source uses Sequelize ↗ to connect to the database. So, you can pass any option that is supported by Sequelize.

Last updated

Was this helpful?