Open rizdaputra opened 3 weeks ago
why is there an exclamation mark after the column names?
if i dont put exclamation mark this is the error i get
Property 'name' has no initializer and is not definitely assigned in the constructor.
i am developing in react native typescript
@rizdaputra Ahh ok I'm not really familiar with typescript, sorry!I actually just realized 2 things that might be your issue:
await database.write(async () => {
await database.batch(user);
});
prepareCreate
on it. Example for the user
I used in my above code:
const user = (await database.get('user')).prepareCreate(x => {
// data assignments
});
Notice the brackets around the (await database.get('user'))
I do not think this is directly related to my issue, but to be honest I do not see many people responding to any GitHub Issues recently so hopefully @KrisLau or @rizdaputra can provide some feedback / advice on my issue with create / prepareCreate 🙏
I am able to get my DB creates to write to my sqlite database fine but the non core properties (i.e., not id
, _status
, _changes
) are always just writing as empty / null values. In my example I simply just have one new field called title
to keep is simple.
[!NOTE]
<Testing2>
is a TypeScript thing (Testing2 is name of my model for 'testing' table) so if you are not familiar with TypeScript, you can ignore it as it is just for type safety / annotations within IDE.- I know above it was mentioned about
awaiting
db.get but actually it is not async. But I tried it that way as well just for completeness sake.create()
isasync
which is why you see anawait
in my first example below, butprepareCreate
is not, so noawait
(second example below). You can see this in the Watermelon api types file.
I have tried with a full write transaction / create:
await newDb.write(async () => {
const newRow = await newDb.get<Testing2>('testing').create((theRow) => {
theRow.title = 'title 1';
});
console.log('newRow:', newRow);
});
As well as just trying to test using prepareCreate before writing it:
const newRow = newDb.get<Testing2>('testing').prepareCreate((theRow) => {
theRow.title = 'title 1';
});
console.log('newRow:', newRow);
In either case, I get the following where I see my title
set properly on the record object (title set to title 1
) but as you can see, the _raw
record property just has title
set to ''
empty string, instead of the value title 1
.
My assumption is that the actual writing to sqlite uses the _raw
record which is why I get results like this, where the title is always just set as an empty string:
Do you know what I could be doing wrong? It seems like at some point in the process, WatermelonDB is supposed to "merge" any properties we set like this.row = "title 1"
into the _raw
property of the record, but it seems like that is not happening?
I am just following the example from the docs here: https://watermelondb.dev/docs/CRUD#create-a-new-record
Thanks in advance for any help, thoughts or feedback you can provide!
i am trying to do batch create but when i prepareCreate i receive this error when following the examples given, can anyone help?
this is roughly my code, the newService is detected as Model and it is said that Model dont ave name baseUrl etc.
this is my model definition