npm i -g sugar-generate
- have mongodb installed and running!
- have nodejs installed
- Create your schema. It could be as simple as:
{
"schema": {
"name": {
"type": "String",
"default": ""
},
"isDead": {
"type": "Boolean",
"default": false
},
"age": {
"type": "Number",
"default": false
}
},
"statics": {}
}
save this to monkey.json
- generate your api
sugar-generate \
--type api \
--name monkey \
--schema ./samples/monkey.json \
--destination /private/var/andrew/generator-tests/3
- Run or build the docker container and visit http://localhost:3000
cd /wrannaman/generator
npm i
npm start
# Or build the docker container!
docker build -t myMonkeys:0.1.0 .
- Generates simple Nodejs code
- Uses Mongodb with Mongoose ORM
- Easy to build / deploy
- Dockerfile included
- Generates CRUD APIs
- create
- get (many, with pagination; supports search, sort, filter, pagination out of the box)
- getOne
- update
- delete
- Generating an initial API
- Microservice oriented
- Ready to deploy (build with docker => deploy)
- idempotent changes (i.e. it doesn't know if you wrote code in there or changed things around)
- working with modified code
- populating table joins
- custom actions inside controller functions
- Feed it a json schema (see below's Example Schema)
- Name it
- Tell it where to put the code.
- Build your generated code with the docker file
- Deploy it and move on
basic generator tests- more tests on get
- tests for generated code
- other databases?
- your ideas?
- react components for the api?!?!
{
"schema": {
"first_name": {
"type": "String",
"default": ""
},
"last_name": {
"type": "String",
"default": ""
},
"email": {
"type": "String",
"trim": true,
"required": true,
"unique": true,
"immutable": true, // Don't let this change from api on update
},
"password": {
"type": "String",
"trim": true,
"select": false,
"immutable": true, // Don't let this change from api on update
},
"intro": {
"type": "Boolean",
"default": false
},
"team": {
"type": "ObjectId", // This references another collection
"ref": "Team"
},
"sub": {
"one": {
"type": "String",
"trim": true,
"required": true
},
"two": {
"type": "Number",
"required": true
}
},
"role": {
"type": "String",
"enum": ["user", "maker"],
"default": "user"
}
},
"statics": {
"statuses": ["created", "under_review", "listed", "deleted"],
"status": {
"active": "active",
"inactive": "inactive",
"deleted": "deleted"
}
}
}
- graphql is supported and gets created by default so you can choose between rest and graphql
graphql is located at http://localhost:777/graphql
# create one
mutation {
createMonkey(first_name:"hello", last_name:"world", age: 10) {
_id
first_name
}
}
# Update one of em
mutation {
updateMonkey(monkeyID: "5cf8531632b58072f0376dab" first_name: "henry"){
_id
first_name
}
}
# Get em all
query {
monkeys {
_id
first_name
age
}
}
# Get One
query {
monkey(_id: "5cf852a71b11e870df383fb1") {
_id
first_name
age
}
}
# Delete One
.
├── 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