feat: a new "island" based architechture (#97)

This commit is contained in:
Luca Casonato
2022-03-04 19:49:27 +01:00
committed by GitHub
parent d441bf2489
commit 8daa280862
70 changed files with 820 additions and 972 deletions
+16
View File
@@ -0,0 +1,16 @@
# fresh project
### Usage
Start the project:
```
deno run -A --watch main.ts
```
After adding, removing, or moving a page in the `routes` or directory, or
adding, removing, or moving an island in the `islands` directory, run:
```
fresh manifest
```
@@ -1,2 +1 @@
export * from "../../runtime.ts";
export * from "../../server.ts";
+18
View File
@@ -0,0 +1,18 @@
// DO NOT EDIT. This file is generated by `fresh`.
// This file SHOULD be checked into source version control.
// To update this file, run `fresh routes`.
import * as $0 from "./routes/index.tsx";
import * as $$0 from "./islands/Counter.tsx";
const manifest = {
routes: {
"./routes/index.tsx": $0,
},
islands: {
"./islands/Counter.tsx": $$0,
},
baseUrl: import.meta.url,
};
export default manifest;
+21
View File
@@ -0,0 +1,21 @@
/** @jsx h */
import { h, IS_BROWSER, useState } from "../client_deps.ts";
interface CounterProps {
start: number;
}
export default function Counter(props: CounterProps) {
const [count, setCount] = useState(props.start);
return (
<div>
<p>{count}</p>
<button onClick={() => setCount(count - 1)} disabled={!IS_BROWSER}>
-1
</button>
<button onClick={() => setCount(count + 1)} disabled={!IS_BROWSER}>
+1
</button>
</div>
);
}
@@ -1,11 +1,10 @@
/// <reference no-default-lib="true" />
/// <reference lib="dom" />
/// <reference lib="dom.iterable" />
/// <reference lib="dom.asynciterable" />
/// <reference lib="deno.ns" />
/// <reference lib="deno.unstable" />
import { start } from "../../server.ts";
import routes from "./routes.gen.ts";
import { start } from "./server_deps.ts";
import manifest from "./fresh.gen.ts";
await start(routes);
await start(manifest);
+15
View File
@@ -0,0 +1,15 @@
/** @jsx h */
import { h } from "../client_deps.ts";
import Counter from "../islands/Counter.tsx";
export default function Home() {
return (
<div>
<p>
Welcome to `fresh`. Try update this message in the ./routes/index.tsx
file, and refresh.
</p>
<Counter start={3} />
</div>
);
}
+1
View File
@@ -0,0 +1 @@
export * from "../../server.ts";
-5
View File
@@ -1,5 +0,0 @@
{
"deno.enable": true,
"deno.lint": true,
"deno.unstable": true
}
-15
View File
@@ -1,15 +0,0 @@
# fresh project
### Usage
Start the project:
```
deno run -A --watch main.ts
```
After adding, removing, or moving a page in the `pages` directory, run:
```
fresh routes
```
-50
View File
@@ -1,50 +0,0 @@
/** @jsx h */
import { h, HandlerContext, PageConfig, Suspense, useData } from "../deps.ts";
import { getDatabase, Todo } from "../utils/database.ts";
export default function TodoListPage() {
return (
<div>
<h1>Your TODOs</h1>
<AddTodoForm />
<Suspense fallback={<p>Loading...</p>}>
<TodoList />
</Suspense>
</div>
);
}
function AddTodoForm() {
return (
<form method="POST">
<input type="text" name="text" />
<button type="submit" style="margin-left: 4px">Add</button>
</form>
);
}
function TodoList() {
const todos = useData<Todo[]>("todos", async () => {
const db = await getDatabase();
const todos = await db.listTodos();
return todos;
});
return (
<ul>
{todos.map((todo) => <li key={todo.id}>{todo.text}</li>)}
</ul>
);
}
export const handler = {
async POST(ctx: HandlerContext): Promise<Response> {
const form = await ctx.req.formData();
const text = form.get("text");
if (typeof text !== "string") {
return new Response("misformed form", { status: 400 });
}
const db = await getDatabase();
await db.addTodo(text);
return Response.redirect(ctx.req.url, 303);
},
};
-14
View File
@@ -1,14 +0,0 @@
// DO NOT EDIT. This file is generated by `fresh`.
// This file SHOULD be checked into source version control.
// To update this file, run `fresh routes`.
import * as $0 from "./pages/index.tsx";
const routes = {
pages: {
"./pages/index.tsx": $0,
},
baseUrl: import.meta.url,
};
export default routes;
-43
View File
@@ -1,43 +0,0 @@
import { Client } from "https://raw.githubusercontent.com/denodrivers/postgres/72f523cb9d8f863204a3fa02f52d534e7ce30ccf/mod.ts";
export interface Todo {
id: number;
text: string;
}
class Database {
#client: Client;
constructor(client: Client) {
this.#client = client;
}
static async connect() {
const client = new Client(Deno.env.get("DATABASE_URL"));
await client.connect();
await client.queryObject`CREATE TABLE IF NOT EXISTS todos (
id SERIAL PRIMARY KEY,
text TEXT NOT NULL
)`;
return new Database(client);
}
async listTodos(): Promise<Todo[]> {
const { rows } = await this.#client.queryObject<Todo>`SELECT * FROM todos`;
return rows;
}
async addTodo(text: string): Promise<Todo> {
const { rows } = await this.#client.queryObject<Todo>`
INSERT INTO todos (text) VALUES (${text})
RETURNING *
`;
return rows[0];
}
}
const db: Database | Promise<Database> = Database.connect();
export async function getDatabase(): Promise<Database> {
return await db;
}