Skip to content

Commit

Permalink
WIP: Adds cli db seed
Browse files Browse the repository at this point in the history
  • Loading branch information
jim-toth committed Jul 23, 2024
1 parent 0c6adc9 commit 4fec844
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,5 @@ lerna-debug.log*
# Warp
cache/*

version.yml
version.yml
data
17 changes: 17 additions & 0 deletions cli/commands/seed-lock.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Prop, Schema, SchemaFactory } from '@nestjs/mongoose'
import { HydratedDocument } from 'mongoose'

@Schema()
export class SeedLock {
@Prop({ type: String, required: true })
seedName: string

@Prop({ type: Number, required: true, default: Date.now })
startedAt?: number

@Prop({ type: Number, required: false })
finishedAt?: number
}

export type SeedLockDocument = HydratedDocument<SeedLock>
export const SeedLockSchema = SchemaFactory.createForClass(SeedLock)
77 changes: 74 additions & 3 deletions cli/commands/seed-relay-sale-data.subcommand.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,79 @@
import { CommandRunner, SubCommand } from 'nest-commander'
import { InjectModel } from '@nestjs/mongoose'
import fs from 'fs'
import { Model } from 'mongoose'
import { CommandRunner, Option, SubCommand } from 'nest-commander'

import { SeedLock } from './seed-lock'
import { RelaySaleData } from '../../src/verification/schemas/relay-sale-data'

@SubCommand({ name: 'relay-sale-data' })
export class RelaySaleDataSubCommand extends CommandRunner {
async run(): Promise<void> {
console.log('TODO -> Seed relay sale data here!')
constructor(
@InjectModel(SeedLock.name)
private readonly seedLockModel: Model<SeedLock>,
@InjectModel(RelaySaleData.name)
private readonly relaySaleDataModel: Model<RelaySaleData>
) {
super()
}

@Option({
flags: '-d, --data <data-path>',
description: 'Path to CSV seed data',
required: true
})
parseSeedFilePath(path: string) {
const exists = fs.existsSync(path)

if (!exists) {
console.error(`File not found: ${path}`)
process.exit(1)
}

return path
}

async run(params: string[], options: { data: string }): Promise<void> {
const seedName = 'relay-sale-data'
const existingLock = await this.seedLockModel.findOne({ seedName })

if (existingLock) {
console.log(
`Found existing seed lock for ${seedName}`,
existingLock.toObject()
)

return
}

const saleDataCsv = fs.readFileSync(options.data).toString('utf-8')
const saleDataLines = saleDataCsv.split('\r\n')

const seedLock = await this.seedLockModel.create<SeedLock>({ seedName })

const session = await this.relaySaleDataModel.startSession()
session.startTransaction()

console.log(`Clearing existing ${seedName} data`)
await this.relaySaleDataModel.deleteMany()

console.log(`Seeding ${saleDataLines.length - 1} documents.`)
for (let i = 1; i < saleDataLines.length; i++) {
const [ serial, unparsedNftId ] = saleDataLines[i].split(',')
const parsedNftId = Number.parseInt(unparsedNftId)
const nftId = Number.isNaN(parsedNftId) ? 0 : parsedNftId

await this.relaySaleDataModel.create<RelaySaleData>({ serial, nftId })
}

await session.commitTransaction()
await session.endSession()

seedLock.finishedAt = Date.now()
await seedLock.save()

console.log(
`Done seeding ${saleDataLines.length - 1} ${seedName} documents.`
)
}
}
4 changes: 3 additions & 1 deletion cli/commands/seed.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { ConfigModule } from '@nestjs/config'
import { MongooseModule } from '@nestjs/mongoose'

import { SeedCommand } from './seed.command'
import { SeedLock, SeedLockSchema } from './seed-lock'
import {
RelaySaleData,
RelaySaleDataSchema
Expand All @@ -12,7 +13,8 @@ import {
imports: [
ConfigModule,
MongooseModule.forFeature([
{ name: RelaySaleData.name, schema: RelaySaleDataSchema }
{ name: RelaySaleData.name, schema: RelaySaleDataSchema },
{ name: SeedLock.name, schema: SeedLockSchema }
])
],
providers: [ ...SeedCommand.registerWithSubCommands() ],
Expand Down

0 comments on commit 4fec844

Please sign in to comment.