Automatic Mongoose REST API - Rest API Module
npm install mongoose-auto-api.rest

> Automatic Mongoose REST API - Rest API Module ☕
- npm i -S mongoose-auto-api.rest
- Model Setup - mongoose-auto-api.info
``javascript
// Common JS
const api = require('mongoose-auto-api.rest').default
// ES6+ and Typescript
import api from 'mongoose-auto-api.rest'
`
- assign port with _serverPort_ field in apiConfig.json
- Runs on this port in production, and this port + 10 by default in development, override with PORT environment variable
- assign cors port (port of your web application) with _webPort_ field in apiConfig.json
- Allows cors on this port in production, and this port + 10 by default in development, override with PORT environment variable
- Uses JSON Web Tokens for verification
- Tokens last 7 days and are refreshed every hour upon api use
- JSON response will contain {"status": "ok"} on success, and {"status": "error"} on error.{"status": "ok", "response": {"message": "success"}}
- JSON response will contain _response_ field with extra data i.e. {"refresh_token": { username, uid, access_token, expires_in }}
- JSON response will contain _refresh_token_ field with refresh token: ?auth_token=xxx
- Routes require JWT to authenticate
- Token can be sent in request with parameter , in _x-access-token_ header, or in _authorization_ header{ status: 'error', response: { message: 'No token provided.'}}
- No token error: { status: 'error', response: { message: 'Invalid token.'}}
- Invalid token error:
- /loginusername, password
- Parameters: { username, uid, access_token, expires_in }
- Success: { messages: ["ERROR_MESSAGE"], codes: ["ERROR_CODE"] }
- Error: { message: "ERROR_MESSAGE", code: "ERROR_CODE", trace: "STACK_TRACE" }
- Exception: /signup
- username, password, secret_key
- Parameters: { username, uid, access_token, expires_in }
- _username_ and _password_ must have at least 8 characters and _password_ must have at least 1 number and 1 special character.
- _secret_key_ is the secret key you set up with the CLI
- This endpoint is self-protecting, after ONE user is added JWT will be required
- Success: { messages: ["ERROR_MESSAGE"], codes: ["ERROR_CODE"] }
- Error: { message: "ERROR_MESSAGE", code: "ERROR_CODE", trace: "STACK_TRACE" }
- Exception: /update_secret_key
- key
- Parameters: {attributes...}
- _key_ must have at least 8 characters, 1 number, and 1 special character
- This endpoint is self-protecting, after ONE user is added JWT will be required
- Success:
- - first insert{ n, nModified, ok }
- - update after first insert{ messages: ["ERROR_MESSAGE"], codes: ["ERROR_CODE"] }
- Error: { message: "ERROR_MESSAGE", code: "ERROR_CODE", trace: "STACK_TRACE" }
- Exception: /update_password
- username, current_password, password
- JWT Required
- Parameters: { status: 'ok', response: { message: 'Password updated.'} }
- _password_ must have at least 8 characters and must have at least 1 number.
- Success: { messages: ["ERROR_MESSAGE"], codes: ["ERROR_CODE"] }
- Error: { message: "ERROR_MESSAGE", code: "ERROR_CODE", trace: "STACK_TRACE" }
- Exception: /verify_token
- auth_token
- JWT Required
- Parameters: { status: 'ok', response: { message: 'Token verified.'}
- Success: { status: 'error', response: { message: 'No token provided.'}}
- Error:
- "x" denotes collection name
- I.E. _/customer/insert?name=...?_
- x/insert{attributes...}
- Inserts record
- Success: { name: "MongoError", code: 1050 }
- Error: x/update, x/push, x/push_unique, x/set
- x/update
- updates recordx/push
- use field _update_primary_ to change the primary key
- pushes comma separated records into listx/push_unique
- Records will be placed regardless if there is an existing matching record in the list
- pushes unique comma separated records into the listx/set
- Only records that do not exist already will be placed in the list
- This WILL NOT delete existing duplicate records
- sets list to comma separated records{ n, nModified, ok }
- Primary key required
- Success: x/delete, x/delete_all
- { n, deletedCount, ok }
- Deletes single record or all records, primary key required for _delete_
- Success: x/get
- /user/get?username=bob
- Gets single record
- Parameters: requires model primary key, i.e. [{attributes...}]
- Success: x/get_all
- [{attributes...}, {}...]
- Gets all records
- Params
- _sort_field_
- Field to sort by
- _sort_order_
- Sort order, -1 for descending, 1 for ascending
- _record_limit_
- Number of records to return
- _record_count_
- Returns document count if true
- _skip_
- Number of records to skip
- Success: x/find
- x/schema
- finds records
- param - _where_
- expects list of objects with attributes _field_, _op_, and _value_
- i.e. [{ field: 'price', op: '$gt', value: 2 }]
- operators
- \$eq - equal
- \$ne - not equal
- \$gt - greater than
- \$gte - greater than or equal to
- \$lt - less than
- \$lte - less than or equal to
- \$in - in array
- \$nin - not in array
- \$strt - starts with string
- \$end - ends with string
- \$cont - contains string
- \$inc - array field includes value
- \$ninc - array field does not include value
- param - _sort_field_
- Field to sort by
- param - _sort_order_
- Sort order, -1 for descending, 1 for ascending
- param - _record_limit_
- Number of records to return
- param - _record_count_
- Returns document count if true
- param - _skip_
- Number of records to skip
- joins collections
- param - _from_
- collection to join
- param - _local_field_
- field from local collection to join
- param - _foreign_field_
- field from foreign collection to join
- param - _as_
- name to assign the joined field in returned document
- if local field is a list, joined field will return a list
- if local field is not a list, joined field will return an object
- { schema: [], primary_key, list_fields: [], encrypt_fields: [], encode_fields: [], subdoc_fields: [] }
- Gets schema information
- Success: x/sterilize`
-
- Removes obsolete fields and indexes after updating schema
- Sets value for given field for all documents (useful for updating old documents after adding schema)
- Parameters: _field_name_ corresponds to collection field name
- v2.0.0
- Codebase converted from Coffeescript -> Typescript
- v2.0.2
- Automatic JWT Rotation w/ JWK kid claims
- v2.0.7
- Retrieve document count in get_all and find, lean optimizations