Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add drizzle-orm example #358

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions drizzle-orm/.eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/** @type {import('eslint').Linter.Config} */
module.exports = {
extends: ["@remix-run/eslint-config", "@remix-run/eslint-config/node"],
};
7 changes: 7 additions & 0 deletions drizzle-orm/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
node_modules

/.cache
/build
/public/build
.env
*.db
22 changes: 22 additions & 0 deletions drizzle-orm/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Drizzle ORM Example

In this example we will setup Drizzle ORM with Remix.

## Preview

Open this example on [CodeSandbox](https://codesandbox.com):

[![Open in CodeSandbox](https://codesandbox.io/static/img/play-codesandbox.svg)](https://codesandbox.io/s/github/remix-run/examples/tree/main/drizzle-orm)

## Example

Relevant files:

- [drizzle](./drizzle) migrations and drizzle-related meta information
- [app/db.server.ts] setup database client
- [app/schema.ts] define database schema
- [package.json] add `generate` script and drizzle dependencies

## Related Links

[Drizzle ORM](https://orm.drizzle.team)
10 changes: 10 additions & 0 deletions drizzle-orm/app/db.server.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import Database from "better-sqlite3";
import { drizzle } from "drizzle-orm/better-sqlite3";
import { migrate } from "drizzle-orm/better-sqlite3/migrator";
import * as schema from "./schema";

const sqlite = new Database("sqlite.db");

export const db = drizzle(sqlite, { schema });

migrate(db, { migrationsFolder: "drizzle" });
32 changes: 32 additions & 0 deletions drizzle-orm/app/root.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import type { MetaFunction } from "@remix-run/node";
import {
Links,
LiveReload,
Meta,
Outlet,
Scripts,
ScrollRestoration,
} from "@remix-run/react";

export const meta: MetaFunction = () => ({
charset: "utf-8",
title: "New Remix App",
viewport: "width=device-width,initial-scale=1",
});

export default function App() {
return (
<html lang="en">
<head>
<Meta />
<Links />
</head>
<body>
<Outlet />
<ScrollRestoration />
<Scripts />
<LiveReload />
</body>
</html>
);
}
61 changes: 61 additions & 0 deletions drizzle-orm/app/routes/_index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { type ActionArgs } from "@remix-run/node";
import { Form, useLoaderData } from "@remix-run/react";
import { sql } from "drizzle-orm";
import { db } from "~/db.server";
import { example } from "~/schema";

export async function action({ request }: ActionArgs) {
const formData = await request.formData();
const intent = formData.get("intent");

if (intent === "add") {
await db.insert(example).values({});
return new Response(null, { status: 201 });
}

if (intent == "reset") {
await db.delete(example);
return new Response(null, { status: 204 });
}

return new Response(null, { status: 400 });
}

export async function loader() {
const rowsQuery = db.query.example.findMany();

const lastUpdatedQuery = db
.select({ updated: sql<string>`MAX(created_at)` })
.from(example)
.execute()
.then((rows) => rows[0].updated);

return {
rows: await rowsQuery,
lastUpdated: await lastUpdatedQuery,
};
}

export default function Index() {
const { rows, lastUpdated } = useLoaderData<typeof loader>();

return (
<div style={{ fontFamily: "system-ui, sans-serif", lineHeight: "1.8" }}>
<h1>Welcome to Remix</h1>
<p>Last Updated: {lastUpdated ?? "--"}</p>
<Form method="post">
<button type="submit" name="intent" value="add">
Add Row
</button>
<button type="submit" name="intent" value="reset">
Reset
</button>
</Form>
<ul>
{rows.map((row) => (
<li key={row.id}>{row.id}</li>
))}
</ul>
</div>
);
}
7 changes: 7 additions & 0 deletions drizzle-orm/app/schema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { sql } from 'drizzle-orm'
import { sqliteTable, integer, text } from "drizzle-orm/sqlite-core";

export const example = sqliteTable('example', {
id: integer('id').primaryKey(),
createdAt: text('created_at').default(sql`CURRENT_TIMESTAMP`),
})
6 changes: 6 additions & 0 deletions drizzle-orm/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import type { Config } from "drizzle-kit";

export default {
schema: "./app/schema.ts",
out: "./drizzle",
} satisfies Config;
4 changes: 4 additions & 0 deletions drizzle-orm/drizzle/0000_mute_groot.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
CREATE TABLE `example` (
`id` integer PRIMARY KEY NOT NULL,
`created_at` text DEFAULT CURRENT_TIMESTAMP
);
38 changes: 38 additions & 0 deletions drizzle-orm/drizzle/meta/0000_snapshot.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
{
"version": "5",
"dialect": "sqlite",
"id": "0be79972-b6bf-4225-88d7-95a723174903",
"prevId": "00000000-0000-0000-0000-000000000000",
"tables": {
"example": {
"name": "example",
"columns": {
"id": {
"name": "id",
"type": "integer",
"primaryKey": true,
"notNull": true,
"autoincrement": false
},
"created_at": {
"name": "created_at",
"type": "text",
"primaryKey": false,
"notNull": false,
"autoincrement": false,
"default": "CURRENT_TIMESTAMP"
}
},
"indexes": {},
"foreignKeys": {},
"compositePrimaryKeys": {},
"uniqueConstraints": {}
}
},
"enums": {},
"_meta": {
"schemas": {},
"tables": {},
"columns": {}
}
}
13 changes: 13 additions & 0 deletions drizzle-orm/drizzle/meta/_journal.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"version": "5",
"dialect": "sqlite",
"entries": [
{
"idx": 0,
"version": "5",
"when": 1697441145549,
"tag": "0000_mute_groot",
"breakpoints": true
}
]
}
34 changes: 34 additions & 0 deletions drizzle-orm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"private": true,
"sideEffects": false,
"scripts": {
"build": "remix build",
"dev": "remix dev",
"generate": "drizzle-kit generate:sqlite",
"start": "remix-serve build",
"typecheck": "tsc"
},
"dependencies": {
"@remix-run/node": "^1.19.3",
"@remix-run/react": "^1.19.3",
"@remix-run/serve": "^1.19.3",
"better-sqlite3": "^9.0.0",
"drizzle-orm": "^0.28.6",
"isbot": "^3.6.5",
"react": "^18.2.0",
"react-dom": "^18.2.0"
},
"devDependencies": {
"@remix-run/dev": "^1.19.3",
"@remix-run/eslint-config": "^1.19.3",
"@types/better-sqlite3": "^7.6.5",
"@types/react": "^18.0.25",
"@types/react-dom": "^18.0.8",
"drizzle-kit": "^0.19.13",
"eslint": "^8.27.0",
"typescript": "^4.8.4"
},
"engines": {
"node": ">=14.0.0"
}
}
Binary file added drizzle-orm/public/favicon.ico
Binary file not shown.
11 changes: 11 additions & 0 deletions drizzle-orm/remix.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/** @type {import('@remix-run/dev').AppConfig} */
module.exports = {
future: {
v2_routeConvention: true,
},
ignoredRouteFiles: ["**/.*"],
// appDirectory: "app",
// assetsBuildDirectory: "public/build",
// publicPath: "/build/",
// serverBuildPath: "build/index.js",
};
2 changes: 2 additions & 0 deletions drizzle-orm/remix.env.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/// <reference types="@remix-run/dev" />
/// <reference types="@remix-run/node" />
7 changes: 7 additions & 0 deletions drizzle-orm/sandbox.config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"hardReloadOnChange": true,
"template": "remix",
"container": {
"port": 3000
}
}
22 changes: 22 additions & 0 deletions drizzle-orm/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"include": ["remix.env.d.ts", "**/*.ts", "**/*.tsx"],
"compilerOptions": {
"lib": ["DOM", "DOM.Iterable", "ES2019"],
"isolatedModules": true,
"esModuleInterop": true,
"jsx": "react-jsx",
"moduleResolution": "node",
"resolveJsonModule": true,
"target": "ES2019",
"strict": true,
"allowJs": true,
"forceConsistentCasingInFileNames": true,
"baseUrl": ".",
"paths": {
"~/*": ["./app/*"]
},

// Remix takes care of building everything in `remix build`.
"noEmit": true
}
}