API Generator - instantly generate REST and GraphQL APIs (openapi (OAS) 3.0.0)
MIT License
npm i -g sugar-generate
if you're running mongodb locally and using the docker container, make sure to start mongo using
sudo mongod --bind_ip_all
otherwise you wont be able to connect to mongo from docker.
{
"schema": {
"name": {
"type": "String",
"default": ""
},
"isDead": {
"type": "Boolean",
"default": false
},
"age": {
"type": "Number",
"default": false
}
},
"statics": {}
}
You can also provide multiple schemas like the following
[
{
"name": "user",
"schema": {
"name": {
"type": "String",
"default": ""
},
"email": {
"type": "String",
"trim": true,
"required": true,
"unique": true,
"immutable": true
},
"intro": {
"type": "Boolean",
"default": false
},
"sub": {
"one": {
"type": "String",
"trim": true,
"required": true
},
"two": {
"type": "String",
"required": true
},
"three": {
"type": "Number"
}
},
"role": {
"type": "String",
"enum": ["user", "maker"],
"default": "user"
}
},
"statics": {
"statuses": ["created", "under_review", "listed", "deleted"],
"status": {
"active": "active",
"inactive": "inactive",
"deleted": "deleted"
}
}
},
{
"name": "team",
"schema": {
"name": {
"type": "String",
"default": ""
},
"users": {
"type": "ObjectId",
"ref": "Users"
}
}
}
]
save this to monkey.json
sugar-generate \
--schema ./monkey.json \
--destination ./my-monkeys
or the short version
sugar-generate \
-s ./monkey.json \
-d ./my-monkeys
Note this is slightly different than previous versions. The schemas require a name now.
cd /my-monkeys
npm i
npm start
# Or build the docker container!
docker build -t myMonkeys:0.1.0 .
graphql is supported and gets created by default so you can choose between rest and graphql
Graphql is on http://localhost:3000/graphql
.
├── configs # Config File
├── connection # DB Connections (mongo, redis)
├── controller # Controllers
│ ├── <model name> # Functions (one file, one function) create, delete, update, get, getOne
├── models # DB Models
├── router # Endpoint Routes
├── tests # Single Test File
** WARNING ** running the tests will pull the config file from configs/config.json and clear the DB
npm run test