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:
npm i pg
Modify the API server's main module:
// 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 theDATABASE_URL
environment will be used
Additional options
- configuration - can be set to the
pg.PoolConfig
options object or toheroku
. When set toheroku
, 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 thesslInDev
option to true.
- Use ssl, with the
- sslInDev - see
configuration:"heroku"
Using Knex.js
Install knex and node-postgres:
npm i knex pg
Modify the API server's main module:
// 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:
npm i knex mysql2
Modify the API server's main module:
// 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:
npm i mongodb
Modify the API server's main module:
// 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
@Entity('tasks')#
class task {
@Fields.string({
dbName: '_id',
valueConverter: {
fieldTypeInDb: 'dbid',
},
})
id: string = ''
@Fields.string()
title: string = ''
}
SQLite
Install knex and sqlite3:
npm i knex sqlite3
Modify the API server's main module:
// 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:
npm i knex tedious
Modify the API server's main module:
// 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:
npm i knex oracledb
Modify the API server's main module:
// 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:
// 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
import { JsonDataProvider, Remult } from 'remult'
export const remultLocalStorage = new Remult(new JsonDataProvider(localStorage))
Session Storage
import { JsonDataProvider, Remult } from 'remult'
export const remultSessionStorage = new Remult(
new JsonDataProvider(sessionStorage),
)
Web Sql
import { Remult, SqlDatabase, WebSqlDataProvider } from 'remult'
export const remultWebSql = new Remult(
new SqlDatabase(new WebSqlDataProvider('db')),
)
In Memory object array
import { Remult, InMemoryDataProvider } from 'remult'
export const remult = new Remult(new InMemoryDataProvider())