import { BotonicEvent , EventTypes , User } from '@botonic/core' ; import { Entity , Table } from 'dynamodb-toolbox' ; import { getConnectionEventEntity , getMessageEventEntities , getUserEntity , getUserEventsTable , GLOBAL_SECONDARY_INDEX_NAME , SORT_KEY_NAME , USER_PREFIX , } from './dynamodb-utils' ; import { DataProvider } from './factory' ; export class DynamoDBDataProvider implements DataProvider { region : string ; tableName : string ; userEventsTable : Table ; userEntity : Entity < any > ; eventEntity : Entity < any > ; messageEventEntities : Record < string , Entity < any > > ; textMessageEventEntity : Entity < any > ; connectionEventEntity : Entity < any > ; constructor ( url ) { try { ; [ this . tableName , this . region ] = url . split ( '://' ) [ 1 ] . split ( '.' ) ; this . userEventsTable = getUserEventsTable ( this . tableName , this . region ) ; this . userEntity = getUserEntity ( this . userEventsTable ) ; this . connectionEventEntity = getConnectionEventEntity ( this . userEventsTable ) ; this . messageEventEntities = getMessageEventEntities ( this . userEventsTable ) ; } catch ( e ) { console . log ( { e } ) ; } } async getUsers ( limit = 10 , offset = 0 ) { const result = await this . userEventsTable . scan ( { filters : { attr : SORT_KEY_NAME , beginsWith : USER_PREFIX } , limit , } ) ; return result . Items ; } async getUser ( id ) { const userById = { id : id , SK : id , } ; const result = await this . userEntity . get ( userById ) ; if ( Object . keys ( result ) . length === 0 ) return undefined ; return result . Item ; } async getUserByWebsocketId ( websocketId ) { const result = await this . userEventsTable . query ( websocketId , { index : GLOBAL_SECONDARY_INDEX_NAME , } ) ; if ( result . Count === 0 ) return undefined ; return result . Items [ 0 ] ; } async getUserByField ( field , value ) { } async saveUser ( user ) { const putUser = { ... user , id : user . id , userId : user . id } ; await this . userEntity . put ( putUser ) ; return user ; } async updateUser ( user ) { const updateUser = { ... user , id : user . id , userId : user . id } ; const res = await this . userEntity . update ( updateUser ) ; return user ; } async deleteUser ( id ) { } async getEvents ( limit = 10 , offset = 0 ) { } async getEvent ( id ) { } async saveEvent ( event ) { if ( event . eventType === EventTypes . CONNECTION ) { await this . connectionEventEntity . put ( event ) ; } if ( event . eventType === EventTypes . MESSAGE ) { await this . messageEventEntities [ event . type ] . put ( event ) ; } return event ; } async updateEvent ( event ) { } async deleteEvent ( id ) { } }