Querying the API
Let's say we have this generated data structure:
By default Marblism will generate all the following endpoint for the table Tweet
:
Api.Tweet.findMany(queryOptions: QueryOptions)
Api.Tweet.findOne(id: string)
Api.Tweet.createOne(Partial<Tweet>)
Api.Tweet.updateOne(id: string, Partial<Tweet>)
Api.Tweet.deleteOne(id: string)
Api.Tweet.findByUserId(id: string)
Api.Tweet.createByUserId(id: string, Partial<Tweet>)
QueryOptions allows you to do four things:
- includes related objects in the objects you are fetching
- filter data
- order data
- pagination
Includes related objects
Comment is related to Tweet. I can include the comments and the user associated to each comment of a specific tweet with just one request:
const tweets = Api.Tweet.findOne('73733892', {
includes: ['comments', 'comments.user'],
})
console.log(tweets)
// {
// 'id': '73733892',
// 'userId': '9393939',
// 'content': 'Im a tweet',
// 'comments': [{
// 'userId': '93493939',
// 'comment': 'Top tweet!',
// 'user': {
// 'id':'9393939',
// 'profilePicture': 'https://...'
// }
// }],
// }
Filter data by attributes
const tweets = Api.Tweet.findMany({
filters: {
content: {
like: 'Im a tweet',
},
},
})
// fetch all tweets with the field 'content' containing "Im a tweet"
Here are all the available filters available:
eq?: number // Equal to
neq?: number // Not equal to
gt?: number // Greater than
gte?: number // Greater than or equal to
lt?: number // Less than
lte?: number // Less than or equal to
in?: any[] // In array
nin?: any[] // Not in array
like?: string // case sensitive
ilike?: string // not case sensitive
Order by date
const tweetsRecent = Api.Tweet.findMany({
orders: { dateCreated: 'DESC' },
})
const tweetsOld = Api.Tweet.findMany({
orders: { dateCreated: 'ASC' },
})
Pagination
const tweetsPage1 = Api.Tweet.findMany({
pagination: {
page: 1
countItems?: 10
}
})