Skip to content

Commit

Permalink
Refactor mysql schema translation for text and object
Browse files Browse the repository at this point in the history
fields
  • Loading branch information
MXPOL committed Dec 5, 2023
1 parent fc6ba95 commit b97c88f
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 43 deletions.
42 changes: 20 additions & 22 deletions libs/external-db-mysql/src/sql_schema_translator.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,17 @@ describe('Sql Schema Column Translator', () => {
})

describe('string fields', () => {
test('string', () => {
expect( env.schemaTranslator.columnToDbColumnSql({ name: ctx.fieldName, type: 'text', subtype: 'string' }) ).toEqual(`${escapeId(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(`${escapeId(ctx.fieldName)} TEXT`)
})

test('string with length', () => {
expect( env.schemaTranslator.columnToDbColumnSql({ name: ctx.fieldName, type: 'text', subtype: 'string', precision: '2055' }) ).toEqual(`${escapeId(ctx.fieldName)} VARCHAR(2055)`)
Expand All @@ -86,29 +94,19 @@ describe('Sql Schema Column Translator', () => {
test('text large', () => {
expect( env.schemaTranslator.columnToDbColumnSql({ name: ctx.fieldName, type: 'text', subtype: 'large' }) ).toEqual(`${escapeId(ctx.fieldName)} LONGTEXT`)
})

test('text language', () => {

expect( env.schemaTranslator.columnToDbColumnSql({ name: ctx.fieldName, type: 'text', subtype: 'language' }) ).toEqual(`${escapeId(ctx.fieldName)} TEXT`)
})
})

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(`${escapeId(ctx.fieldName)} JSON`)
})
Expand Down
11 changes: 6 additions & 5 deletions libs/external-db-mysql/src/sql_schema_translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,11 @@ export default class SchemaColumnTranslato {
return 'TIMESTAMP DEFAULT CURRENT_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 @@ -108,19 +113,15 @@ export default class SchemaColumnTranslato {

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
14 changes: 7 additions & 7 deletions libs/velo-external-db-core/src/utils/schema_utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,15 @@ describe('Schema utils functions', () => {
[VeloFieldTypeEnum.dataTime, FieldType.datetime],
[VeloFieldTypeEnum.time, FieldType.datetime],
[VeloFieldTypeEnum.boolean, FieldType.boolean],
[VeloFieldTypeEnum.image, FieldType.object],
[VeloFieldTypeEnum.document, FieldType.object],
[VeloFieldTypeEnum.video, FieldType.object],
[VeloFieldTypeEnum.image, FieldType.text],
[VeloFieldTypeEnum.document, FieldType.text],
[VeloFieldTypeEnum.video, FieldType.text],
[VeloFieldTypeEnum.any, FieldType.object],
[VeloFieldTypeEnum.arrayString, FieldType.object],
[VeloFieldTypeEnum.arrayDocument, FieldType.object],
[VeloFieldTypeEnum.audio, FieldType.object],
[VeloFieldTypeEnum.audio, FieldType.text],
[VeloFieldTypeEnum.language, FieldType.text],
[VeloFieldTypeEnum.richContent, FieldType.object],
[VeloFieldTypeEnum.richContent, FieldType.text],
[VeloFieldTypeEnum.mediaGallery, FieldType.object],
[VeloFieldTypeEnum.address, FieldType.object],
[VeloFieldTypeEnum.pageLink, FieldType.object],
Expand All @@ -78,9 +78,9 @@ describe('Schema utils functions', () => {
[VeloFieldTypeEnum.url, 'string'],
[VeloFieldTypeEnum.richText, 'string'],
[VeloFieldTypeEnum.number, 'float'],
[VeloFieldTypeEnum.date, 'datetime'],
[VeloFieldTypeEnum.date, 'date'],
[VeloFieldTypeEnum.dataTime, 'datetime'],
[VeloFieldTypeEnum.time, 'datetime'],
[VeloFieldTypeEnum.time, 'time'],
[VeloFieldTypeEnum.boolean, 'boolean'],
[VeloFieldTypeEnum.image, 'image'],
[VeloFieldTypeEnum.document, 'document'],
Expand Down
19 changes: 10 additions & 9 deletions libs/velo-external-db-core/src/utils/schema_utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,27 +32,27 @@ export const wixDataEnumToFieldType = (fieldEnum: collectionSpi.FieldType): stri
case collectionSpi.FieldType.url:
case collectionSpi.FieldType.richText:
case collectionSpi.FieldType.language:
case collectionSpi.FieldType.richContent:
case collectionSpi.FieldType.image:
case collectionSpi.FieldType.video:
case collectionSpi.FieldType.document:
case collectionSpi.FieldType.audio:
return FieldType.text

case collectionSpi.FieldType.number:
return FieldType.number

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

case collectionSpi.FieldType.boolean:
return FieldType.boolean

case collectionSpi.FieldType.image:
case collectionSpi.FieldType.document:
case collectionSpi.FieldType.video:
case collectionSpi.FieldType.any:
case collectionSpi.FieldType.arrayString:
case collectionSpi.FieldType.arrayDocument:
case collectionSpi.FieldType.audio:
case collectionSpi.FieldType.richContent:
case collectionSpi.FieldType.mediaGallery:
case collectionSpi.FieldType.address:
case collectionSpi.FieldType.pageLink:
Expand All @@ -73,21 +73,22 @@ export const fieldTypeToSubtype = (fieldEnum: collectionSpi.FieldType): string =
case collectionSpi.FieldType.url:
case collectionSpi.FieldType.richText:
return 'string'


case collectionSpi.FieldType.language:
return 'language'

case collectionSpi.FieldType.number:
return 'float'
case collectionSpi.FieldType.date:
return 'date'
case collectionSpi.FieldType.dataTime:
case collectionSpi.FieldType.time:
return 'datetime'
case collectionSpi.FieldType.time:
return 'time'
case collectionSpi.FieldType.boolean:
return 'boolean'

// Object subtypes
case collectionSpi.FieldType.image:
return 'image'
case collectionSpi.FieldType.document:
Expand Down

0 comments on commit b97c88f

Please sign in to comment.