pubkey / rxdb

A fast, local first, reactive Database for JavaScript Applications https://rxdb.info/
https://rxdb.info/
Apache License 2.0
20.94k stars 1.02k forks source link

collection.onRemove is not a function - function is missing from new collections #6134

Closed MrChadMWood closed 1 week ago

MrChadMWood commented 1 week ago

Collections I've built seem to be missing the collection.onRemove() function now. This occurred after refactoring a lot of code in my project, so I'm open to the idea that I messed something up on my end. I've not found any luck finding the issue though, and trying earlier commits also produces the error for some reason.

The error I receive is (in browser):

 Error initializing database: TypeError: collection.onRemove is undefined
    createDatabase initialize.js:63
    createDatabase initialize.js:60

Upon checking in the console, this seems to be the case. The onDestroy method is there, but not the onRemove method.

15:49:47.804 >>> space_task_schedule
15:49:47.821
Object { storageInstance: {…}, timeouts: Set [], incrementalWriteQueue: {…}, _incrementalUpsertQueues: Map(0), synced: false, hooks: {}, _subs: (2) […], _docCache: {…}, _queryCache: {…}, "$": {…}, … }

15:49:50.050 >>> space_task_schedule.hasOwnProperty("onRemove")
15:49:50.073 false 

Here is my database initialization, the schema, and trying to add the onRemove function to the collection. I comment on the area that produces the error:

// ./src/database/initialize.js

import { createRxDatabase } from 'rxdb';
import { getRxStorageDexie } from 'rxdb/plugins/storage-dexie';
import { addRxPlugin } from 'rxdb';
import { RxDBDevModePlugin } from 'rxdb/plugins/dev-mode';
import { RxDBQueryBuilderPlugin } from 'rxdb/plugins/query-builder';
import { RxDBLeaderElectionPlugin } from 'rxdb/plugins/leader-election';
import { RxDBUpdatePlugin } from 'rxdb/plugins/update';
import { RxDBJsonDumpPlugin } from 'rxdb/plugins/json-dump';
import { RxDBLocalDocumentsPlugin } from 'rxdb/plugins/local-documents';

addRxPlugin(RxDBDevModePlugin);
addRxPlugin(RxDBQueryBuilderPlugin);
addRxPlugin(RxDBLeaderElectionPlugin);
addRxPlugin(RxDBUpdatePlugin);
addRxPlugin(RxDBJsonDumpPlugin);
addRxPlugin(RxDBLocalDocumentsPlugin);

const spaceTaskScheduleSchema = {
    version: 0,
    primaryKey: 'space_task_id',
    type: 'object',
    properties: {
        space_task_id: { type: 'string', maxLength: 100 },
        workcenter_id: { type: 'string' },
        space_id: { type: 'string' },
        task_id: { type: 'string' },
        building_name: { type: 'string' },
        section_name: { type: 'string' },
        space_pretty_name: { type: 'string' },
        space_name: { type: 'string' },
        task_name: { type: 'string' },
        task_ordinal_position: { type: 'number' },
        expected_start_date: { type: 'string', format: 'date' },
        expected_finish_date: { type: 'string', format: 'date' },
        actual_start_date: { type: 'string', format: 'date', maxLength: 100 },
        actual_finish_date: { type: 'string', format: 'date', maxLength: 100 },
        workcenter_comment: { type: 'string' },
        space_task_comment: { type: 'string' },
        status: { type: 'string' },
        float_backward: { type: 'number' },
        float_forward: { type: 'number' },
        updated_at: { type: 'string', format: 'date-time' },
        _deleted: { type: 'boolean' }
    },
    required: ['space_task_id'],
    indexes: [
        'actual_start_date',
        'actual_finish_date'
    ]
};

const createDatabase = async () => {
    const db = await createRxDatabase({
        name: 'project_schedule',
        storage: getRxStorageDexie(),
        multiInstance: true,
        eventReduce: true,
    });

    const collections = await db.addCollections({
        space_task_schedule: { schema: spaceTaskScheduleSchema, localDocuments: true }
    });

    // This will throw the error
    Object.entries(collections).forEach(([name, collection]) => {
        collection.onRemove.push(() => console.log('Collection removed!'));
    });

    return { db, collections };
}

export default createDatabase;

My package.json, which might be involved, as I also reproduced this error by trying a previously working commit on my project:

{
    "name": "react-mui-app",
    "version": "0.1.0",
    "private": true,
    "dependencies": {
        "@emotion/react": "^11.7.1",
        "@emotion/styled": "^11.6.0",
        "@mui/material": "^5.2.1",
        "@mui/x-data-grid": "^5.0.1",
        "react": "^17.0.2",
        "react-dom": "^17.0.2",
        "react-hooks": "^1.0.1",
        "react-scripts": "4.0.3",
        "rxdb": "15.24.0",
        "rxdb-hooks": "^5.0.2",
        "rxjs": "7.8.1",
        "web-vitals": "^1.1.2"
    },
    "scripts": {
        "start": "react-scripts start",
        "build": "react-scripts build",
        "test": "react-scripts test",
        "eject": "react-scripts eject"
    },
    "eslintConfig": {
        "extends": [
            "react-app",
            "react-app/jest"
        ]
    },
    "browserslist": [
        ">0.2%",
        "not dead",
        "not op_mini all"
    ],
    "exports": {
        "./plugins/storage-dexie": {
            "types": "./dist/types/plugins/storage-dexie/index.d.ts",
            "require": "./dist/cjs/plugins/storage-dexie/index.js",
            "import": "./dist/esm/plugins/storage-dexie/index.js",
            "default": "./dist/esm/plugins/storage-dexie/index.js"
        }
    }
}

Pull request (did not reproduce due to testing error)

MrChadMWood commented 1 week ago

Very odd, but the code seems to have started working. I'm not sure what changed, but closing this because I guess this could be unrelated to RxDB. Likely an issue on my part.