CRUD APIs
With DryerJS, you can easily create CRUD APIs for your models.
Simple Model
Suppose you have a model called Tag
:
import { Id, ObjectId, Property, Definition } from 'dryerjs';
@Definition()
export class Tag {
@Id()
id: ObjectId;
@Property()
name: string;
}
Remember to add it into the
definitions
array in theDryerModule
.
Generated APIs
Your generated CRUD APIs will be:
Create
mutation {
createTag(input: { name: "Sale" }) {
id
name
}
}
Find One
query {
tag(id: "000000000000000000000000") {
id
name
}
}
If there are no tags with the given id, the API will return an error with message: No Tag found with ID: 000000000000000000000000
On Update and Remove APIs, find one will be called first to check if the document exists.
Update
mutation {
updateTag(input: {
id: "000000000000000000000000",
name: "SaleOff"
}) {
id
name
}
}
Remove
mutation {
removeTag(id: "000000000000000000000000") {
success
}
}
It will be complicated if you want to remove a document with relations, check out Remove Modes for more information.
Paginate
query {
paginateTags(page: 1, limit: 10) {
docs {
id
name
}
totalDocs
page
limit
totalPages
hasPrevPage
pagingCounter
}
}
There will be filter
and sort
arguments which will be covered in the Filter and Sort section.
Find All
query {
allTags {
id
name
}
}
Warning: This query will return all documents in the collection. Use with caution. This API is not enabled by default. You can enable it by using Whitelist APIs.
There will be filter
and sort
arguments which will be covered in the Filter and Sort section.
Bulk APIs
Bulk APIs are covered in the Bulk APIs section.
Whitelist APIs
You can whitelist APIs by adding them into the allowedApis
array in the DryerModule.register#definitions
.
DryerModule.register({
definitions: [
{
definition: Tag,
allowedApis: [
'paginate',
'create',
'update',
'findOne',
'remove',
'findAll',
'bulkCreate',
'bulkUpdate',
'bulkRemove',
],
},
],
}),
You can use *
for all APIs or essentials
for the essential APIs (create
, update
, findOne
, remove
, paginate
).
The default value is essentials
.