Merge branch 'pr-6' into single-source

This commit is contained in:
david raistrick
2026-07-06 23:24:25 -04:00
4 changed files with 53 additions and 43 deletions
+18
View File
@@ -294,6 +294,7 @@ function runStorageContract(name, factory) {
describe('subscribeCollection queryConstraints', () => {
const orderByC = (field, dir) => ({ __type: 'orderBy', field, dir });
const limitC = (n) => ({ __type: 'limit', n });
const offsetC = (n) => ({ __type: 'offset', offset: n });
beforeEach(async () => {
// seed 5 log docs, timestamps out of order
@@ -324,6 +325,23 @@ function runStorageContract(name, factory) {
});
expect(result).toHaveLength(5);
});
// Log page pagination (LogsView pageQuery): orderBy + limit + offset.
// Server pushes offset to SQL; firebase adapter emulates (widen limit,
// slice). Both MUST return the same page.
test('offset skips first N after ordering (pagination)', async () => {
const result = await new Promise((resolve) => {
storage.subscribeCollection('logs', resolve, [orderByC('timestamp', 'desc'), limitC(2), offsetC(2)]);
});
expect(result.map(d => d.msg)).toEqual(['two', 'four']);
});
test('offset past end returns empty page', async () => {
const result = await new Promise((resolve) => {
storage.subscribeCollection('logs', resolve, [orderByC('timestamp', 'desc'), limitC(2), offsetC(10)]);
});
expect(result).toEqual([]);
});
});
});
}
+27 -21
View File
@@ -71,6 +71,27 @@ export function getAuthInstance() { return authInstance; }
// App.js can now import { storage } and call storage.setDoc(path, data).
// Hooks (useFirestoreDocument etc) still use SDK directly for now.
// Translate neutral {__type} constraints (src/storage/index.js builders) to
// SDK builders. The client SDK has no offset, so emulate it: widen limit to
// offset+limit and report offsetN for the caller to slice off the leading
// docs. Read cost matches native offset (admin SDK also bills skipped docs);
// the server adapter pushes offset into SQL instead. Unknown constraint types
// are dropped — a raw object handed to query() throws and white-screens.
function toSdkConstraints(queryConstraints) {
let offsetN = 0;
let limitN = null;
const fbConstraints = [];
for (const c of queryConstraints || []) {
if (!c || !c.__type) continue;
if (c.__type === 'where') fbConstraints.push(where(c.field, c.op, c.value));
else if (c.__type === 'orderBy') fbConstraints.push(orderBy(c.field, c.dir || 'asc'));
else if (c.__type === 'limit') limitN = c.n;
else if (c.__type === 'offset') offsetN = c.offset || 0;
}
if (limitN !== null) fbConstraints.push(limit(limitN + offsetN));
return { fbConstraints, offsetN };
}
export function createFirebaseStorage() {
const db = dbInstance;
if (!db) throw new Error('Firestore not initialized. Call initFirebase() first.');
@@ -101,18 +122,10 @@ export function createFirebaseStorage() {
},
async getCollection(collectionPath, queryConstraints = []) {
// Translate neutral {__type} constraints to firebase SDK builders.
const fbConstraints = [];
for (const c of queryConstraints || []) {
if (!c || !c.__type) continue;
if (c.__type === 'where') fbConstraints.push(where(c.field, c.op, c.value));
else if (c.__type === 'orderBy') fbConstraints.push(orderBy(c.field, c.dir || 'asc'));
else if (c.__type === 'limit') fbConstraints.push(limit(c.n));
// firebase SDK has no offset; skip (UI uses server adapter in dev).
}
const { fbConstraints, offsetN } = toSdkConstraints(queryConstraints);
const q = fbConstraints.length ? query(collection(db, collectionPath), ...fbConstraints) : collection(db, collectionPath);
const snapshot = await getDocsReal(q);
return snapshot.docs.map(d => ({ id: d.id, ...d.data() }));
return snapshot.docs.slice(offsetN).map(d => ({ id: d.id, ...d.data() }));
},
async countCollection(collectionPath) {
@@ -174,19 +187,12 @@ export function createFirebaseStorage() {
subscribeCollection(collectionPath, cb, queryConstraints = [], errCb) {
recordAdapterCall({ fn: 'subscribeCollection', path: collectionPath });
// queryConstraints = neutral {__type} builders (from index.js).
// Translate to SDK orderBy/limit.
const sdkConstraints = queryConstraints.map(c => {
if (c.__type === 'where') return where(c.field, c.op, c.value);
if (c.__type === 'orderBy') return orderBy(c.field, c.dir);
if (c.__type === 'limit') return limit(c.n);
return c; // pass-through (forward compat)
});
const q = sdkConstraints.length > 0
? query(collection(db, collectionPath), ...sdkConstraints)
const { fbConstraints, offsetN } = toSdkConstraints(queryConstraints);
const q = fbConstraints.length > 0
? query(collection(db, collectionPath), ...fbConstraints)
: collection(db, collectionPath);
return onSnapshot(q, (snap) => {
cb(snap.docs.map(d => ({ id: d.id, ...d.data() })));
cb(snap.docs.slice(offsetN).map(d => ({ id: d.id, ...d.data() })));
}, (err) => {
console.error(`subscribeCollection ${collectionPath}:`, err);
if (typeof errCb === 'function') errCb(err);