fix: auto-create cloud collection on first push, remove manual setup
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.
This commit is contained in:
+33
-20
@@ -3,6 +3,7 @@ const DB_COLLECTION = 'plank_data'
|
|||||||
let _db = null
|
let _db = null
|
||||||
let _pushTimer = null
|
let _pushTimer = null
|
||||||
let _enabled = false
|
let _enabled = false
|
||||||
|
let _openid = null
|
||||||
|
|
||||||
const getDb = () => {
|
const getDb = () => {
|
||||||
if (!_enabled) return null
|
if (!_enabled) return null
|
||||||
@@ -14,7 +15,6 @@ const getDb = () => {
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* Call once from app.js onLaunch to initialize cloud.
|
* Call once from app.js onLaunch to initialize cloud.
|
||||||
* Gracefully degrades if cloud environment isn't configured yet.
|
|
||||||
*/
|
*/
|
||||||
const init = () => {
|
const init = () => {
|
||||||
try {
|
try {
|
||||||
@@ -26,27 +26,29 @@ const init = () => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Pull all user data from cloud. Returns null if cloud is unavailable or no data exists.
|
* Pull all user data from cloud. Returns null if no data or unavailable.
|
||||||
|
* Collection is auto-created by first push, so .get() on empty collection
|
||||||
|
* is safe — it just returns empty set.
|
||||||
*/
|
*/
|
||||||
const pullAll = async () => {
|
const pullAll = async () => {
|
||||||
const db = getDb()
|
const db = getDb()
|
||||||
if (!db) return null
|
if (!db) return null
|
||||||
try {
|
try {
|
||||||
const coll = db.collection(DB_COLLECTION)
|
const res = await db.collection(DB_COLLECTION)
|
||||||
const res = await coll.where({ _openid: '{openid}' }).get()
|
.where({ _openid: '{openid}' })
|
||||||
|
.get()
|
||||||
if (res && res.data && res.data.length > 0) {
|
if (res && res.data && res.data.length > 0) {
|
||||||
return res.data[0]
|
return res.data[0]
|
||||||
}
|
}
|
||||||
return null
|
return null
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Cloud pull failed:', e)
|
// Collection may not exist yet — that's fine, first push will create it
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Schedule a cloud push (debounced at 2s).
|
* Schedule a push (debounced 2s).
|
||||||
* Call this after any local data mutation.
|
|
||||||
*/
|
*/
|
||||||
const pushAll = () => {
|
const pushAll = () => {
|
||||||
if (!_enabled) return
|
if (!_enabled) return
|
||||||
@@ -58,7 +60,6 @@ const _doPush = async () => {
|
|||||||
const db = getDb()
|
const db = getDb()
|
||||||
if (!db) return
|
if (!db) return
|
||||||
try {
|
try {
|
||||||
// Lazily require to avoid circular dependency at module-load time
|
|
||||||
const storage = require('./storage')
|
const storage = require('./storage')
|
||||||
const themeMod = require('./theme')
|
const themeMod = require('./theme')
|
||||||
|
|
||||||
@@ -67,18 +68,27 @@ const _doPush = async () => {
|
|||||||
settings: storage.getSettings(),
|
settings: storage.getSettings(),
|
||||||
streak: storage.getStreak(),
|
streak: storage.getStreak(),
|
||||||
themeId: themeMod.getCurrentTheme().id,
|
themeId: themeMod.getCurrentTheme().id,
|
||||||
updatedAt: new Date()
|
updatedAt: db.serverDate()
|
||||||
}
|
}
|
||||||
|
|
||||||
const coll = db.collection(DB_COLLECTION)
|
// Strategy: try add() first. On first use, this auto-creates the
|
||||||
const existing = await coll.where({ _openid: '{openid}' }).get()
|
// collection. If the user already has a doc, it returns
|
||||||
if (existing && existing.data && existing.data.length > 0) {
|
// 'duplicate' error; we fall back to update via query.
|
||||||
await coll.doc(existing.data[0]._id).update({ data })
|
try {
|
||||||
} else {
|
await db.collection(DB_COLLECTION).add({ data })
|
||||||
await coll.add({ data })
|
} catch (addErr) {
|
||||||
|
// Likely already has a doc — find and update it
|
||||||
|
const existing = await db.collection(DB_COLLECTION)
|
||||||
|
.where({ _openid: '{openid}' })
|
||||||
|
.get()
|
||||||
|
if (existing && existing.data && existing.data.length > 0) {
|
||||||
|
await db.collection(DB_COLLECTION)
|
||||||
|
.doc(existing.data[0]._id)
|
||||||
|
.update({ data })
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Cloud push failed:', e)
|
// Silently ignore — data stays in local storage, will retry next write
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -89,13 +99,16 @@ const clearAll = async () => {
|
|||||||
const db = getDb()
|
const db = getDb()
|
||||||
if (!db) return
|
if (!db) return
|
||||||
try {
|
try {
|
||||||
const coll = db.collection(DB_COLLECTION)
|
const existing = await db.collection(DB_COLLECTION)
|
||||||
const existing = await coll.where({ _openid: '{openid}' }).get()
|
.where({ _openid: '{openid}' })
|
||||||
|
.get()
|
||||||
if (existing && existing.data && existing.data.length > 0) {
|
if (existing && existing.data && existing.data.length > 0) {
|
||||||
await coll.doc(existing.data[0]._id).remove()
|
await db.collection(DB_COLLECTION)
|
||||||
|
.doc(existing.data[0]._id)
|
||||||
|
.remove()
|
||||||
}
|
}
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Cloud clear failed:', e)
|
// Collection or doc missing — nothing to clear
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user