Skip to content

Commit 00e9787

Browse files
Ryan MottleyCharlieC3
andauthored
feat: implement 'isConnected' helper function (#55) (#56)
* ci: support npm trusted publishers * Upgrade checkout and semantic-release actions * ci: add registry URL --------- Co-authored-by: CharlieC3 <[email protected]>
1 parent fd86ff0 commit 00e9787

File tree

3 files changed

+40
-14
lines changed

3 files changed

+40
-14
lines changed

.github/workflows/ci.yml

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@ jobs:
1717
lint:
1818
runs-on: ubuntu-latest
1919
steps:
20-
- uses: actions/checkout@v4
20+
- uses: actions/checkout@v6
2121

2222
- name: Use Node.js
23-
uses: actions/setup-node@v4
23+
uses: actions/setup-node@v6
2424
with:
2525
node-version-file: '.nvmrc'
2626

@@ -57,12 +57,12 @@ jobs:
5757
PGPASSWORD: postgres
5858
PGDATABASE: postgres
5959
steps:
60-
- uses: actions/checkout@v4
60+
- uses: actions/checkout@v6
6161
with:
6262
fetch-depth: 0
6363

6464
- name: Use Node.js
65-
uses: actions/setup-node@v4
65+
uses: actions/setup-node@v6
6666
with:
6767
node-version-file: '.nvmrc'
6868

@@ -94,7 +94,7 @@ jobs:
9494
run: npm run test -- --coverage
9595

9696
- name: Upload coverage to Codecov
97-
uses: codecov/codecov-action@v3
97+
uses: codecov/codecov-action@v5
9898

9999
- name: Print integration environment logs
100100
run: cat docker-compose-logs.txt
@@ -107,6 +107,7 @@ jobs:
107107
build-publish:
108108
permissions:
109109
contents: write
110+
id-token: write
110111
issues: write
111112
pull-requests: write
112113
runs-on: ubuntu-latest
@@ -116,26 +117,26 @@ jobs:
116117
steps:
117118
- name: Generate release bot app token
118119
id: generate_token
119-
uses: actions/create-github-app-token@v1
120+
uses: actions/create-github-app-token@v2
120121
with:
121122
app-id: ${{ secrets.HIROSYSTEMS_RELEASE_BOT_ID }}
122123
private-key: ${{ secrets.HIROSYSTEMS_RELEASE_BOT_PEM }}
123124

124-
- uses: actions/checkout@v4
125+
- uses: actions/checkout@v6
125126
with:
126-
token: ${{ secrets.GH_TOKEN || secrets.GITHUB_TOKEN }}
127127
fetch-depth: 0
128-
persist-credentials: false
128+
129129
- name: Get bot user ID
130130
id: bot-user-id
131131
run: |
132132
echo "user-id=$(gh api "/users/${{ steps.generate_token.outputs.app-slug }}[bot]" --jq .id)" >> "$GITHUB_OUTPUT"
133133
env:
134134
GH_TOKEN: ${{ steps.generate_token.outputs.token }}
135135

136-
- uses: actions/setup-node@v4
136+
- uses: actions/setup-node@v6
137137
with:
138138
node-version-file: '.nvmrc'
139+
registry-url: 'https://registry.npmjs.org'
139140

140141
- name: Install deps
141142
run: npm ci --audit=false
@@ -144,18 +145,16 @@ jobs:
144145
run: npm run build
145146

146147
- name: Semantic Release
147-
uses: cycjimmy/semantic-release-action@v4
148+
uses: cycjimmy/semantic-release-action@b12c8f6015dc215fe37bc154d4ad456dd3833c90 # v6
148149
# Only run on non-PR events or only PRs that aren't from forks
149150
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
150151
env:
151152
GITHUB_TOKEN: ${{ steps.generate_token.outputs.token }}
152-
NPM_TOKEN: ${{ secrets.NPM_TOKEN }}
153153
SEMANTIC_RELEASE_PACKAGE: ${{ github.event.repository.name }}
154154
GIT_AUTHOR_EMAIL: "${{ steps.bot-user-id.outputs.user-id }}+${{ steps.generate_token.outputs.app-slug }}[bot]@users.noreply.github.com"
155155
GIT_COMMITTER_EMAIL: "${{ steps.bot-user-id.outputs.user-id }}+${{ steps.generate_token.outputs.app-slug }}[bot]@users.noreply.github.com"
156156
with:
157-
semantic_version: 19
158157
extra_plugins: |
159158
@semantic-release/[email protected]
160159
@semantic-release/[email protected]
161-
conventional-changelog-conventionalcommits@6.1.0
160+
conventional-changelog-conventionalcommits@9.1.0

src/postgres/__tests__/base-pg-store.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,4 +115,15 @@ describe('BasePgStore', () => {
115115
expect(sqlTransactionContext.getStore()).toBeUndefined();
116116
expect(db.sql).toEqual(obj);
117117
});
118+
119+
test('isConnected returns true when the connection is alive', async () => {
120+
const connected = await db.isConnected();
121+
expect(connected).toBe(true);
122+
});
123+
124+
test('isConnected returns false when the connection is not alive', async () => {
125+
jest.spyOn(db, 'sql').mockRejectedValueOnce(new Error('Connection lost'));
126+
const connected = await db.isConnected();
127+
expect(connected).toBe(false);
128+
});
118129
});

src/postgres/base-pg-store.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,19 @@ export abstract class BasePgStore {
8686
isProdEnv ? this.sql`CONCURRENTLY` : this.sql``
8787
} ${this.sql(viewName)}`;
8888
}
89+
90+
/**
91+
* Checks if the database connection is alive.
92+
* @returns True if connected, false otherwise.
93+
*/
94+
async isConnected(): Promise<boolean> {
95+
try {
96+
await this.sql`SELECT NOW()`;
97+
return true;
98+
} catch (error) {
99+
return false;
100+
}
101+
}
89102
}
90103

91104
/**
@@ -116,4 +129,7 @@ export abstract class BasePgStoreModule {
116129
async refreshMaterializedView(viewName: string): Promise<void> {
117130
return this.parent.refreshMaterializedView(viewName);
118131
}
132+
async isConnected(): Promise<boolean> {
133+
return this.parent.isConnected();
134+
}
119135
}

0 commit comments

Comments
 (0)