Skip to content

Commit

Permalink
Merge pull request #440 from fastrodev/store
Browse files Browse the repository at this point in the history
Store
  • Loading branch information
ynwd authored Sep 16, 2024
2 parents eca641a + f534426 commit 2165511
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 3 deletions.
3 changes: 2 additions & 1 deletion core/server/mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ const createResponse = (
};

export default class Server implements Fastro {
constructor(options?: Map<string, any>) {
constructor(options?: Record<string, any>) {
this.serverOptions = options ?? {};
this.#handler = this.#createHandler();
this.#addPropsEndpoint();
Expand Down Expand Up @@ -510,6 +510,7 @@ if (root) fetchProps(root);
ctx.url = url;
ctx.server = this;
ctx.kv = this.serverOptions["kv"];
ctx.store = this.store;
return ctx;
};

Expand Down
5 changes: 3 additions & 2 deletions deno.json
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,11 @@
"params_query": "deno run -A examples/params_query.ts",
"static_file_string": "deno run -A examples/static_file_string.ts",
"static_file_image": "deno run -A examples/static_file_image.ts",
"markdown_middleware": "GITHUB_CLIENT_ID=abe04d0d76dd2b169380 GITHUB_CLIENT_SECRET=e9a16ec40a87c8a6d1f4eff3ea955248059b67ee deno run -A --unstable-kv examples/markdown_middleware.ts",
"markdown_middleware": "deno run -A --env --unstable-kv examples/markdown_middleware.ts",
"group": "deno run -A examples/group.ts",
"app_middleware": "deno run -A examples/app_middleware.ts",
"route_middleware": "deno run -A examples/route_middleware.ts",
"server_rendering": "deno run -A examples/server_rendering.tsx"
"server_rendering": "deno run -A examples/server_rendering.tsx",
"store": "deno run -A --env examples/store.ts"
}
}
53 changes: 53 additions & 0 deletions examples/store.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import fastro, { Context, HttpRequest } from "@app/mod.ts";

const f = new fastro();

// set default value for the store
f.store.set("hello", "hello world");

// update default value
f.post(
"/",
(_req: HttpRequest, ctx: Context) => {
ctx.store.set("hello", "hello world v2");
return ctx.send("Helo world", 200);
},
);

// update default value with TTL
f.post(
"/ttl",
(_req: HttpRequest, ctx: Context) => {
ctx.store.set("hello", "world", 1000);
return ctx.send("ttl", 200);
},
);

// save store to github
f.post(
"/commit",
async (_req: HttpRequest, ctx: Context) => {
await ctx.store.commit();
return ctx.send("commit", 200);
},
);

// destroy file
f.post(
"/destroy",
async (_req: HttpRequest, ctx: Context) => {
await ctx.store.destroy();
return ctx.send("destroy", 200);
},
);

// get the value
f.get(
"/",
async (_req: HttpRequest, ctx: Context) => {
const res = await ctx.store.get("hello");
return Response.json({ value: res });
},
);

await f.serve();

0 comments on commit 2165511

Please sign in to comment.