SolidJS + MongoDB
MongoDB with Mongoose: a model, /api/todos routes and a local docker-compose. This page shows exactly what pickstack adds to a SolidJS project for MongoDB: the packages, the new files and the changes to existing ones. Every combination is installed, built, linted and tested in CI.
$ npm create pickstack@latest my-app -- --framework solid --modules express,mongodb --yesCustomize in the configurator
New files
# Local MongoDB for development: docker compose up -d
services:
mongo:
image: mongo:8
ports:
- "27017:27017"
volumes:
- mongo-data:/data/db
volumes:
mongo-data:
PORT=3001 MONGODB_URI=mongodb://localhost:27017/my-app
node_modules dist .env
{
"name": "my-app-server",
"private": true,
"version": "0.1.0",
"type": "module",
"scripts": {
"dev": "tsx watch --env-file-if-exists=.env src/index.ts",
"build": "tsc",
"start": "node --env-file-if-exists=.env dist/index.js",
"typecheck": "tsc --noEmit"
},
"dependencies": {
"express": "^5.3.0",
"mongoose": "^9.13.1"
},
"devDependencies": {
"@types/express": "^5.0.6",
"@types/node": "^24.19.2",
"tsx": "^4.23.15",
"typescript": "~6.0.3"
},
"engines": {
"node": ">=22.12.0"
}
}
# my-app-server
Express 5 API on port 3001, with MongoDB (Mongoose). Copy `.env.example` to `.env` to change settings.
```bash
npm install
npm run dev
```
import express from "express";
import { Todo } from "./models/todo.js";
export const app = express();
app.use(express.json());
app.get("/api/health", (_req, res) => {
res.json({ ok: true });
});
app.get("/api/hello", (req, res) => {
const name = typeof req.query.name === "string" ? req.query.name : "world";
res.json({ message: `Hello, ${name}!` });
});
app.get("/api/todos", async (_req, res) => {
const todos = await Todo.find().sort({ createdAt: -1 }).limit(100);
res.json(todos);
});
app.post("/api/todos", async (req, res) => {
const title =
typeof req.body?.title === "string" ? req.body.title.trim() : "";
if (!title) {
res.status(400).json({ error: "title is required" });
return;
}
const todo = await Todo.create({ title });
res.status(201).json(todo);
});
import mongoose from "mongoose";
/** Opens the connection Mongoose models use; call it once, before serving requests. */
export async function connectDb(uri: string) {
await mongoose.connect(uri);
console.log(`Connected to ${mongoose.connection.name}`);
}
import process from "node:process";
import { app } from "./app.js";
import { connectDb } from "./db.js";
const port = Number(process.env.PORT ?? 3001);
await connectDb(process.env.MONGODB_URI ?? "mongodb://localhost:27017/my-app");
app.listen(port, () => {
console.log(`API listening on http://localhost:${port}`);
});
import { model, Schema } from "mongoose";
const todoSchema = new Schema(
{
title: { type: String, required: true, trim: true, maxlength: 200 },
done: { type: Boolean, default: false },
},
{ timestamps: true },
);
export const Todo = model("Todo", todoSchema);
{
"compilerOptions": {
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext",
"rootDir": "src",
"outDir": "dist",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"verbatimModuleSyntax": true,
"types": ["node"]
},
"include": ["src"]
}
node_modules dist .env .env.local .DS_Store
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>my-app</title>
</head>
<body>
<div id="root"></div>
<script type="module" src="/src/index.tsx"></script>
</body>
</html>
{
"name": "my-app",
"private": true,
"version": "0.1.0",
"type": "module",
"scripts": {
"dev": "vite",
"build": "tsc -b && vite build",
"preview": "vite preview",
"typecheck": "tsc -b"
},
"dependencies": {
"solid-js": "^1.9.20"
},
"devDependencies": {
"@types/node": "^24.19.2",
"typescript": "~6.0.3",
"vite": "^8.3.8",
"vite-plugin-solid": "^2.11.17"
}
}
# my-app
Stack: SolidJS, Vite, TypeScript, Express, MongoDB.
## Getting started
```bash
npm install
npm run dev
```
## Scripts
- `npm run build`
- `npm run preview`
- `npm run typecheck`
## API
The API lives in `../server` (Express). Start it with `npm run dev` there; requests to `/api` from this app are proxied to it in development.
## Push to GitHub
Create an empty repository on GitHub, then:
```bash
git init
git add .
git commit -m "Initial commit"
git branch -M main
git remote add origin https://github.com/<your-username>/my-app.git
git push -u origin main
```
import ApiGreeting from "./components/ApiGreeting";
import Counter from "./components/Counter";
export default function App() {
return (
<main class="app">
<h1 class="title">my-app</h1>
<Counter />
<ApiGreeting />
</main>
);
}
import { createSignal, onMount } from "solid-js";
export default function ApiGreeting() {
const [message, setMessage] = createSignal("Calling the API…");
onMount(() => {
fetch("/api/hello?name=my-app")
.then((response) => {
if (!response.ok) throw new Error(`HTTP ${response.status}`);
return response.json();
})
.then((data) => setMessage(data.message))
.catch(() => setMessage("The API isn't running: start it in server/."));
});
return <p class="text">{message()}</p>;
}
import { createSignal } from "solid-js";
export default function Counter() {
const [count, setCount] = createSignal(0);
return (
<div class="card">
<p class="count">{count()}</p>
<div class="row">
<button
type="button"
class="btn"
onClick={() => setCount((value) => value - 1)}
>
-1
</button>
<button
type="button"
class="btn"
onClick={() => setCount((value) => value + 1)}
>
+1
</button>
</div>
</div>
);
}
:root {
font-family: system-ui, sans-serif;
color: #0f172a;
background: #f8fafc;
}
body {
margin: 0;
}
.app {
min-height: 100vh;
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
gap: 2rem;
padding: 2rem;
box-sizing: border-box;
}
.title {
font-size: 1.875rem;
margin: 0;
}
.nav {
display: flex;
gap: 1rem;
}
.link {
color: #475569;
text-decoration: none;
font-weight: 500;
}
.link:hover {
color: #0f172a;
}
.page {
display: flex;
flex-direction: column;
align-items: center;
gap: 1.5rem;
}
.text {
max-width: 28rem;
text-align: center;
color: #475569;
}
.card {
background: #fff;
padding: 2rem;
border-radius: 1rem;
box-shadow: 0 1px 3px rgba(15, 23, 42, 0.08);
text-align: center;
}
.count {
font-size: 3rem;
font-weight: 700;
margin: 0;
font-variant-numeric: tabular-nums;
}
.row {
margin-top: 1.5rem;
display: flex;
justify-content: center;
gap: 0.75rem;
}
.btn {
background: #0f172a;
color: #fff;
border: 0;
border-radius: 0.5rem;
padding: 0.5rem 1rem;
font: inherit;
cursor: pointer;
}
.btn:hover {
background: #334155;
}
/* @refresh reload */
import { render } from "solid-js/web";
import App from "./App";
import "./index.css";
const root = document.getElementById("root");
render(() => <App />, root!);
{
"compilerOptions": {
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.app.tsbuildinfo",
"target": "es2023",
"module": "esnext",
"lib": ["ES2023", "DOM"],
"types": ["vite/client"],
"allowArbitraryExtensions": true,
"skipLibCheck": true,
"moduleResolution": "bundler",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"moduleDetection": "force",
"noEmit": true,
"jsx": "preserve",
"jsxImportSource": "solid-js",
"noUnusedLocals": true,
"noUnusedParameters": true,
"erasableSyntaxOnly": true,
"noFallthroughCasesInSwitch": true
},
"include": ["src"]
}
{
"files": [],
"references": [
{
"path": "./tsconfig.app.json"
},
{
"path": "./tsconfig.node.json"
}
]
}
{
"compilerOptions": {
"tsBuildInfoFile": "./node_modules/.tmp/tsconfig.node.tsbuildinfo",
"target": "es2023",
"lib": ["ES2023"],
"types": ["node"],
"skipLibCheck": true,
"module": "nodenext",
"allowImportingTsExtensions": true,
"verbatimModuleSyntax": true,
"moduleDetection": "force",
"noEmit": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"erasableSyntaxOnly": true,
"noFallthroughCasesInSwitch": true
},
"include": ["vite.config.ts"]
}
import { defineConfig } from "vite";
import solid from "vite-plugin-solid";
export default defineConfig({
plugins: [solid()],
server: { proxy: { "/api": "http://localhost:3001" } },
});
Project files
README.mddocker-compose.ymlserver/.env.exampleserver/.gitignoreserver/README.mdserver/package.jsonserver/src/app.tsserver/src/db.tsserver/src/index.tsserver/src/models/todo.tsserver/tsconfig.jsonweb/.gitignoreweb/README.mdweb/index.htmlweb/package.jsonweb/src/App.tsxweb/src/components/ApiGreeting.tsxweb/src/components/Counter.tsxweb/src/index.cssweb/src/index.tsxweb/tsconfig.app.jsonweb/tsconfig.jsonweb/tsconfig.node.jsonweb/vite.config.ts