@@ -12,7 +12,9 @@ import {
1212 fieldTypeToWixDataEnum ,
1313 WixFormatFieldsToInputFields ,
1414 responseFieldToWixFormat ,
15- compareColumnsInDbAndRequest
15+ compareColumnsInDbAndRequest ,
16+ dataOperationsToWixDataQueryOperators ,
17+ collectionOperationsToWixDataCollectionOperations ,
1618} from '../utils/schema_utils'
1719
1820
@@ -29,11 +31,11 @@ export default class SchemaService {
2931 async list ( collectionIds : string [ ] ) : Promise < collectionSpi . ListCollectionsResponsePart > {
3032 const collections = ( ! collectionIds || collectionIds . length === 0 ) ?
3133 await this . storage . list ( ) :
32- await Promise . all ( collectionIds . map ( async ( collectionName : string ) => ( { id : collectionName , fields : await this . schemaInformation . schemaFieldsFor ( collectionName ) } ) ) )
34+ await Promise . all ( collectionIds . map ( ( collectionName : string ) => this . schemaInformation . schemaFor ( collectionName ) ) )
3335
34- return {
35- collection : collections . map ( this . formatCollection . bind ( this ) )
36- }
36+ return {
37+ collection : collections . map ( this . formatCollection . bind ( this ) )
38+ }
3739 }
3840
3941 async create ( collection : collectionSpi . Collection ) : Promise < collectionSpi . CreateCollectionResponse > {
@@ -51,8 +53,8 @@ export default class SchemaService {
5153 }
5254
5355 const collectionColumnsInRequest = collection . fields
54- const collectionColumnsInDb = await this . storage . describeCollection ( collection . id )
55-
56+ const { fields : collectionColumnsInDb } = await this . storage . describeCollection ( collection . id ) as Table
57+
5658 const {
5759 columnsToAdd,
5860 columnsToRemove,
@@ -83,9 +85,9 @@ export default class SchemaService {
8385 }
8486
8587 async delete ( collectionId : string ) : Promise < collectionSpi . DeleteCollectionResponse > {
86- const collectionFields = await this . storage . describeCollection ( collectionId )
88+ const { fields : collectionFields } = await this . storage . describeCollection ( collectionId ) as Table
8789 await this . storage . drop ( collectionId )
88- await this . schemaInformation . refresh ( )
90+ this . schemaInformation . refresh ( )
8991 return { collection : {
9092 id : collectionId ,
9193 fields : responseFieldToWixFormat ( collectionFields ) ,
@@ -100,45 +102,30 @@ export default class SchemaService {
100102 }
101103
102104 private formatCollection ( collection : Table ) : collectionSpi . Collection {
103- // remove in the end of development
104- if ( ! this . storage . capabilities || ! this . storage . columnCapabilitiesFor ) {
105- throw new Error ( 'Your storage does not support the new collection capabilities API' )
106- }
107- const capabilities = this . formatCollectionCapabilities ( this . storage . capabilities ( ) )
108105 return {
109106 id : collection . id ,
110107 fields : this . formatFields ( collection . fields ) ,
111- capabilities
108+ capabilities : collection . capabilities ? this . formatCollectionCapabilities ( collection . capabilities ) : undefined
112109 }
113110 }
114111
115112 private formatFields ( fields : ResponseField [ ] ) : collectionSpi . Field [ ] {
116- const fieldCapabilitiesFor = ( type : string ) : collectionSpi . FieldCapabilities => {
117- // remove in the end of development
118- if ( ! this . storage . columnCapabilitiesFor ) {
119- throw new Error ( 'Your storage does not support the new collection capabilities API' )
120- }
121- const { sortable, columnQueryOperators } = this . storage . columnCapabilitiesFor ( type )
122- return {
123- sortable,
124- queryOperators : queriesToWixDataQueryOperators ( columnQueryOperators )
125- }
126- }
127-
128- return fields . map ( ( f ) => ( {
129- key : f . field ,
130- // TODO: think about how to implement this
113+ return fields . map ( field => ( {
114+ key : field . field ,
131115 encrypted : false ,
132- type : fieldTypeToWixDataEnum ( f . type ) ,
133- capabilities : fieldCapabilitiesFor ( f . type )
116+ type : fieldTypeToWixDataEnum ( field . type ) ,
117+ capabilities : {
118+ sortable : field . capabilities ? field . capabilities . sortable : undefined ,
119+ queryOperators : field . capabilities ? queriesToWixDataQueryOperators ( field . capabilities . columnQueryOperators ) : undefined
120+ }
134121 } ) )
135122 }
136123
137124 private formatCollectionCapabilities ( capabilities : CollectionCapabilities ) : collectionSpi . CollectionCapabilities {
138125 return {
139- dataOperations : capabilities . dataOperations as unknown as collectionSpi . DataOperation [ ] ,
140- fieldTypes : capabilities . fieldTypes as unknown as collectionSpi . FieldType [ ] ,
141- collectionOperations : capabilities . collectionOperations as unknown as collectionSpi . CollectionOperation [ ] ,
126+ dataOperations : capabilities . dataOperations . map ( dataOperationsToWixDataQueryOperators ) ,
127+ fieldTypes : capabilities . fieldTypes . map ( fieldTypeToWixDataEnum ) ,
128+ collectionOperations : capabilities . collectionOperations . map ( collectionOperationsToWixDataCollectionOperations ) ,
142129 }
143130 }
144131
0 commit comments