To use the Polybase database you can either use the default one that the data programs team has created or you can choose to read from a different one. To use the default database you will need the following import.
import {DB} from"@dataprograms/repdao-polybase";
To read from a different database you will have to add the below where NAME_SPACE is the name of the space you want to use.
The following examples will use the default Polybase database provided. It should also be noted that results are paginated and the developer is responsible for paging through the results if 100 rows is not enough.
Code Samples
Filtering by single column value in single collection
Example is getting one row where the provider is f01889512 in the filfox collection. Currently filtering and sorting on multiple columns is not supported because you have to create an index on the columns to support multiple where statements. see doc.
import {DB, filfox} from"@dataprograms/repdao-polybase";constprovider='f01889512'constdoc= (awaitDB.collection('filfox').where('provider','==', provider).limit(1).get()) .data[0].data asfilfoxconsole.log(`Filfox record for ${provider}: total rewards: ${doc.totalRewards}`)
You can however have multiple where statements on the same column name.
import {DB, filfox} from"@dataprograms/repdao-polybase";constdoc= (awaitDB.collection('filfox').where('provider','>','f01889512').where('provider','<=','f01889612').limit(1).get()) .data[0].data asfilfoxconsole.log(doc)console.log(`Filfox record has total rewards: ${doc.totalRewards}`)
Iterating over all the provided collections
import {PolybaseError } from"@polybase/client"import {DB, CollectionNames} from"@dataprograms/repdao-polybase";for (constcollectionNameof CollectionNames) {let responsetry { response =awaitDB.collection(collectionName).limit(1).get()console.log(`Data for polybase collection ${collectionName}`)console.log(response.data[0].data) } catch (e:any) {if (e instanceofPolybaseError) {console.error(`Polybase error: ${e.code}${e.message} when retrieving ${collectionName}.`)continue }throw e }}
Sorting column to find max or min
This example finds the max epoch that is less than 2849899 in the filfox collection.
Each collection has a column called date_stamp that can be used to search for rows before, after or between given dates.
// This example won't work with the current private key // in the repo because date stamp was added after import {PolybaseError } from"@polybase/client"import { DB, CollectionNames} from"@dataprograms/repdao-polybase";for (constcollectionNameof CollectionNames) {let responsetry {let daysToLookBack =2//X response =awaitDB.collection(collectionName).where("date_stamp",">=", (newDate(newDate().getTime() - (daysToLookBack *24*60*60*1000))).toISOString().substring(0,10)).sort("date_stamp","asc").limit(2).get();console.log(`Data for polybase collection ${collectionName}`)response.data.forEach(element => {console.log(element.data) }); } catch (e:any) {if (e instanceofPolybaseError) {console.error(`Polybase error: ${e.code}${e.message} when retrieving ${collectionName}.`)continue }throw e }}