Cloudflare D1
To use D1 as the database provider in your Remult-based application meant for deployment on Cloudflare, you'll need to setup the remult-d1 data provider.
Step 1: Install the cloudflare
package
Run the following command to install the necessary cloudflare
sh
npm i cloudflare
Step 2: Set the dataProvider
Property
In your index.ts
or whichever server file in your framework where you define the remult api, dataProvider
can be configured by:
ts
import { createD1DataProvider } from 'remult/remult-d1'
// Consult your specific framework and wrangler.jsonc
// for how to obtain D1_BINDING.
// Cloudflare typically provides it via server environment variables
const dataProvider = createD1DataProvider(D1_BINDING)
// If you're using d1-http instead,
// then create the database provider by providing the appropriate credentials
const dataProviderHttp = createD1HttpDataProvider(){
accountId: "--cloudflare--account--id",
apiToken: "--cloudflare--api--token",
//get this from wrangler.jsonc or cloudflare dashboard
databaseId: "--the--uuid--of--this--database"
}
Example in the framework SolidStart
ts
// api.ts
import { remultApi as solidStartRemultApi } from "remult/remult-solid-start"
import { createD1DataProvider } from "remult/remult-d1"
function getServerEnv() {
"user server"
const event = getRequestEvent()
return event.nativeEvent.context.cloudflare?.env as Env
}
// assumption: you have MY_D1 defined for your cloudflare project in wrangler.jsonc
export const remultApi = solidStartRemultApi({
entities: [...],
controllers: [...],
dataProvider: createD1DataProvider(getServerEnv.MY_D1),
})