Nozbe / WatermelonDB

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

pullChanges sync creates new records with `_raw` data but null properties #1798

Closed maxwelljmckee closed 3 months ago

maxwelljmckee commented 3 months ago

Hi there,

I'm struggling to set up my first backend synchronization. I've double- and triple-checked my configurations against the documentation, but still I'm getting the same unexpected behavior. Here's where I'm at:

Take a look at this screenshot for reference:

Screenshot 2024-05-17 at 2 38 30 PM

Here are a few of my configurations for reference:

// db.init.ts
import LokiJSAdapter from "@nozbe/watermelondb/adapters/lokijs";
import { schemaMigrations } from "@nozbe/watermelondb/Schema/migrations";
import { appSchema, Database } from "@nozbe/watermelondb";
import { coursesSchema } from "./table-schemas/courses.schema";
import { Course } from "./models/course.model";

// const noop = () => null;

const dbSchema = appSchema({
  version: 1,
  tables: [coursesSchema],
});

const dbMigrations = schemaMigrations({
  migrations: [],
});

const dbAdapter = new LokiJSAdapter({
  schema: dbSchema,
  migrations: dbMigrations,
  useWebWorker: false,
  useIncrementalIndexedDB: true,

  // onQuotaExceededError: noop,
  // onSetUpError: noop,
  // extraIncrementalIDBOptions: {
  //   onDidOverwrite: noop,
  //   onversionchange: noop,
  // },
});

export const initializeWatermelonDB = () => {
  return new Database({
    adapter: dbAdapter,
    modelClasses: [Course],
  });
};
// course.model.ts
import { Model } from "@nozbe/watermelondb";
import { text, field, readonly, date } from "@nozbe/watermelondb/decorators";
import { Associations } from "@nozbe/watermelondb/Model";
import { TableRegistry } from "../db.types";

export class Course extends Model {
  static table = TableRegistry.courses;
  static associations: Associations = {
    recipes: { type: "has_many", foreignKey: "course_id" },
  };

  @text("title") title!: string;
  @field("owned") owned!: boolean;

  @field("remote_id") remote_id!: number;
  @readonly @date("created_at") createdAt!: Date;
  @readonly @date("updated_at") updatedAt!: Date;
}
// courses.schema.ts
import { tableSchema } from "@nozbe/watermelondb";
import { baseTableConfig } from "../db.utils";
import { TableRegistry } from "../db.types";

export const coursesSchema = tableSchema({
  name: TableRegistry.courses,
  columns: [
    { name: "title", type: "string" },
    { name: "owned", type: "boolean", isOptional: true },
    { name: "remote_id", type: "number", isIndexed: true },
    { name: "created_at", type: "number" },
    { name: "updated_at", type: "number" },
  ],
});

I understand that tracking separate remote/client ids is not recommended, but I think it will be necessary for my use case. I'm only asking for support on correctly persisting my model in the backend sync process. I'd be super appreciative if you can help me out!! Thanks in advance 🙏

primus11 commented 3 months ago

Hey,

Maybe also show how you do sync and how you use model itself then.

maxwelljmckee commented 3 months ago

I've decided to try working with RxDB for now. Closing the issue