Skip to content

Instantly share code, notes, and snippets.

@bangbang93
Created June 6, 2020 08:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bangbang93/23e4cb4ad0aec27631496e28d4fa1ba7 to your computer and use it in GitHub Desktop.
Save bangbang93/23e4cb4ad0aec27631496e28d4fa1ba7 to your computer and use it in GitHub Desktop.
typeorm stream
import {Column, CreateDateColumn, Entity, PrimaryGeneratedColumn} from 'typeorm'
@Entity()
export class Test {
@PrimaryGeneratedColumn() id: number
@Column() category: string
@CreateDateColumn() createdAt: Date
}
import {createConnection, getRepository} from 'typeorm'
import {Test} from './entity'
async function main() {
await createConnection({
type: 'mysql',
url: 'mysql://root@localhost/temp',
entities: [Test],
synchronize: true,
logging: true,
})
const repository = getRepository(Test)
const query = repository.createQueryBuilder('t')
.select('t.category')
.groupBy('t.category')
console.log('getMany', await query.getMany())
console.log('getRawMany', await query.getRawMany())
const stream = await query.stream()
stream.on('result', (e) => console.log('result', e))
stream.on('end', (e) => console.log('end'))
for await(const row of stream) {
console.log('stream', row)
}
}
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment