import { omit } from 'lodash-unified' ; import type { RedPacketRecord , RedPacketRecordInDatabase , RedPacketNftRecordInDatabase } from '../types' ; import { RedPacketPluginID } from '../constants' ; import { createPluginDatabase } from '../../../database/Plugin' ; export const RedPacketDatabase = createPluginDatabase < RedPacketRecordInDatabase | RedPacketNftRecordInDatabase > ( RedPacketPluginID ) ; export async function getAllRedpackets ( ids ) { const records = [ ] ; for await ( const record of RedPacketDatabase . iterate ( 'red-packet' ) ) { if ( ids . includes ( record . value . id ) ) records . push ( RedPacketRecordOutDB ( record . value ) ) ; } return records ; } export async function getRedPacket ( id ) { const record = await RedPacketDatabase . get ( 'red-packet' , id ) ; return record ? RedPacketRecordOutDB ( record ) : undefined ; } export async function addRedPacket ( record ) { return RedPacketDatabase . add ( RedPacketRecordIntoDB ( record ) ) ; } function RedPacketRecordIntoDB ( x ) { const record = x as RedPacketRecordInDatabase ; record . type = 'red-packet' ; return record ; } function RedPacketRecordOutDB ( x ) { const record = x ; return omit ( record , [ 'type' ] ) ; }