From 245363f603f3710e32d0343bba29e9cab06ac2a2 Mon Sep 17 00:00:00 2001 From: Barry Gitarts Date: Tue, 15 Jan 2019 17:29:02 -0500 Subject: [PATCH] add event sourcing actions --- app/actions/lpEvents.js | 41 +++++++++++++++++++++++++++++++++++++++++ app/dapp.js | 17 ++++++++++++++--- app/model/lpEvents.js | 12 ++++++++++++ app/model/schema.js | 1 + 4 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 app/actions/lpEvents.js diff --git a/app/actions/lpEvents.js b/app/actions/lpEvents.js new file mode 100644 index 0000000..0c792ad --- /dev/null +++ b/app/actions/lpEvents.js @@ -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 +} diff --git a/app/dapp.js b/app/dapp.js index ab40248..2ca412b 100644 --- a/app/dapp.js +++ b/app/dapp.js @@ -11,7 +11,7 @@ import { FundingContext } from './context' import { cancelProfile } from './utils/fundProfiles' import MainCointainer from './components/MainCointainer' import { getTransfersMemo } from './selectors/pledging' -import database from './db' +import { getLpEventById, addEvent, batchAddEvents } from './actions/lpEvents' const { getNetworkType } = web3.eth.net @@ -33,6 +33,9 @@ class App extends React.Component { const { environment } = EmbarkJS const isInitialized = await vaultPledgingNeedsInit() if (!!isInitialized) { + const events = await getLpEventById(5) + console.log({events}) + if (environment === 'development') console.log('mock_time:', await LiquidPledging.mock_time.call()) const lpAllowance = await getLpAllowance() const fundProfiles = await getProfileEvents() @@ -42,8 +45,16 @@ class App extends React.Component { const allLpEvents = await getAllLPEvents() const vaultEvents = await getAllVaultEvents() 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({ account, network, diff --git a/app/model/lpEvents.js b/app/model/lpEvents.js index 8896fb2..2b629da 100644 --- a/app/model/lpEvents.js +++ b/app/model/lpEvents.js @@ -1,4 +1,5 @@ import { Model } from '@nozbe/watermelondb' +import { action } from '@nozbe/watermelondb/decorators' export default class LpEvent extends Model { static table = 'lp_events' @@ -6,5 +7,16 @@ export default class LpEvent extends Model { @field('event_id') eventId @field('address') address @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 + }) + } } diff --git a/app/model/schema.js b/app/model/schema.js index 064f4fb..4d51b68 100644 --- a/app/model/schema.js +++ b/app/model/schema.js @@ -9,6 +9,7 @@ export default appSchema({ { name: 'address', type: 'string' }, { name: 'event', type: 'string' }, { name: 'event_id', type: 'string' }, + { name: 'block_number', type: 'number', isIndexed: true }, ] }) ]