-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #38 from rsksmart/feat/prisma-txPending-txpool
feat: added txPending and txpool converters, insert, update and delete
- Loading branch information
Showing
5 changed files
with
129 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
function rawTxPendingToEntity (data) { | ||
return { | ||
hash: data.hash, | ||
blockHash: data.blockHash, | ||
from: data.from, | ||
to: data.to, | ||
blockNumber: data.blockNumber, | ||
transactionIndex: data.transactionIndex, | ||
nonce: data.nonce, | ||
gas: data.gas, | ||
gasPrice: data.gasPrice, | ||
value: data.value, | ||
input: data.input, | ||
status: data.status | ||
} | ||
} | ||
|
||
function rawTxInPoolToEntity (data) { | ||
return { | ||
...rawTxPendingToEntity(data), | ||
poolId: data.poolId | ||
} | ||
} | ||
|
||
function rawTxPoolToEntity (data) { | ||
return { | ||
blockNumber: data.blockNumber, | ||
pending: data.pending, | ||
queued: data.queued, | ||
timestamp: String(data.timestamp) | ||
} | ||
} | ||
|
||
export {rawTxPendingToEntity, rawTxPoolToEntity, rawTxInPoolToEntity} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,35 @@ | ||
import { prismaClient } from '../lib/Setup' | ||
import {rawTxPendingToEntity, rawTxInPoolToEntity} from '../converters/txPending.converters' | ||
import { mongoQueryToPrisma } from './utils' | ||
|
||
export const txPendingRepository = { | ||
findOne (query = {}, project = {}, collection) { | ||
return collection.findOne(query, project) | ||
}, | ||
find (query = {}, project = {}, collection, sort = {}, limit = 0, isArray = true) { | ||
if (isArray) { | ||
return collection | ||
.find(query, project) | ||
.sort(sort) | ||
.limit(limit) | ||
.toArray() | ||
} else { | ||
return collection | ||
.find(query, project) | ||
.sort(sort) | ||
.limit(limit) | ||
} | ||
}, | ||
countDocuments (query = {}, collection) { | ||
return collection.countDocuments(query) | ||
async findOne (query = {}, project = {}, collection) { | ||
const txPending = await prismaClient.transaction_pending.findFirst({where: mongoQueryToPrisma(query)}) | ||
|
||
return txPending | ||
}, | ||
aggregate (aggregate, collection) { | ||
return collection.aggregate(aggregate).toArray() | ||
async find (query = {}, project = {}, collection, sort = {}, limit = 0, isArray = true) { | ||
const txsPending = await prismaClient.transaction_pending.findMany({where: mongoQueryToPrisma(query)}) | ||
|
||
return txsPending | ||
}, | ||
async deleteOne (query, collection) { | ||
await prismaClient.transaction_pending.deleteMany({where: query}) | ||
|
||
const mongoRes = collection.deleteOne(query) | ||
const mongoRes = await collection.deleteOne(query) | ||
return mongoRes | ||
}, | ||
updateOne (filter, update, options = {}, collection) { | ||
return collection.updateOne(filter, update, options) | ||
}, | ||
insertOne (data, collection) { | ||
return collection.insertOne(data) | ||
async updateOne (filter, update, options = {}, collection) { | ||
const {$set: data, poolId} = update | ||
const newTxPending = rawTxPendingToEntity(data) | ||
|
||
await prismaClient.$transaction([ | ||
prismaClient.transaction_pending.upsert({where: filter, update: newTxPending, create: newTxPending}), | ||
prismaClient.transaction_in_pool.create({data: rawTxInPoolToEntity({...data, poolId})}) | ||
]) | ||
|
||
delete update.poolId | ||
const mongoRes = await collection.updateOne(filter, update, options) | ||
return mongoRes | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,51 @@ | ||
import { prismaClient } from '../lib/Setup' | ||
import {rawTxPoolToEntity} from '../converters/txPending.converters' | ||
import {mongoQueryToPrisma} from './utils' | ||
|
||
export const txPoolRepository = { | ||
findOne (query = {}, project = {}, collection) { | ||
return collection.findOne(query, project) | ||
async findOne (query = {}, project = {}, collection) { | ||
const txPool = await prismaClient.txpool.findFirst({ | ||
where: mongoQueryToPrisma(query), | ||
orderBy: {id: 'desc'}, | ||
include: {transaction_in_pool: true} | ||
}) | ||
|
||
txPool.txs = [...txPool.transaction_in_pool] | ||
delete txPool.transaction_in_pool | ||
|
||
txPool._id = txPool.id | ||
delete txPool.id | ||
|
||
txPool.timestamp = Number(txPool.timestamp) | ||
|
||
return txPool | ||
}, | ||
find (query = {}, project = {}, collection, sort = {}, limit = 0, isArray = true) { | ||
if (isArray) { | ||
return collection | ||
.find(query, project) | ||
.sort(sort) | ||
.limit(limit) | ||
.toArray() | ||
} else { | ||
return collection | ||
.find(query, project) | ||
.sort(sort) | ||
.limit(limit) | ||
} | ||
async find (query = {}, project = {}, collection, sort = {}, limit = 0, isArray = true) { | ||
let txPools = await prismaClient.txpool.findMany({ | ||
where: query, | ||
orderBy: {timestamp: 'desc'}, | ||
include: {transaction_in_pool: true}, | ||
take: limit | ||
}) | ||
|
||
txPools = txPools.map(txPool => { | ||
txPool.txs = [...txPool.transaction_in_pool] | ||
delete txPool.transaction_in_pool | ||
|
||
txPool._id = txPool.id | ||
delete txPool.id | ||
|
||
txPool.timestamp = Number(txPool.timestamp) | ||
|
||
return txPool | ||
}) | ||
|
||
return txPools | ||
}, | ||
countDocuments (query = {}, collection) { | ||
return collection.countDocuments(query) | ||
}, | ||
aggregate (aggregate, collection) { | ||
return collection.aggregate(aggregate).toArray() | ||
}, | ||
insertOne (data, collection) { | ||
return collection.insertOne(data) | ||
async insertOne (data, collection) { | ||
const txpool = await prismaClient.txpool.create({data: rawTxPoolToEntity(data)}) | ||
|
||
const mongoRes = await collection.insertOne(data) | ||
return {...mongoRes, id: txpool.id} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters