radicalplatforms / shafa

A wardrobe logging, composition, and organization app
https://shafa.app
Mozilla Public License 2.0
1 stars 0 forks source link

feat: migrate d1 to neon, polish APIs #56

Closed rak3rman closed 6 months ago

rak3rman commented 6 months ago

Closes #26, #27, #55.

github-actions[bot] commented 6 months ago

Successfully Deployed to Stage Environment Cloudflare Staging URL :rocket: : 56-stage.api.shafa.radison.workers.dev/* Neon Branch :elephant: : https://console.neon.tech/app/projects/old-wave-86470546/branches/br-snowy-grass-a5um7l39

github-actions[bot] commented 6 months ago

Successfully Deployed to Staging Environment Cloudflare Staging URL :rocket: : https://56-stage-shafa-hono.radicalplatforms.workers.dev Neon Branch :elephant: : https://console.neon.tech/app/projects/old-wave-86470546/branches/br-snowy-grass-a5um7l39

github-actions[bot] commented 6 months ago

Successfully Deployed to Staging Environment Cloudflare Staging URL :rocket: : https://56-stage-shafa-hono.radicalplatforms.workers.dev Neon Branch :elephant: : https://console.neon.tech/app/projects/old-wave-86470546/branches/br-snowy-grass-a5um7l39

github-actions[bot] commented 6 months ago

Successfully Deployed to Staging Environment Cloudflare Staging URL :rocket: https://56-stage-shafa-hono.radicalplatforms.workers.dev Neon Branch :elephant: https://console.neon.tech/app/projects/old-wave-86470546/branches/br-delicate-paper-a544ploe

github-actions[bot] commented 6 months ago

Successfully Deployed to Staging Environment Cloudflare Staging URL :rocket: https://shafa-hono-stage-56.radicalplatforms.workers.dev Neon Branch :elephant: https://console.neon.tech/app/projects/old-wave-86470546/branches/br-hidden-firefly-a5otjman

rak3rman commented 6 months ago

Would also be a good idea to update the README.md to reflect new changes.

rak3rman commented 6 months ago

SQLite D1 data also needs to be migrated over to prod Postgres instance.

github-actions[bot] commented 6 months ago

Successfully Deployed to Staging Environment Cloudflare Staging URL :rocket: https://shafa-hono-stage-56.radicalplatforms.workers.dev Neon Branch :elephant: https://console.neon.tech/app/projects/old-wave-86470546/branches/br-hidden-firefly-a5otjman

github-actions[bot] commented 6 months ago

Successfully Deployed to Staging Environment Cloudflare Staging URL :rocket: https://shafa-hono-stage-56.radicalplatforms.workers.dev Neon Branch :elephant: https://console.neon.tech/app/projects/old-wave-86470546/branches/br-hidden-firefly-a5otjman

github-actions[bot] commented 6 months ago

Successfully Deployed to Staging Environment Cloudflare Staging URL :rocket: https://shafa-hono-stage-56.radicalplatforms.workers.dev Neon Branch :elephant: https://console.neon.tech/app/projects/old-wave-86470546/branches/br-hidden-firefly-a5otjman

github-actions[bot] commented 6 months ago

Successfully Deployed to Staging Environment Cloudflare Staging URL :rocket: https://shafa-hono-stage-56.radicalplatforms.workers.dev Neon Branch :elephant: https://console.neon.tech/app/projects/old-wave-86470546/branches/br-hidden-firefly-a5otjman

rak3rman commented 6 months ago

Here's the crappy script that I used to migrate SQLite (D1) to Postgres (Neon)

import { neonConfig } from '@neondatabase/serverless'
import ws from 'ws'
import axios from 'axios'
import app from './src/index'
import type { items } from './src/schema'

neonConfig.webSocketConstructor = ws

function convertIntToString(index: number): string {
  const itemTypeEnum: [string, ...string[]] = ['layer', 'top', 'bottom', 'footwear', 'accessory']
  if (index >= 0 && index < itemTypeEnum.length) {
    return itemTypeEnum[index]
  } else {
    throw new Error('Index out of bounds')
  }
}

const main = async () => {
  try {
    let response = await axios.get('https://api.shafa.app/api/outfits')

    let itemIdMap = new Map()

    for (let outfit of response.data) {
      console.log(outfit)

      let itemIdsTypes = []

      // Loop over each item in outfit
      for (let itemWithType of outfit.itemsToOutfits) {
        let item = itemWithType.item
        let type = convertIntToString(itemWithType.type)

        // Create item in db if it doesn't exist in map
        if (!itemIdMap.has(item.id)) {
          let resItems = await app.request('/api/items', {
            method: 'POST',
            body: JSON.stringify({
              name: item.name,
              brand: item.brand,
              photoUrl: item.photo,
              type: convertIntToString(item.type),
              rating: Math.floor((item.rating + item.quality) / 2),
            }),
            headers: { 'Content-Type': 'application/json' },
          })
          const json = (await resItems.json()) as (typeof items)[]
          console.log(JSON.stringify(json))
          itemIdMap.set(item.id, json[0].id)
        }

        // Add itemIdTypes
        itemIdsTypes.push({ id: itemIdMap.get(item.id), itemType: type })
      }

      // Create outfit
      let newOutfitResponse = await app.request('/api/outfits', {
        method: 'POST',
        body: JSON.stringify({
          rating: outfit.rating,
          wearDate: outfit.wearDate,
          itemIdsTypes: itemIdsTypes,
        }),
        headers: { 'Content-Type': 'application/json' },
      })
    }
  } catch (error) {
    console.error(error)
  }

  process.exit(0)
}

main().then()
github-actions[bot] commented 6 months ago

Successfully Deployed to Staging Environment Cloudflare Staging URL :rocket: https://shafa-hono-stage-56.radicalplatforms.workers.dev Neon Branch :elephant: https://console.neon.tech/app/projects/old-wave-86470546/branches/br-curly-hat-a5d43735

github-actions[bot] commented 6 months ago

Successfully Deployed to Staging Environment Cloudflare Staging URL :rocket: https://shafa-hono-stage-56.radicalplatforms.workers.dev Neon Branch :elephant: https://console.neon.tech/app/projects/old-wave-86470546/branches/br-curly-hat-a5d43735