Skip to content

Commit

Permalink
Merge pull request #2 from Jmurp11/fix/error-handling
Browse files Browse the repository at this point in the history
Fix/error handling
  • Loading branch information
Jmurp11 authored Jan 12, 2024
2 parents 6875fad + 1071f6e commit 49fa54c
Show file tree
Hide file tree
Showing 11 changed files with 350 additions and 348 deletions.
Binary file modified .DS_Store
Binary file not shown.
51 changes: 17 additions & 34 deletions .eslintrc.json
Original file line number Diff line number Diff line change
@@ -1,36 +1,19 @@
{
"env": {
"browser": true,
"es2021": true
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 12,
"sourceType": "module"
},
"plugins": [
"@typescript-eslint"
],
"rules": {
"indent": [
"error",
"tab"
],
"linebreak-style": [
"error",
"unix"
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"always"
]
}
"env": {
"browser": true,
"es2022": true
},
"extends": ["eslint:recommended", "plugin:@typescript-eslint/recommended"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 12,
"sourceType": "module"
},
"plugins": ["@typescript-eslint"],
"rules": {
"indent": ["error", "tab"],
"linebreak-style": ["error", "unix"],
"quotes": ["error", "single"],
"semi": ["error", "always"]
}
}
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# excel-to-postgres

Quickly export an Excel Workbook to a Postgres DB

# Installation
Expand Down Expand Up @@ -32,12 +33,14 @@ excelToPostgresDb({

Supports four options, all of which are optional:

* *createDatabase* - _true | false_ (Defaults to false)
* *createTables* - _true | false_ (Defaults to false)
* *generatePrimaryKey* - _true | false_ (Defaults to false. Generates 'id' column to be used as a primary key. Cannot be used with 'useExistingPrimaryKeys' option)
* *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)
- _createDatabase_ - _true | false_ (Defaults to false)
- _createTables_ - _true | false_ (Defaults to false)
- _dropTables_ - _true | false_ (Defaults to false. When creating table, drop the table if it already exists)
- _generatePrimaryKey_ - _true | false_ (Defaults to false. Generates 'id' column to be used as a primary key. Cannot be used with 'useExistingPrimaryKeys' option)
- _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)

# Testing

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

`npm test`
`npm test`
11 changes: 0 additions & 11 deletions package-lock.json

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

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@
"@types/jest": "~29.5.11",
"@types/node": "~20.10.6",
"@types/pg": "~8.10.9",
"@types/xlsx": "~0.0.35",
"@typescript-eslint/eslint-plugin": "~6.18.0",
"@typescript-eslint/parser": "~6.18.0",
"cz-conventional-changelog": "~3.3.0",
Expand Down
67 changes: 34 additions & 33 deletions src/etl-processes.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
export enum SQLType {
VARCHAR = 'VARCHAR',
BOOLEAN = 'BOOLEAN',
FLOAT = 'FLOAT',
INT = 'INT',
VARCHAR = 'VARCHAR',
BOOLEAN = 'BOOLEAN',
FLOAT = 'FLOAT',
INT = 'INT',
}

export enum SQLKeyword {
PRIMARY_KEY = 'PRIMARY KEY',
NOT_NULL = 'NOT NULL',
SERIAL = 'SERIAL',
PRIMARY_KEY = 'PRIMARY KEY',
NOT_NULL = 'NOT NULL',
SERIAL = 'SERIAL',
}
export interface Column {
name: string;
type: string;
name: string;
type: string;
}

export interface Fields<T> {
names: string[];
values: T[];
names: string[];
values: T[];
}

interface FormatColumnsResult {
formattedColumns: string[];
primaryKeyIndex: number[];
formattedColumns: string[];
primaryKeyIndex: number[];
}

export function getFields<T>(data: T): Fields<T> {
Expand All @@ -35,32 +35,33 @@ export function getFields<T>(data: T): Fields<T> {
export function getColumns<T>(fields: Fields<T>): Column[] {
return fields.values.map((value: T, index: number) => {
switch (typeof value) {
case 'string':
return {
name: fields.names[index],
type: SQLType.VARCHAR,
};
case 'number':
return {
name: fields.names[index],
type: SQLType.FLOAT,
};
case 'boolean':
return {
name: fields.names[index],
type: SQLType.BOOLEAN,
};
default:
break;
case 'string':
return {
name: fields.names[index],
type: SQLType.VARCHAR,
};
case 'number':
return {
name: fields.names[index],
type: SQLType.FLOAT,
};
case 'boolean':
return {
name: fields.names[index],
type: SQLType.BOOLEAN,
};
default:
break;
}
});
}

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

const formattedColumns: string[] = columns.map((col: Column, index: number) =>
formatColumn(col, primaryKeyIndex, index)
const formattedColumns: string[] = columns.map(
(col: Column, index: number) =>
formatColumn(col, primaryKeyIndex, index)
);

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

if (
col.substring(col.length, col.length - 3).toUpperCase() ===
primaryKeyIndicator.toUpperCase()
primaryKeyIndicator.toUpperCase()
) {
return true;
}
Expand Down
8 changes: 4 additions & 4 deletions src/excel.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import * as XLSX from 'xlsx';

interface Worksheet<T> {
title: string;
data: T[];
title: string;
data: T[];
}

export function readExcel<T>(file: string): Worksheet<T>[] {
try {
const workbook = XLSX.readFile(file);
return getWorkSheets(workbook);
} catch (err) {
console.error(err);
throw new Error(err);
}
}

Expand All @@ -23,6 +23,6 @@ function getWorkSheets<T>(workbook: XLSX.WorkBook): Worksheet<T>[] {
}),
}));
} catch (err) {
console.error(err);
throw new Error(err);
}
}
2 changes: 1 addition & 1 deletion src/main.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { excelToPostgresDb } from "./sql";
export { excelToPostgresDb } from './sql';
Loading

0 comments on commit 49fa54c

Please sign in to comment.