-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
96 additions
and
5 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 |
---|---|---|
|
@@ -40,4 +40,5 @@ lerna-debug.log* | |
# Warp | ||
cache/* | ||
|
||
version.yml | ||
version.yml | ||
data |
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,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) |
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,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.` | ||
) | ||
} | ||
} |
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