add field generator fn

This commit is contained in:
Barry Gitarts 2019-01-16 13:57:07 -05:00
parent 689ca5e287
commit 88cdc66593
3 changed files with 19 additions and 9 deletions

View File

@ -10,7 +10,6 @@ export const addEvent = async data => {
lpEvent.address = address
lpEvent.event = event
lpEvent.blockNumber = blockNumber
return lpEvent
})
return res
})
@ -24,7 +23,6 @@ export const batchAddEvents = async events => {
lpEvent.address = address
lpEvent.event = event
lpEvent.blockNumber = blockNumber
return lpEvent
})
})
console.log({batch})
@ -34,7 +32,7 @@ export const batchAddEvents = async events => {
export const getLpEventById = async id => {
const event = await lpCollection.query(
//Q.where('event_id', id)
Q.where('event_id', id)
).fetch()
console.log({event})
return event

View File

@ -1,13 +1,18 @@
import { Model } from '@nozbe/watermelondb'
import { action } from '@nozbe/watermelondb/decorators'
import { fieldGenerator } from '../utils/db'
export default class LpEvent extends Model {
static table = 'lp_events'
constructor(...args) {
super(...args)
const field = fieldGenerator(this)
field('event_id', 'eventId')
field('address')
field('event')
field('block_number', 'blockNumber')
}
@field('event_id') eventId
@field('address') address
@field('event') event
@field('block_number') blockNumber
static table = 'lp_events'
@action async addEvent(data) {
return await this.create(lpEvent => {
@ -18,5 +23,4 @@ export default class LpEvent extends Model {
lpEvent.blockNumber = blockNumber
})
}
}

8
app/utils/db.js Normal file
View File

@ -0,0 +1,8 @@
export const fieldGenerator = self => (column, name) => {
Object.defineProperty(self, name || column, {
get() { return self._getRaw(column) },
set(value) { self._setRaw(column, value) },
enumerable: true,
configurable: true
})
}