@ovotech/potygen-pg-stream
TypeScript icon, indicating that this package has built-in type declarations

0.3.1 • Public • Published

Stream processing for potygen using node-postgres

Perform streaming query with @ovotech/potygen.

Supports:

  • async iterators
  • node streams
  • forEach function

Allows you to use Cursor to split up a big query response and retrieve only a subsection of it at a time, efficiently iterating through it. And helps integrating with other tools that use generators / node-streams.

Each Batch

The simplest of the helpers just calls a callback for each "batch" of items until all the results have been exhaousted.

examples/each-batch.ts:(query)

const productsQuery = toEachBatch(sql`SELECT product FROM orders WHERE region = $region`, { batchSize: 2 });

await productsQuery(db, { region: 'Sofia' }, async (batch) => {
  console.log(batch);
});

Async Iterator

Utilizing javascript's async iterators you can iterate through the results by keeping only a single batch in memory, using the humble for of loop.

examples/async-iterator.ts:(query)

const productsQuery = toAsyncIterator(sql`SELECT product FROM orders WHERE region = $region`, { batchSize: 2 });

for await (const item of productsQuery(db, { region: 'Sofia' })) {
  console.log(item);
}

Async Batch Iterator

The same as the toAsyncIterator, but keeps the batches intact and retrieves them whole.

examples/async-batch-iterator.ts:(query)

const productsQuery = toAsyncBatchIterator(sql`SELECT product FROM orders WHERE region = $region`, { batchSize: 2 });

for await (const batch of productsQuery(db, { region: 'Sofia' })) {
  console.log(batch);
}

Stream

You can also utilize node streams to process the data either in batches or one by one

examples/stream.ts:(query)

const productsQuery = toReadable(sql`SELECT product FROM orders WHERE region = $region`, { batchSize: 2 });

const sink = new Writable({
  objectMode: true,
  write: (chunk, encoding, callback) => {
    console.log(chunk);
    callback();
  },
});
const source = productsQuery(db, { region: 'Sofia' });

await asyncPipeline(source, sink);
console.log('Done');

Mapped queries

All of the streaming helpers support mapped queries, and the map will be executed on each batch after its retrieval.

examples/async-iterator-mapped.ts:(query)

const productsQuery = sql<MyQuery>`SELECT product FROM orders WHERE region = $region`;

const mappedProductsQuery = mapResult(
  (rows) => rows.map((row) => ({ ...row, productLength: row.product.length })),
  productsQuery,
);

const secondMappedProductsQuery = mapResult(
  (rows) => rows.map((row) => ({ ...row, productLengthSquare: Math.pow(row.productLength, 2) })),
  mappedProductsQuery,
);

const productsIterator = toAsyncIterator(secondMappedProductsQuery, { batchSize: 2 });

for await (const item of productsIterator(db, { region: 'Sofia' })) {
  console.log(item);
}

Readme

Keywords

none

Package Sidebar

Install

npm i @ovotech/potygen-pg-stream

Weekly Downloads

327

Version

0.3.1

License

Apache-2.0

Unpacked Size

17.9 kB

Total Files

18

Last publish

Collaborators

  • irina.dimitriu
  • puzzledbytheweb
  • paul.gallagher
  • mike.holloway
  • pedro.caldeira
  • harrisonbaxter
  • rob.desbois
  • sophiar27
  • ion.nasu
  • manoj-ovo
  • jeffer_ovo
  • bohdan-kozerema
  • ryan.lee1
  • zolegovo
  • pablomuro-ovotech
  • irena3888
  • dvoroniuc-ovo
  • arpad.fesis.ovo
  • pkoretsovo
  • marknreynolds
  • ovotarasrusyniak
  • gabriel.hulubei
  • jnysevilla
  • pallait
  • mableyip
  • shonnguen
  • ik1004kaluza
  • kseniya.belevich
  • willshawmedia
  • joewhittles
  • rachelknightkaluza
  • philip.fol.ovo
  • zach-smith-kza
  • phala.kantan
  • mikitadzianisevich
  • romans-ovo
  • iain.rawson.ovo
  • kantan_m_rutter
  • ruhi-choudhury-kaluza
  • cohen990-ovo
  • andrew-brook-rad
  • sean-kantan
  • george.anthony
  • ovox
  • eloisechilvers
  • kaluzajianzu
  • mikemchugh
  • trickkaluza
  • david.ovo
  • jubril
  • sophiesillmanovo
  • emmadavids
  • harry.faulkner
  • willcorrigan-ovo
  • edvinas.ovo
  • jadamiec-ovo
  • crektek
  • andrewinci-dev
  • mbartish_ovo
  • saamiyayousuf
  • veljko.popovic
  • vipul_vk
  • cenkovic_ovo
  • zoelanham
  • pedro.costa.kaluza
  • ekeith-kaluza
  • iharea-ovo
  • gracyde
  • mihaidavidovo
  • kupxc
  • will.crick
  • andreiracasan-ovo
  • kim-wysocki-ovo
  • stumacovo
  • oep-accounts-bot
  • filoseovo
  • tombolinep
  • nick.long
  • morris27sky
  • laurawady
  • daniel-billing-foundations
  • callumdenby
  • abilash999
  • tigranargit
  • srinivas.bada
  • stevedaykaluza
  • kelemensanyi
  • warren-ovo
  • catalin.andrei
  • bilaal-kaluza
  • jacktreble
  • nish1406
  • hentielouw
  • dtheasby-kaluza
  • ovo.backstage.admins
  • dlaird-ovo
  • garylaikaluza
  • rsh-ovo
  • gabrielngovo
  • robert-g-j
  • darinakulishovo
  • tommaso.bruno
  • djmelonz
  • r.midyk
  • jamieovo
  • luke.parsons.kaluza
  • bisongee
  • olliebatchelor
  • mikecsmith-ovo
  • gordok
  • andymay44
  • mark-b-ovo
  • brianbroughton
  • maricel-ovo
  • tc-kaluza
  • iuna4e
  • fulvio.ovo
  • chris.brookes
  • ivan.mihov-ovo
  • prashant16
  • aleksandyr
  • mishabruml
  • markwood23
  • lilbyteltd
  • fraserhamiltonovo
  • thomas.finch
  • benjamin.golding
  • mosman128
  • wjbenfold
  • ovo-markswaffer
  • jcholewa
  • benaston13
  • bookings-team
  • vitalii_khudenko
  • andbrss
  • ezio-intravaia-ovo
  • stevendiplock
  • georgexcollins
  • laxmi06
  • shnist
  • maieutiquer
  • orion-bot
  • kingfruit85
  • caolan.derry
  • manikant16
  • stefan-dospinescu-ovo
  • bare7aovo
  • jlucktay
  • lewisdick-ovo
  • phil-pinkowski
  • rekaelek-ovo
  • kaluzian
  • eugenovo
  • dyl349
  • wtaylor-ovo
  • bizval-bot
  • dangrebovic
  • mwidurek
  • raluza
  • brettburman-code
  • richard-mounter
  • miguel-catarino
  • jrdavenport
  • oeptariffs
  • mikethorpe-ovo
  • davekaluza
  • tarlingovo
  • mesh00
  • jthomasovo
  • props
  • adam-mcdevitt
  • vslepkan
  • melcbuckov
  • vkovetskyi
  • delaluza
  • rafael_kaluza
  • raraujo-ovo
  • zyurii
  • marcuskielly
  • michaelwheeler
  • xenjke
  • sulgee.kim
  • cifdso
  • liam-chambers
  • r0bturner
  • veselin.stamenov
  • adamblance-kaluza
  • kathryn.allan
  • lewright
  • friendigo
  • ovo-james
  • ovo-dc
  • samrumley88
  • t.vytrykush
  • metering-reads-health-bot
  • kuwotu
  • darren_thomas_ovo
  • lughino
  • jchoskins
  • ovotech-identity
  • vkobyletskyi_ovo
  • jamesnoble1
  • david.regula.ovo
  • danmaly
  • vasil.dininski
  • andy-heywood-ovo
  • tom.harrison-ovo
  • cristinapetrov
  • k-fernandez
  • vukovo
  • carolinelywood
  • prtn-ovo
  • m_heald
  • laurierw
  • csillabarna
  • marina-ovo
  • jennyunchan
  • paceteamkaluza
  • trading-and-dispatch
  • pkari
  • retail-payg-tech
  • cwkaluza
  • accrecovo
  • lenardprattovo
  • esra.kaya
  • ovo.trading.tech
  • simonmclean-ovo
  • qe-team
  • jonnyleakaluza
  • ovotech-smart-thermostat
  • scott-thomson239
  • laranjoeduardo
  • aga-ovo
  • gregshielkaluza
  • rise-team
  • nevenablagoeva
  • scdf
  • andrewjtn
  • mbrignall
  • ronald.nsabiyera
  • jcdclark
  • engagement-insights
  • askomaro
  • ovo_steve
  • rmcnovo
  • freddybushboy
  • ovo-aarongibbison
  • madalinadulhac
  • vcheban-ovo
  • gordonmartin
  • ievgen-paliichuk-ovo
  • fahedarshad
  • ellafutkowska
  • chrisfordkaluza
  • shubham-npm-ovo
  • jameswelshkaluza
  • petro.pavlenko
  • vzahakailo
  • harvey-appleton
  • mwz
  • dominicboston-kaluza
  • joepurnell-ovo
  • radhika-bijibilla
  • g-tibbs
  • jagreenwood1
  • vcobzaru
  • dwfullerton
  • per.linnett
  • edpatrick-ovo
  • mattgrayovo
  • kimnil
  • benvaughanjones
  • samwest
  • mtardugno-ovo
  • gjain-npm-ovo
  • alexvicolovo
  • rparkhomchuk
  • weiliangc3
  • mykola.p
  • ovo-josh
  • cbousie
  • david.chellapah.ovo
  • franciscodiasovo
  • vmary
  • a.calderwood
  • jamesbaum
  • retrojetpacks
  • samcooper720x
  • lebaptiste
  • iovana.pavlovici
  • tom.sherman
  • myovo-self-serve-service-account
  • admrply
  • serena-ahah
  • alicia.bowers
  • kelveden
  • mike.walters
  • khadra.ismail2
  • tashacallow
  • alan_smith_ovo
  • rohith-kaluza
  • ylukomskyi-corgi
  • achagan_kaluza
  • mike-gregory-kaluza
  • thomasgdane
  • anglox
  • mladjan-perceptive
  • amolrindhe
  • sujitkumar.shil
  • nathanmarshovo
  • andrixb
  • tudor.harries-kaluza
  • bphenriqueskaluza
  • anthony_tonev
  • michal-at-kantan
  • nick-ovox
  • asellick
  • npmpwoo
  • stewart-kantan
  • jamie-eb
  • luis.gashi
  • n-jeremic-npm
  • anthonykaluza
  • jackbott-ovo
  • klaudia.marzec
  • ponchosb
  • oliwia-lakatosz
  • luke-adams-ovo
  • jgok
  • dela_kaluza
  • maxandre.zils
  • chris-cooney-ovo
  • adam.vile
  • dhasovo
  • samrushton
  • maxatko
  • miksg
  • bindubp
  • ettie.eyre
  • ian.white
  • jonnycundall
  • mars-rover
  • ape-team
  • kgray-kaluza
  • daogilvie
  • kaluza-devex
  • ohs-aurora
  • kaluza-rnr
  • ipa-bot
  • gideoncaspi
  • kawbot
  • data.discovery.ovo
  • ovotech-sg
  • ovotech-qs
  • tomshawovo
  • potsec
  • rosario-ovo
  • zoejm
  • peterh-ovo
  • apjm
  • ovoenergyapps
  • homemoves
  • ovo-oot-bot
  • cp-ui-tooling
  • ovo-bit-tech
  • sir_hiss