|
1 | 1 | import { BlogPost, BlogPostId } from "@effect-app-boilerplate/models/Blog" |
| 2 | +import { RepositoryDefaultImpl } from "@effect-app/infra/services/RepositoryBase" |
| 3 | +import { RepoLive } from "api/migrate.js" |
2 | 4 |
|
3 | | -export interface BlogPostRepo { |
4 | | - all: Effect<never, never, readonly BlogPost[]> |
5 | | - find: (id: BlogPostId) => Effect<never, never, Option<BlogPost>> |
6 | | - save: (post: BlogPost) => Effect<never, never, void> |
| 5 | +export interface BlogPostPersistenceModel extends BlogPost.From { |
| 6 | + _etag: string | undefined |
7 | 7 | } |
8 | | -export const BlogPostRepo = Tag<BlogPostRepo>() |
9 | 8 |
|
10 | | -export const BlogPostRepoLive = Layer |
11 | | - .sync(BlogPostRepo, () => { |
12 | | - const items: BlogPost[] = [ |
13 | | - new BlogPost({ |
14 | | - id: BlogPostId("post-test123"), |
15 | | - title: NonEmptyString255("Test post"), |
16 | | - body: NonEmptyString2k("imma test body") |
17 | | - }) |
18 | | - ] |
| 9 | +export type BlogPostSeed = "sample" | "" |
19 | 10 |
|
20 | | - return { |
21 | | - all: Effect.sync(() => [...items]), |
22 | | - find: (id) => Effect.sync(() => items.findFirst((_) => _.id === id)), |
23 | | - save: (post) => Effect.sync(() => items.push(post)) |
24 | | - } |
| 11 | +/** |
| 12 | + * @tsplus type BlogPostRepo |
| 13 | + * @tsplus companion BlogPostRepo.Ops |
| 14 | + */ |
| 15 | +export class BlogPostRepo extends RepositoryDefaultImpl<BlogPostRepo>()<BlogPostPersistenceModel>()( |
| 16 | + "BlogPost", |
| 17 | + BlogPost |
| 18 | +) {} |
| 19 | + |
| 20 | +/** |
| 21 | + * @tsplus static BlogPostRepo.Ops Live |
| 22 | + */ |
| 23 | +export const LiveBlogPostRepo = Effect |
| 24 | + .sync(() => { |
| 25 | + const seed = "sample" |
| 26 | + const makeInitial = Effect.sync(() => { |
| 27 | + const items = seed === "sample" |
| 28 | + ? [ |
| 29 | + new BlogPost({ |
| 30 | + id: BlogPostId("post-test123"), |
| 31 | + title: NonEmptyString255("Test post"), |
| 32 | + body: NonEmptyString2k("imma test body") |
| 33 | + }) |
| 34 | + ] as const |
| 35 | + : [] |
| 36 | + return items |
| 37 | + }) |
| 38 | + return BlogPostRepo |
| 39 | + .makeWith({ makeInitial }, (_) => new BlogPostRepo(_)) |
| 40 | + .toLayer(BlogPostRepo) |
25 | 41 | }) |
| 42 | + .unwrapLayer |
| 43 | + .provide(RepoLive) |
0 commit comments