Before: add-first-then-update caused duplicate docs on each push
because add() always succeeds once collection exists.
Now: query {_openid} first → update if found, add if not.
Each user always has exactly one document in plank_data.
Also removed debug console.log — only production-scale errors remain.
The collection 'plank_data' no longer needs to be created manually.
db.collection().add() auto-creates the collection on first write.
Changed push strategy from 'query-then-add' to 'add-first':
- add() succeeds → collection created, data stored
- add() fails (duplicate) → fall back to query + update
- .get() on non-existent collection now caught gracefully
Also switched updatedAt to db.serverDate() for consistent timestamps.
Problem: All data stored in wx.StorageSync is permanently lost when
user deletes the mini-program or clears cache.
Solution: Sync data to WeChat CloudBase (wx.cloud.database).
- Graceful degradation: if cloud env isn't configured, all local
storage works exactly as before.
- On app launch: if local records are empty, pull from cloud.
- On writes: push to cloud (debounced 2s) after saveRecord,
deleteRecord, saveSettings, updateStreak, setTheme.
- Clear data in settings also clears cloud.
Files:
- utils/cloud.js: cloud sync wrapper (init, pullAll, pushAll, clearAll)
- app.js: cloud.init() + restore on fresh install
- utils/storage.js: cloud.pushAll() after every mutation
- utils/theme.js: cloud.pushAll() after setTheme
- pages/settings/settings.js: cloud.clearAll() when clearing data
- project.config.json: cloudfunctionRoot added
Setup required (one-time in WeChat DevTools):
1. Open 'Cloud Development' panel → enable CloudBase
2. Create environment → copy env ID
3. In app.js, change wx.cloud.init() → wx.cloud.init({ env: 'YOUR-ENV-ID' })
4. Create 'plank_data' collection in cloud database
5. Set collection permission to 'Only creator can read/write'