Skip to content

Commit

Permalink
feat(core): wip: initial attribute aliasing support
Browse files Browse the repository at this point in the history
  • Loading branch information
whimzyLive committed May 20, 2021
1 parent 33fe5cb commit b6d05dc
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 34 deletions.
1 change: 1 addition & 0 deletions packages/common/private-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ export * from './src/metadata/metadata-manager';
export * from './src/metadata/metadata-storage';
export * from './src/logger/debug-logger';
export * from './src/error';
export * from './src/index-options';
12 changes: 7 additions & 5 deletions packages/common/src/decorators/entity.decorator.ts
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
import {MetadataManager} from '../metadata/metadata-manager';
import {EntityRawMetadataOptions} from '../metadata/metadata-storage';

export function Entity({
type Constructor = {new (...args: any[]): {}};

export function Entity<E>({
table,
primaryKey,
indexes,
name,
}: Pick<
EntityRawMetadataOptions,
EntityRawMetadataOptions<E>,
'name' | 'table' | 'indexes' | 'primaryKey'
>): ClassDecorator {
return target => {
>) {
return function <E extends Constructor>(target: E) {
const originalTarget = target;

MetadataManager.metadataStorage.addRawEntity({
name,
table,
target,
target: target as any,
primaryKey,
indexes,
});
Expand Down
41 changes: 41 additions & 0 deletions packages/common/src/index-options.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import {INDEX_TYPE} from './enums';
import {Replace} from './helpers/replace-type';

export interface GSIIndexOptions {
type: INDEX_TYPE.GSI;
partitionKey: string;
sortKey: string;
/**
* DynamoDB only adds item to index if both Partition Key and Sort Key defined
* for any given GSI contains value
*
* Indexes missing sort key value will not be added to index.
*/
isSparse?: boolean;
}

export interface LSIIndexOptions {
type: INDEX_TYPE.LSI;
sortKey: string;
isSparse?: boolean;
}
export type IndexOptions = GSIIndexOptions | LSIIndexOptions;

type EntityAliasOrString<Entity> = string | {$alias: keyof Entity};

export type IndexOptionsWithAlias<Entity> =
| Replace<
GSIIndexOptions,
'partitionKey' | 'sortKey',
{
partitionKey: EntityAliasOrString<Entity>;
sortKey: EntityAliasOrString<Entity>;
}
>
| Replace<
LSIIndexOptions,
'sortKey',
{
sortKey: string;
}
>;
13 changes: 7 additions & 6 deletions packages/common/src/metadata/metadata-storage.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import {AUTO_GENERATE_ATTRIBUTE_STRATEGY, EntityTarget} from '@typedorm/common';
import {IndexOptions, Table} from '../table';
import {Table} from '../table';
import {AttributeOptionsUniqueType} from '../decorators/attribute.decorator';
import {ScalarType} from '../helpers/scalar-type';
import {IndexOptionsWithAlias} from '../index-options';

export const IsAutoGenerateAttributeRawMetadataOptions = (
attr: any
Expand All @@ -17,15 +18,15 @@ export type CompositePrimaryKey = {
sortKey: string;
};

export type Indexes = {
[key: string]: IndexOptions;
export type Indexes<Entity = any> = {
[key: string]: IndexOptionsWithAlias<Entity>;
};

export interface EntityRawMetadataOptions {
export interface EntityRawMetadataOptions<Entity = any> {
name: string;
target: EntityTarget<any>;
target: EntityTarget<Entity>;
primaryKey: PrimaryKey;
indexes?: Indexes;
indexes?: Indexes<Entity>;
table?: Table;
}

Expand Down
23 changes: 1 addition & 22 deletions packages/common/src/table.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {INDEX_TYPE} from './enums';
import {IndexOptions} from './index-options';
import {CompositePrimaryKey} from './metadata/metadata-storage';

export const IsCompositePrimaryKey = (
Expand All @@ -18,27 +18,6 @@ export interface TableOptions {
};
}

export interface GSIIndexOptions {
type: INDEX_TYPE.GSI;
partitionKey: string;
sortKey: string;
/**
* DynamoDB only adds item to index if both Partition Key and Sort Key defined
* for any given GSI contains value
*
* Indexes missing sort key value will not be added to index.
*/
isSparse?: boolean;
}

export interface LSIIndexOptions {
type: INDEX_TYPE.LSI;
sortKey: string;
isSparse?: boolean;
}

export type IndexOptions = GSIIndexOptions | LSIIndexOptions;

export class Table {
constructor(private options: TableOptions) {}

Expand Down
1 change: 1 addition & 0 deletions packages/core/src/classes/metadata/entity-metadata.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ export class EntityMetadata extends BaseMetadata {

const currentIndex = indexes[key];

// FIXME: check for index type before trying to parse it.
validateKey(currentIndex.sortKey, attributes);
// validates and gets and fill set indexes interpolations of sort key
const sortKeyInterpolations = getInterpolatedKeys(currentIndex.sortKey);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
EntityTarget,
IndexOptions,
INDEX_TYPE,
QUERY_ORDER,
Replace,
RETURN_VALUES,
Table,
UpdateAttributes,
TRANSFORM_TYPE,
IndexOptions,
} from '@typedorm/common';
import {DynamoDB} from 'aws-sdk';
import {dropProp} from '../../helpers/drop-prop';
Expand Down

0 comments on commit b6d05dc

Please sign in to comment.