TypeORM-readme-2.ts
· 467 B · TypeScript
Raw
const userRepository = MyDataSource.getRepository(User)
const user = new User()
user.firstName = "Timber"
user.lastName = "Saw"
user.age = 25
await userRepository.save(user)
const allUsers = await userRepository.find()
const firstUser = await userRepository.findOneBy({
id: 1,
}) // find by id
const timber = await userRepository.findOneBy({
firstName: "Timber",
lastName: "Saw",
}) // find by firstName and lastName
await userRepository.remove(timber)
1 | const userRepository = MyDataSource.getRepository(User) |
2 | |
3 | const user = new User() |
4 | user.firstName = "Timber" |
5 | user.lastName = "Saw" |
6 | user.age = 25 |
7 | await userRepository.save(user) |
8 | |
9 | const allUsers = await userRepository.find() |
10 | const firstUser = await userRepository.findOneBy({ |
11 | id: 1, |
12 | }) // find by id |
13 | const timber = await userRepository.findOneBy({ |
14 | firstName: "Timber", |
15 | lastName: "Saw", |
16 | }) // find by firstName and lastName |
17 | |
18 | await userRepository.remove(timber) |