-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
276 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,17 @@ | ||
import { Test, TestingModule } from '@nestjs/testing'; | ||
|
||
import { AppController } from './app.controller'; | ||
import { AppService } from './app.service'; | ||
|
||
describe('AppController', () => { | ||
let app: TestingModule; | ||
|
||
beforeAll(async () => { | ||
app = await Test.createTestingModule({ | ||
controllers: [AppController], | ||
providers: [AppService], | ||
providers: [], | ||
}).compile(); | ||
}); | ||
|
||
describe('getData', () => { | ||
it('should return "Hello API"', () => { | ||
const appController = app.get<AppController>(AppController); | ||
expect(appController.getData()).toEqual({ message: 'Hello API' }); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,96 @@ | ||
import { Controller, Get } from '@nestjs/common'; | ||
|
||
import { AppService } from './app.service'; | ||
import { Controller, Get, OnModuleInit } from '@nestjs/common'; | ||
import { Image } from './models/image.model'; | ||
import { User } from './models/user.model'; | ||
import { db, schema } from '@gradii/fedaco'; | ||
import { faker } from '@faker-js/faker'; | ||
|
||
@Controller() | ||
export class AppController { | ||
constructor(private readonly appService: AppService) {} | ||
export class AppController implements OnModuleInit { | ||
onModuleInit() { | ||
db().enableQueryLog(); | ||
} | ||
|
||
@Get('/init-table') | ||
async initTable() { | ||
await schema().create('users', table => { | ||
table.increments('id'); | ||
table.char('name').withLength(250); | ||
table.char('email').withLength(250); | ||
table.timestamps(); | ||
}); | ||
|
||
await schema().create('posts', table => { | ||
table.increments('id'); | ||
table.char('name').withLength(250); | ||
table.timestamps(); | ||
}); | ||
|
||
await schema().create('images', function (table) { | ||
table.increments('id'); | ||
table.morphs('imageable'); | ||
table.string('url'); | ||
table.timestamps(); | ||
}); | ||
|
||
} | ||
|
||
@Get('/all-users') | ||
async getAllUsers() { | ||
db().flushQueryLog(); | ||
const list = await User.createQuery().get(); | ||
|
||
const logs = db().getQueryLog(); | ||
return {list, logs}; | ||
} | ||
|
||
@Get('/add-user') | ||
async addUser() { | ||
await User.createQuery().create({ | ||
name : faker.person.fullName(), | ||
email: faker.internet.email() | ||
}); | ||
} | ||
|
||
|
||
@Get('/all-images') | ||
async getImages() { | ||
db().flushQueryLog(); | ||
const list = await Image.createQuery().get(); | ||
|
||
const logs = db().getQueryLog(); | ||
return {list, logs}; | ||
} | ||
|
||
@Get('/create-user-image') | ||
async createUserImage() { | ||
db().flushQueryLog(); | ||
const user = await User.createQuery().first(); | ||
|
||
await user.NewRelation('image').create({ | ||
url: faker.internet.url() | ||
}); | ||
|
||
const logs = await db().getQueryLog(); | ||
return {user, logs}; | ||
} | ||
|
||
@Get('/get-user-image') | ||
async getUserImage() { | ||
db().flushQueryLog(); | ||
|
||
const user = await User.createQuery().first(); | ||
const image = await user.image; | ||
const logs = await db().getQueryLog(); | ||
return {user, image, logs}; | ||
} | ||
|
||
@Get('/get-image-user') | ||
async getImageUser() { | ||
db().flushQueryLog(); | ||
|
||
@Get() | ||
getData() { | ||
return this.appService.getData(); | ||
const image = await Image.createQuery().first(); | ||
const user = await image.imageable; | ||
const logs = await db().getQueryLog(); | ||
return {image, user, logs}; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,20 @@ | ||
import { Module } from '@nestjs/common'; | ||
|
||
import { AppController } from './app.controller'; | ||
import { AppService } from './app.service'; | ||
import { FedacoModule } from '@gradii/nest-fedaco'; | ||
|
||
@Module({ | ||
imports: [], | ||
imports: [ | ||
FedacoModule.forRoot({ | ||
'default': { | ||
driver: 'mysql', | ||
database: 'test', | ||
username: 'root', | ||
password: '123456' | ||
} | ||
}) | ||
], | ||
controllers: [AppController], | ||
providers: [AppService], | ||
providers: [], | ||
}) | ||
export class AppModule {} |
21 changes: 0 additions & 21 deletions
21
apps/polymorphic-relationships/src/app/app.service.spec.ts
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
28 changes: 28 additions & 0 deletions
28
apps/polymorphic-relationships/src/app/models/image.model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { Column, forwardRef, Model, MorphToColumn, PrimaryGeneratedColumn, Table } from '@gradii/fedaco'; | ||
import { Post } from './post.model'; | ||
import { User } from './user.model'; | ||
|
||
|
||
@Table({ | ||
tableName: 'images' | ||
}) | ||
export class Image extends Model { | ||
_fillable = ['url']; | ||
|
||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
url: string; | ||
|
||
@MorphToColumn({ | ||
morphTypeMap: { | ||
'Post': forwardRef(() => Post), | ||
'User': forwardRef(() => User), | ||
'test_user': forwardRef(() => User), | ||
'test_post': forwardRef(() => Post), | ||
} | ||
}) | ||
imageable: any; | ||
|
||
} |
35 changes: 35 additions & 0 deletions
35
apps/polymorphic-relationships/src/app/models/post.model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import { | ||
Column, | ||
CreatedAtColumn, FedacoRelationType, | ||
forwardRef, | ||
MorphOneColumn, | ||
PrimaryGeneratedColumn, | ||
Table, | ||
UpdatedAtColumn | ||
} from '@gradii/fedaco'; | ||
import { Image } from './image.model'; | ||
|
||
@Table({ | ||
tableName: 'posts', | ||
morphTypeName: 'test_post' | ||
}) | ||
export class Post { | ||
|
||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
name: string; | ||
|
||
@MorphOneColumn({ | ||
related: forwardRef(() => Image), | ||
morphName: 'imageable' | ||
}) | ||
public image: FedacoRelationType<Image>; | ||
|
||
@CreatedAtColumn() | ||
created_at: Date; | ||
|
||
@UpdatedAtColumn() | ||
updated_at: Date; | ||
} |
40 changes: 40 additions & 0 deletions
40
apps/polymorphic-relationships/src/app/models/user.model.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { | ||
Column, | ||
CreatedAtColumn, | ||
forwardRef, | ||
Model, | ||
MorphOneColumn, | ||
PrimaryGeneratedColumn, RelationType, | ||
Table, UpdatedAtColumn, | ||
FedacoRelationType | ||
} from '@gradii/fedaco'; | ||
import { Image } from './image.model'; | ||
|
||
@Table({ | ||
tableName: 'users', | ||
morphTypeName: 'test_user' | ||
}) | ||
export class User extends Model { | ||
_fillable = ['name', 'email']; | ||
|
||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
name: string; | ||
|
||
@Column() | ||
email: string; | ||
|
||
@MorphOneColumn({ | ||
related: forwardRef(() => Image), | ||
morphName: 'imageable' | ||
}) | ||
public image: FedacoRelationType<Image>; | ||
|
||
@CreatedAtColumn() | ||
created_at: Date; | ||
|
||
@UpdatedAtColumn() | ||
updated_at: Date; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.