HMR causes multiple instances

Abierto
#628 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
52/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
javascript, next.js, node.js

Línea de trabajo

Start by reading the open() usage in the issue and how lowdb manages database instances and their lifecycle. Reproduce the duplicate-open error in a Next.js server component with HMR, then inspect the relevant instance and close APIs. Done means a supported pattern prevents multiple instances writing the same file during HMR and is documented clearly.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

In a Next.js application changing a file causes a HMR update to occur. This is pretty standard practice, but LowDB is not properly designed to handle it.

The error:

[browser] Uncaught Error: lowdb: media.jsonl is already open in this process. Call open() once and share the returned db; two instances writing the same file would diverge.

And before you panick: it actually runs on the server. I don't know why it says [browser] there, but it does, so I thought it might be worth including. The lib is called upon from a server component.

The code:

import { open, DeepReadonly } from "lowdb";
import { Data } from "./type"; // actual type doesn't matter

const init = { media: [] };
const db = open<Data>("data/media.jsonl", init);

export const getData = (): DeepReadonly<Data> => {
  return db.data;
}

Where should I put close()?
Not at the and of the file - that would immedaitely close the db after opening. Not at the end of the getData function. That would close the db after the first retrieval, and then stop working because the db is not open. I cannot wrap this in a using block - you can't export from such a block, which is understandable.

PS, I'm totally guessing that this is how one creates a lib for db handling stuff. There's no proper documentation on how to do this, so I have to resort to asking questions that probably look dumb.

Lenguaje dominante
JavaScript
Estrellas
22.6k
Forks
965
Merge medio
45 min
PR fusionados (30 d)
2

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de typicode/lowdb

Todos los issues de typicode/lowdb

Issues similares

Más issues de JavaScript

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.