WindmillWWindmill
Powered by
IceCactusI
Windmill•3y ago•
25 replies
IceCactus

Why does this code sometimes work but 95% of the time it doesnt.

import * as wmill from "windmill-client"
import { Client } from "pg"
import { from as copyFrom } from "pg-copy-streams"

// Defining the type for Postgres resource
type Postgresql = {
  host: string,
  port: number,
  user: string,
  dbname: string,
  sslmode: string,
  password: string,
  root_certificate_pem: string
}

export async function  main(csvFile: string, postgresqlResource: Postgresql) {

  //Start up db connection
  //Creating a new postgres client
  const client = new Client({
    user: postgresqlResource.user,
    database: postgresqlResource.dbname,
    password: postgresqlResource.password,
    host: postgresqlResource.host, // Changed from hostname to host
    port: postgresqlResource.port
  });

let stream;
const time = new Date().toISOString();


  try {
    await client.connect();

    //test to make sure connection is working.
    //await client.query("INSERT INTO inventory (sku,warehouse_id,time,qty) VALUES ('test',2,now(),25)");


    stream = await client.query(copyFrom('COPY inventory (sku,warehouse_id,time,qty) FROM STDIN'));

    let numOfRows = 0;   

    stream.write('partnumber' + '\t' + '2' + '\t' + time + '\t' + '43' + '\n');
    //await stream.end();
    
  }
  catch (e)
  {
    console.log(e);
  }
  finally
  {
    stream.end();  
  }
import * as wmill from "windmill-client"
import { Client } from "pg"
import { from as copyFrom } from "pg-copy-streams"

// Defining the type for Postgres resource
type Postgresql = {
  host: string,
  port: number,
  user: string,
  dbname: string,
  sslmode: string,
  password: string,
  root_certificate_pem: string
}

export async function  main(csvFile: string, postgresqlResource: Postgresql) {

  //Start up db connection
  //Creating a new postgres client
  const client = new Client({
    user: postgresqlResource.user,
    database: postgresqlResource.dbname,
    password: postgresqlResource.password,
    host: postgresqlResource.host, // Changed from hostname to host
    port: postgresqlResource.port
  });

let stream;
const time = new Date().toISOString();


  try {
    await client.connect();

    //test to make sure connection is working.
    //await client.query("INSERT INTO inventory (sku,warehouse_id,time,qty) VALUES ('test',2,now(),25)");


    stream = await client.query(copyFrom('COPY inventory (sku,warehouse_id,time,qty) FROM STDIN'));

    let numOfRows = 0;   

    stream.write('partnumber' + '\t' + '2' + '\t' + time + '\t' + '43' + '\n');
    //await stream.end();
    
  }
  catch (e)
  {
    console.log(e);
  }
  finally
  {
    stream.end();  
  }



Disclaimer: Im learning javascript. Im trying to bulk import into postgres using BUN. For some reason, randomly this work work like i expect but 95% of the time i run the script it won't. If i uncomment the insert, that will work 100% of the time.
WindmillJoin
3,362Members
Resources
Recent Announcements

Similar Threads

Was this page helpful?
Recent Announcements
henri-c

Weekly kenote to tell you about our latest updates https://discord.com/channels/930051556043276338/1278977038430240813 https://youtube.com/live/2dGd9TdT8xs?feature=share

henri-c · 4d ago

Pyra

### HTTP tracing (EE) Capture HTTP requests made by job scripts as observability spans Features: - View HTTP request traces (method, URL, status, timing) in the job details UI - Auto-instrumentation for Native TypeScript, MITM proxy for other languages - Integrates with external OpenTelemetry collectors changelog: https://www.windmill.dev/changelog/http-tracing docs: https://www.windmill.dev/docs/advanced/instance_settings#http-tracing Additionally jobs memory metrics are now fully OSS!

Pyra · 2w ago

henri-c

First keynote of the year here https://discord.com/channels/930051556043276338/1278977038430240813 🙂

henri-c · 4w ago

Similar Threads

Why does it sometimes not seem to schedule the next step?
TobsebTTobseb / help
2y ago
rest request script does not work
ChaftalieCChaftalie / help
2y ago
chromium tag code doesn't work suddenly.
Dylan ParkDDylan Park / help
15mo ago
Meaning of this error ?
ym1198Yym1198 / help
3y ago