データストレージ
この章からはデータストレージを用いてデータの永続化する方法を学んでいく。
最初は JSON 形式のテキストファイルをデータストレージとして利用。特に難しいところはない。 id
の生成に UUID を使うことも想定の範囲内。
UUID のドキュメント uuid
For the creation of RFC4122 UUIDs
API
ディレクトリ、ファイル操作は頻繁に利用する。頻繁に使うだろうと思われるメソッドのドキュメントのリンクを貼って、今日は学習した気分になってく。
The callback is given a possible exception and, if recursive is true, the first directory path created, (err, [path]).
await fs.promises.mkdir('path/to/file.txt', { recursive: true });
The path.parse() method returns an object whose properties represent significant elements of the path.
const filePath = 'path/to/file.txt'; path.parse(filePath); { root: '', dir: 'path/to', base: 'file.txt', ext: '.txt', name: 'file' }
The path.join() method joins all given path segments together using the platform-specific separator as a delimiter, then normalizes the resulting path.
path.join('path1', 'path2'); ('path1/path2');
The path.resolve() method resolves a sequence of paths or path segments into an absolute path.
path.resolve('path1', 'path2'); ('/home/xxx/yyy/todos/path1/path2');