New with Cassandra, i would like to store some datas in a table with UDT. So i create a UDT with a map column :
CREATE TYPE IF NOT EXISTS test.lifestyle (
name text,
values map<timestamp, int>
);
Then i create a table using this UDT :
CREATE TABLE IF NOT EXISTS test.account_lifestyle (
account_id varchar PRIMARY KEY,
life_styles frozen<set<lifestyle>>
);
Use a simple NestJS application to create an API, so, my models are :
import { LifeStyleModel } from './../life-style-model/life-style-model'
export class AccountLifeStyleModel {
accountId: string
lifeStyles: Set<LifeStyleModel>
}
And :
export class LifeStyleModel {
name: string
values: Map<number, number>
}
Finally i create a Repository from cassandra-driver :
@Injectable()
export class LifeStyleRepositoryService implements OnModuleInit {
lifeStyleMapper: mapping.ModelMapper<AccountLifeStyleModel>
private readonly mappingOptions: mapping.MappingOptions = {
models: {
'LifeStyle': {
tables: [
'account_lifestyle'
],
mappings: new mapping.UnderscoreCqlToCamelCaseMappings
}
}
}
constructor(private _dbService: DbService) {}
onModuleInit() {
this.lifeStyleMapper = this._dbService
.createMapper(this.mappingOptions)
.forModel('LifeStyle')
}
async getAll() {
return (await this.lifeStyleMapper.findAll()).toArray()
}
async add(accountLifeStyle: AccountLifeStyleModel) {
return (await this.lifeStyleMapper.insert(accountLifeStyle)).toArray()
}
}
Then use Postman to test my endpoint passing this JSON :
{
"accountId": "64578abc7859az",
"lifeStyles": [
{
"name": "Sport",
"values": [
[1683099227, 1]
]
},
{
"name": "Tobaco",
"values": [
[1683099227, 4]
]
},
{
"name": "Alcohol",
"values": [
[1683099227, 4]
]
}
]
}
When endpoint was reached, i got the following error :
[Nest] 9045 - 03/05/2023 17:39:54 ERROR [ExceptionsHandler] Expected Number, obtained [ 1683099227, 1 ]
So, i mean my JSON does not correspond to expected format of a map, but i don't know how to set the correct JSON or modify my models to pass correct JSON.
Any help would be appreciate.
Regards,
JL