Skip to content

Commit

Permalink
Merge branch 'grammy' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
roziscoding committed Oct 21, 2022
2 parents 3f1c8bf + 338db76 commit 43d3ddf
Showing 1 changed file with 56 additions and 0 deletions.
56 changes: 56 additions & 0 deletions migrate.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { MongoClient } from 'mongodb'
export type Target = {
key: string
value: {
pixKey: string
city: string
name: string
query: string
}
}

export type Origin = {
telegramId: number
pixKey: string
city: string
name: string
}

function convert(origin: Origin): Target {
return {
key: origin.telegramId.toString(),
value: {
pixKey: origin.pixKey,
city: origin.city,
name: origin.name,
query: ''
}
}
}

const DB_URI = process.env.DB_URI ?? ''
const PROD_DBNAME = 'amandapix'

async function main() {
const client = new MongoClient(DB_URI)
await client.connect()
const db = client.db(PROD_DBNAME)
const users = db.collection<Origin>('users')
const sessions = db.collection<Target>('sessions')

const migrated = await users
.find({})
.toArray()
.then((users) => users.filter((user) => user.pixKey))
.then((users) => users.map(convert))

await sessions.insertMany(migrated)

await client.close()
}

main()
.then(() => {
console.log('Done')
})
.catch(console.error)

1 comment on commit 43d3ddf

@vercel
Copy link

@vercel vercel bot commented on 43d3ddf Oct 21, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.