add event sourcing actions

This commit is contained in:
Barry Gitarts 2019-01-15 17:29:02 -05:00
parent 4e92be1ce3
commit 245363f603
4 changed files with 68 additions and 3 deletions

41
app/actions/lpEvents.js Normal file
View File

@ -0,0 +1,41 @@
import { Q } from '@nozbe/watermelondb'
import database from '../db'
const lpCollection = database.collections.get('lp_events')
export const addEvent = async data => {
await database.action(async () => {
const res = await lpCollection.create(lpEvent => {
const { event, address, id, blockNumber } = data
lpEvent.eventId = id
lpEvent.address = address
lpEvent.event = event
lpEvent.blockNumber = blockNumber
return lpEvent
})
return res
})
}
export const batchAddEvents = async events => {
const batch = events.map(e => {
return lpCollection.prepareCreate(lpEvent => {
const { event, address, id, blockNumber } = e
lpEvent.eventId = id
lpEvent.address = address
lpEvent.event = event
lpEvent.blockNumber = blockNumber
return lpEvent
})
})
console.log({batch})
return await database.action(async () => await database.batch(...batch))
}
export const getLpEventById = async id => {
const event = await lpCollection.query(
//Q.where('event_id', id)
).fetch()
console.log({event})
return event
}

View File

@ -11,7 +11,7 @@ import { FundingContext } from './context'
import { cancelProfile } from './utils/fundProfiles' import { cancelProfile } from './utils/fundProfiles'
import MainCointainer from './components/MainCointainer' import MainCointainer from './components/MainCointainer'
import { getTransfersMemo } from './selectors/pledging' import { getTransfersMemo } from './selectors/pledging'
import database from './db' import { getLpEventById, addEvent, batchAddEvents } from './actions/lpEvents'
const { getNetworkType } = web3.eth.net const { getNetworkType } = web3.eth.net
@ -33,6 +33,9 @@ class App extends React.Component {
const { environment } = EmbarkJS const { environment } = EmbarkJS
const isInitialized = await vaultPledgingNeedsInit() const isInitialized = await vaultPledgingNeedsInit()
if (!!isInitialized) { if (!!isInitialized) {
const events = await getLpEventById(5)
console.log({events})
if (environment === 'development') console.log('mock_time:', await LiquidPledging.mock_time.call()) if (environment === 'development') console.log('mock_time:', await LiquidPledging.mock_time.call())
const lpAllowance = await getLpAllowance() const lpAllowance = await getLpAllowance()
const fundProfiles = await getProfileEvents() const fundProfiles = await getProfileEvents()
@ -42,8 +45,16 @@ class App extends React.Component {
const allLpEvents = await getAllLPEvents() const allLpEvents = await getAllLPEvents()
const vaultEvents = await getAllVaultEvents() const vaultEvents = await getAllVaultEvents()
const transfers = getTransfersMemo({ allLpEvents }) const transfers = getTransfersMemo({ allLpEvents })
const lpCollection = database.collections.get('lp_events')
console.log({lpCollection}) //TODO remove
const batching = await batchAddEvents(allLpEvents)
console.log({batching})
/* allLpEvents.forEach(async e => {
* const event = await getLpEventById(e.id)
* //const event = await addEvent(e)
* console.log({e, event})
* }) */
this.setState({ this.setState({
account, account,
network, network,

View File

@ -1,4 +1,5 @@
import { Model } from '@nozbe/watermelondb' import { Model } from '@nozbe/watermelondb'
import { action } from '@nozbe/watermelondb/decorators'
export default class LpEvent extends Model { export default class LpEvent extends Model {
static table = 'lp_events' static table = 'lp_events'
@ -6,5 +7,16 @@ export default class LpEvent extends Model {
@field('event_id') eventId @field('event_id') eventId
@field('address') address @field('address') address
@field('event') event @field('event') event
@field('block_number') blockNumber
@action async addEvent(data) {
return await this.create(lpEvent => {
const { event, address, id, blockNumber } = data
lpEvent.eventId = id
lpEvent.address = address
lpEvent.event = event
lpEvent.blockNumber = blockNumber
})
}
} }

View File

@ -9,6 +9,7 @@ export default appSchema({
{ name: 'address', type: 'string' }, { name: 'address', type: 'string' },
{ name: 'event', type: 'string' }, { name: 'event', type: 'string' },
{ name: 'event_id', type: 'string' }, { name: 'event_id', type: 'string' },
{ name: 'block_number', type: 'number', isIndexed: true },
] ]
}) })
] ]