Nozbe / WatermelonDB

🍉 Reactive & asynchronous database for powerful React and React Native apps ⚡️
https://watermelondb.dev
MIT License
10.49k stars 589 forks source link

_raw is always empty #1282

Closed amhed closed 2 years ago

amhed commented 2 years ago

I'm running into an issue similar to these ones:

When using create the write operation succeeds into writing, but the object is always written with empty values. Inspecting the return object I can see that _raw has all the fields defined by the schema, but they all have the default values instead.

schema

import { appSchema, tableSchema } from '@nozbe/watermelondb';

export const schema = appSchema({
  version: 5,
  tables: [
    tableSchema({
      name: 'wallet',
      columns: [
        { name: 'walletId', type: 'string', isIndexed: true },
        { name: 'currencyCodeStr', type: 'string', isIndexed: true },
      ],
    }),
  ],
});

Model

import { Model } from '@nozbe/watermelondb';
import { field } from '@nozbe/watermelondb/decorators';

export class WalletWMO extends Model {
  static table = 'wallet';

  @field('walletId')
  walletId!: string;

  @field('currencyCodeStr')
  currencyCodeStr!: string;
}

Code to insert

database.write(async () => {
  const table = database.get<WalletWMO>('wallet');
  const result = await table.create((record: WalletWMO) => {
    record.walletId = this.id;
    record.currencyCodeStr = this.currencyCode.rawValue;
  };);
});

And yet, the underlying `_raw values are empty/default

image

Tsconfig has

"experimentalDecorators": true,
"emitDecoratorMetadata": true,

BabelConfig has

const plugins = [
    'react-hot-loader/babel',
    ['@babel/plugin-proposal-private-methods', { loose: true }],
    ['@babel/plugin-proposal-private-property-in-object', { loose: true }],
    ['@babel/plugin-proposal-class-properties', { loose: true }],
    ['@babel/plugin-proposal-decorators', { legacy: true }],
    [
      '@babel/plugin-transform-runtime',
      {
        helpers: true,
        regenerator: true,
      },
    ],
]
amhed commented 2 years ago

Inspecting transpiled JS it looks like decorator metadata is being issued:

__decorate([
  (0,decorators.field)('walletId'),
  __metadata("design:type", String)
], WalletWMO.prototype, "walletId", void 0);
__decorate([
  (0,decorators.field)('currencyCodeStr'),
  __metadata("design:type", String)
], WalletWMO.prototype, "currencyCodeStr", void 0);
amhed commented 2 years ago

Update: I'm deploying this on a shared data layer for both react and react-native. The React-Native side executes flawlessly. Only the web side bombs :o

amhed commented 2 years ago

Aaaaand I had a different target for the web version. Set to ES2020 :)

leejunhui commented 1 year ago

@amhed Hi, I met the same issue - the _raw always empty. The web version you set is the version in babel config or ts config? Formerly my project use webpack, so everything is fine.But when I try to migrate to vite, on the dev environment, the _raw field in watermelondb is always empty.

kegesch commented 1 year ago

@leejunhui I have the same problem. Just migrated to Vite + Typescript and the _raw fields are empty. Did you get it running in the end?

leejunhui commented 1 year ago

@leejunhui I have the same problem. Just migrated to Vite + Typescript and the _raw fields are empty. Did you get it running in the end?

sorry, I haven't sovled the problem.I think the truly cause is the lack of support of javascript decorator of esbuild~

parsagholipour commented 8 months ago

Any solution to use this with Vite?

Alarees commented 4 months ago

add ._raw in create function

database.write(async () => { const contact = await contactsCollection.create(contact => { contact._raw.contact_name = new_contact.contact_name; contact._raw.contact_phone = new_contact.contact_phone; }); });

GitMurf commented 1 week ago

@amhed Hi, I met the same issue - the _raw always empty. The web version you set is the version in babel config or ts config? Formerly my project use webpack, so everything is fine.But when I try to migrate to vite, on the dev environment, the _raw field in watermelondb is always empty.

@leejunhui did you ever figure this out?! I am running into exactly the same issue using vite and the nodejs sqlite adapter in Watermelon.

GitMurf commented 1 week ago

@kegesch @parsagholipour did you end up settling on a solution / workaround or figure out the problem? I also am using vite and TypeScript and running into the EXACT same issues.

@Alarees your solution does work (hard setting the _raw properties) but in the Watermelon docs it explicitly says you should not do that. So I am wondering how this can be fixed (or a workaround) in a supported / recommend way by Watermelon. @radex can you provide some guidance here please? Thanks so much! You can see here a full explanation of my issue (similar to described above) and how I followed the docs with no success: https://github.com/Nozbe/WatermelonDB/issues/1826#issuecomment-2353292866