WatermelonDB icon indicating copy to clipboard operation
WatermelonDB copied to clipboard

why returned data from query has _ in their keys and can i change it?

Open peyman-hakemi opened this issue 2 years ago • 2 comments

I created a chat like this.

my model

export default class Chats extends Model {
static table = 'chats';
@field('is_group') isGroup;
}

my schema

 tableSchema({
  name: 'chats',
  columns: [
    {name: 'is_group', type: 'boolean'},
  ],
}),

and the actions

await database.write(async () => {
  const CHATS = uniqueChats.map(item =>
      chats.prepareCreate(entry => {
      entry.isGroup = item.isGroup;
    }),
  );
  await database
    .batch(CHATS)
    .then(res => console.log('log after save chat', res));
});

I am using a query in this way to get my data.

database.collections.get('messages').query().observe()

And the result from this is like this.

  [{"__changes": null, "_isEditing": false, "_preparedState": null, "_raw": {"_changed": "_id": "H0ZN4jaIbEyXHFh7lo7L", "_status": "created", "is_group": false}, "_subscribers": [], "collection": {"_cache": [RecordCache], "_subscribers": [Array], "changes": [Subject], "database": [Database], "modelClass": [Function Chats]}}]

How can I change is_group to isGroup like it is in my initial data before creating it in the database?

peyman-hakemi avatar Sep 14 '22 08:09 peyman-hakemi

In the console.log the object does not show "parsed fields". But if you try to access isGroup, will return the right value.

@PedroAugustoRamalhoDuarte Good catch. Also, let TS know what model is coming back

return database
      .get<YourModelHere>("table_name")
      .query(Q.take(10));

radulescuandrew avatar Mar 30 '24 15:03 radulescuandrew