Skip to content

Commit 9830417

Browse files
committed
working version of using the pg commonjs library
1 parent f02216d commit 9830417

File tree

7 files changed

+62
-6
lines changed

7 files changed

+62
-6
lines changed

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
node_modules
22
yarn.lock
3-
.vscode
3+
.vscode

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,5 @@ Would love suggestions on how to make it better or to point to a better example!
2727
1. Install latest Node (Notice that I've placed the `engines` field on `package.json` so try to make sure you have the exact version or simply delete it from `package.json`
2828
2. Install dependencies - `yarn`
2929
3. Compile with `tsc -w` and run Node with `nodemon` - `yarn dev`
30-
4. You can also use `yarn compile` and `yarn start` separately
30+
4. You can also use `yarn compile` and `yarn start` separately
31+

dist/index.js

Lines changed: 21 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/index.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,12 @@
55
"dev": "concurrently \"tsc -w\" \"nodemon --es-module-specifier-resolution=node dist/index.js\"",
66
"build": "tsc"
77
},
8-
"dependencies": {},
8+
"dependencies": {
9+
"pg": "7.17.1"
10+
},
911
"devDependencies": {
1012
"@types/node": "13.1.8",
13+
"@types/pg": "7.14.1",
1114
"concurrently": "5.0.2",
1215
"nodemon": "2.0.2",
1316
"typescript": "3.8.0-beta"

src/index.ts

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,33 @@ import { printSomething } from './anotherFile';
22

33
console.log('hello world');
44

5-
printSomething('new string from function in another file');
5+
printSomething('new string from function in another file');
6+
7+
// Node says that when importing from commonjs you only can bring
8+
// const pg = require('pg'); // That works is we change Typescript and Node to use regular commonjs
9+
// import * as pg from 'pg'; // Won't work as this does equal this that:
10+
import pg from 'pg';
11+
const { Pool } = pg;
12+
13+
async function main() {
14+
15+
const pool = new Pool({
16+
user: "postgres",
17+
host: "localhost",
18+
database: "test",
19+
password: "test",
20+
port: 5432
21+
});
22+
23+
// second bonus is to try to move this into Top level await
24+
let res = await pool.query(`
25+
SELECT 1+1
26+
`);
27+
28+
console.log(res.rows[0]);
29+
}
30+
31+
main()
32+
.catch(e => console.error(e));
33+
34+

tsconfig.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
"target": "es2020",
55
"strict": true,
66
"sourceMap": true,
7-
"outDir": "dist"
7+
"outDir": "dist",
8+
"moduleResolution": "node", // Needed for npm modules that use commonjs
9+
"allowSyntheticDefaultImports": true // So we can import default value from node_modules https://stackoverflow.com/a/54302557/1426570
810
},
911
"include": ["src/**/*"]
1012
}

0 commit comments

Comments
 (0)