Skip to content

Commit

Permalink
Merge pull request #388 from vikejs/brillout/improve-file-database
Browse files Browse the repository at this point in the history
  • Loading branch information
magne4000 committed Sep 17, 2024
2 parents 0a07b7b + 458d362 commit 536a3a3
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 5 deletions.
13 changes: 10 additions & 3 deletions boilerplates/shared-no-db/files/database/todoItems.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,16 @@ interface TodoItem {
text: string;
}

const todos = {
todo: [{ text: "Buy milk" }, { text: "Buy strawberries" }],
};
const todosDefault = [{ text: "Buy milk" }, { text: "Buy strawberries" }];

const database =
// We create an in-memory database.
// - We use globalThis so that the database isn't reset upon HMR.
// - The database is reset when restarting the server, use a proper database (SQLite/PostgreSQL/...) if you want persistent data.
// biome-ignore lint:
((globalThis as unknown as { __database: { todos: TodoItem[] } }).__database ??= { todos: todosDefault });

const { todos } = database;

export { todos };
export type { TodoItem };
2 changes: 1 addition & 1 deletion boilerplates/shared-todo/files/pages/todo/+data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ export default async function data(_pageContext: PageContextServer): Promise<Dat

return { todo };
} else {
return todos;
return { todo: todos };
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ export async function onNewTodo({ text }: { text: string }) {
const context = getContext();
await d1Queries.insertTodo(context.db, text);
} else {
todos.todo.push({ text });
todos.push({ text });
}
}

0 comments on commit 536a3a3

Please sign in to comment.