Interface AssetRepository

An interface for a repository of Assets.

Provide your own cache implementation to avoid HTTP requests.

Example

The following example shows how to use a custom cache implementation to avoid HTTP requests using the Dexie indexeddb wrapper.

Dependencies

npm i dexie @phoobynet/alpaca-services

assetRepository.ts

// assetRepository.ts
import type { Asset, ØAssetRepository } from '@phoobynet/alpaca-services'
import { getAssets, cleanSymbol } from '@phoobynet/alpaca-services'
import Dexie from 'dexie'

class AlpacaServicesAssetsDatabase extends Dexie {
public assets!: Dexie.Table<Asset>

constructor() {
super('AlpacaServicesAssetsDatabase')

this.version(1).stores({
assets: 'id,class,status,symbol,exchange,name,tradable,shortable,marginable,easy_to_borrow',
})
}
}

const database = new AlpacaServicesAssetsDatabase()

let assetsReady = false

const isEmpty = async (): Promise<void> => {
if (!assetsReady) {
const c = await database.assets.count()
if (c === 0) {
// set forceHTTP to true to ensure data is retrieved from the API (not from cache (which will be empty (which is loop badness)))
const assets = await getAssets(true)
await database.assets.bulkPut(assets)
}
assetsReady = true
}
}

export const assetRepository: AssetRepository = {
async find(symbol: string): Promise<Asset | undefined> {
await isEmpty()
return database.assets.where('symbol').equalsIgnoreCase(symbol).first()
},
async findAll(): Promise<Asset[]> {
await isEmpty()
return database.assets.toArray()
},
}

Hierarchy

  • AssetRepository

Properties

Methods

Properties

isDefault?: boolean

Indicates this is using the in-memory asset repository (not recommended)

Methods

  • find(symbol: string): Promise<undefined | Asset>
  • Parameters

    • symbol: string

    Returns Promise<undefined | Asset>

  • findAll(): Promise<Asset[]>
  • Returns Promise<Asset[]>

Generated using TypeDoc