may be you can use my awesome code :
db.js
const sqlite3 = require("sqlite3").verbose()
const db = new sqlite3.Database("./book.db")
module.exports = db
setup.js
const db = require("./db.js")
const query = [
`
CREATE TABLE IF NOT EXISTS
contacts (
contactID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
name TEXT,
company TEXT,
telephone TEXT,
email TEXT UNIQUE
)`,
`
CREATE TABLE IF NOT EXISTS
groups (
groupID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
name TEXT
)`,
`
CREATE TABLE IF NOT EXISTS
group_contacts (
group_contactsID INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
contactID INTEGER NOT NULL,
groupID INTEGER NOT NULL,
FOREIGN KEY(contactID) REFERENCES contacts(contactID),
FOREIGN KEY(groupID) REFERENCES groups(groupID)
)`
]
for (let i = 0; i < query.length; i++){
db.run(query[i], (err) =>{
console.log(err)
})
}
index.js
const controller = require("./controller.js")
let input = process.argv.slice(2)
let cmd = input[0]
let data = input.slice(1)
switch(cmd){
case "createContact":
controller.createContact(data)
break
controller.js
const contact = require("./model/contact.js")
const group = require("./model/group.js")
const groupContact = require("./model/contact-group.js")
const view = require("./view.js")
class Controller{
static createContact(data){
contact.create(data, (err, output) => {
if (err){
view.createError(err)
} else {
view.createSuccess(output)
}
})
}
static readContact(data){
contact.read(data, (err, output) => {
if (err){
view.readError(err)
} else {
view.readSuccess(output)
}
})
}
static updateContact(data){
contact.update(data, (err) => {
if (err){
view.updateError(err)
} else {
view.updateSuccess(data)
}
})
}
static deleteContact(data){
contact.delete(data, (err) => {
if (err){
view.deleteError(err)
} else {
view.deleteSuccess(data[0])
}
})
}
}
module.exports = Controller
model.js
const db = require("../db.js")
class Contact{
constructor(name, company, phone, email){
this.name = name
this.company = company
this.telephone = phone
this.email = email
}
static create(data, cb){
db.serialize((err) => {
if (err){
cb(err, null)
} else {
let newContact = new Contact(data[0], data[1], data[2], data[3])
let query = `
INSERT INTO contacts (name, company, telephone, email)
VALUES ('${data[0]}', '${data[1]}', '${data[2]}', '${data[3]}')`
db.run(query, (err) => {
if (err){
cb(err, null)
} else {
cb(null, newContact)
}
})
}
})
}
static update(data, cb){
db.serialize((err) => {
if (err){
cb(err, null)
} else {
let query = `
UPDATE contacts SET ${data[1]} = '${data[2]}' WHERE contactID = ${data[0]}`
db.get(query, (err) => {
if (err){
cb(err)
} else {
cb(null)
}
})
}
})
}
static read(data, cb){
db.serialize((err) => {
if (err){
cb(err, null)
} else {
let query = `
SELECT contactID, name, company, telephone, email FROM contacts WHERE contactID = ${data[0]}`
db.get(query, (err, result) => {
if (err){
cb(err, null)
} else {
cb(null, result)
}
})
}
})
}
static delete(data, cb){
db.serialize((err) => {
if (err){
cb(err, null)
} else {
let query = `
DELETE FROM contacts WHERE contactID = ${data[0]}`
db.get(query, (err) => {
if (err){
cb(err)
} else {
cb(null)
}
})
}
})
}
}
module.exports = Contact
view.js
class View {
static createError(err){
console.log(err)
console.log('=====> ERROR')
}
static createSuccess(output){
console.log(output)
console.log('=====> SUCCESS')
}
static readError(err){
console.log(err)
console.log('=====> ERROR')
}
static readSuccess(output){
console.log(output)
console.log('=====> SUCCESS')
}
static updateError(err){
console.log(err)
console.log('=====> ERROR')
}
static updateSuccess(output){
console.log(output)
console.log('=====> SUCCESS')
}
static deleteError(err){
console.log(err)
console.log('=====> ERROR')
}
static deleteSuccess(output){
console.log(output)
console.log('=====> SUCCESS')
}
static undefined(output){
console.log(output)
console.log('=====> UNDEFINED')
}
}
module.exports = View