Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { defineMigration, type MigrationContext } from '@holo-js/db'

export default defineMigration({
async up({ schema }: MigrationContext) {
await schema.createTable('comments', (table) => {
table.id()
table.integer('post_id')
table.integer('user_id')
table.text('body')
table.string('status').default('pending')
table.timestamps()
})
},
async down({ schema }: MigrationContext) {
await schema.dropTable('comments')
},
})
13 changes: 12 additions & 1 deletion apps/blog-next/server/db/schema.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ export const categories = defineGeneratedTable("categories", {
"updated_at": column.timestamp().defaultNow(),
})

export const comments = defineGeneratedTable("comments", {
"id": column.id(),
"post_id": column.integer(),
"user_id": column.integer(),
"body": column.text(),
"status": column.string().default("pending"),
"created_at": column.timestamp().defaultNow(),
"updated_at": column.timestamp().defaultNow(),
})

export const emailVerificationTokens = defineGeneratedTable("email_verification_tokens", {
"id": column.uuid().primaryKey(),
"provider": column.string().default("users"),
Expand Down Expand Up @@ -134,6 +144,7 @@ declare module '@holo-js/db' {
"_holo_migrations": typeof holoMigrations
"auth_identities": typeof authIdentities
"categories": typeof categories
"comments": typeof comments
"email_verification_tokens": typeof emailVerificationTokens
"notifications": typeof notifications
"password_reset_tokens": typeof passwordResetTokens
Expand All @@ -146,6 +157,6 @@ declare module '@holo-js/db' {
}
}

export const tables = { "_holo_migrations": holoMigrations, "auth_identities": authIdentities, "categories": categories, "email_verification_tokens": emailVerificationTokens, "notifications": notifications, "password_reset_tokens": passwordResetTokens, "personal_access_tokens": personalAccessTokens, "post_tags": postTags, "posts": posts, "sessions": sessions, "tags": tags, "users": users } as const
export const tables = { "_holo_migrations": holoMigrations, "auth_identities": authIdentities, "categories": categories, "comments": comments, "email_verification_tokens": emailVerificationTokens, "notifications": notifications, "password_reset_tokens": passwordResetTokens, "personal_access_tokens": personalAccessTokens, "post_tags": postTags, "posts": posts, "sessions": sessions, "tags": tags, "users": users } as const

registerGeneratedTables(tables)
57 changes: 57 additions & 0 deletions apps/blog-next/server/holo-models.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import type {
BelongsToManyRelationDefinition,
BelongsToRelationDefinition,
EmptyScopeMap,
GeneratedSchemaTable,
HasManyRelationDefinition,
ModelReference,
RelationMap,
} from '@holo-js/db'

type CategoryTable = GeneratedSchemaTable<'categories'>
type CommentTable = GeneratedSchemaTable<'comments'>
type PostTable = GeneratedSchemaTable<'posts'>
type PostTagTable = GeneratedSchemaTable<'post_tags'>
type TagTable = GeneratedSchemaTable<'tags'>
type UserTable = GeneratedSchemaTable<'users'>

interface CategoryRelations extends RelationMap {
readonly posts: HasManyRelationDefinition<PostModel>
}

interface CommentRelations extends RelationMap {
readonly post: BelongsToRelationDefinition<PostModel>
readonly user: BelongsToRelationDefinition<UserModel>
}

interface PostRelations extends RelationMap {
readonly user: BelongsToRelationDefinition<UserModel>
readonly category: BelongsToRelationDefinition<CategoryModel>
readonly tags: BelongsToManyRelationDefinition<TagModel, PostTagTable>
readonly comments: HasManyRelationDefinition<CommentModel>
}

interface TagRelations extends RelationMap {
readonly posts: BelongsToManyRelationDefinition<PostModel, PostTagTable>
}

interface UserRelations extends RelationMap {
readonly posts: HasManyRelationDefinition<PostModel>
readonly comments: HasManyRelationDefinition<CommentModel>
}

type CategoryModel = ModelReference<CategoryTable, EmptyScopeMap, CategoryRelations>
type CommentModel = ModelReference<CommentTable, EmptyScopeMap, CommentRelations>
type PostModel = ModelReference<PostTable, EmptyScopeMap, PostRelations>
type TagModel = ModelReference<TagTable, EmptyScopeMap, TagRelations>
type UserModel = ModelReference<UserTable, EmptyScopeMap, UserRelations>

declare module '@holo-js/db' {
interface RegisteredModels {
Category: CategoryModel
Comment: CommentModel
Post: PostModel
Tag: TagModel
User: UserModel
}
}
5 changes: 4 additions & 1 deletion apps/blog-next/server/models/Category.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { defineModel } from '@holo-js/db'
import { defineModel, hasMany } from '@holo-js/db'

export default defineModel('categories', {
fillable: ['name', 'slug', 'description'],
relations: {
posts: hasMany('Post', { foreignKey: 'category_id' }),
},
})
9 changes: 9 additions & 0 deletions apps/blog-next/server/models/Comment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { belongsTo, defineModel } from '@holo-js/db'

export default defineModel('comments', {
fillable: ['post_id', 'user_id', 'body', 'status'],
relations: {
post: belongsTo('Post', { foreignKey: 'post_id' }),
user: belongsTo('User', { foreignKey: 'user_id' }),
},
})
11 changes: 5 additions & 6 deletions apps/blog-next/server/models/Post.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { belongsTo, belongsToMany, defineModel } from '@holo-js/db'

import Category from './Category'
import Tag from './Tag'
import { belongsTo, belongsToMany, defineModel, hasMany } from '@holo-js/db'

const relations = {
category: belongsTo(() => Category, { foreignKey: 'category_id' }),
tags: belongsToMany(() => Tag, {
user: belongsTo('User', { foreignKey: 'user_id' }),
category: belongsTo('Category', { foreignKey: 'category_id' }),
tags: belongsToMany('Tag', {
pivotTable: 'post_tags',
foreignPivotKey: 'post_id',
relatedPivotKey: 'tag_id',
}),
comments: hasMany('Comment', { foreignKey: 'post_id' }),
}

export default defineModel('posts', {
Expand Down
9 changes: 8 additions & 1 deletion apps/blog-next/server/models/Tag.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { defineModel } from '@holo-js/db'
import { belongsToMany, defineModel } from '@holo-js/db'

export default defineModel('tags', {
fillable: ['name', 'slug'],
relations: {
posts: belongsToMany('Post', {
pivotTable: 'post_tags',
foreignPivotKey: 'tag_id',
relatedPivotKey: 'post_id',
}),
},
})
6 changes: 5 additions & 1 deletion apps/blog-next/server/models/User.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { defineModel } from '@holo-js/db'
import { defineModel, hasMany } from '@holo-js/db'

export default defineModel('users', {
fillable: ['name', 'email', 'password', 'avatar'],
hidden: ['password'],
relations: {
posts: hasMany('Post', { foreignKey: 'user_id' }),
comments: hasMany('Comment', { foreignKey: 'user_id' }),
},
})
8 changes: 8 additions & 0 deletions apps/blog-next/tests/blog-logic.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { initializeHoloAdapterProject } from '@holo-js/core'
import Category from '../server/models/Category.ts'
import Post from '../server/models/Post.ts'
import Tag from '../server/models/Tag.ts'
import User from '../server/models/User.ts'
import {
createCategory,
createPost,
Expand Down Expand Up @@ -37,6 +38,13 @@ try {
assert.equal(home.categories.length, 2)
assert.equal(home.tags.length, 3)

const firstUser = await User.with('posts').first()
assert.ok(firstUser)
assert.ok(Array.isArray(firstUser.posts))

const featuredPost = await Post.with('user').where('slug', home.featured?.slug ?? '').first()
assert.ok(featuredPost?.user)

const dashboard = await getAdminDashboardData()
assert.equal(dashboard.postCount, 2)
assert.equal(dashboard.publishedCount, 2)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { defineMigration, type MigrationContext } from '@holo-js/db'

export default defineMigration({
async up({ schema }: MigrationContext) {
await schema.createTable('comments', (table) => {
table.id()
table.integer('post_id')
table.integer('user_id')
table.text('body')
table.string('status').default('pending')
table.timestamps()
})
},
async down({ schema }: MigrationContext) {
await schema.dropTable('comments')
},
})
13 changes: 12 additions & 1 deletion apps/blog-nuxt/server/db/schema.generated.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@ export const categories = defineGeneratedTable("categories", {
"updated_at": column.timestamp().defaultNow(),
})

export const comments = defineGeneratedTable("comments", {
"id": column.id(),
"post_id": column.integer(),
"user_id": column.integer(),
"body": column.text(),
"status": column.string().default("pending"),
"created_at": column.timestamp().defaultNow(),
"updated_at": column.timestamp().defaultNow(),
})

export const emailVerificationTokens = defineGeneratedTable("email_verification_tokens", {
"id": column.uuid().primaryKey(),
"provider": column.string().default("users"),
Expand Down Expand Up @@ -134,6 +144,7 @@ declare module '@holo-js/db' {
"_holo_migrations": typeof holoMigrations
"auth_identities": typeof authIdentities
"categories": typeof categories
"comments": typeof comments
"email_verification_tokens": typeof emailVerificationTokens
"notifications": typeof notifications
"password_reset_tokens": typeof passwordResetTokens
Expand All @@ -146,6 +157,6 @@ declare module '@holo-js/db' {
}
}

export const tables = { "_holo_migrations": holoMigrations, "auth_identities": authIdentities, "categories": categories, "email_verification_tokens": emailVerificationTokens, "notifications": notifications, "password_reset_tokens": passwordResetTokens, "personal_access_tokens": personalAccessTokens, "post_tags": postTags, "posts": posts, "sessions": sessions, "tags": tags, "users": users } as const
export const tables = { "_holo_migrations": holoMigrations, "auth_identities": authIdentities, "categories": categories, "comments": comments, "email_verification_tokens": emailVerificationTokens, "notifications": notifications, "password_reset_tokens": passwordResetTokens, "personal_access_tokens": personalAccessTokens, "post_tags": postTags, "posts": posts, "sessions": sessions, "tags": tags, "users": users } as const

registerGeneratedTables(tables)
57 changes: 57 additions & 0 deletions apps/blog-nuxt/server/holo-models.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import type {
BelongsToManyRelationDefinition,
BelongsToRelationDefinition,
EmptyScopeMap,
GeneratedSchemaTable,
HasManyRelationDefinition,
ModelReference,
RelationMap,
} from '@holo-js/db'

type CategoryTable = GeneratedSchemaTable<'categories'>
type CommentTable = GeneratedSchemaTable<'comments'>
type PostTable = GeneratedSchemaTable<'posts'>
type PostTagTable = GeneratedSchemaTable<'post_tags'>
type TagTable = GeneratedSchemaTable<'tags'>
type UserTable = GeneratedSchemaTable<'users'>

interface CategoryRelations extends RelationMap {
readonly posts: HasManyRelationDefinition<PostModel>
}

interface CommentRelations extends RelationMap {
readonly post: BelongsToRelationDefinition<PostModel>
readonly user: BelongsToRelationDefinition<UserModel>
}

interface PostRelations extends RelationMap {
readonly user: BelongsToRelationDefinition<UserModel>
readonly category: BelongsToRelationDefinition<CategoryModel>
readonly tags: BelongsToManyRelationDefinition<TagModel, PostTagTable>
readonly comments: HasManyRelationDefinition<CommentModel>
}

interface TagRelations extends RelationMap {
readonly posts: BelongsToManyRelationDefinition<PostModel, PostTagTable>
}

interface UserRelations extends RelationMap {
readonly posts: HasManyRelationDefinition<PostModel>
readonly comments: HasManyRelationDefinition<CommentModel>
}

type CategoryModel = ModelReference<CategoryTable, EmptyScopeMap, CategoryRelations>
type CommentModel = ModelReference<CommentTable, EmptyScopeMap, CommentRelations>
type PostModel = ModelReference<PostTable, EmptyScopeMap, PostRelations>
type TagModel = ModelReference<TagTable, EmptyScopeMap, TagRelations>
type UserModel = ModelReference<UserTable, EmptyScopeMap, UserRelations>

declare module '@holo-js/db' {
interface RegisteredModels {
Category: CategoryModel
Comment: CommentModel
Post: PostModel
Tag: TagModel
User: UserModel
}
}
5 changes: 4 additions & 1 deletion apps/blog-nuxt/server/models/Category.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { defineModel } from '@holo-js/db'
import { defineModel, hasMany } from '@holo-js/db'

export default defineModel('categories', {
fillable: ['name', 'slug', 'description'],
relations: {
posts: hasMany('Post', { foreignKey: 'category_id' }),
},
})
11 changes: 11 additions & 0 deletions apps/blog-nuxt/server/models/Comment.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { belongsTo, defineModel } from '@holo-js/db'

const relations = {
post: belongsTo('Post', { foreignKey: 'post_id' }),
user: belongsTo('User', { foreignKey: 'user_id' }),
}

export default defineModel('comments', {
fillable: ['post_id', 'user_id', 'body', 'status'],
relations,
})
11 changes: 5 additions & 6 deletions apps/blog-nuxt/server/models/Post.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
import { belongsTo, belongsToMany, defineModel } from '@holo-js/db'

import Category from './Category'
import Tag from './Tag'
import { belongsTo, belongsToMany, defineModel, hasMany } from '@holo-js/db'

const relations = {
category: belongsTo(() => Category, { foreignKey: 'category_id' }),
tags: belongsToMany(() => Tag, {
user: belongsTo('User', { foreignKey: 'user_id' }),
category: belongsTo('Category', { foreignKey: 'category_id' }),
tags: belongsToMany('Tag', {
pivotTable: 'post_tags',
foreignPivotKey: 'post_id',
relatedPivotKey: 'tag_id',
}),
comments: hasMany('Comment', { foreignKey: 'post_id' }),
}

export default defineModel('posts', {
Expand Down
9 changes: 8 additions & 1 deletion apps/blog-nuxt/server/models/Tag.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { defineModel } from '@holo-js/db'
import { belongsToMany, defineModel } from '@holo-js/db'

export default defineModel('tags', {
fillable: ['name', 'slug'],
relations: {
posts: belongsToMany('Post', {
pivotTable: 'post_tags',
foreignPivotKey: 'tag_id',
relatedPivotKey: 'post_id',
}),
},
})
6 changes: 5 additions & 1 deletion apps/blog-nuxt/server/models/User.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import { defineModel } from '@holo-js/db'
import { defineModel, hasMany } from '@holo-js/db'

export default defineModel('users', {
fillable: ['name', 'email', 'password', 'avatar'],
hidden: ['password'],
relations: {
posts: hasMany('Post', { foreignKey: 'user_id' }),
comments: hasMany('Comment', { foreignKey: 'user_id' }),
},
})
8 changes: 8 additions & 0 deletions apps/blog-nuxt/tests/blog-logic.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { initializeHoloAdapterProject } from '@holo-js/core'
import Category from '../server/models/Category.ts'
import Post from '../server/models/Post.ts'
import Tag from '../server/models/Tag.ts'
import User from '../server/models/User.ts'
import {
createCategory,
createPost,
Expand Down Expand Up @@ -37,6 +38,13 @@ try {
assert.equal(home.categories.length, 2)
assert.equal(home.tags.length, 3)

const firstUser = await User.with('posts').first()
assert.ok(firstUser)
assert.ok(Array.isArray(firstUser.posts))

const featuredPost = await Post.with('user').where('slug', home.featured?.slug ?? '').first()
assert.ok(featuredPost?.user)

const dashboard = await getAdminDashboardData()
assert.equal(dashboard.postCount, 2)
assert.equal(dashboard.publishedCount, 2)
Expand Down
Loading