campsi-ci v1.2.1
campsi/api
Manage and expose document-oriented content through a configurable RESTful API.
- Create schemas with campsi/architect.
- Talk to the API with campsi/admin.
Features
API
- Store and manage both collections or individual documents
- Describe complex data structures, using campsi components
- State-based document lifecycle for approval workflows
POST,PUTandDELETEasynchronous webhooks for distributed architecture- Truly Stateless
- Patch updates
- Conflicts resolver
Querying
- Embed related documents
?embed=shippingAddresses - Complex filtering
?data.name=Thomas&data.address.city=London - Complex sorting
?sort=name,-birthday - Pagination
?page=4&perPage=20 - Stored searches
Authentication & authorization
- Role-Based Access-Control
- Generate invitation tokens by role
- PassportJS integration
- Custom authentification providers
- Rate limiter
Other
- Swagger / API doc generation
- Generate random mock data
Assets Management
- Stream based upload system
- Filtered and paginated GET route
- Custom storage adapters
- Local storage with rotating subfolders
- Amazon S3
- Azure Blob Storage
- Google Cloud Platform
- GraphicsMagick Metadata recognition
- Resize and convert media derivatives
Concepts
States & role permissions
States represent the different stages in a document lifecycle, like published, draft and archived. To each state is associated specific role-based permissions, defining allowed HTTP methods :
| Method | Possible actions on the state |
|---|---|
GET | view and query the documents |
POST | create a new document |
PUT | edit an existing document or put a document in this state |
DELETE | delete the state for the document |
A document can be in different states at the same time, for example:
- the
publishedversion, visible on the website and the mobile app by everyone - the
waiting_for_approvalversion that has to be validated by an admin - the new
draftversion an editor is already working on.
A document can migrate from a state to another, for example:
- You create a resource
ticketfor your customers error reporting. customerrole is allowed toPOST /docs/ticketsin the default statesubmittedsupportrole is allowed toPUT /docs/ticketsin the statesrepliedorread
You can create any number of states:
publisheddraftwaiting_for_approvalapprovedrejectedpopulationApopulationBsubmittedreadreplied- …
You can create any number of roles:
admineditormanagercustomeruserpublicvisitor- …
Resource
The resource is the document template, it describes the fields and validation rules.
Document
A document is an instance of a resource. It has an id and a value for each defined state, which is encapsulated in a property named data.
Anatomy of a document
let doc = {
id: ObjectId("58205e4fa5dc6c3b381a0e9b"),
states: {
published: {
createdAt: "2016-11-07 10:58:23.950Z",
createdBy: "58277c406d6157a751399052",
data: {
title: "Hello World",
content: "I'm the published version"
}
},
draft: {
createdAt: "2016-11-07 11:22:12.950Z",
createdBy: "58277c406d6157a751399052",
data: {
title: "Hello World",
content: "I'm the draft version"
}
},
}
}Relationships & document embedding
Relationships between resources are resolved:
- automatically, if the relationship has the property
embedset totrue - on demand, if a
embed={rel}parameter is passed in the query string
Because there are no JOIN in MongoDB, embedding documents requires the execution of supplementary queries. For performance reason, the results are memoized during the request lifetime.
Webhook
You can configure webhooks that are triggered when an request processed by campsi/api match a specific scope (action, state, resource). Once triggered, the webhook send an asynchronous HTTP request to the endpoint uri specified in its configuration.
todo distributed architecture example
Usage
node auth-index.js
# or
npm startFlags
--schema "path/to/the/schema.json" # specifies the json schema to use
--port 3000 # set the HTTP port to listen to
--data "/mnt/nfs/data" # repository for uploadcampsi/schema specification
Root
| Property | Type | Description |
|---|---|---|
name | String | unique identifier of your api |
title | String | title of the API |
description | String | markdown description |
roles | <Role> | roles hashmap |
types | <ResourceType> | resource types hashmap |
resources | <Resource> | resources hasmap |
Role
| Property | Type | Description |
|---|---|---|
label | String | unique identifier of your api |
auth | Boolean | title of the API |
admin | Boolean | markdown descripition |
ResourceType
| Property | Type | Description |
|---|---|---|
defaultState | String | name of the state any request will default to |
states | <State> | hashmap of the states |
permissions | <Role, <State, Method>> | Allowed HTTP methods by role and by state |
State
| Property | type | Description |
|---|---|---|
name | String | name of the state any request will default to |
label | String | hashmap of the states |
validate | Boolean | wether the data has to be valid to be saved or not |
Resource
| Property | type | Description |
|---|---|---|
title | String | readable title |
description | String | markdown description |
type | String | name of the ResourceType |
fields | [Object] | list of the fields composing the model |
hooks | [Hook] | list of hooks bound to the resource |
rels | <Rel> | hashmap of relationships |
Rel
| Property | type | Description |
|---|---|---|
path | String | property path |
resource | String | name of the resource it points to (self reference is OK) |
embed | Boolean | resolve relation automatically |
fields | [String] | list of the fields that gets embedded |
Hook
| Property | type | Description |
|---|---|---|
name | String | hook identifier |
uri | String | todo support parameter |
method | String | one of POST GET PUT DELETE |
payload | Boolean | if true and method is POST or PUT, send the data |
on | [String] | list of actions |
states | [String] | list of states |
retry | Number | number of time the HTTP client tries to reach the endpoint |
timeout | Number | number of seconds before the HTTP client hangs up |
headers | <String,String> | hashmap of the request headers |
Requirements
- NodeJS v16.0.0
- MongoDB v2.6.0
- Optional
- ImageMagick
- Graphics Magick