Skip to content

Commit 5287f9a

Browse files
committed
refactor: only create statements when needed
1 parent 9bd08d6 commit 5287f9a

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

tests/recordRequests.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ db.exec(`CREATE TABLE IF NOT EXISTS nocks (
1212
status INTEGER NOT NULL
1313
)`);
1414

15-
const insertNockStatement = db.prepare(`INSERT OR REPLACE INTO nocks (hash, body, headers, status) VALUES (?, ?, jsonb(?), ?)`);
16-
const getNockStatement = db.prepare(`SELECT body, json(headers) as headers, status FROM nocks WHERE hash = ?`);
1715

1816
/**
1917
* @param {string | URL} input
@@ -41,6 +39,7 @@ function getRequestHash(input, init) {
4139
}
4240

4341
if (process.env.NOCK_ENV === `record`) {
42+
const insertNockStatement = db.prepare(`INSERT OR REPLACE INTO nocks (hash, body, headers, status) VALUES (?, ?, jsonb(?), ?)`);
4443
const realFetch = globalThis.fetch;
4544
globalThis.fetch = async (input, init) => {
4645
const response = await realFetch(input, init);
@@ -68,6 +67,8 @@ if (process.env.NOCK_ENV === `record`) {
6867
});
6968
};
7069
} else if (process.env.NOCK_ENV === `replay`) {
70+
const getNockStatement = db.prepare(`SELECT body, json(headers) as headers, status FROM nocks WHERE hash = ?`);
71+
7172
globalThis.fetch = async (input, init) => {
7273
const requestHash = getRequestHash(input, init);
7374

0 commit comments

Comments
 (0)