Skip to content

Commit

Permalink
working version of using the pg commonjs library
Browse files Browse the repository at this point in the history
  • Loading branch information
Urigo committed Jan 17, 2020
1 parent f02216d commit 9830417
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
node_modules
yarn.lock
.vscode
.vscode
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,5 @@ Would love suggestions on how to make it better or to point to a better example!
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`
2. Install dependencies - `yarn`
3. Compile with `tsc -w` and run Node with `nodemon` - `yarn dev`
4. You can also use `yarn compile` and `yarn start` separately
4. You can also use `yarn compile` and `yarn start` separately

21 changes: 21 additions & 0 deletions dist/index.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion dist/index.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@
"dev": "concurrently \"tsc -w\" \"nodemon --es-module-specifier-resolution=node dist/index.js\"",
"build": "tsc"
},
"dependencies": {},
"dependencies": {
"pg": "7.17.1"
},
"devDependencies": {
"@types/node": "13.1.8",
"@types/pg": "7.14.1",
"concurrently": "5.0.2",
"nodemon": "2.0.2",
"typescript": "3.8.0-beta"
Expand Down
31 changes: 30 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,33 @@ import { printSomething } from './anotherFile';

console.log('hello world');

printSomething('new string from function in another file');
printSomething('new string from function in another file');

// Node says that when importing from commonjs you only can bring
// const pg = require('pg'); // That works is we change Typescript and Node to use regular commonjs
// import * as pg from 'pg'; // Won't work as this does equal this that:
import pg from 'pg';
const { Pool } = pg;

async function main() {

const pool = new Pool({
user: "postgres",
host: "localhost",
database: "test",
password: "test",
port: 5432
});

// second bonus is to try to move this into Top level await
let res = await pool.query(`
SELECT 1+1
`);

console.log(res.rows[0]);
}

main()
.catch(e => console.error(e));


4 changes: 3 additions & 1 deletion tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
"target": "es2020",
"strict": true,
"sourceMap": true,
"outDir": "dist"
"outDir": "dist",
"moduleResolution": "node", // Needed for npm modules that use commonjs
"allowSyntheticDefaultImports": true // So we can import default value from node_modules https://stackoverflow.com/a/54302557/1426570
},
"include": ["src/**/*"]
}

3 comments on commit 9830417

@SerkanSipahi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any difference between .mjs and .js when using --es-module-specifier-resolution=node ?

@SerkanSipahi
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Update: i found it out! when file extension is .mjs then type=module is not required in package.json.

@Urigo
Copy link
Owner Author

@Urigo Urigo commented on 9830417 May 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for commenting! I've added a comment to the readme about it

Please sign in to comment.