Now, we can write code to use this database in the Notes application.
Create models/notes-sqlite3.mjs file:
import util from 'util';import Note from './Note';import sqlite3 from 'sqlite3';import DBG from 'debug';const debug = DBG('notes:notes-sqlite3'); const error = DBG('notes:error-sqlite3'); var db; // store the database connection here async function connectDB() { if (db) return db; var dbfile = process.env.SQLITE_FILE || "notes.sqlite3"; await new Promise((resolve, reject) => { db = new sqlite3.Database(dbfile, sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE, err => { if (err) return reject(err); resolve(db); }); }); return db;}
This serves the same purpose as the connectDB function in notes-level.mjs: to manage ...