-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): expose attribuite value generator helper over the public api
- Loading branch information
1 parent
a646bc1
commit ceca7d0
Showing
7 changed files
with
92 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
packages/common/src/error/unknown-attribute-value-generation-strategy-error.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(', ')}`; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
packages/core/src/helpers/__test__/auto-generate-attribute-value.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
26
packages/core/src/helpers/auto-generate-attribute-value.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |