在[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))
明天最後一天看能不能將部分功能給完成。