diff --git a/qr-code/node/.gitignore b/qr-code/node/.gitignore index 50f5a4f..1ce559b 100644 --- a/qr-code/node/.gitignore +++ b/qr-code/node/.gitignore @@ -25,3 +25,6 @@ dist/ # Without this, they may get a warning if using a different package manager to us yarn.lock package-lock.json + +# default database files +*.sqlite diff --git a/qr-code/node/package.json b/qr-code/node/package.json index bad1131..d934967 100644 --- a/qr-code/node/package.json +++ b/qr-code/node/package.json @@ -12,9 +12,13 @@ "deploy": "shopify app deploy" }, "dependencies": { - "@shopify/cli": "2.0.2", "@shopify/app": "2.0.2", - "react": "17.0.2" + "@shopify/cli": "2.0.2", + "react": "17.0.2", + "sqlite3": "^5.0.8" }, - "author": "paulo" -} \ No newline at end of file + "author": "Shopify", + "devDependencies": { + "@types/sqlite3": "^3.1.8" + } +} diff --git a/qr-code/node/web/backend/index.js b/qr-code/node/web/backend/index.js index f37c095..04f92a1 100644 --- a/qr-code/node/web/backend/index.js +++ b/qr-code/node/web/backend/index.js @@ -1,5 +1,5 @@ // @ts-check -import { join } from "path"; +import path from 'path'; import express from "express"; import cookieParser from "cookie-parser"; import { Shopify, ApiVersion } from "@shopify/shopify-api"; @@ -7,6 +7,7 @@ import { Shopify, ApiVersion } from "@shopify/shopify-api"; import applyAuthMiddleware from "./middleware/auth.js"; import verifyRequest from "./middleware/verify-request.js"; import { setupGDPRWebHooks } from "./gdpr.js"; +import { QRCodesDB } from './qr-codes-db.js'; const USE_ONLINE_TOKENS = true; const TOP_LEVEL_OAUTH_COOKIE = "shopify_top_level_oauth"; @@ -18,6 +19,9 @@ const isTest = process.env.NODE_ENV === "test" || !!process.env.VITE_TEST_BUILD; const DEV_INDEX_PATH = `${process.cwd()}/frontend/`; const PROD_INDEX_PATH = `${process.cwd()}/dist/`; +const sessionDbFile = path.join(process.cwd(), 'session_db.sqlite'); +const qrCodesDbFile = path.join(process.cwd(), 'qr_codes_db.sqlite'); + Shopify.Context.initialize({ API_KEY: process.env.SHOPIFY_API_KEY, API_SECRET_KEY: process.env.SHOPIFY_API_SECRET, @@ -27,9 +31,11 @@ Shopify.Context.initialize({ API_VERSION: ApiVersion.April22, IS_EMBEDDED_APP: true, // This should be replaced with your preferred storage strategy - SESSION_STORAGE: new Shopify.Session.MemorySessionStorage(), + SESSION_STORAGE: new Shopify.Session.SQLiteSessionStorage(sessionDbFile), }); +const qrCodesDB = new QRCodesDB(qrCodesDbFile); + // Storing the currently active shops in memory will force them to re-login when your server restarts. You should // persist this object in your app. const ACTIVE_SHOPIFY_SHOPS = {}; @@ -128,7 +134,7 @@ export async function createServer( } else { // res.set('X-Shopify-App-Nothing-To-See-Here', '1'); const fs = await import("fs"); - const fallbackFile = join( + const fallbackFile = path.join( isProd ? PROD_INDEX_PATH : DEV_INDEX_PATH, "index.html" ); diff --git a/qr-code/node/web/backend/qr-codes-db.js b/qr-code/node/web/backend/qr-codes-db.js new file mode 100644 index 0000000..5b5e93e --- /dev/null +++ b/qr-code/node/web/backend/qr-codes-db.js @@ -0,0 +1,52 @@ +import sqlite3 from 'sqlite3'; + +export class QRCodesDB { + qrTableName = "qr_codes"; + db = null; + ready = null; + + constructor(filename) { + this.db = new sqlite3.Database(filename); + this.ready = this.init(); + } + + async hasQrCodesTable() { + const query = ` + SELECT name FROM sqlite_schema + WHERE + type = 'table' AND + name = ?; + `; + const rows = await this.query(query, [this.qrTableName]); + return rows.length === 1; + } + + async init() { + const hasQrCodesTable = await this.hasQrCodesTable(); + if (!hasQrCodesTable) { + const query = ` + CREATE TABLE ${this.qrTableName} ( + id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + productId VARCHAR(255) NOT NULL, + goToCheckout TINYINT NOT NULL, + discountCode VARCHAR(255) NOT NULL, + hits INTEGER, + conversions INTEGER + ) + `; + await this.query(query); + } + } + + query(sql, params = []) { + return new Promise((resolve, reject) => { + this.db.all(sql, params, (err, result) => { + if (err) { + reject(err); + return; + } + resolve(result); + }); + }); + } +} diff --git a/qr-code/node/web/qr_codes_db.sqlite b/qr-code/node/web/qr_codes_db.sqlite new file mode 100644 index 0000000..70c3b46 Binary files /dev/null and b/qr-code/node/web/qr_codes_db.sqlite differ diff --git a/qr-code/node/web/database.sqlite b/qr-code/node/web/session_db.sqlite similarity index 100% rename from qr-code/node/web/database.sqlite rename to qr-code/node/web/session_db.sqlite