11const { checkFieldPropertiesChanged } = require ( './common' ) ;
22
3- const getAddCollectionScript = ( app , dbVersion ) => collection => {
4- const _ = app . require ( 'lodash' ) ;
5- const { getEntityName } = require ( '../../utils/general' ) ( _ ) ;
6- const { createColumnDefinitionBySchema } = require ( './createColumnDefinition' ) ( _ ) ;
7- const ddlProvider = require ( '../../ddlProvider' ) ( null , null , app ) ;
8-
9- const schemaName = collection . compMod . keyspaceName ;
10- const schemaData = { schemaName, dbVersion } ;
11- const jsonSchema = { ...collection , ...( _ . omit ( collection ?. role , 'properties' ) || { } ) } ;
12- const columnDefinitions = _ . toPairs ( jsonSchema . properties ) . map ( ( [ name , column ] ) =>
13- createColumnDefinitionBySchema ( {
14- name,
15- jsonSchema : column ,
16- parentJsonSchema : jsonSchema ,
17- ddlProvider,
3+ const getAddCollectionScript =
4+ ( { app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions } ) =>
5+ collection => {
6+ const _ = app . require ( 'lodash' ) ;
7+ const { getEntityName } = require ( '../../utils/general' ) ( _ ) ;
8+ const { createColumnDefinitionBySchema } = require ( './createColumnDefinition' ) ( app ) ;
9+ const ddlProvider = require ( '../../ddlProvider' ) ( null , null , app ) ;
10+ const { getDefinitionByReference } = app . require ( '@hackolade/ddl-fe-utils' ) ;
11+
12+ const schemaName = collection . compMod . keyspaceName ;
13+ const schemaData = { schemaName, dbVersion } ;
14+ const jsonSchema = { ...collection , ...( _ . omit ( collection ?. role , 'properties' ) || { } ) } ;
15+ const columnDefinitions = _ . toPairs ( jsonSchema . properties ) . map ( ( [ name , column ] ) => {
16+ const definitionJsonSchema = getDefinitionByReference ( {
17+ propertySchema : column ,
18+ modelDefinitions,
19+ internalDefinitions,
20+ externalDefinitions,
21+ } ) ;
22+
23+ return createColumnDefinitionBySchema ( {
24+ name,
25+ jsonSchema : column ,
26+ parentJsonSchema : jsonSchema ,
27+ ddlProvider,
28+ schemaData,
29+ definitionJsonSchema,
30+ } ) ;
31+ } ) ;
32+ const checkConstraints = ( jsonSchema . chkConstr || [ ] ) . map ( check =>
33+ ddlProvider . createCheckConstraint ( ddlProvider . hydrateCheckConstraint ( check ) ) ,
34+ ) ;
35+ const tableData = {
36+ name : getEntityName ( jsonSchema ) ,
37+ columns : columnDefinitions . map ( ddlProvider . convertColumnDefinition ) ,
38+ checkConstraints : checkConstraints ,
39+ foreignKeyConstraints : [ ] ,
1840 schemaData,
19- } ) ,
20- ) ;
21- const checkConstraints = ( jsonSchema . chkConstr || [ ] ) . map ( check =>
22- ddlProvider . createCheckConstraint ( ddlProvider . hydrateCheckConstraint ( check ) ) ,
23- ) ;
24- const tableData = {
25- name : getEntityName ( jsonSchema ) ,
26- columns : columnDefinitions . map ( ddlProvider . convertColumnDefinition ) ,
27- checkConstraints : checkConstraints ,
28- foreignKeyConstraints : [ ] ,
29- schemaData,
30- columnDefinitions,
31- } ;
32- const hydratedTable = ddlProvider . hydrateTable ( { tableData, entityData : [ jsonSchema ] , jsonSchema } ) ;
41+ columnDefinitions,
42+ } ;
43+ const hydratedTable = ddlProvider . hydrateTable ( { tableData, entityData : [ jsonSchema ] , jsonSchema } ) ;
3344
34- return ddlProvider . createTable ( hydratedTable , jsonSchema . isActivated ) ;
35- } ;
45+ return ddlProvider . createTable ( hydratedTable , jsonSchema . isActivated ) ;
46+ } ;
3647
3748const getDeleteCollectionScript = app => collection => {
3849 const _ = app . require ( 'lodash' ) ;
@@ -47,33 +58,44 @@ const getDeleteCollectionScript = app => collection => {
4758 return `DROP TABLE IF EXISTS ${ fullName } ;` ;
4859} ;
4960
50- const getAddColumnScript = ( app , dbVersion ) => collection => {
51- const _ = app . require ( 'lodash' ) ;
52- const { getEntityName } = require ( '../../utils/general' ) ( _ ) ;
53- const { getNamePrefixedWithSchemaName } = require ( '../general' ) ( { _ } ) ;
54- const { createColumnDefinitionBySchema } = require ( './createColumnDefinition' ) ( _ ) ;
55- const ddlProvider = require ( '../../ddlProvider' ) ( null , null , app ) ;
56-
57- const collectionSchema = { ...collection , ...( _ . omit ( collection ?. role , 'properties' ) || { } ) } ;
58- const tableName = getEntityName ( collectionSchema ) ;
59- const schemaName = collectionSchema . compMod ?. keyspaceName ;
60- const fullName = getNamePrefixedWithSchemaName ( tableName , schemaName ) ;
61- const schemaData = { schemaName, dbVersion } ;
62-
63- return _ . toPairs ( collection . properties )
64- . filter ( ( [ name , jsonSchema ] ) => ! jsonSchema . compMod )
65- . map ( ( [ name , jsonSchema ] ) =>
66- createColumnDefinitionBySchema ( {
67- name,
68- jsonSchema,
69- parentJsonSchema : collectionSchema ,
70- ddlProvider,
71- schemaData,
72- } ) ,
73- )
74- . map ( ddlProvider . convertColumnDefinition )
75- . map ( script => `ALTER TABLE IF EXISTS ${ fullName } ADD COLUMN IF NOT EXISTS ${ script } ;` ) ;
76- } ;
61+ const getAddColumnScript =
62+ ( { app, dbVersion, modelDefinitions, internalDefinitions, externalDefinitions } ) =>
63+ collection => {
64+ const _ = app . require ( 'lodash' ) ;
65+ const { getEntityName } = require ( '../../utils/general' ) ( _ ) ;
66+ const { getNamePrefixedWithSchemaName } = require ( '../general' ) ( { _ } ) ;
67+ const { createColumnDefinitionBySchema } = require ( './createColumnDefinition' ) ( app ) ;
68+ const ddlProvider = require ( '../../ddlProvider' ) ( null , null , app ) ;
69+ const { getDefinitionByReference } = app . require ( '@hackolade/ddl-fe-utils' ) ;
70+
71+ const collectionSchema = { ...collection , ...( _ . omit ( collection ?. role , 'properties' ) || { } ) } ;
72+ const tableName = getEntityName ( collectionSchema ) ;
73+ const schemaName = collectionSchema . compMod ?. keyspaceName ;
74+ const fullName = getNamePrefixedWithSchemaName ( tableName , schemaName ) ;
75+ const schemaData = { schemaName, dbVersion } ;
76+
77+ return _ . toPairs ( collection . properties )
78+ . filter ( ( [ name , jsonSchema ] ) => ! jsonSchema . compMod )
79+ . map ( ( [ name , jsonSchema ] ) => {
80+ const definitionJsonSchema = getDefinitionByReference ( {
81+ propertySchema : jsonSchema ,
82+ modelDefinitions,
83+ internalDefinitions,
84+ externalDefinitions,
85+ } ) ;
86+
87+ return createColumnDefinitionBySchema ( {
88+ name,
89+ jsonSchema,
90+ parentJsonSchema : collectionSchema ,
91+ ddlProvider,
92+ schemaData,
93+ definitionJsonSchema,
94+ } ) ;
95+ } )
96+ . map ( ddlProvider . convertColumnDefinition )
97+ . map ( script => `ALTER TABLE IF EXISTS ${ fullName } ADD COLUMN IF NOT EXISTS ${ script } ;` ) ;
98+ } ;
7799
78100const getDeleteColumnScript = app => collection => {
79101 const _ = app . require ( 'lodash' ) ;
0 commit comments