Skip to content

Commit

Permalink
Adds SQLite database for QR codes
Browse files Browse the repository at this point in the history
Also renames database.sqlite to session_db.sqlite (for session storage)
  • Loading branch information
mkevinosullivan committed May 16, 2022
1 parent 6bfb7e2 commit afe02a9
Show file tree
Hide file tree
Showing 6 changed files with 72 additions and 7 deletions.
3 changes: 3 additions & 0 deletions qr-code/node/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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
12 changes: 8 additions & 4 deletions qr-code/node/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
"author": "Shopify",
"devDependencies": {
"@types/sqlite3": "^3.1.8"
}
}
12 changes: 9 additions & 3 deletions qr-code/node/web/backend/index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
// @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";

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";
Expand All @@ -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,
Expand All @@ -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 = {};
Expand Down Expand Up @@ -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"
);
Expand Down
52 changes: 52 additions & 0 deletions qr-code/node/web/backend/qr-codes-db.js
Original file line number Diff line number Diff line change
@@ -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);
});
});
}
}
Binary file added qr-code/node/web/qr_codes_db.sqlite
Binary file not shown.
File renamed without changes.

0 comments on commit afe02a9

Please sign in to comment.