Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: implement a db to store data #54

Open
haykkh opened this issue Oct 11, 2022 · 5 comments
Open

feat: implement a db to store data #54

haykkh opened this issue Oct 11, 2022 · 5 comments

Comments

@haykkh
Copy link
Owner

haykkh commented Oct 11, 2022

Probably something like https://docs.deta.sh/docs/base/about/

Also look into https://developer.spotify.com/documentation/general/guides/working-with-playlists/#version-control-and-snapshots

@haykkh
Copy link
Owner Author

haykkh commented Nov 29, 2022

Initially when creating the server routes I made separate, specific routes for each databases' routes
eg:

[server/api/db/playlist/put-many.post.ts]

import { type DetaType } from "deta/dist/types/types/basic"

import { useDetaBase } from "@/composables/deta"
// need to import composable as they aren't auto imported into /server
// https://nuxt.com/docs/guide/concepts/auto-imports
import { type IPlaylist } from "@/stores"
import { chunker } from "@/utils"

type DetaPlaylist = DetaType & IPlaylist

export default defineEventHandler(async (event) => {
  // calls a Deta Base put many method
  // https://docs.deta.sh/docs/base/sdk/#put-many

  const db = useDetaBase("playlists")
  const { playlists }: { playlists: DetaPlaylist[] } = await readBody(event)

  const mappedPlaylists = playlists.map(playlist => (
    // if playlist is spreadable (object type)
    typeof playlist === "object"
      // spread it
      ? {
        ...playlist,
        key: playlist.id
      }
      // else return empty obj
      : {}
  ))

  const chunks = chunker(mappedPlaylists, 25)

  return await Promise.all(chunks.map(async chunk => await db.putMany(chunk)))
})

and
[server/api/db/playlist/fetch.get.ts]

import { useDetaBase } from "@/composables/deta"
// need to import composable as they aren't auto imported into /server
// https://nuxt.com/docs/guide/concepts/auto-imports
import { chunker } from "@/utils"

export default defineEventHandler(async (event) => {
  // calls a Deta Base fetch method
  // https://docs.deta.sh/docs/base/sdk/#fetch

  const { id } = getQuery(event)

  const db = useDetaBase("playlists")

  if (Array.isArray(id)) {
    const queries = id.map(id => ({ id }))

    // no idea why
    // but testing showed that db.fetch failed with 37+ queries
    const chunks = chunker(queries, 37)

    return await Promise.all(chunks.map(async chunk => await db.fetch(chunk)))
  } else {
    return await db.fetch({ id })
  }
})

In the end I realized that it's probably better to match route parameters for database name (eg [server/api/db/[dbName]/fetch]), and make generalized routes for Deta Base Put/Get/Put Many etc methods

@haykkh
Copy link
Owner Author

haykkh commented Nov 29, 2022

Advanced settings have disappeared
Screenshot 2022-11-29 at 22 39 54

moved to #69

@haykkh
Copy link
Owner Author

haykkh commented Dec 6, 2022

Create profile page:

  • update playlists
  • store data checkmark

@haykkh
Copy link
Owner Author

haykkh commented Dec 6, 2022

Ask user during auth if they want data stored

@haykkh
Copy link
Owner Author

haykkh commented Dec 6, 2022

Seems more and more like data processing etc should be moved to a separate backend

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant