Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds SQLite database for QR codes #17

Merged
merged 1 commit into from
May 17, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.