Koa-OAI-Mongoose
Features
- Built-in Swagger-UI, easy view and debug
- Auto generate route by OpenAPI/Swagger api doc, and validate parameters
- Support OpenAPI/Swagger2.0 Specification with yaml or json file
- Support Json Schema v4, validate query,body,path,header
- Support custom Json Schema format
- Support custom error handler
- Autogenerate CRUD api for mongoose connection
- Support custom api for mongoose schema
Installation
For koa@>=2.x:
npm install koa-oai-mongoose --save
For koa@<2.x:
Not Support Yet!
Quick Start
Because this module based on koa-oai-router, so you must read it before.
This example's source code is
Creating model
You must create mongoose schema, model and connection at first. So create mongo.js with content below.
; const connection = mongoose; const bookSchema = name: type: String required: true index: true unique: true author: ref: 'people' type: mongooseSchemaTypesObjectId ; const userSchema = name: type: String required: true default: 'mike' index: true unique: true age: Number email: String address: String male: Boolean bornAt: type: Date default: likes: String; connection;connection; ;
Creating api doc
Create a simple api.yaml api doc with some base info.
swagger: '2.0'info: version: 1.0.0 title: koa-oai-mongoose exampleconsumes: - application/jsonproduces: - application/jsonbasePath: /apipaths:
Creating koa app
Create app.js with content below.
;;;; ; const app = ; const server = app; const opt = apiDoc: './example/api/api.yaml' controllerDir: './example/controller' port: serverport versioning: true mongo: connection; const router = opt; app;app; app;app;
Explorering your api
At this moment, you can open api explorer http://127.0.0.1:9001/api-explorer.