Repository: typedorm/typedorm (ts)

Issue:
Missing table config throws non user friendly error
When building metadata for entity with missing table configuration, currently misleading error saying "TypeError: Cannot read property 'partitionKey' of undefined", this should be "Missing required table config" or something

Pull request:
fix(core): Misleading error when missing required table config
closes #60 

Patch:
diff --git a/packages/common/src/error/index.ts b/packages/common/src/error/index.ts
index c9c658c..8d41ff3 100644
--- a/packages/common/src/error/index.ts
+++ b/packages/common/src/error/index.ts
@@ -1,2 +1,3 @@
 export * from './attribute-metadata-unsupported-default-value-error';
 export * from './parse-error';
+export * from './missing-required-table-config';
diff --git a/packages/common/src/error/missing-required-table-config.ts b/packages/common/src/error/missing-required-table-config.ts
new file mode 100644
index 0000000..268f472
--- /dev/null
+++ b/packages/common/src/error/missing-required-table-config.ts
@@ -0,0 +1,11 @@
+/**
+ * Thrown when none of global or per entity table exists
+ */
+export class MissingRequiredTableConfig extends Error {
+  name = 'MissingRequiredTableConfig';
+  constructor(entityName: string) {
+    super();
+    this.message = `No table config could be resolved for entity "${entityName}", entity must have at least
+    one of "connection table" or "entity table" configured.`;
+  }
+}
diff --git a/packages/core/src/classes/connection/connection.ts b/packages/core/src/classes/connection/connection.ts
index 588076e..c6d1e34 100644
--- a/packages/core/src/classes/connection/connection.ts
+++ b/packages/core/src/classes/connection/connection.ts
@@ -4,6 +4,7 @@ import {
   Replace,
   Table,
   DebugLogger,
+  MissingRequiredTableConfig,
 } from '@typedorm/common';
 import {DynamoDB} from 'aws-sdk';
 import {isUsedForPrimaryKey} from '../../helpers/is-used-for-primary-key';
@@ -77,6 +78,9 @@ export class Connection {
       this.isConnected = true;
       return this;
     } catch (err) {
+      if (err instanceof MissingRequiredTableConfig) {
+        throw err;
+      }
       // Failed to connect to connection, clear self from connection manager
       this.destroySelf(this.name);
       return;
diff --git a/packages/core/src/classes/connection/entity-metadata-builder.ts b/packages/core/src/classes/connection/entity-metadata-builder.ts
index 3044394..5d13bb9 100644
--- a/packages/core/src/classes/connection/entity-metadata-builder.ts
+++ b/packages/core/src/classes/connection/entity-metadata-builder.ts
@@ -1,4 +1,8 @@
-import {MetadataManager, Table} from '@typedorm/common';
+import {
+  MetadataManager,
+  MissingRequiredTableConfig,
+  Table,
+} from '@typedorm/common';
 import {
   AttributeMetadataType,
   EntityMetadata,
@@ -31,6 +35,10 @@ export class EntityMetadataBuilder {
         this.table = this.connection.table;
       }
 
+      if (!this.table) {
+        throw new MissingRequiredTableConfig(decoratedEntityClass.name);
+      }
+
       const inheritedClasses = this.recursiveGetInheritanceTree(
         decoratedEntityClass
       );


Test patch:
diff --git a/packages/core/src/classes/connection/__test__/entity-metadata-builder.spec.ts b/packages/core/src/classes/connection/__test__/entity-metadata-builder.spec.ts
index 7ead6fc..44debc6 100644
--- a/packages/core/src/classes/connection/__test__/entity-metadata-builder.spec.ts
+++ b/packages/core/src/classes/connection/__test__/entity-metadata-builder.spec.ts
@@ -7,6 +7,7 @@ import {
   AutoGenerateAttribute,
   AUTO_GENERATE_ATTRIBUTE_STRATEGY,
   Entity,
+  MissingRequiredTableConfig,
   Table,
 } from '@typedorm/common';
 import {table} from '../../../../__mocks__/table';
@@ -258,3 +259,27 @@ test('overrides property of base class if it is defined again on derived class w
     },
   ]);
 });
+
+/**
+ * Issue #60
+ */
+test('throws user friendly error when no table config is found for entity', () => {
+  @Entity({
+    name: 'no-table-entity',
+    primaryKey: {
+      partitionKey: 'id',
+    },
+  })
+  class NoTableEntity {
+    @Attribute()
+    id: string;
+  }
+
+  const tempConnection = () =>
+    createTestConnection({
+      name: 'temp',
+      entities: [NoTableEntity],
+    });
+
+  expect(tempConnection).toThrow(MissingRequiredTableConfig);
+});