Skip to content

Commit

Permalink
load the session
Browse files Browse the repository at this point in the history
  • Loading branch information
hassanelnajjar committed Sep 2, 2023
1 parent 109de43 commit c916d8c
Show file tree
Hide file tree
Showing 5 changed files with 219 additions and 232 deletions.
3 changes: 1 addition & 2 deletions web/database/build.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
BEGIN;

DROP TABLE IF EXISTS shopify_sessions;
CREATE TABLE IF NOT EXISTS shopify_sessions (
id TEXT PRIMARY KEY,
shop TEXT NOT NULL,
Expand All @@ -10,5 +10,4 @@ CREATE TABLE IF NOT EXISTS shopify_sessions (
scope TEXT,
expires INTEGER
);

COMMIT;
18 changes: 12 additions & 6 deletions web/database/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@ const isInsertOrUpdateRegex = new RegExp(/(UPDATE(.|\n)*SET)|(INSERT INTO)/i);
// eslint-disable-next-line no-underscore-dangle
let __pool;
if (env !== 'production' || connectionString.includes('localhost')) {
__pool = new Pool({ connectionString });
__pool = new Pool({ max: 20, connectionString });
} else {
__pool = new Pool({
max: 20,
connectionString,
ssl: { rejectUnauthorized: false, require: true }
});
Expand Down Expand Up @@ -99,13 +100,18 @@ const query = async (text, _params, client) => {
params = sanitizeCSVInjection(_params);
}

const res = await _pool.query(text, params);
try {
const res = await _pool.query(text, params);

if (res && res.rows) {
const rows = toCamelCase(toParentChild(res.rows));
res.rows = rows;
if (res && res.rows) {
const rows = toCamelCase(toParentChild(res.rows));
res.rows = rows;
}
return res;
} catch (e) {
console.log('Error from query fun', e, 'with query', text);
throw new Error(e);
}
return res;
};

const readSqlFile = async (filePath) => {
Expand Down
26 changes: 3 additions & 23 deletions web/database/initTestData.js
Original file line number Diff line number Diff line change
@@ -1,26 +1,6 @@
import { query } from './connect.js';
import deleteAllShopifySessionsData from './shopify_sessions/deleteAllShopifySessionsData.js';
import { join } from 'path';
import { readSqlFile as readSQLFileAndQuery } from './connect.js';

const initTestData = async () => {
console.log('initTestData');
await query(
`
BEGIN;
CREATE TABLE IF NOT EXISTS shopify_sessions (
id TEXT PRIMARY KEY,
shop TEXT NOT NULL,
state TEXT NOT NULL,
"isOnline" BOOLEAN NOT NULL DEFAULT FALSE,
"accessToken" TEXT NOT NULL,
"onlineAccessInfo" TEXT,
scope TEXT,
expires INTEGER
);
COMMIT;`
);
await deleteAllShopifySessionsData();
};
const initTestData = async () => Promise.resolve();

export default initTestData;
1 change: 1 addition & 0 deletions web/database/shopify_sessions/InsertSession.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const insertSession = async ({ id, shop, state, accessToken }) => {
(id, "accessToken", shop, state,"isOnline")
VALUES
($1, $2, $3, $4,FALSE)
on conflict (id) DO NOTHING
RETURNING *
`;

Expand Down
Loading

0 comments on commit c916d8c

Please sign in to comment.