Database
Add the survey tables to the database you already use.
Add dimah_survey, dimah_survey_version, and dimah_response to your
schema, migrate with the tool you already use, and pass the same database to
both server audiences. dimah-survey does not run migrations.
npm i @dimah-survey/db fumadbModel keys are survey, surveyVersion, and response. Copy the tables into
your application. Do not import them from @dimah-survey/db at runtime.
Add these tables to your Drizzle schema. This copy is SQLite. On PostgreSQL or
MySQL, use that driver's table builder and set provider to match. Keep the
partial unique index.
import { defineRelations, sql } from "drizzle-orm";
import {
blob,
check,
foreignKey,
index,
integer,
sqliteTable,
text,
uniqueIndex,
} from "drizzle-orm/sqlite-core";
export const survey = sqliteTable(
"dimah_survey",
{
id: text("id", { length: 255 }).primaryKey().notNull(),
slug: text("slug", { length: 255 }).notNull(),
status: text("status").notNull(),
draftJson: blob("draft_json", { mode: "json" }).notNull(),
publishedVersionId: text("published_version_id", { length: 255 }),
publishedAt: integer("published_at", { mode: "timestamp" }),
settings: blob("settings", { mode: "json" }).notNull(),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.defaultNow(),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.defaultNow(),
},
(table) => [
uniqueIndex("dimah_survey_slug_unique").on(table.slug),
check(
"dimah_survey_status_check",
sql`${table.status} in ('draft', 'active', 'archived')`,
),
index("dimah_survey_status_updated_at_idx").on(
table.status,
table.updatedAt,
),
],
);
export const surveyVersion = sqliteTable(
"dimah_survey_version",
{
id: text("id", { length: 255 }).primaryKey().notNull(),
surveyId: text("survey_id", { length: 255 }).notNull(),
definition: blob("definition", { mode: "json" }).notNull(),
contentHash: text("content_hash", { length: 64 }).notNull(),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.defaultNow(),
},
(table) => [
foreignKey({
columns: [table.surveyId],
foreignColumns: [survey.id],
name: "dimah_survey_version_survey_fk",
})
.onUpdate("restrict")
.onDelete("restrict"),
uniqueIndex("dimah_survey_version_survey_hash").on(
table.surveyId,
table.contentHash,
),
index("dimah_survey_version_survey_id_created_at_idx").on(
table.surveyId,
table.createdAt,
),
],
);
export const response = sqliteTable(
"dimah_response",
{
id: text("id", { length: 255 }).primaryKey().notNull(),
surveyId: text("survey_id", { length: 255 }).notNull(),
respondentId: text("respondent_id", { length: 255 }),
status: text("status").notNull(),
versionId: text("version_id", { length: 255 }).notNull(),
data: blob("data", { mode: "json" }).notNull(),
submittedAt: integer("submitted_at", { mode: "timestamp" }),
createdAt: integer("created_at", { mode: "timestamp" })
.notNull()
.defaultNow(),
updatedAt: integer("updated_at", { mode: "timestamp" })
.notNull()
.defaultNow(),
},
(table) => [
foreignKey({
columns: [table.surveyId],
foreignColumns: [survey.id],
name: "dimah_response_survey_fk",
})
.onUpdate("restrict")
.onDelete("restrict"),
foreignKey({
columns: [table.versionId],
foreignColumns: [surveyVersion.id],
name: "dimah_response_version_fk",
})
.onUpdate("restrict")
.onDelete("restrict"),
check(
"dimah_response_status_check",
sql`${table.status} in ('draft', 'submitted', 'abandoned')`,
),
index("dimah_response_survey_id_updated_at_idx").on(
table.surveyId,
table.updatedAt,
),
index("dimah_response_respondent_lookup_idx").on(
table.surveyId,
table.respondentId,
table.status,
),
uniqueIndex("dimah_response_one_open_draft")
.on(table.surveyId, table.respondentId)
.where(
sql`${table.status} = 'draft' and ${table.respondentId} is not null`,
),
],
);
export const privateDimahSurveySettings = sqliteTable(
"private_dimah_survey_settings",
{
id: text("id", { length: 255 }).primaryKey().notNull(),
version: text("version", { length: 255 }).notNull().default("1.0.0"),
},
);
export const relations = defineRelations(
{ survey, surveyVersion, response, privateDimahSurveySettings },
(helpers) => ({
survey: {
versions: helpers.many.surveyVersion({
from: helpers.survey.id,
to: helpers.surveyVersion.surveyId,
}),
responses: helpers.many.response({
from: helpers.survey.id,
to: helpers.response.surveyId,
}),
},
surveyVersion: {
survey: helpers.one.survey({
from: helpers.surveyVersion.surveyId,
to: helpers.survey.id,
}),
responses: helpers.many.response({
from: helpers.surveyVersion.id,
to: helpers.response.versionId,
}),
},
response: {
survey: helpers.one.survey({
from: helpers.response.surveyId,
to: helpers.survey.id,
}),
version: helpers.one.surveyVersion({
from: helpers.response.versionId,
to: helpers.surveyVersion.id,
}),
},
}),
);import { DimahSurveyDB, db } from "@dimah-survey/db";
import { drizzleAdapter } from "fumadb/adapters/drizzle";
export const database = db(
DimahSurveyDB.client(
drizzleAdapter({ db: drizzleDb, provider: "sqlite" }),
),
);drizzleDb is the Drizzle client you already open. Pass relations from the
schema into that client.
The store accepts provider: "mysql", but this page does not include a MySQL
schema. Translate the tables and indexes before connecting the adapter.
Next.js
Keep the store on the Node.js runtime. The last entry is the ORM package that
opens the connection: drizzle-orm, @prisma/client, or kysely.
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
serverExternalPackages: ["@dimah-survey/db", "fumadb", "drizzle-orm"],
};
export default nextConfig;In memory
memoryAdapter() from @dimah-survey/server stores rows in the process. They
disappear when the process exits. Use it for tests, or when you do not want
the SQL tables. Pass it as database the same way as the SQL store.
import { memoryAdapter } from "@dimah-survey/server";
export const database = memoryAdapter();Custom store
import type { SurveyStore } from "@dimah-survey/core";Prop
Type
Start and submit callbacks are server-only. They are never accepted from the HTTP body:
import type {
StartResponseLifecycle,
SubmitResponseLifecycle,
} from "@dimah-survey/core";Prop
Type
Prop
Type
A conforming store keeps draftJson, the published survey, and settings
independent. New surveys receive default settings; only saveSurveySettings
changes them afterward. Publish stores the document and reuses that row when
it is unchanged. At start, the response stores that document's id and never
changes it. Partial save replaces data with the object the server passes.
Apply these inside the same lock or transaction as the write:
- Compare
expectedUpdatedAtinside the write. - Apply start reuse, the collection window, the response cap, and the one-open-draft rule there too.
- Keep one draft per survey and identified respondent. The partial unique index is the database backstop.
- Run the start and submit callbacks inside that same boundary.
- Anonymous start always inserts. Identified start follows
"one-open"or"single". - Start, partial save, and submit enforce the collection window. New starts and submits enforce the response cap.
- Reopen enforces
reopenand refuses a second identified draft. - Lists are paginated. Survey and response lists sort by
updatedAtdescending. Summaries omitdataand includeversionId.