Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 | 22x 22x 22x 22x 22x 22x 22x 1x 1x 1x 1x 1x 22x 22x 22x 22x 1x 1x 1x 1x 1x 22x 1x 1x 1x 1x | import sqlite3 from 'sqlite3'; import {SessionInterface} from '../types'; import {SessionStorage} from '../session_storage'; import {sessionFromEntries, sessionEntries} from '../session-utils'; export interface SQLiteSessionStorageOptions { sessionTableName: string; } const defaultSQLiteSessionStorageOptions: SQLiteSessionStorageOptions = { sessionTableName: 'shopify_sessions', };E export class SQLiteSessionStorage implements SessionStorage { private options: SQLiteSessionStorageOptions; private db: sqlite3.Database; private ready: Promise<void>; constructor( private filename: string, opts: Partial<SQLiteSessionStorageOptions> = {}, ) { this.options = {...defaultSQLiteSessionStorageOptions, ...opts}; this.db = new sqlite3.Database(this.filename); this.ready = this.init(); } public async storeSession(session: SessionInterface): Promise<boolean> { await this.ready; const entries = sessionEntries(session); const query = ` INSERT OR REPLACE INTO ${this.options.sessionTableName} (${entries.map(([key]) => key).join(', ')}) VALUES (${entries.map(() => '?').join(', ')}); `; await this.query( query, entries.map(([_key, value]) => value), ); return true; } public async loadSession(id: string): Promise<SessionInterface | undefined> { await this.ready; const query = ` SELECT * FROM ${this.options.sessionTableName} WHERE id = ?; `; const rows = await this.query(query, [id]); if (!Array.isArray(rows) || rows?.length !== 1) return undefined; const rawResult = rows[0] as any; return sessionFromEntries(Object.entries(rawResult)); } public async deleteSession(id: string): Promise<boolean> { await this.ready; const query = ` DELETE FROM ${this.options.sessionTableName} WHERE id = ?; `; await this.query(query, [id]); return true; } private async hasSessionTable(): Promise<boolean> { const query = ` SELECT name FROM sqlite_schema WHERE type = 'table' AND name = ?; `; const rows = await this.query(query, [this.options.sessionTableName]); return rows.length === 1; } private async init() { const hasSessionTable = await this.hasSessionTable(); if (!hasSessionTable) { const query = ` CREATE TABLE ${this.options.sessionTableName} ( id varchar(255) NOT NULL PRIMARY KEY, shop varchar(255) NOT NULL, state varchar(255) NOT NULL, isOnline integer NOT NULL, expires integer, scope varchar(255), accessToken varchar(255), onlineAccessInfo varchar(255) ) `; await this.query(query); } } private query(sql: string, params: any[] = []): Promise<any[]> { return new Promise((resolve, reject) => { this.db.all(sql, params, (err, result) => { if (err) { reject(err); return; } resolve(result); }); }); } } |