This quickstart guide will walk you through building a complete blogging application with Esix. You’ll learn how to define models, perform CRUD operations, query data, and work with relationships.By the end of this tutorial, you’ll have a working blog application with authors, posts, and the ability to query, filter, and aggregate data.
Create a file called models.ts and define your data models by extending BaseModel:
models.ts
import { BaseModel } from 'esix'class Author extends BaseModel { public name = '' public email = '' public bio = '' public isActive = true // Define relationship to posts posts() { return this.hasMany(Post, 'authorId') }}class Post extends BaseModel { public title = '' public content = '' public authorId = '' public tags: string[] = [] public views = 0 public publishedAt: Date | null = null}export { Author, Post }
Every model automatically includes id, createdAt, and updatedAt fields. You don’t need to define them.
import { Author, Post } from './models'// Create an authorconst author = await Author.create({ name: 'Jane Doe', email: '[email protected]', bio: 'Technology writer and blogger'})console.log(`Created author with ID: ${author.id}`)// Create posts for this authorconst post1 = await Post.create({ title: 'Getting Started with TypeScript', content: 'TypeScript is a powerful superset of JavaScript...', authorId: author.id, tags: ['typescript', 'programming'], publishedAt: new Date()})const post2 = await Post.create({ title: 'MongoDB Best Practices', content: 'Learn how to structure your MongoDB databases...', authorId: author.id, tags: ['mongodb', 'database'], publishedAt: new Date()})console.log(`Created ${2} posts`)
// Find a specific authorconst foundAuthor = await Author.find(author.id)console.log(`Found: ${foundAuthor?.name}`)// Find a specific postconst foundPost = await Post.find(post1.id)console.log(`Found: ${foundPost?.title}`)
// Get all authorsconst allAuthors = await Author.all()console.log(`Total authors: ${allAuthors.length}`)// Get all postsconst allPosts = await Post.all()console.log(`Total posts: ${allPosts.length}`)
// Posts with more than 100 viewsconst popularPosts = await Post.where('views', '>', 100).get()// Posts with at least 50 viewsconst moderatelyPopular = await Post.where('views', '>=', 50).get()// Posts with fewer than 10 viewsconst lowViewPosts = await Post.where('views', '<', 10).get()// Posts that are not nullconst publishedPosts = await Post.where('publishedAt', '!=', null).get()
// Find published posts with high viewsconst topPublishedPosts = await Post .where('publishedAt', '!=', null) .where('views', '>', 100) .get()// Find active authors with specific email domainconst companyAuthors = await Author .where('isActive', true) .where('email', '[email protected]') .get()
// Get first 10 postsconst firstPage = await Post.limit(10).get()// Get second page (posts 11-20)const secondPage = await Post.skip(10).limit(10).get()// Get top 5 most viewed postsconst top5 = await Post .orderBy('views', 'desc') .limit(5) .get()
// Get the first matching postconst firstPublished = await Post .where('publishedAt', '!=', null) .orderBy('publishedAt', 'asc') .first()console.log(`First published post: ${firstPublished?.title}`)
Use the hasMany() method to query related records:
// Get all posts by an authorconst author = await Author.find(author.id)const authorPosts = await author.posts().get()console.log(`${author.name} has ${authorPosts.length} posts`)// Get only published posts by this authorconst publishedPosts = await author .posts() .where('publishedAt', '!=', null) .get()// Get the author's most popular postsconst popularPosts = await author .posts() .where('views', '>', 50) .orderBy('views', 'desc') .limit(5) .get()// Count author's postsconst postCount = await author.posts().count()console.log(`Total posts: ${postCount}`)
// Count all postsconst totalPosts = await Post.count()// Count published postsconst publishedCount = await Post .where('publishedAt', '!=', null) .count()console.log(`${publishedCount} of ${totalPosts} posts are published`)
// Total views across all postsconst totalViews = await Post.sum('views')console.log(`Total views: ${totalViews}`)// Total views for an author's postsconst authorViews = await author.posts().sum('views')console.log(`Author's total views: ${authorViews}`)
// Average views per postconst avgViews = await Post.average('views')console.log(`Average views per post: ${avgViews}`)// Average for published posts onlyconst avgPublishedViews = await Post .where('publishedAt', '!=', null) .average('views')
// 95th percentile of post viewsconst p95Views = await Post.percentile('views', 95)console.log(`95% of posts have fewer than ${p95Views} views`)// Median viewsconst medianViews = await Post.percentile('views', 50)console.log(`Median views: ${medianViews}`)
// Get all post titlesconst titles = await Post.pluck('title')console.log('All titles:', titles)// Get all unique tags (you'll need to flatten the array)const allTags = await Post.pluck('tags')const uniqueTags = [...new Set(allTags.flat())]console.log('Unique tags:', uniqueTags)