add pledges model, actions, relationship

This commit is contained in:
Barry Gitarts 2019-01-21 17:32:42 -05:00
parent 4e58e3430b
commit 6d726a9b9e
5 changed files with 100 additions and 4 deletions

56
app/actions/pledges.js Normal file
View File

@ -0,0 +1,56 @@
import { pick } from 'ramda'
import { Q } from '@nozbe/watermelondb'
import database from '../db'
import { getLatestProfileEvents } from './lpEvents'
import { formatFundProfileEvent } from '../utils/events'
const createPledge = (pledge, data) => {
const { id, owner, amount, token, commitTime, nDelegates, pledgeState, intendedProject } = data
pledge.pledgeId = id
pledge.owner = Number(owner)
pledge.amount = Number(amount)
pledge.token = token
pledge.commitTime = Number(commitTime)
pledge.nDelegates = Number(nDelegates)
pledge.pledgeState = pledgeState
pledge.intendedProject = Number(intendedProject)
pledge.profile.set(Number(id))
}
const pledgesCollection = database.collections.get('pledges')
export const addPledge = async data => {
return await database.action(async () => {
const res = await pledgesCollection.create(pledge => createPledge(pledge, data))
return res
})
}
export const batchAddPledges = async profiles => {
const batch = profiles.map(data => {
return pledgesCollection.prepareCreate(pledge => createPledge(pledge, data))
})
console.log({batch})
return await database.action(async () => await database.batch(...batch))
}
export const addFormattedProfiles = async () => {
const allProfiles = await getAllProfiles()
const allEventIds = allProfiles.map(p => p.eventId)
const events = await getLatestProfileEvents(allEventIds)
const formattedEvents = await Promise.all(
events.map(formatFundProfileEvent)
)
await batchAddPledges(formattedEvents)
}
export const getAllProfiles = async () => {
const events = await pledgesCollection.query().fetch()
return events
}
export const getPledgeById = async id => {
const event = await pledgesCollection.query(
Q.where('id_profile', id)
).fetch()
return event
}

View File

@ -4,6 +4,7 @@ import LokiJSAdapter from '@nozbe/watermelondb/adapters/lokijs'
import schema from './model/schema'
import LpEvent from './model/lpEvents'
import Profile from './model/profile'
import Pledge from './model/pledge'
const dbName = 'LiquidFunding'
const adapter = new LokiJSAdapter({
@ -15,7 +16,8 @@ const database = new Database({
adapter,
modelClasses: [
LpEvent,
Profile
Profile,
Pledge
],
actionsEnabled: true,
})

21
app/model/pledge.js Normal file
View File

@ -0,0 +1,21 @@
import { action, field, relation } from '@nozbe/watermelondb/decorators'
import { LiquidModel } from '../utils/models'
export default class Pledge extends LiquidModel {
static table = 'pledges'
static associations = {
profiles: { type: 'belongs_to', key: 'id_profile' },
}
@field('pledge_id') pledgeId
@field('owner_id') owner
@field('amount') amount
@field('token') token
@field('commit_time') commitTime
@field('n_delegates') nDelegates
@field('intended_project') intendedProject
@field('pledge_state') pledgeState
@relation('profiles', 'id_profile') profile
}

View File

@ -1,9 +1,12 @@
import { action, field } from '@nozbe/watermelondb/decorators'
import { action, field, children } from '@nozbe/watermelondb/decorators'
import { LiquidModel } from '../utils/models'
export default class Profile extends LiquidModel {
static table = 'profiles'
static associations = {
pledges: { type: 'has_many', foreignKey: 'id_profile' }
}
@field('addr') addr
@field('event_id') eventId
@ -13,6 +16,7 @@ export default class Profile extends LiquidModel {
@field('name') name
@field('url') url
@field('id_profile') idProfile
@children('pledges') pledges
@action async markAsCanceled() {
await this.update(profile => {

View File

@ -23,9 +23,22 @@ export default appSchema({
{ name: 'type', type: 'string' },
{ name: 'name', type: 'string' },
{ name: 'url', type: 'string' },
{ name: 'id_profile', type: 'number' }
{ name: 'id_profile', type: 'number', isIndexed: true }
]
}),
tableSchema({
name: 'pledges',
columns: [
{ name: 'pledge_id', type: 'number' },
{ name: 'owner_id', type: 'number', isIndexed: true },
{ name: 'amount', type: 'number' },
{ name: 'token', type: 'string' },
{ name: 'commit_time', type: 'number' },
{ name: 'n_delegates', type: 'number' },
{ name: 'intended_project', type: 'number' },
{ name: 'pledge_state', type: 'number' },
{ name: 'id_profile', type: 'number', isIndexed: true }
]
})
]
})