Skip to content

Commit

Permalink
add fastDB for sync
Browse files Browse the repository at this point in the history
  • Loading branch information
kilbot committed Jun 6, 2024
1 parent 31ca2e1 commit d76a1b7
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 14 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@wcpos/query",
"version": "1.5.5",
"version": "1.5.6",
"description": "Query and Replication for WooCommerce POS",
"author": "kilbot <[email protected]>",
"license": "MIT",
Expand Down
26 changes: 16 additions & 10 deletions src/collection-replication-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export interface QueryHooks {

interface CollectionReplicationConfig<Collection> {
collection: Collection;
syncCollection: any;
httpClient: any;
hooks?: any;
endpoint: string;
Expand All @@ -49,6 +50,7 @@ export class CollectionReplicationState<T extends Collection> extends Subscribab
private hooks: CollectionReplicationConfig<T>['hooks'];
public readonly endpoint: string;
public readonly collection: T;
public readonly syncCollection: any;
public readonly storeDB: StoreDatabase;
public readonly httpClient: any;
private errorSubject: Subject<Error>;
Expand Down Expand Up @@ -88,13 +90,15 @@ export class CollectionReplicationState<T extends Collection> extends Subscribab
*/
constructor({
collection,
syncCollection,
httpClient,
hooks,
endpoint,
errorSubject,
}: CollectionReplicationConfig<T>) {
super();
this.collection = collection;
this.syncCollection = syncCollection;
this.storeDB = collection.database;
this.httpClient = httpClient;
this.hooks = hooks || {};
Expand All @@ -118,12 +122,10 @@ export class CollectionReplicationState<T extends Collection> extends Subscribab
* Subscribe to remote and local collections to calculate the total number of documents
*/
private setupDocumentCount() {
const remoteCount$ = this.storeDB.collections.sync
.find({ selector: { endpoint: this.endpoint } })
.$.pipe(
map((docs) => docs?.length || 0),
distinctUntilChanged()
);
const remoteCount$ = this.syncCollection.find({ selector: { endpoint: this.endpoint } }).$.pipe(
map((docs) => docs?.length || 0),
distinctUntilChanged()
);

const newLocalCount$ = this.collection.find({ selector: { id: { $eq: null } } }).$.pipe(
map((docs) => docs?.length || 0),
Expand Down Expand Up @@ -183,6 +185,11 @@ export class CollectionReplicationState<T extends Collection> extends Subscribab
}
}

/**
* @TODO - split audit into fullAudit and recentChanges
*/
async fullAudit() {}

/**
*
*/
Expand Down Expand Up @@ -241,16 +248,15 @@ export class CollectionReplicationState<T extends Collection> extends Subscribab
* - it's tempting to just bulk delete and insert, but it causes doc totals to flash in the UI
*/
const data = response.data.map((doc) => ({ ...doc, endpoint: this.endpoint }));
const syncCollection = this.storeDB.collections.sync;
const ids = await this.getStoredRemoteIDs();
const orphanedIds = ids.filter((id) => !data.map((doc) => doc.id).includes(id));
if (orphanedIds.length > 0) {
log.warn('removing remote', orphanedIds, 'from', this.collection.name);
await syncCollection
await this.syncCollection
.find({ selector: { endpoint: this.endpoint, id: { $in: orphanedIds } } })
.remove();
}
const { error } = await this.storeDB.collections.sync.bulkUpsert(data);
const { error } = await this.syncCollection.bulkUpsert(data);

if (error.length > 0) {
log.error('Error saving remote state for ' + this.endpoint, error);
Expand Down Expand Up @@ -294,7 +300,7 @@ export class CollectionReplicationState<T extends Collection> extends Subscribab
*
*/
async getStoredRemoteDocs() {
return this.storeDB.collections.sync.find({ selector: { endpoint: this.endpoint } }).exec();
return this.syncCollection.find({ selector: { endpoint: this.endpoint } }).exec();
}

/**
Expand Down
16 changes: 15 additions & 1 deletion src/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ export class Manager<TDatabase extends RxDatabase> extends SubscribableBase {

private constructor(
private localDB: TDatabase,
private fastLocalDB,
private httpClient,
private locale: string
) {
Expand Down Expand Up @@ -94,13 +95,15 @@ export class Manager<TDatabase extends RxDatabase> extends SubscribableBase {

public static getInstance<TDatabase extends RxDatabase>(
localDB: TDatabase,
fastLocalDB,
httpClient,
locale: string
) {
// Check if instance exists and dependencies are the same
if (
Manager.instance &&
Manager.instance.localDB === localDB &&
Manager.instance.fastLocalDB === fastLocalDB &&
// Manager.instance.httpClient === httpClient && // @TODO - look into this
Manager.instance.locale === locale
) {
Expand All @@ -113,7 +116,7 @@ export class Manager<TDatabase extends RxDatabase> extends SubscribableBase {
}

// Create a new instance
Manager.instance = new Manager(localDB, httpClient, locale);
Manager.instance = new Manager(localDB, fastLocalDB, httpClient, locale);
return Manager.instance as Manager<TDatabase>;
}

Expand Down Expand Up @@ -207,6 +210,15 @@ export class Manager<TDatabase extends RxDatabase> extends SubscribableBase {
return this.localDB[collectionName];
}

getSyncCollection(collectionName: string) {
if (!this.fastLocalDB[collectionName]) {
this.subjects.error.next(
new Error(`Sync collection with name: ${collectionName} not found.`)
);
}
return this.fastLocalDB[collectionName];
}

getQuery(queryKeys: (string | number | object)[]) {
const key = this.stringify(queryKeys);
const query = this.queryStates.get(key);
Expand Down Expand Up @@ -298,10 +310,12 @@ export class Manager<TDatabase extends RxDatabase> extends SubscribableBase {
*/
registerCollectionReplication({ collection, endpoint }) {
const replicationState = this.replicationStates.get(endpoint);
const syncCollection = this.getSyncCollection(collection.name);
if (!replicationState || !(replicationState instanceof CollectionReplicationState)) {
const collectionReplication = new CollectionReplicationState({
httpClient: this.httpClient,
collection,
syncCollection,
endpoint,
errorSubject: this.subjects.error,
});
Expand Down
6 changes: 4 additions & 2 deletions src/provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const QueryContext = React.createContext<Manager<RxDatabase> | undefined>(undefi

interface QueryProviderProps<T extends RxDatabase> {
localDB: T;
fastLocalDB: any;
http: any; // Replace 'any' with the actual type of your HTTP client
locale: string;
children: React.ReactNode;
Expand All @@ -18,13 +19,14 @@ interface QueryProviderProps<T extends RxDatabase> {
*/
export const QueryProvider = <T extends RxDatabase>({
localDB,
fastLocalDB,
http,
children,
locale,
}: QueryProviderProps<T>) => {
const manager = React.useMemo(() => {
return Manager.getInstance<T>(localDB, http, locale);
}, [localDB, http, locale]);
return Manager.getInstance<T>(localDB, fastLocalDB, http, locale);
}, [localDB, fastLocalDB, http, locale]);

return <QueryContext.Provider value={manager}>{children}</QueryContext.Provider>;
};
Expand Down

0 comments on commit d76a1b7

Please sign in to comment.