HMR causes multiple instances

オープン
#628 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
52/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
javascript, next.js, node.js
領域
backend, databases

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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.

主要言語
JavaScript
スター
22.6k
フォーク
965
平均マージ
45分
マージ済み PR(30日)
2

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

typicode/lowdb のほかの issue

typicode/lowdb の issue をすべて見る

似ている issue

JavaScript の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。