Skip to content

Connecting to a Database

To set a database connection for Remult, set the dataProvider property of the options argument of the remult Express middleware.

Here are examples of connecting to some commonly used back-end databases:

Remult can also be used to connect entities to local frontend databases.

Postgres

Using node-postgres (pg) only

Install node-postgres:

sh
npm i pg

Modify the API server's main module:

ts
// index.ts

import express from "express"
import { remultExpress } from "remult/remult-express"
import { createPostgresDataProvider } from "remult/postgres"

const app = express()

const connectionString = "postgres://user:password@host:5432/database"

app.use(
  remultExpress({
    dataProvider:
      createPostgresDataProvider({
        connectionString // default: process.env["DATABASE_URL"]
      })
  })
)
  • If connectionString is not provided the DATABASE_URLenvironment will be used
Additional options
  • configuration - can be set to the pg.PoolConfig options object or to heroku. When set to heroku, it'll:
    • Use ssl, with the rejectUnauthorized:false flag as required by postgres on heroku
    • Disable ssl for non production environments (process.env["NODE_ENV"] !== "production"). To use ssl also for dev, set the sslInDev option to true.
  • sslInDev - see configuration:"heroku"

Using Knex.js

Install knex and node-postgres:

sh
npm i knex pg

Modify the API server's main module:

ts
// index.ts

import express from "express"
import { remultExpress } from "remult/remult-express"
import { createKnexDataProvider } from "remult/remult-knex"

const app = express()

app.use(
  remultExpress({
    dataProvider: createKnexDataProvider({
      // Knex client configuration for Postgres
      client: "pg",
      connection: "postgres://user:password@host:5432/database"
    })
  })
)

MySQL

Install knex and mysql2:

sh
npm i knex mysql2

Modify the API server's main module:

ts
// index.ts

import express from "express"
import { remultExpress } from "remult/remult-express"
import { createKnexDataProvider } from "remult/remult-knex"

const app = express()

app.use(
  remultExpress({
    dataProvider: createKnexDataProvider({
      // Knex client configuration for MySQL
      client: "mysql2",
      connection: {
        user: "your_database_user",
        password: "your_database_password",
        host: "127.0.0.1",
        database: "test"
      }
    })
  })
)

MongoDB

Install mongodb:

sh
npm i mongodb

Modify the API server's main module:

ts
// index.ts

import express from "express"
import { remultExpress } from "remult/remult-express"
import { MongoClient } from "mongodb"
import { MongoDataProvider } from "remult/remult-mongo"

const app = express()

app.use(
  remultExpress({
    dataProvider: async () => {
      const client = new MongoClient("mongodb://localhost:27017/local")
      await client.connect()
      return new MongoDataProvider(client.db("test"), client)
    }
  })
)

Using ObjectId in Mongodb

To indicate that a column is of type object id, change it's dbFieldType to dbid

ts
@Entity('tasks')#
class task {
  @Fields.string({
    dbName: '_id',
    valueConverter: {
      fieldTypeInDb: 'dbid',
    },
  })
  id: string = ''
  @Fields.string()
  title: string = ''
}

SQLite

Install knex and sqlite3:

sh
npm i knex sqlite3

Modify the API server's main module:

ts
// index.ts

import express from "express"
import { remultExpress } from "remult/remult-express"
import { createKnexDataProvider } from "remult/remult-knex"

const app = express()

app.use(
  remultExpress({
    dataProvider: createKnexDataProvider({
      // Knex client configuration for SQLite
      client: "sqlite3",
      connection: {
        filename: "./mydb.sqlite"
      }
    })
  })
)

Microsoft SQL Server

Install knex and tedious:

sh
npm i knex tedious

Modify the API server's main module:

ts
// index.ts

import express from "express"
import { remultExpress } from "remult/remult-express"
import { createKnexDataProvider } from "remult/remult-knex"

const app = express()

app.use(
  remultExpress({
    dataProvider: createKnexDataProvider({
      // Knex client configuration for MSSQL
      client: "mssql",
      connection: {
        server: "127.0.0.1",
        database: "test",
        user: "your_database_user",
        password: "your_database_password",
        options: {
          enableArithAbort: true,
          encrypt: false,
          instanceName: `sqlexpress`
        }
      }
    })
  })
)

Oracle

Install knex and oracledb:

sh
npm i knex oracledb

Modify the API server's main module:

ts
// index.ts

import express from "express"
import { remultExpress } from "remult/remult-express"
import { createKnexDataProvider } from "remult/remult-knex"

const app = express()

app.use(
  remultExpress({
    dataProvider: createKnexDataProvider({
      // Knex client configuration for Oracle
      client: "oracledb",
      connection: {
        user: "your_database_user",
        password: "your_database_password",
        connectString: "SERVER"
      }
    })
  })
)

JSON Files

Modify the API server's main module:

ts
// index.ts

import express from "express"
import { remultExpress } from "remult/remult-express"
import { JsonDataProvider } from "remult"
import { JsonEntityFileStorage } from "remult/server"

const app = express()

app.use(
  remultExpress({
    dataProvider: async () =>
      new JsonDataProvider(new JsonEntityFileStorage("./db"))
  })
)

Note

This is the default database used by Remult if no other dataProvider is set.

Frontend Databases

Although the common use case of Remult on the front end, is to call the backend using rest api, in some use cases using a local in browser database can be useful.

Local Storage

ts
import { JsonDataProvider, Remult } from 'remult'
export const remultLocalStorage = new Remult(new JsonDataProvider(localStorage))

Session Storage

ts
import { JsonDataProvider, Remult } from 'remult'
export const remultSessionStorage = new Remult(
  new JsonDataProvider(sessionStorage),
)

Web Sql

ts
import { Remult, SqlDatabase, WebSqlDataProvider } from 'remult'
export const remultWebSql = new Remult(
  new SqlDatabase(new WebSqlDataProvider('db')),
)

In Memory object array

ts
import { Remult, InMemoryDataProvider } from 'remult'
export const remult = new Remult(new InMemoryDataProvider())

MIT Licensed | Made by the Remult team with ❤️