mocody 0.8.2 → 0.8.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -3750,12 +3750,12 @@ var MocodyInitializerCouch = class {
3750
3750
  this.baseConfig = baseConfig;
3751
3751
  }
3752
3752
  async deleteIndex({ ddoc, name }) {
3753
- const path = ["_index", ddoc, "json", name].join("/");
3753
+ const path2 = ["_index", ddoc, "json", name].join("/");
3754
3754
  const instance = await this.getInstance();
3755
3755
  const result = await instance.relax({
3756
3756
  db: this.baseConfig.couchConfig.databaseName,
3757
3757
  method: "DELETE",
3758
- path,
3758
+ path: path2,
3759
3759
  content_type: "application/json"
3760
3760
  });
3761
3761
  return result;
@@ -5959,6 +5959,7 @@ var MongoDataOperation = class extends RepoModel {
5959
5959
 
5960
5960
  // src/pouch/pouch-initializer.ts
5961
5961
  var import_throat4 = __toESM(require("throat"), 1);
5962
+ var import_node_path = __toESM(require("path"), 1);
5962
5963
  var import_pouchdb_core = __toESM(require("pouchdb-core"), 1);
5963
5964
  var import_pouchdb_find = __toESM(require("pouchdb-find"), 1);
5964
5965
  var import_pouchdb_adapter_http = __toESM(require("pouchdb-adapter-http"), 1);
@@ -6100,18 +6101,51 @@ var MocodyInitializerPouch = class {
6100
6101
  getPouchWithPlugins() {
6101
6102
  return import_pouchdb_core.default.plugin(import_pouchdb_adapter_http.default).plugin(import_pouchdb_mapreduce.default).plugin(import_pouchdb_replication.default).plugin(import_pouchdb_find.default);
6102
6103
  }
6104
+ createRemoteDB({
6105
+ proxyAuthToken,
6106
+ remoteConnectionUrl
6107
+ }) {
6108
+ const PouchDB = this.getPouchWithPlugins();
6109
+ return new PouchDB(remoteConnectionUrl, {
6110
+ fetch: (url, opts) => {
6111
+ const headers = new Headers(opts?.headers);
6112
+ if (proxyAuthToken) {
6113
+ headers.set("Authorization", `Bearer ${proxyAuthToken}`);
6114
+ }
6115
+ return PouchDB.fetch(url, {
6116
+ ...opts,
6117
+ headers,
6118
+ credentials: "omit"
6119
+ });
6120
+ }
6121
+ });
6122
+ }
6103
6123
  async pouch_getDbInstanceBase() {
6104
6124
  if (this._pouchInstance) return this._pouchInstance;
6105
6125
  const {
6106
6126
  //
6107
- liveSync,
6108
6127
  indexes,
6109
6128
  connType,
6110
- localSqliteDbFilePath,
6111
- remoteConnectionUrl
6129
+ localSqliteDbConfig,
6130
+ remoteConnectionConfig,
6131
+ syncConfig
6112
6132
  } = this.baseConfig;
6113
6133
  const isLocalFirst = connType === "LOCAL_FIRST";
6114
6134
  const isRemoteFirst = connType === "REMOTE_FIRST";
6135
+ const remoteConnectionUrl = (() => {
6136
+ const connUrl = remoteConnectionConfig?.connectionUrl;
6137
+ if (!connUrl) return void 0;
6138
+ return connUrl;
6139
+ })();
6140
+ const localSqliteDbFilePath = (() => {
6141
+ const dbName01 = localSqliteDbConfig?.fileName;
6142
+ const fileExtension01 = localSqliteDbConfig?.fileExt || ".sqlite";
6143
+ if (!localSqliteDbConfig?.folderFullPath) return void 0;
6144
+ if (!localSqliteDbConfig?.fileExt) return void 0;
6145
+ if (!dbName01) return void 0;
6146
+ const fileName = `${dbName01}${fileExtension01}`;
6147
+ return import_node_path.default.join(localSqliteDbConfig.folderFullPath, fileName);
6148
+ })();
6115
6149
  if (isLocalFirst) {
6116
6150
  if (!localSqliteDbFilePath) {
6117
6151
  throw new MocodyGenericError("localSqliteDbFilePath not defined");
@@ -6122,8 +6156,10 @@ var MocodyInitializerPouch = class {
6122
6156
  if (!remoteConnectionUrl) {
6123
6157
  throw new MocodyGenericError("couchRemoteConnectionUrl not defined");
6124
6158
  }
6125
- const PouchDB = this.getPouchWithPlugins();
6126
- this._pouchInstance = new PouchDB(remoteConnectionUrl);
6159
+ this._pouchInstance = this.createRemoteDB({
6160
+ remoteConnectionUrl,
6161
+ proxyAuthToken: remoteConnectionConfig?.proxyAuthToken
6162
+ });
6127
6163
  }
6128
6164
  try {
6129
6165
  if (indexes?.length) {
@@ -6134,38 +6170,85 @@ var MocodyInitializerPouch = class {
6134
6170
  } catch (error) {
6135
6171
  LoggingService.error(error);
6136
6172
  }
6173
+ const filterSelector01 = syncConfig?.filterSelector || void 0;
6174
+ const heartbeat01 = syncConfig?.heartbeat || 2e4;
6175
+ const batchSize01 = syncConfig?.batchSize || 100;
6176
+ const backoffDelaySeconds01 = syncConfig?.backoffDelayInSeconds || 4e4;
6177
+ if (syncConfig?.isSyncFilterSelectorRequired && !filterSelector01) {
6178
+ throw new MocodyGenericError("filterSelector not defined");
6179
+ }
6137
6180
  if (isLocalFirst && remoteConnectionUrl) {
6138
- if (liveSync) {
6181
+ if (syncConfig?.isLiveSync) {
6139
6182
  this._pouchInstance.sync(remoteConnectionUrl, {
6140
6183
  live: true,
6141
- retry: true
6184
+ retry: true,
6185
+ batch_size: batchSize01,
6186
+ heartbeat: heartbeat01,
6187
+ timeout: heartbeat01 * 2,
6188
+ selector: filterSelector01,
6189
+ back_off_function: (delay) => {
6190
+ if (delay === 0) return 2e3;
6191
+ return Math.min(delay * 2, backoffDelaySeconds01);
6192
+ }
6142
6193
  }).on("change", (change) => {
6143
- LoggingService.log({ replication_change: change });
6194
+ LoggingService.log({ pouch_replication_change: change });
6144
6195
  }).on("paused", (info) => {
6145
- LoggingService.log({ replication_paused: info });
6196
+ LoggingService.log({ pouch_replication_paused: info });
6146
6197
  }).on("error", (err) => {
6147
- LoggingService.log({ replication_error: err });
6198
+ LoggingService.log({ pouch_replication_error: err });
6199
+ }).on("active", () => {
6200
+ LoggingService.log(`Pouch:::Connection active`);
6201
+ }).on("denied", (err) => {
6202
+ LoggingService.log(`Pouch:::Connection denied`);
6148
6203
  });
6149
6204
  } else {
6150
- this._pouchInstance.sync(remoteConnectionUrl).on("error", (err) => {
6151
- LoggingService.log({ replication_error: err });
6205
+ this._pouchInstance.sync(remoteConnectionUrl, { selector: filterSelector01 }).on("change", (change) => {
6206
+ LoggingService.log({ pouch_replication_change: change });
6207
+ }).on("paused", (info) => {
6208
+ LoggingService.log({ pouch_replication_paused: info });
6209
+ }).on("error", (err) => {
6210
+ LoggingService.log({ pouch_replication_error: err });
6211
+ }).on("active", () => {
6212
+ LoggingService.log(`Pouch:::Connection active`);
6213
+ }).on("denied", (err) => {
6214
+ LoggingService.log(`Pouch:::Connection denied`);
6152
6215
  });
6153
6216
  }
6154
6217
  } else if (isRemoteFirst && localSqliteDbFilePath) {
6155
- if (liveSync) {
6218
+ if (syncConfig?.isLiveSync) {
6156
6219
  this._pouchInstance.sync(localSqliteDbFilePath, {
6157
6220
  live: true,
6158
- retry: true
6221
+ retry: true,
6222
+ batch_size: batchSize01,
6223
+ heartbeat: heartbeat01,
6224
+ timeout: heartbeat01 * 2,
6225
+ selector: filterSelector01,
6226
+ back_off_function: (delay) => {
6227
+ if (delay === 0) return 2e3;
6228
+ return Math.min(delay * 2, backoffDelaySeconds01);
6229
+ }
6159
6230
  }).on("change", (change) => {
6160
- LoggingService.log({ replication_change: change });
6231
+ LoggingService.log({ pouch_replication_change: change });
6161
6232
  }).on("paused", (info) => {
6162
- LoggingService.log({ replication_paused: info });
6233
+ LoggingService.log({ pouch_replication_paused: info });
6163
6234
  }).on("error", (err) => {
6164
- LoggingService.log({ replication_error: err });
6235
+ LoggingService.log({ pouch_replication_error: err });
6236
+ }).on("active", () => {
6237
+ LoggingService.log(`Pouch:::Connection active`);
6238
+ }).on("denied", (err) => {
6239
+ LoggingService.log(`Pouch:::Connection denied`);
6165
6240
  });
6166
6241
  } else {
6167
- this._pouchInstance.sync(localSqliteDbFilePath).on("error", (err) => {
6168
- LoggingService.log({ replication_error: err });
6242
+ this._pouchInstance.sync(localSqliteDbFilePath, { selector: filterSelector01 }).on("change", (change) => {
6243
+ LoggingService.log({ pouch_replication_change: change });
6244
+ }).on("paused", (info) => {
6245
+ LoggingService.log({ pouch_replication_paused: info });
6246
+ }).on("error", (err) => {
6247
+ LoggingService.log({ pouch_replication_error: err });
6248
+ }).on("active", () => {
6249
+ LoggingService.log(`Pouch:::Connection active`);
6250
+ }).on("denied", () => {
6251
+ LoggingService.log(`Pouch:::Connection denied`);
6169
6252
  });
6170
6253
  }
6171
6254
  }
package/dist/index.d.cts CHANGED
@@ -735,9 +735,29 @@ type IFindOptions = {
735
735
  };
736
736
  type IOptions$1 = {
737
737
  connType: "LOCAL_FIRST" | "REMOTE_FIRST";
738
- localSqliteDbFilePath?: string;
739
- remoteConnectionUrl?: string;
740
- liveSync?: boolean;
738
+ localSqliteDbConfig?: {
739
+ fileName: string;
740
+ folderFullPath: string;
741
+ fileExt?: ".sqlite" | ".db";
742
+ };
743
+ remoteConnectionConfig?: {
744
+ connectionUrl: string;
745
+ databaseName?: string;
746
+ proxyAuthToken?: string;
747
+ isProxy?: boolean;
748
+ };
749
+ syncConfig?: {
750
+ isLiveSync?: boolean;
751
+ backoffDelayInSeconds?: number;
752
+ heartbeat?: number;
753
+ batchSize?: number;
754
+ filterSelector?: {
755
+ [field: string]: {
756
+ $eq: string;
757
+ };
758
+ };
759
+ isSyncFilterSelectorRequired: boolean;
760
+ };
741
761
  indexes?: {
742
762
  indexName: string;
743
763
  fields: string[];
@@ -796,6 +816,7 @@ declare class MocodyInitializerPouch<T extends IMocodyCoreEntityModel> {
796
816
  }): Promise<PouchDB.Core.Response>;
797
817
  pouch_getDbInstance(): Promise<PouchDB.Database<T>>;
798
818
  private getPouchWithPlugins;
819
+ private createRemoteDB;
799
820
  private pouch_getDbInstanceBase;
800
821
  private toBasicDoc_RemovePouchMeta;
801
822
  private docMetaFieldsObject;
package/dist/index.d.ts CHANGED
@@ -735,9 +735,29 @@ type IFindOptions = {
735
735
  };
736
736
  type IOptions$1 = {
737
737
  connType: "LOCAL_FIRST" | "REMOTE_FIRST";
738
- localSqliteDbFilePath?: string;
739
- remoteConnectionUrl?: string;
740
- liveSync?: boolean;
738
+ localSqliteDbConfig?: {
739
+ fileName: string;
740
+ folderFullPath: string;
741
+ fileExt?: ".sqlite" | ".db";
742
+ };
743
+ remoteConnectionConfig?: {
744
+ connectionUrl: string;
745
+ databaseName?: string;
746
+ proxyAuthToken?: string;
747
+ isProxy?: boolean;
748
+ };
749
+ syncConfig?: {
750
+ isLiveSync?: boolean;
751
+ backoffDelayInSeconds?: number;
752
+ heartbeat?: number;
753
+ batchSize?: number;
754
+ filterSelector?: {
755
+ [field: string]: {
756
+ $eq: string;
757
+ };
758
+ };
759
+ isSyncFilterSelectorRequired: boolean;
760
+ };
741
761
  indexes?: {
742
762
  indexName: string;
743
763
  fields: string[];
@@ -796,6 +816,7 @@ declare class MocodyInitializerPouch<T extends IMocodyCoreEntityModel> {
796
816
  }): Promise<PouchDB.Core.Response>;
797
817
  pouch_getDbInstance(): Promise<PouchDB.Database<T>>;
798
818
  private getPouchWithPlugins;
819
+ private createRemoteDB;
799
820
  private pouch_getDbInstanceBase;
800
821
  private toBasicDoc_RemovePouchMeta;
801
822
  private docMetaFieldsObject;
package/dist/index.js CHANGED
@@ -3720,12 +3720,12 @@ var MocodyInitializerCouch = class {
3720
3720
  this.baseConfig = baseConfig;
3721
3721
  }
3722
3722
  async deleteIndex({ ddoc, name }) {
3723
- const path = ["_index", ddoc, "json", name].join("/");
3723
+ const path2 = ["_index", ddoc, "json", name].join("/");
3724
3724
  const instance = await this.getInstance();
3725
3725
  const result = await instance.relax({
3726
3726
  db: this.baseConfig.couchConfig.databaseName,
3727
3727
  method: "DELETE",
3728
- path,
3728
+ path: path2,
3729
3729
  content_type: "application/json"
3730
3730
  });
3731
3731
  return result;
@@ -5929,6 +5929,7 @@ var MongoDataOperation = class extends RepoModel {
5929
5929
 
5930
5930
  // src/pouch/pouch-initializer.ts
5931
5931
  import throat4 from "throat";
5932
+ import path from "path";
5932
5933
  import PouchDB00 from "pouchdb-core";
5933
5934
  import pouchdbFind from "pouchdb-find";
5934
5935
  import pouchdbhttp from "pouchdb-adapter-http";
@@ -6070,18 +6071,51 @@ var MocodyInitializerPouch = class {
6070
6071
  getPouchWithPlugins() {
6071
6072
  return PouchDB00.plugin(pouchdbhttp).plugin(pouchdbmapreduce).plugin(pouchdbreplication).plugin(pouchdbFind);
6072
6073
  }
6074
+ createRemoteDB({
6075
+ proxyAuthToken,
6076
+ remoteConnectionUrl
6077
+ }) {
6078
+ const PouchDB = this.getPouchWithPlugins();
6079
+ return new PouchDB(remoteConnectionUrl, {
6080
+ fetch: (url, opts) => {
6081
+ const headers = new Headers(opts?.headers);
6082
+ if (proxyAuthToken) {
6083
+ headers.set("Authorization", `Bearer ${proxyAuthToken}`);
6084
+ }
6085
+ return PouchDB.fetch(url, {
6086
+ ...opts,
6087
+ headers,
6088
+ credentials: "omit"
6089
+ });
6090
+ }
6091
+ });
6092
+ }
6073
6093
  async pouch_getDbInstanceBase() {
6074
6094
  if (this._pouchInstance) return this._pouchInstance;
6075
6095
  const {
6076
6096
  //
6077
- liveSync,
6078
6097
  indexes,
6079
6098
  connType,
6080
- localSqliteDbFilePath,
6081
- remoteConnectionUrl
6099
+ localSqliteDbConfig,
6100
+ remoteConnectionConfig,
6101
+ syncConfig
6082
6102
  } = this.baseConfig;
6083
6103
  const isLocalFirst = connType === "LOCAL_FIRST";
6084
6104
  const isRemoteFirst = connType === "REMOTE_FIRST";
6105
+ const remoteConnectionUrl = (() => {
6106
+ const connUrl = remoteConnectionConfig?.connectionUrl;
6107
+ if (!connUrl) return void 0;
6108
+ return connUrl;
6109
+ })();
6110
+ const localSqliteDbFilePath = (() => {
6111
+ const dbName01 = localSqliteDbConfig?.fileName;
6112
+ const fileExtension01 = localSqliteDbConfig?.fileExt || ".sqlite";
6113
+ if (!localSqliteDbConfig?.folderFullPath) return void 0;
6114
+ if (!localSqliteDbConfig?.fileExt) return void 0;
6115
+ if (!dbName01) return void 0;
6116
+ const fileName = `${dbName01}${fileExtension01}`;
6117
+ return path.join(localSqliteDbConfig.folderFullPath, fileName);
6118
+ })();
6085
6119
  if (isLocalFirst) {
6086
6120
  if (!localSqliteDbFilePath) {
6087
6121
  throw new MocodyGenericError("localSqliteDbFilePath not defined");
@@ -6092,8 +6126,10 @@ var MocodyInitializerPouch = class {
6092
6126
  if (!remoteConnectionUrl) {
6093
6127
  throw new MocodyGenericError("couchRemoteConnectionUrl not defined");
6094
6128
  }
6095
- const PouchDB = this.getPouchWithPlugins();
6096
- this._pouchInstance = new PouchDB(remoteConnectionUrl);
6129
+ this._pouchInstance = this.createRemoteDB({
6130
+ remoteConnectionUrl,
6131
+ proxyAuthToken: remoteConnectionConfig?.proxyAuthToken
6132
+ });
6097
6133
  }
6098
6134
  try {
6099
6135
  if (indexes?.length) {
@@ -6104,38 +6140,85 @@ var MocodyInitializerPouch = class {
6104
6140
  } catch (error) {
6105
6141
  LoggingService.error(error);
6106
6142
  }
6143
+ const filterSelector01 = syncConfig?.filterSelector || void 0;
6144
+ const heartbeat01 = syncConfig?.heartbeat || 2e4;
6145
+ const batchSize01 = syncConfig?.batchSize || 100;
6146
+ const backoffDelaySeconds01 = syncConfig?.backoffDelayInSeconds || 4e4;
6147
+ if (syncConfig?.isSyncFilterSelectorRequired && !filterSelector01) {
6148
+ throw new MocodyGenericError("filterSelector not defined");
6149
+ }
6107
6150
  if (isLocalFirst && remoteConnectionUrl) {
6108
- if (liveSync) {
6151
+ if (syncConfig?.isLiveSync) {
6109
6152
  this._pouchInstance.sync(remoteConnectionUrl, {
6110
6153
  live: true,
6111
- retry: true
6154
+ retry: true,
6155
+ batch_size: batchSize01,
6156
+ heartbeat: heartbeat01,
6157
+ timeout: heartbeat01 * 2,
6158
+ selector: filterSelector01,
6159
+ back_off_function: (delay) => {
6160
+ if (delay === 0) return 2e3;
6161
+ return Math.min(delay * 2, backoffDelaySeconds01);
6162
+ }
6112
6163
  }).on("change", (change) => {
6113
- LoggingService.log({ replication_change: change });
6164
+ LoggingService.log({ pouch_replication_change: change });
6114
6165
  }).on("paused", (info) => {
6115
- LoggingService.log({ replication_paused: info });
6166
+ LoggingService.log({ pouch_replication_paused: info });
6116
6167
  }).on("error", (err) => {
6117
- LoggingService.log({ replication_error: err });
6168
+ LoggingService.log({ pouch_replication_error: err });
6169
+ }).on("active", () => {
6170
+ LoggingService.log(`Pouch:::Connection active`);
6171
+ }).on("denied", (err) => {
6172
+ LoggingService.log(`Pouch:::Connection denied`);
6118
6173
  });
6119
6174
  } else {
6120
- this._pouchInstance.sync(remoteConnectionUrl).on("error", (err) => {
6121
- LoggingService.log({ replication_error: err });
6175
+ this._pouchInstance.sync(remoteConnectionUrl, { selector: filterSelector01 }).on("change", (change) => {
6176
+ LoggingService.log({ pouch_replication_change: change });
6177
+ }).on("paused", (info) => {
6178
+ LoggingService.log({ pouch_replication_paused: info });
6179
+ }).on("error", (err) => {
6180
+ LoggingService.log({ pouch_replication_error: err });
6181
+ }).on("active", () => {
6182
+ LoggingService.log(`Pouch:::Connection active`);
6183
+ }).on("denied", (err) => {
6184
+ LoggingService.log(`Pouch:::Connection denied`);
6122
6185
  });
6123
6186
  }
6124
6187
  } else if (isRemoteFirst && localSqliteDbFilePath) {
6125
- if (liveSync) {
6188
+ if (syncConfig?.isLiveSync) {
6126
6189
  this._pouchInstance.sync(localSqliteDbFilePath, {
6127
6190
  live: true,
6128
- retry: true
6191
+ retry: true,
6192
+ batch_size: batchSize01,
6193
+ heartbeat: heartbeat01,
6194
+ timeout: heartbeat01 * 2,
6195
+ selector: filterSelector01,
6196
+ back_off_function: (delay) => {
6197
+ if (delay === 0) return 2e3;
6198
+ return Math.min(delay * 2, backoffDelaySeconds01);
6199
+ }
6129
6200
  }).on("change", (change) => {
6130
- LoggingService.log({ replication_change: change });
6201
+ LoggingService.log({ pouch_replication_change: change });
6131
6202
  }).on("paused", (info) => {
6132
- LoggingService.log({ replication_paused: info });
6203
+ LoggingService.log({ pouch_replication_paused: info });
6133
6204
  }).on("error", (err) => {
6134
- LoggingService.log({ replication_error: err });
6205
+ LoggingService.log({ pouch_replication_error: err });
6206
+ }).on("active", () => {
6207
+ LoggingService.log(`Pouch:::Connection active`);
6208
+ }).on("denied", (err) => {
6209
+ LoggingService.log(`Pouch:::Connection denied`);
6135
6210
  });
6136
6211
  } else {
6137
- this._pouchInstance.sync(localSqliteDbFilePath).on("error", (err) => {
6138
- LoggingService.log({ replication_error: err });
6212
+ this._pouchInstance.sync(localSqliteDbFilePath, { selector: filterSelector01 }).on("change", (change) => {
6213
+ LoggingService.log({ pouch_replication_change: change });
6214
+ }).on("paused", (info) => {
6215
+ LoggingService.log({ pouch_replication_paused: info });
6216
+ }).on("error", (err) => {
6217
+ LoggingService.log({ pouch_replication_error: err });
6218
+ }).on("active", () => {
6219
+ LoggingService.log(`Pouch:::Connection active`);
6220
+ }).on("denied", () => {
6221
+ LoggingService.log(`Pouch:::Connection denied`);
6139
6222
  });
6140
6223
  }
6141
6224
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mocody",
3
- "version": "0.8.2",
3
+ "version": "0.8.3",
4
4
  "description": "Implementation of single table design, and unified query access for MongoDB, CouchDB, and dynamoDB",
5
5
  "main": "./dist/index.cjs",
6
6
  "module": "./dist/index.js",
@@ -55,7 +55,7 @@
55
55
  "dist"
56
56
  ],
57
57
  "engines": {
58
- "node": ">=26"
58
+ "node": ">= 26"
59
59
  },
60
60
  "tsup": {
61
61
  "entry": [