create-nanoka-app 1.0.2 → 1.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "create-nanoka-app",
3
- "version": "1.0.2",
3
+ "version": "1.0.4",
4
4
  "description": "Create a new Nanoka (Hono + D1) project",
5
5
  "type": "module",
6
6
  "bin": {
@@ -1,12 +1,7 @@
1
1
  import { defineConfig } from '@nanokajs/core/config'
2
- import { userFields, userTableName } from './src/models/user'
2
+ import { postFields, postTableName } from './src/models/posts'
3
3
 
4
4
  export default defineConfig({
5
- models: [
6
- {
7
- name: userTableName,
8
- fields: userFields,
9
- },
10
- ],
5
+ models: [{ name: postTableName, fields: postFields }],
11
6
  output: './drizzle/schema.ts',
12
7
  })
@@ -1,6 +1,5 @@
1
1
  import { d1Adapter, nanoka } from '@nanokajs/core'
2
- import { z } from 'zod'
3
- import { userFields, userTableName } from './models/user'
2
+ import { postFields, postTableName } from './models/posts'
4
3
 
5
4
  export interface Env {
6
5
  DB: D1Database
@@ -8,29 +7,22 @@ export interface Env {
8
7
 
9
8
  export default {
10
9
  async fetch(req: Request, env: Env, ctx: ExecutionContext) {
11
- const app = nanoka<{ Bindings: Env }>(d1Adapter(env.DB))
12
- const User = app.model(userTableName, userFields)
10
+ const app = nanoka(d1Adapter(env.DB))
11
+ const Post = app.model(postTableName, postFields)
13
12
 
14
- app.post(
15
- '/users',
16
- User.validator('json', { omit: ['id', 'passwordHash', 'createdAt'] }),
17
- async (c) => {
18
- const body = c.req.valid('json')
19
- const created = await User.create({
20
- ...body,
21
- id: crypto.randomUUID(),
22
- passwordHash: 'hashed_value_here',
23
- createdAt: new Date(),
24
- })
25
- const user = User.outputSchema({ omit: ['passwordHash'] }).parse(created)
26
- return c.json(user, 201)
27
- },
28
- )
13
+ app.post('/posts', Post.validator('json', { omit: ['id', 'createdAt'] }), async (c) => {
14
+ const body = c.req.valid('json')
15
+ const created = await Post.create({
16
+ ...body,
17
+ id: crypto.randomUUID(),
18
+ createdAt: new Date(),
19
+ })
20
+ return c.json(created, 201)
21
+ })
29
22
 
30
- app.get('/users', async (c) => {
31
- const users = await User.findMany({ limit: 20 })
32
- const result = z.array(User.outputSchema({ omit: ['passwordHash'] })).parse(users)
33
- return c.json(result)
23
+ app.get('/posts', async (c) => {
24
+ const posts = await Post.findMany({ limit: 20 })
25
+ return c.json(posts)
34
26
  })
35
27
 
36
28
  return app.fetch(req, env, ctx)
@@ -0,0 +1,10 @@
1
+ import { t } from '@nanokajs/core'
2
+
3
+ export const postTableName = 'posts'
4
+ export const postFields = {
5
+ id: t.uuid().primary(),
6
+ title: t.string().min(1).max(200),
7
+ body: t.string().min(1),
8
+ published: t.boolean().default(false),
9
+ createdAt: t.timestamp().default(() => new Date()),
10
+ }
@@ -1,12 +0,0 @@
1
- import { t } from '@nanokajs/core'
2
-
3
- export const userTableName = 'users'
4
-
5
- export const userFields = {
6
- id: t.uuid().primary(),
7
- name: t.string().min(1).max(100),
8
- email: t.string().email().unique(),
9
- passwordHash: t.string().writeOnly(),
10
- serverToken: t.string().serverOnly().optional(),
11
- createdAt: t.timestamp().default(() => new Date()),
12
- }