Skip to content

Commit 49fa54c

Browse files
authored
Merge pull request #2 from Jmurp11/fix/error-handling
Fix/error handling
2 parents 6875fad + 1071f6e commit 49fa54c

File tree

11 files changed

+350
-348
lines changed

11 files changed

+350
-348
lines changed

.DS_Store

0 Bytes
Binary file not shown.

.eslintrc.json

Lines changed: 17 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,19 @@
11
{
2-
"env": {
3-
"browser": true,
4-
"es2021": true
5-
},
6-
"extends": [
7-
"eslint:recommended",
8-
"plugin:@typescript-eslint/recommended"
9-
],
10-
"parser": "@typescript-eslint/parser",
11-
"parserOptions": {
12-
"ecmaVersion": 12,
13-
"sourceType": "module"
14-
},
15-
"plugins": [
16-
"@typescript-eslint"
17-
],
18-
"rules": {
19-
"indent": [
20-
"error",
21-
"tab"
22-
],
23-
"linebreak-style": [
24-
"error",
25-
"unix"
26-
],
27-
"quotes": [
28-
"error",
29-
"single"
30-
],
31-
"semi": [
32-
"error",
33-
"always"
34-
]
35-
}
2+
"env": {
3+
"browser": true,
4+
"es2022": true
5+
},
6+
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
7+
"parser": "@typescript-eslint/parser",
8+
"parserOptions": {
9+
"ecmaVersion": 12,
10+
"sourceType": "module"
11+
},
12+
"plugins": ["@typescript-eslint"],
13+
"rules": {
14+
"indent": ["error", "tab"],
15+
"linebreak-style": ["error", "unix"],
16+
"quotes": ["error", "single"],
17+
"semi": ["error", "always"]
18+
}
3619
}

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# excel-to-postgres
2+
23
Quickly export an Excel Workbook to a Postgres DB
34

45
# Installation
@@ -32,12 +33,14 @@ excelToPostgresDb({
3233

3334
Supports four options, all of which are optional:
3435

35-
* *createDatabase* - _true | false_ (Defaults to false)
36-
* *createTables* - _true | false_ (Defaults to false)
37-
* *generatePrimaryKey* - _true | false_ (Defaults to false. Generates 'id' column to be used as a primary key. Cannot be used with 'useExistingPrimaryKeys' option)
38-
* *useExistingPrimaryKeys* - _true | false_ (Defaults to false. Supports multiple primary keys. Append '_pk' to the column name in the workbook that will be the primary key. Cannot be used with 'generatePrimaryKey' option)
36+
- _createDatabase_ - _true | false_ (Defaults to false)
37+
- _createTables_ - _true | false_ (Defaults to false)
38+
- _dropTables_ - _true | false_ (Defaults to false. When creating table, drop the table if it already exists)
39+
- _generatePrimaryKey_ - _true | false_ (Defaults to false. Generates 'id' column to be used as a primary key. Cannot be used with 'useExistingPrimaryKeys' option)
40+
- _useExistingPrimaryKeys_ - _true | false_ (Defaults to false. Supports multiple primary keys. Append '\_pk' to the column name in the workbook that will be the primary key. Cannot be used with 'generatePrimaryKey' option)
41+
3942
# Testing
4043

41-
This package's tests are written using [Jest](https://jestjs.io/). To execute, run:
44+
This package's tests are written using [Jest](https://jestjs.io/). To execute, run:
4245

43-
`npm test`
46+
`npm test`

package-lock.json

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

package.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
"@types/jest": "~29.5.11",
3939
"@types/node": "~20.10.6",
4040
"@types/pg": "~8.10.9",
41-
"@types/xlsx": "~0.0.35",
4241
"@typescript-eslint/eslint-plugin": "~6.18.0",
4342
"@typescript-eslint/parser": "~6.18.0",
4443
"cz-conventional-changelog": "~3.3.0",

src/etl-processes.ts

Lines changed: 34 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,28 @@
11
export enum SQLType {
2-
VARCHAR = 'VARCHAR',
3-
BOOLEAN = 'BOOLEAN',
4-
FLOAT = 'FLOAT',
5-
INT = 'INT',
2+
VARCHAR = 'VARCHAR',
3+
BOOLEAN = 'BOOLEAN',
4+
FLOAT = 'FLOAT',
5+
INT = 'INT',
66
}
77

88
export enum SQLKeyword {
9-
PRIMARY_KEY = 'PRIMARY KEY',
10-
NOT_NULL = 'NOT NULL',
11-
SERIAL = 'SERIAL',
9+
PRIMARY_KEY = 'PRIMARY KEY',
10+
NOT_NULL = 'NOT NULL',
11+
SERIAL = 'SERIAL',
1212
}
1313
export interface Column {
14-
name: string;
15-
type: string;
14+
name: string;
15+
type: string;
1616
}
1717

1818
export interface Fields<T> {
19-
names: string[];
20-
values: T[];
19+
names: string[];
20+
values: T[];
2121
}
2222

2323
interface FormatColumnsResult {
24-
formattedColumns: string[];
25-
primaryKeyIndex: number[];
24+
formattedColumns: string[];
25+
primaryKeyIndex: number[];
2626
}
2727

2828
export function getFields<T>(data: T): Fields<T> {
@@ -35,32 +35,33 @@ export function getFields<T>(data: T): Fields<T> {
3535
export function getColumns<T>(fields: Fields<T>): Column[] {
3636
return fields.values.map((value: T, index: number) => {
3737
switch (typeof value) {
38-
case 'string':
39-
return {
40-
name: fields.names[index],
41-
type: SQLType.VARCHAR,
42-
};
43-
case 'number':
44-
return {
45-
name: fields.names[index],
46-
type: SQLType.FLOAT,
47-
};
48-
case 'boolean':
49-
return {
50-
name: fields.names[index],
51-
type: SQLType.BOOLEAN,
52-
};
53-
default:
54-
break;
38+
case 'string':
39+
return {
40+
name: fields.names[index],
41+
type: SQLType.VARCHAR,
42+
};
43+
case 'number':
44+
return {
45+
name: fields.names[index],
46+
type: SQLType.FLOAT,
47+
};
48+
case 'boolean':
49+
return {
50+
name: fields.names[index],
51+
type: SQLType.BOOLEAN,
52+
};
53+
default:
54+
break;
5555
}
5656
});
5757
}
5858

5959
export function formatColumns(columns: Column[]): FormatColumnsResult {
6060
const primaryKeyIndex: number[] = [];
6161

62-
const formattedColumns: string[] = columns.map((col: Column, index: number) =>
63-
formatColumn(col, primaryKeyIndex, index)
62+
const formattedColumns: string[] = columns.map(
63+
(col: Column, index: number) =>
64+
formatColumn(col, primaryKeyIndex, index)
6465
);
6566

6667
return { formattedColumns, primaryKeyIndex: primaryKeyIndex };
@@ -85,7 +86,7 @@ export function checkPrimaryKey(col: string): boolean {
8586

8687
if (
8788
col.substring(col.length, col.length - 3).toUpperCase() ===
88-
primaryKeyIndicator.toUpperCase()
89+
primaryKeyIndicator.toUpperCase()
8990
) {
9091
return true;
9192
}

src/excel.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
import * as XLSX from 'xlsx';
22

33
interface Worksheet<T> {
4-
title: string;
5-
data: T[];
4+
title: string;
5+
data: T[];
66
}
77

88
export function readExcel<T>(file: string): Worksheet<T>[] {
99
try {
1010
const workbook = XLSX.readFile(file);
1111
return getWorkSheets(workbook);
1212
} catch (err) {
13-
console.error(err);
13+
throw new Error(err);
1414
}
1515
}
1616

@@ -23,6 +23,6 @@ function getWorkSheets<T>(workbook: XLSX.WorkBook): Worksheet<T>[] {
2323
}),
2424
}));
2525
} catch (err) {
26-
console.error(err);
26+
throw new Error(err);
2727
}
2828
}

src/main.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export { excelToPostgresDb } from "./sql";
1+
export { excelToPostgresDb } from './sql';

0 commit comments

Comments
 (0)