iT邦幫忙

2023 iThome 鐵人賽

DAY 29
0
Software Development

從實戰中學習:Take Home Assignment review & refactor系列 第 29

[Day 29] Simple Auth App : 修復TypeORM 問題與DataSource

  • 分享至 

  • xImage
  •  

[Day 27] Simple Auth App:ORM & DB setting中有設定了TypeORM,當中有問題與createConnection在新版的TypeORM中deprecated了,
修正src/entity/User.ts宣告

import {Entity, PrimaryGeneratedColumn, Column, UpdateDateColumn, CreateDateColumn} from "typeorm";

@Entity()
export class User {
    @PrimaryGeneratedColumn()
    id!: number;

    @Column({ unique: true })
    email!: string;

    @Column()
    password!: string;

    @Column({ default: '' })
    name!: string;

    // Initialize emailVerified to false by default.
    @Column({ default: false })
    emailVerified: boolean = false;

    // Mark emailVerificationToken as optional.
    @Column({ nullable: true })
    emailVerificationToken!: string;

    @Column({ default: 0 })
    loginCount!: number;

    @CreateDateColumn()
    signUpTimestamp!: Date;

    @UpdateDateColumn()
    lastLoginTimestamp!: Date;
}

再來建立src/data-source.ts

import "reflect-metadata"
import { DataSource } from "typeorm"
import { User } from "./entity/User"

export const AppDataSource = new DataSource({
    type: 'sqlite',
    database: 'auth-db.sqlite3',
    synchronize: true,
    logging: false,
    entities: [User],
    migrations: [],
    subscribers: [],
})

最後則在src/index.ts使用新的方式來呼叫

import { AppDataSource } from "./data-source"
import { User } from "./entity/User";

AppDataSource.initialize().then(async () => {
    const result = await AppDataSource.getRepository(User)
        .createQueryBuilder('user')
        .getMany()
    console.log(result)
}).catch(error => console.log(error))

明天最後一天看能不能將部分功能給完成。


上一篇
[Day 28] Simple Auth App:註冊、登入實作
下一篇
[Day 30] Simple Auth App : 來做個收尾
系列文
從實戰中學習:Take Home Assignment review & refactor30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言