Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ node_modules
.pnpm-debug.log
dist

build
build
mydb.sqlite
3 changes: 3 additions & 0 deletions example2/db/db.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

import { drizzle } from 'drizzle-orm/bun-sqlite';
export const db = drizzle('mydb.sqlite');
3 changes: 3 additions & 0 deletions example2/db/model/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

export * from './user.model'
export * from './post.model'
30 changes: 30 additions & 0 deletions example2/db/model/post.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { relations } from 'drizzle-orm';
import { int, sqliteTable, text } from 'drizzle-orm/sqlite-core';
import { usersTable } from './user.model';




export const postTable = sqliteTable('posts', {
id: int().primaryKey({ autoIncrement: true }),
userId: int()
.notNull()
.references(() => usersTable.id),
title: text().notNull(),
content: text().notNull(),
createdAt: int()
.notNull()
.$defaultFn(() => Date.now()),
updatedAt: int()
.notNull()
.$defaultFn(() => Date.now())
})



export const postsRelations = relations(postTable, ({ one }) => ({
user: one(usersTable, {
fields: [postTable.userId],
references: [usersTable.id]
})
}))
41 changes: 41 additions & 0 deletions example2/db/model/user.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { relations } from 'drizzle-orm';
import { int, sqliteTable, text } from 'drizzle-orm/sqlite-core';
import { createInsertSchema, createSelectSchema, createUpdateSchema } from 'drizzle-zod';
import { postTable } from './post.model';
import z from 'zod';

// 1. Drizzle 表定义
export const usersTable = sqliteTable('users', {
id: int().primaryKey({ autoIncrement: true }),
name: text().notNull().unique(),
bio: text(),
createdAt: int()
.notNull()
.$defaultFn(() => Date.now())
})



// 2. Zod Schema(基于 Drizzle 表生成,并可扩展校验)
export const selectUsersSchema = createSelectSchema(usersTable);
export const insertUsersSchema = createInsertSchema(usersTable);
export const updateUsersSchema = createUpdateSchema(usersTable)




// 3. 类型定义(可选,但推荐) 导出 TypeScript 类型(方便路由、service 等使用)

export const usersModel = {
insertUsersDto: insertUsersSchema
.omit({ id: true, createdAt: true })
.describe('创建用户请求'),
updateUsersDto: updateUsersSchema.omit({ id: true, createdAt: true }),
selectUsersTable: selectUsersSchema
.describe('用户信息响应')
}
export const usersRelations = relations(usersTable, ({ many }) => ({
posts: many(postTable)
}))

export type SelectUsersTable = z.infer<typeof usersModel.selectUsersTable>
9 changes: 9 additions & 0 deletions example2/drizzle.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { defineConfig } from 'drizzle-kit';
export default defineConfig({
out: './drizzle',
schema: './example2/db/index.ts',
dialect: 'sqlite',
dbCredentials: {
url: 'mydb.sqlite',
},
});
80 changes: 80 additions & 0 deletions example2/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import { Elysia, redirect } from "elysia"
import openapi from "../src"

import { db } from "./db/db"
import z from "zod/v4"
import { eq } from "drizzle-orm"
import { JSONSchema } from "effect"
import { fromTypes } from "../src/gen"

import path from 'node:path'
import { usersModel, usersTable } from "./db/model"
console.log("tsconfig path:", path.join(import.meta.dir, '../tsconfig.json'))
export const app = new Elysia()
.use(
openapi({
references: fromTypes(
process.env.NODE_ENV === "production"
? "dist/index.d.ts"
: "example2/index.ts",
{
tsconfigPath: path.join(import.meta.dir, '../tsconfig.json')
}
),
provider: 'scalar',
mapJsonSchema: {
zod: z.toJSONSchema,
effect: JSONSchema.make
},
documentation: {
info: {
title: 'Elysia Scalar',
version: '1.3.1a'
},
tags: [
{
name: 'Test',
description: 'Hello'
}
],
components: {
securitySchemes: {
bearer: {
type: 'http',
scheme: 'bearer'
},
cookie: {
type: 'apiKey',
in: 'cookie',
name: 'session_id'
}
}
}
}
})
)
.model(usersModel)
.get('/', () => {
redirect('/openapi')
})
.post('/', async ({ body }) => {
// create user
return await db.insert(usersTable).values({ id: Math.floor(Math.random() * 10), ...body })
}, {
body: 'insertUsersDto'
})
.put('/:id', async ({ params: { id }, body }) => {
return await db.update(usersTable).set(body).where(eq(usersTable.id, id))
}, {
params: z.object({
id: z.coerce.number()
}),
body: 'updateUsersDto'
})
.get('/list', async () => {
return await db.select().from(usersTable)
})
.listen(4050)


console.log("http://localhost:4050")
183 changes: 94 additions & 89 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,93 +1,98 @@
{
"name": "@elysiajs/openapi",
"version": "1.4.3",
"description": "Plugin for Elysia to auto-generate API documentation",
"author": {
"name": "saltyAom",
"url": "https://github.com/SaltyAom",
"email": "[email protected]"
},
"main": "./dist/cjs/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/cjs/index.js"
},
"./gen": {
"types": "./dist/gen/index.d.ts",
"import": "./dist/gen/index.mjs",
"require": "./dist/cjs/gen/index.js"
},
"./openapi": {
"types": "./dist/openapi.d.ts",
"import": "./dist/openapi.mjs",
"require": "./dist/cjs/openapi.js"
},
"./scalar": {
"types": "./dist/scalar/index.d.ts",
"import": "./dist/scalar/index.mjs",
"require": "./dist/cjs/scalar/index.js"
},
"./scalar/theme": {
"types": "./dist/scalar/theme.d.ts",
"import": "./dist/scalar/theme.mjs",
"require": "./dist/cjs/scalar/theme.js"
},
"./swagger": {
"types": "./dist/swagger/index.d.ts",
"import": "./dist/swagger/index.mjs",
"require": "./dist/cjs/swagger/index.js"
},
"./swagger/types": {
"types": "./dist/swagger/types.d.ts",
"import": "./dist/swagger/types.mjs",
"require": "./dist/cjs/swagger/types.js"
},
"./types": {
"types": "./dist/types.d.ts",
"import": "./dist/types.mjs",
"require": "./dist/cjs/types.js"
}
},
"keywords": [
"elysia",
"openapi",
"swagger",
"scalar"
],
"homepage": "https://github.com/elysiajs/elysia-openapi",
"repository": {
"type": "git",
"url": "https://github.com/elysiajs/elysia-openapi"
},
"bugs": "https://github.com/elysiajs/elysia-openapi/issues",
"license": "MIT",
"scripts": {
"dev": "bun run --watch example/index.ts",
"test": "bun test && npm run test:node",
"test:node": "npm install --prefix ./test/node/cjs/ && npm install --prefix ./test/node/esm/ && node ./test/node/cjs/index.js && node ./test/node/esm/index.js",
"build": "bun build.ts",
"release": "npm run build && npm run test && npm publish --access public"
},
"name": "@elysiajs/openapi",
"version": "1.4.3",
"description": "Plugin for Elysia to auto-generate API documentation",
"author": {
"name": "saltyAom",
"url": "https://github.com/SaltyAom",
"email": "[email protected]"
},
"main": "./dist/cjs/index.js",
"module": "./dist/index.mjs",
"types": "./dist/index.d.ts",
"exports": {
"./package.json": "./package.json",
".": {
"types": "./dist/index.d.ts",
"import": "./dist/index.mjs",
"require": "./dist/cjs/index.js"
},
"./gen": {
"types": "./dist/gen/index.d.ts",
"import": "./dist/gen/index.mjs",
"require": "./dist/cjs/gen/index.js"
},
"./openapi": {
"types": "./dist/openapi.d.ts",
"import": "./dist/openapi.mjs",
"require": "./dist/cjs/openapi.js"
},
"./scalar": {
"types": "./dist/scalar/index.d.ts",
"import": "./dist/scalar/index.mjs",
"require": "./dist/cjs/scalar/index.js"
},
"./scalar/theme": {
"types": "./dist/scalar/theme.d.ts",
"import": "./dist/scalar/theme.mjs",
"require": "./dist/cjs/scalar/theme.js"
},
"./swagger": {
"types": "./dist/swagger/index.d.ts",
"import": "./dist/swagger/index.mjs",
"require": "./dist/cjs/swagger/index.js"
},
"./swagger/types": {
"types": "./dist/swagger/types.d.ts",
"import": "./dist/swagger/types.mjs",
"require": "./dist/cjs/swagger/types.js"
},
"./types": {
"types": "./dist/types.d.ts",
"import": "./dist/types.mjs",
"require": "./dist/cjs/types.js"
}
},
"keywords": [
"elysia",
"openapi",
"swagger",
"scalar"
],
"homepage": "https://github.com/elysiajs/elysia-openapi",
"repository": {
"type": "git",
"url": "https://github.com/elysiajs/elysia-openapi"
},
"bugs": "https://github.com/elysiajs/elysia-openapi/issues",
"license": "MIT",
"scripts": {
"dev": "bun run --watch example/index.ts",
"dev2": "bun run --watch example2/index.ts",
"db:push": "bunx drizzle-kit push --config=example2/drizzle.config.ts ",
"test": "bun test && npm run test:node",
"test:node": "npm install --prefix ./test/node/cjs/ && npm install --prefix ./test/node/esm/ && node ./test/node/cjs/index.js && node ./test/node/esm/index.js",
"build": "bun build.ts",
"release": "npm run build && npm run test && npm publish --access public"
},
"dependencies": {},
"devDependencies": {
"@apidevtools/swagger-parser": "^12.0.0",
"@scalar/types": "^0.2.13",
"@sinclair/typemap": "^0.10.1",
"devDependencies": {
"@apidevtools/swagger-parser": "^12.0.0",
"@scalar/types": "^0.2.13",
"@sinclair/typemap": "^0.10.1",
"@types/bun": "1.2.20",
"effect": "^3.17.13",
"elysia": "1.4.6",
"eslint": "9.6.0",
"openapi-types": "^12.1.3",
"tsup": "^8.5.0",
"typescript": "^5.9.2",
"zod": "^4.1.5"
},
"peerDependencies": {
"elysia": ">= 1.4.0"
}
"effect": "^3.17.13",
"elysia": "1.4.6",
"eslint": "9.6.0",
"openapi-types": "^12.1.3",
"tsup": "^8.5.0",
"typescript": "^5.9.2",
"zod": "^4.1.5",
"drizzle-orm": "^0.44.5",
"drizzle-zod": "^0.8.3",
"drizzle-kit": "^0.31.4"
},
"peerDependencies": {
"elysia": ">= 1.4.0"
}
}