Skip to content

Commit

Permalink
Refactor postgres schema translator & schema utils
Browse files Browse the repository at this point in the history
  • Loading branch information
MXPOL committed Dec 5, 2023
1 parent b97c88f commit cc2d4eb
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 28 deletions.
8 changes: 6 additions & 2 deletions libs/external-db-mysql/src/sql_schema_translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ export default class SchemaColumnTranslato {
case 'float':
case 'double':
case 'decimal':
case 'year':
return 'number'

case 'date':
return 'date'

case 'datetime':
case 'timestamp':
case 'time':
case 'year':
return 'datetime'

case 'time':
return 'time'

case 'varchar':
case 'text':
Expand Down
45 changes: 27 additions & 18 deletions libs/external-db-postgres/src/sql_schema_translator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,17 @@ describe('Sql Schema Column Translator', () => {
})

describe('string fields', () => {
test('string', () => {
expect( env.schemaTranslator.columnToDbColumnSql({ name: ctx.fieldName, type: 'text', subtype: 'string' }) ).toEqual(`${escapeIdentifier(ctx.fieldName)} text`)
})
test.each([
'string',
'richcontent',
'image',
'video',
'audio',
'document',
'language',
])('%s', (subtype) => {
expect(env.schemaTranslator.columnToDbColumnSql({ name: ctx.fieldName, type: 'text', subtype })).toEqual(`${escapeIdentifier(ctx.fieldName)} text`)
})

test('string with length', () => {
expect( env.schemaTranslator.columnToDbColumnSql({ name: ctx.fieldName, type: 'text', subtype: 'string', precision: '2055' }) ).toEqual(`${escapeIdentifier(ctx.fieldName)} varchar(2055)`)
Expand Down Expand Up @@ -86,20 +94,15 @@ describe('Sql Schema Column Translator', () => {

describe('json fields', () => {
test.each([
['object'],
['image'],
['document'],
['video'],
['audio'],
['any'],
['mediaGallery'],
['address'],
['pageLink'],
['reference'],
['multiReference'],
['arrayString'],
['arrayDocument'],
['richContent'],
'object',
'any',
'mediaGallery',
'address',
'pageLink',
'reference',
'multiReference',
'arrayDocument',
'arrayString',
])('%s', (subtype) => {
expect(env.schemaTranslator.columnToDbColumnSql({ name: ctx.fieldName, type: 'object', subtype })).toEqual(`${escapeIdentifier(ctx.fieldName)} json`)
})
Expand Down Expand Up @@ -133,7 +136,13 @@ describe('Sql Schema Column Translator', () => {

describe('date time fields', () => {
test('date', () => {
['date', 'time', 'timez', 'timestamp', 'timestamptz'].forEach(t => {
expect( env.schemaTranslator.translateType('date') ).toEqual('date')
})
test('time', () => {
expect( env.schemaTranslator.translateType('time') ).toEqual('time')
})
test('datetime', () => {
['timez', 'timestamp', 'timestamptz'].forEach(t => {
expect( env.schemaTranslator.translateType(t) ).toEqual('datetime')
})
})
Expand Down
17 changes: 10 additions & 7 deletions libs/external-db-postgres/src/sql_schema_translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,12 @@ export default class SchemaColumnTranslator {
return 'number'

case 'date':
return 'date'
case 'time':
case 'timez':
return 'time'

case 'timestamp':
case 'timez':
case 'timestamptz':
return 'datetime'

Expand Down Expand Up @@ -97,6 +100,11 @@ export default class SchemaColumnTranslator {
return 'timestamp'

case 'text_string':
case 'text_richcontent':
case 'text_image':
case 'text_video':
case 'text_audio':
case 'text_document':
return precision ? `varchar${this.parseLength(precision)}` : 'text'

case 'text_small':
Expand All @@ -109,22 +117,17 @@ export default class SchemaColumnTranslator {
case 'boolean_boolean':
return 'boolean'

case 'object':
case 'object_':
case 'object_object':
case 'object_image':
case 'object_document':
case 'object_video':
case 'object_any':
case 'object_audio':
case 'object_mediagallery':
case 'object_address':
case 'object_pagelink':
case 'object_reference':
case 'object_multireference':
case 'object_arraystring':
case 'object_arraydocument':
case 'object_richcontent':
case 'object_array':
return 'json'

default:
Expand Down
6 changes: 6 additions & 0 deletions libs/velo-external-db-core/src/utils/schema_utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ describe('Schema utils functions', () => {
test('datetime type', () => {
expect(fieldTypeToWixDataEnum('datetime')).toBe(VeloFieldTypeEnum.dataTime)
})
test('date type', () => {
expect(fieldTypeToWixDataEnum('date')).toBe(VeloFieldTypeEnum.date)
})
test('time type', () => {
expect(fieldTypeToWixDataEnum('time')).toBe(VeloFieldTypeEnum.time)
})

test('unsupported type will throw an error', () => {
expect(() => fieldTypeToWixDataEnum('unsupported-type')).toThrowError()
Expand Down
6 changes: 5 additions & 1 deletion libs/velo-external-db-core/src/utils/schema_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ export const fieldTypeToWixDataEnum = ( fieldType: string ): collectionSpi.Field
return collectionSpi.FieldType.singleReference
case FieldType.multiReference:
return collectionSpi.FieldType.multiReference

case FieldType.time:
return collectionSpi.FieldType.time
case FieldType.date:
return collectionSpi.FieldType.date

default:
throw new Error(`${fieldType} - Unsupported field type`)
}
Expand Down
2 changes: 2 additions & 0 deletions libs/velo-external-db-types/src/collection_types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ export enum CollectionOperationSPI { // rename it

export enum FieldType {
text = 'text',
time = 'time',
date = 'date',
number = 'number',
boolean = 'boolean',
datetime = 'datetime',
Expand Down

0 comments on commit cc2d4eb

Please sign in to comment.