【问题标题】:how to insert bulk of data into postgres by using nestJS without loophow to insert bulk of data into postgres by using nestJS without loop
【发布时间】:2022-12-27 22:50:10
【问题描述】:

i am beginner in nestJS. how can I insert the bulk of data into Postgres without using a loop. can anybody share a piece of code that will be helpful for me? thanks.

import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { Feature } from './feature.entity';

@Injectable()
export class AppService {
  constructor(@InjectRepository(Feature) private readonly featureRepository: Repository<Feature>){}

  async addData(data: any){
    
    for(let i = 0; i< data.length; i++){
      await this.featureRepository.manager.query('INSERT INTO public.feature(id, name, phone) VALUES ($1, $2, $3)', [data[i].id, data[i].name, data[i].phone])
    }
    return true;
  }
}

【问题讨论】:

  • It'd be helpful if you added more details such as how are you currently inserting any data into postgres from your app, are you using any ORM etc? Details along with sn-ps of existing code would be very helpful in understanding
  • yeah, i added a sample of code that I am using. this is not actual code but i am using this style for coding
  • can you help me with a better style of coding without loop. because my application inserts thousands of records into the database at once
  • you can generate a string of values first using the for loop, then you can insert all the values at once which should result in a much faster query. For example INSERT INTO table(a, b, c) VALUES (6, 5, 7), (1, 2, 6), (8, 6, 5) ...

标签: javascript node.js postgresql nestjs


【解决方案1】:

you can inject DataSource to your service. and use it for saving multiple entities at once.

@Injectable()
export class AppService {
  constructor(@InjectRepository(Feature) private readonly featureRepository: Repository<Feature>, private readonly dataSource: DataSource){}

  async addData(data: any){
    const features: Feature[] = []
    for(let i = 0; i< data.length; i++){
      features.push(this.featureRepository.create({/* put your data here */})
    }
    return await this.dataSource.manager.save(features)
  }
}

actually, you don't even need the featureRepository you can instantiate Feature class and fill its property without needing the create helper method.

There is multiple approaches to do it, just read the typeorm documentation.

【讨论】:

    【解决方案2】:

    Prisma provides the methods to insert multiple records into the database in a single query.

    Check the below link

    https://www.prisma.io/docs/concepts/components/prisma-client/crud#update-or-create-records

    const data = [
      {
        id: 1,
        email: 'bennison@yopmail.com',
        last_name: 'Devadoss',
        first_name: 'Bennison'
      },
      {
        id: 1,
        email: 'gibson@yopmail.com',
        last_name: 'Devadoss',
        first_name: 'Bennison'
      },
      {
        id: 1,
        email: 'edison@yopmail.com',
        last_name: 'Devadoss',
        first_name: 'Bennison'
      }
    ];
    
    
    
    await prisma.user.upsert({
        where: { id: 1 },
        update: data,
        create: data,
      });
    

    Here I have variable data. which has an array of user data. In that array, there are three objects. Here what I have done is, instead of using the loop. I have used the Prisma upsert method, what it does is, creates or update the data by checking the where clause property in DB, if the where clause property is present in the database it updates data, or else it creates the new data into the database.

    【讨论】:

      猜你喜欢
      • 2022-12-02
      • 2022-12-19
      • 2022-12-02
      • 2022-12-27
      • 2022-12-27
      • 2022-12-27
      • 2015-08-15
      • 2022-12-02
      • 2022-12-26
      相关资源
      最近更新 更多