create-nanoka-app 1.0.2 → 1.0.3
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,12 +1,7 @@
|
|
|
1
1
|
import { defineConfig } from '@nanokajs/core/config'
|
|
2
|
-
import {
|
|
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 {
|
|
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
|
|
12
|
-
const
|
|
10
|
+
const app = nanoka(d1Adapter(env.DB))
|
|
11
|
+
const Post = app.model(postTableName, postFields)
|
|
13
12
|
|
|
14
|
-
app.post(
|
|
15
|
-
'
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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('/
|
|
31
|
-
const
|
|
32
|
-
|
|
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
|
-
}
|