Add a dependency configuration environment (note the Target Electron version)
1: Yarn add sqlite3@latest --build-from-source -- Runtime =electron --target=8.5.2 - dist - url = https://atom.io/download/electron 2: yarn add electron - rebuild node > 12.19.0 need to upgrade the latest version of the node 3: Yarn add AWS - SDK Install aws- SDK, or the rebuild fails. 4: package.json Add command "rebuild: SQL ": "electron-rebuild -f -w sqlite3" 5: Yarn Rebuild: SQL verification is availableCopy the code
Create SQlite.js under main process of encapsulating SQL method
// import sqlite3 from 'sqlite3' const sqlite3 = require('sqlite3'); const sqlite = sqlite3.verbose(); class Sqlite { constructor () { this.sqlInstance = null; this.db = null; } connect (path) {return new Promise((resolve, reject) => {this.db = new sqlite.database (path, reject) (err) => { if (err) { reject(err); } else { resolve(1); }}); }); Return new Promise((resolve, reject) => {this.db.run(SQL, params); (err) => { if (err) { reject(err); } else { resolve(1); }}); }); Return new Promise((resolve, reject) => {this.db.exec(SQL, reject); (err) => { if (err) { reject(err); } else { resolve(1); }}); }); Get (SQL, params) {return new Promise(resolve, reject) => {this.db.get(SQL, params, (err, err, data) => { if (err) { reject(err); } else { resolve(data); }}); }); All (SQL, params) {return new Promise((resolve, reject) => {this.db.all(SQL, params, (err, err)) data) => { if (err) { reject(err); } else { resolve(data); }}); }); } close() {this.db.close(); } static getInstance () {this.sqlinstance = this.sqlinstance? this.sqlInstance : new Sqlite(); return this.sqlInstance; } } export default Sqlite;Copy the code
Three: The main process registers Sqlite service
// database service import Sqlite from './utils/ Sqlite '; const sqlite = new Sqlite(); context.sqlite = sqlite; Context is defined as a global contextCopy the code
Create UserListTable data model userListtable.js with User data
class UserListTable { constructor (context) { this.mContext = context; } insertAllContacts (args, userInfo) { const list = args.data; Let SQL = "return this. MContext. Sqlite. The exec (SQL); } searchUserList (params) { params = {userId: 123}; return this.mContext.sqlite.all('SELECT * FROM test'); } } export default UserListTable;Copy the code
Create User controller userListcorl.js call
import BaseClass from './baseClass';
class UserListCorl extends BaseClass {
async insertAllContacts (args, userInfo) {
return this.userListTable.insertAllContacts(args, userInfo);
}
}
export default UserListCorl;
Copy the code
supplement
baseClass.js
// All models registered in base consume import InitDB from '.. /models/initDB'; import UserListTable from '.. /models/userListTable'; class BaseClass { constructor (context) { this.mContext = context; this.mInitDB = new InitDB(context); this.userListTable = new UserListTable(context); } } export default BaseClass;Copy the code
InitDB.js
Copy the code
class InitDB { constructor (content) { this.mContext = content; } connectDB (path) { return this.mContext.sqlite.connect(path); } createTable () {return this. / / to create table mContext. Sqlite. The exec (SQL); } insertTestData (params) {return this.mcontext.sqlite.run (SQL); } } export default InitDB;Copy the code