Skip to content

Commit

Permalink
feat(core): expose attribuite value generator helper over the public api
Browse files Browse the repository at this point in the history
  • Loading branch information
whimzyLive committed Apr 24, 2022
1 parent a646bc1 commit ceca7d0
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 24 deletions.
1 change: 1 addition & 0 deletions packages/common/src/error/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,4 @@ export * from './duplicate-entity-physical-name-error';
export * from './invalid-unique-attribute-update-error';
export * from './invalid-parallel-scan-limit-option-error';
export * from './invalid-dynamic-update-attribute-value';
export * from './unknown-attribute-value-generation-strategy-error';
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import {AUTO_GENERATE_ATTRIBUTE_STRATEGY} from '../enums';

export class UnknownAttributeValueGenerationStrategyError extends Error {
name = 'UnknownAttributeValueGenerationStrategyError';

constructor(strategy: string) {
super();
this.message = `Unknown attribute value generation strategy provided: "${strategy}".
Valid attribute value generation strategies are: ${Object.keys(
AUTO_GENERATE_ATTRIBUTE_STRATEGY
)
.map(key => `"${key}"`)
.join(', ')}`;
}
}
3 changes: 3 additions & 0 deletions packages/core/public-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ export * from './src/classes/manager/transaction-manager';
// classes
export {Connection} from './src/classes/connection/connection';

// helpers
export * from './src/helpers/auto-generate-attribute-value';

// public method exports

export function createConnection(options: ConnectionOptions) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import {AUTO_GENERATE_ATTRIBUTE_STRATEGY} from '@typedorm/common';
import KSUID from 'ksuid';
import {v4} from 'uuid';
import {autoGenerateValue} from '../../helpers/auto-generate-attribute-value';
import {
BaseAttributeMetadata,
BaseAttributeMetadataOptions,
Expand Down Expand Up @@ -29,26 +28,6 @@ export class AutoGeneratedAttributeMetadata extends BaseAttributeMetadata {
}

get value() {
return this.autoGenerateValue(this.strategy);
}

autoGenerateValue(strategy: AUTO_GENERATE_ATTRIBUTE_STRATEGY) {
switch (strategy) {
case AUTO_GENERATE_ATTRIBUTE_STRATEGY.UUID4: {
return v4();
}
case AUTO_GENERATE_ATTRIBUTE_STRATEGY.KSUID: {
return KSUID.randomSync(new Date()).string;
}
case AUTO_GENERATE_ATTRIBUTE_STRATEGY.ISO_DATE: {
return new Date().toISOString();
}
case AUTO_GENERATE_ATTRIBUTE_STRATEGY.EPOCH_DATE: {
return Math.ceil(new Date().valueOf() / 1000);
}
default: {
throw new Error('Unknown attribute strategy.');
}
}
return autoGenerateValue(this.strategy);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {KeyConditionOptions} from '../expression/key-condition-options-type';
import {UpdateBody} from '../expression/update-body-type';
import {isObject} from '../../helpers/is-object';
import {DocumentClientTypes} from '@typedorm/document-client';
import {autoGenerateValue} from '../../helpers/auto-generate-attribute-value';

export interface ManagerToDynamoPutItemOptions {
/**
Expand Down Expand Up @@ -404,7 +405,7 @@ export class DocumentClientRequestTransformer extends BaseTransformer {
// check if auto update attributes are not referenced by primary key
const formattedAutoUpdateAttributes = autoUpdateAttributes.reduce(
(acc, attr) => {
acc[attr.name] = attr.autoGenerateValue(attr.strategy);
acc[attr.name] = autoGenerateValue(attr.strategy);
return acc;
},
{} as {[key: string]: any}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {AUTO_GENERATE_ATTRIBUTE_STRATEGY} from '@typedorm/common';
import {autoGenerateValue} from '../auto-generate-attribute-value';

jest.mock('uuid', () => ({
v4: () => 'c0ac5395-ba7c-41bf-bbc3-09a6087bcca2',
}));
jest.mock('ksuid', () => ({
randomSync: () => ({
string: '1irj9jenZH4PfTWp3NrDgpSF7lg',
}),
}));

test('returns generated attribute value using uuid strategy', () => {
const value = autoGenerateValue(AUTO_GENERATE_ATTRIBUTE_STRATEGY.UUID4);
expect(value).toEqual('c0ac5395-ba7c-41bf-bbc3-09a6087bcca2');
});

test('returns generated attribute value using ksuid strategy', () => {
const value = autoGenerateValue(AUTO_GENERATE_ATTRIBUTE_STRATEGY.KSUID);
expect(value).toEqual('1irj9jenZH4PfTWp3NrDgpSF7lg');
});

test('returns generated attribute value using epoch strategy', () => {
jest.useFakeTimers('modern').setSystemTime(new Date('2020-01-01'));

const value = autoGenerateValue(AUTO_GENERATE_ATTRIBUTE_STRATEGY.EPOCH_DATE);
expect(value).toEqual(1577836800);
});

test('returns generated attribute value using iso date strategy', () => {
jest.useFakeTimers('modern').setSystemTime(new Date('2020-01-01'));

const value = autoGenerateValue(AUTO_GENERATE_ATTRIBUTE_STRATEGY.ISO_DATE);
expect(value).toEqual('2020-01-01T00:00:00.000Z');
});

test('throws for unknown strategy', () => {
const valueCreator = () => autoGenerateValue('INVALID' as any);

expect(valueCreator)
.toThrowError(`Unknown attribute value generation strategy provided: "INVALID".
Valid attribute value generation strategies are: "UUID4", "KSUID", "EPOCH_DATE", "ISO_DATE"`);
});
26 changes: 26 additions & 0 deletions packages/core/src/helpers/auto-generate-attribute-value.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import KSUID from 'ksuid';
import {
AUTO_GENERATE_ATTRIBUTE_STRATEGY,
UnknownAttributeValueGenerationStrategyError,
} from '@typedorm/common';
import {v4} from 'uuid';

export function autoGenerateValue(strategy: AUTO_GENERATE_ATTRIBUTE_STRATEGY) {
switch (strategy) {
case AUTO_GENERATE_ATTRIBUTE_STRATEGY.UUID4: {
return v4();
}
case AUTO_GENERATE_ATTRIBUTE_STRATEGY.KSUID: {
return KSUID.randomSync(new Date()).string;
}
case AUTO_GENERATE_ATTRIBUTE_STRATEGY.ISO_DATE: {
return new Date().toISOString();
}
case AUTO_GENERATE_ATTRIBUTE_STRATEGY.EPOCH_DATE: {
return Math.ceil(new Date().valueOf() / 1000);
}
default: {
throw new UnknownAttributeValueGenerationStrategyError(strategy);
}
}
}

0 comments on commit ceca7d0

Please sign in to comment.