amplifyquery 1.0.19 → 1.0.21

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/README.md CHANGED
@@ -159,6 +159,17 @@ const UserSettingsService =
159
159
  AmplifyQuery.createAmplifyService<UserSettingsModel>("UserSettings"),
160
160
  AmplifyQuery.getModelIds.UserSettings
161
161
  );
162
+
163
+ // Singleton hook (recommended)
164
+ const {
165
+ item: settings,
166
+ isLoading: isSettingsLoading,
167
+ update: upsertSettings, // creates if missing
168
+ refresh: refreshSettings,
169
+ } = UserSettingsService.useSigletoneHook();
170
+
171
+ // Legacy alias (deprecated)
172
+ // UserSettingsService.useCurrentHook()
162
173
  ```
163
174
 
164
175
  ### 4. Data Fetching and Saving
package/dist/config.d.ts CHANGED
@@ -1,6 +1,10 @@
1
1
  /**
2
2
  * Global configuration for AmplifyQuery library
3
3
  */
4
+ export declare function setDebug(debug: boolean): void;
5
+ export declare function isDebugEnabled(): boolean;
6
+ export declare function debugLog(...args: any[]): void;
7
+ export declare function debugWarn(...args: any[]): void;
4
8
  /**
5
9
  * Set the global model owner query mapping
6
10
  * @param queryMap Mapping of model names to their owner query names
@@ -35,3 +39,18 @@ export declare function getDefaultAuthMode(): "apiKey" | "iam" | "identityPool"
35
39
  * Reset global configuration (mainly for testing)
36
40
  */
37
41
  export declare function resetConfig(): void;
42
+ /**
43
+ * Enable/disable singleton auto-create behavior for singleton hooks.
44
+ *
45
+ * - **New**: `useSigletoneHook()` (recommended)
46
+ * - **Legacy**: `useCurrentHook()` (deprecated alias)
47
+ */
48
+ export declare function setSingletonAutoCreate(config: {
49
+ enabled?: boolean;
50
+ models?: string[];
51
+ }): void;
52
+ export declare function getSingletonAutoCreate(): {
53
+ enabled?: boolean;
54
+ models?: string[];
55
+ } | undefined;
56
+ export declare function isSingletonAutoCreateEnabledForModel(modelName: string): boolean;
package/dist/config.js CHANGED
@@ -3,21 +3,50 @@
3
3
  * Global configuration for AmplifyQuery library
4
4
  */
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.setDebug = setDebug;
7
+ exports.isDebugEnabled = isDebugEnabled;
8
+ exports.debugLog = debugLog;
9
+ exports.debugWarn = debugWarn;
6
10
  exports.setModelOwnerQueryMap = setModelOwnerQueryMap;
7
11
  exports.getModelOwnerQueryMap = getModelOwnerQueryMap;
8
12
  exports.getOwnerQueryName = getOwnerQueryName;
9
13
  exports.setDefaultAuthMode = setDefaultAuthMode;
10
14
  exports.getDefaultAuthMode = getDefaultAuthMode;
11
15
  exports.resetConfig = resetConfig;
16
+ exports.setSingletonAutoCreate = setSingletonAutoCreate;
17
+ exports.getSingletonAutoCreate = getSingletonAutoCreate;
18
+ exports.isSingletonAutoCreateEnabledForModel = isSingletonAutoCreateEnabledForModel;
12
19
  // Global configuration state
13
20
  let globalConfig = {};
21
+ function isDebugEnabledInternal() {
22
+ // Debug logging must be explicitly enabled via AmplifyQuery.configure({ debug: true }).
23
+ return globalConfig.debug === true;
24
+ }
25
+ function setDebug(debug) {
26
+ globalConfig.debug = debug === true;
27
+ }
28
+ function isDebugEnabled() {
29
+ return isDebugEnabledInternal();
30
+ }
31
+ function debugLog(...args) {
32
+ if (!isDebugEnabledInternal())
33
+ return;
34
+ // eslint-disable-next-line no-console
35
+ console.log(...args);
36
+ }
37
+ function debugWarn(...args) {
38
+ if (!isDebugEnabledInternal())
39
+ return;
40
+ // eslint-disable-next-line no-console
41
+ console.warn(...args);
42
+ }
14
43
  /**
15
44
  * Set the global model owner query mapping
16
45
  * @param queryMap Mapping of model names to their owner query names
17
46
  */
18
47
  function setModelOwnerQueryMap(queryMap) {
19
48
  globalConfig.modelOwnerQueryMap = Object.assign({}, queryMap);
20
- console.log("🔧 AmplifyQuery: Global model owner query map configured");
49
+ debugLog("🔧 AmplifyQuery: Global model owner query map configured");
21
50
  }
22
51
  /**
23
52
  * Get the global model owner query mapping
@@ -45,7 +74,7 @@ function getOwnerQueryName(modelName) {
45
74
  */
46
75
  function setDefaultAuthMode(authMode) {
47
76
  globalConfig.defaultAuthMode = authMode;
48
- console.log(`🔧 AmplifyQuery: Default auth mode set to ${authMode}`);
77
+ debugLog(`🔧 AmplifyQuery: Default auth mode set to ${authMode}`);
49
78
  }
50
79
  /**
51
80
  * Get the default auth mode
@@ -59,5 +88,30 @@ function getDefaultAuthMode() {
59
88
  */
60
89
  function resetConfig() {
61
90
  globalConfig = {};
62
- console.log("🔧 AmplifyQuery: Global configuration reset");
91
+ debugLog("🔧 AmplifyQuery: Global configuration reset");
92
+ }
93
+ /**
94
+ * Enable/disable singleton auto-create behavior for singleton hooks.
95
+ *
96
+ * - **New**: `useSigletoneHook()` (recommended)
97
+ * - **Legacy**: `useCurrentHook()` (deprecated alias)
98
+ */
99
+ function setSingletonAutoCreate(config) {
100
+ globalConfig.singletonAutoCreate = {
101
+ // Default: enabled (singleton services are meant to be convenient)
102
+ enabled: config.enabled !== false,
103
+ models: Array.isArray(config.models) ? config.models : undefined,
104
+ };
105
+ debugLog("🔧 AmplifyQuery: Singleton auto-create configured", globalConfig.singletonAutoCreate);
106
+ }
107
+ function getSingletonAutoCreate() {
108
+ return globalConfig.singletonAutoCreate;
109
+ }
110
+ function isSingletonAutoCreateEnabledForModel(modelName) {
111
+ const cfg = getSingletonAutoCreate();
112
+ if (!(cfg === null || cfg === void 0 ? void 0 : cfg.enabled))
113
+ return false;
114
+ if (!cfg.models || cfg.models.length === 0)
115
+ return true;
116
+ return cfg.models.includes(modelName);
63
117
  }
package/dist/index.d.ts CHANGED
@@ -39,37 +39,6 @@ export declare const Utils: {
39
39
  getDateString: (dateStr: string | undefined) => string;
40
40
  removeOwnerField: <T extends Record<string, any>>(data: T, operation: "create" | "update" | "upsert") => Omit<T, "owner">;
41
41
  };
42
- export declare const Storage: {
43
- _types: {
44
- Item: {
45
- url: string;
46
- expiresAt: number;
47
- };
48
- CacheData: {
49
- [key: string]: {
50
- url: string;
51
- expiresAt: number;
52
- };
53
- };
54
- };
55
- _urlCache: Map<string, {
56
- url: string;
57
- expiresAt: number;
58
- }>;
59
- _CACHE_KEY: string;
60
- _initialized: boolean;
61
- _initCache: () => void;
62
- _saveCache: () => void;
63
- uploadImage: (file: Blob | File, key?: string) => Promise<string>;
64
- getFileUrl: (key: string, options?: {
65
- forceRefresh?: boolean;
66
- }) => Promise<string>;
67
- deleteFile: (key: string) => Promise<void>;
68
- clearUrlCache: () => void;
69
- clearUrlCacheForKey: (key: string) => void;
70
- clearExpiredUrlCache: () => void;
71
- downloadAudioFile: (audioKey: string) => Promise<string>;
72
- };
73
42
  export declare const Auth: {
74
43
  getCurrentUserInfo: () => Promise<{
75
44
  userId: string;
@@ -92,37 +61,6 @@ export declare const AmplifyQuery: {
92
61
  getDateString: (dateStr: string | undefined) => string;
93
62
  removeOwnerField: <T extends Record<string, any>>(data: T, operation: "create" | "update" | "upsert") => Omit<T, "owner">;
94
63
  };
95
- Storage: {
96
- _types: {
97
- Item: {
98
- url: string;
99
- expiresAt: number;
100
- };
101
- CacheData: {
102
- [key: string]: {
103
- url: string;
104
- expiresAt: number;
105
- };
106
- };
107
- };
108
- _urlCache: Map<string, {
109
- url: string;
110
- expiresAt: number;
111
- }>;
112
- _CACHE_KEY: string;
113
- _initialized: boolean;
114
- _initCache: () => void;
115
- _saveCache: () => void;
116
- uploadImage: (file: Blob | File, key?: string) => Promise<string>;
117
- getFileUrl: (key: string, options?: {
118
- forceRefresh?: boolean;
119
- }) => Promise<string>;
120
- deleteFile: (key: string) => Promise<void>;
121
- clearUrlCache: () => void;
122
- clearUrlCacheForKey: (key: string) => void;
123
- clearExpiredUrlCache: () => void;
124
- downloadAudioFile: (audioKey: string) => Promise<string>;
125
- };
126
64
  Auth: {
127
65
  getCurrentUserInfo: () => Promise<{
128
66
  userId: string;
package/dist/index.js CHANGED
@@ -14,7 +14,7 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
14
  for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
15
  };
16
16
  Object.defineProperty(exports, "__esModule", { value: true });
17
- exports.AmplifyQuery = exports.getAppUrl = exports.setAppUrl = exports.Auth = exports.Storage = exports.Utils = exports.getClient = exports.createQueryKeys = exports.ensureMutationsFlushed = exports.invalidateAll = exports.invalidateModelByField = exports.invalidateModelItem = exports.invalidateModel = exports.getQueryClient = exports.queryClient = void 0;
17
+ exports.AmplifyQuery = exports.getAppUrl = exports.setAppUrl = exports.Auth = exports.Utils = exports.getClient = exports.createQueryKeys = exports.ensureMutationsFlushed = exports.invalidateAll = exports.invalidateModelByField = exports.invalidateModelItem = exports.invalidateModel = exports.getQueryClient = exports.queryClient = void 0;
18
18
  exports.configure = configure;
19
19
  const client_1 = require("./client");
20
20
  Object.defineProperty(exports, "getClient", { enumerable: true, get: function () { return client_1.getClient; } });
@@ -57,6 +57,10 @@ const config_1 = require("./config");
57
57
  * ```
58
58
  */
59
59
  function configure(config) {
60
+ // Debug flag first (affects internal logs)
61
+ if (typeof config.debug === "boolean") {
62
+ (0, config_1.setDebug)(config.debug);
63
+ }
60
64
  // Configure Amplify client
61
65
  if (config.client) {
62
66
  (0, client_1.setClient)(config.client);
@@ -69,13 +73,17 @@ function configure(config) {
69
73
  if (config.defaultAuthMode) {
70
74
  (0, config_1.setDefaultAuthMode)(config.defaultAuthMode);
71
75
  }
76
+ // Set singleton auto-create
77
+ if (config.singletonAutoCreate) {
78
+ (0, config_1.setSingletonAutoCreate)(config.singletonAutoCreate);
79
+ }
72
80
  // Apply React Query settings
73
81
  (0, query_1.configure)({
74
82
  isCachingEnabled: config.isCachingEnabled,
75
83
  queryClientConfig: config.queryClientConfig,
76
84
  storage: config.storage,
77
85
  });
78
- console.log("🔌 AmplifyQuery initialized successfully.");
86
+ (0, config_1.debugLog)("🔌 AmplifyQuery initialized successfully.");
79
87
  }
80
88
  // Re-export types
81
89
  __exportStar(require("./types"), exports);
@@ -83,7 +91,6 @@ __exportStar(require("./types"), exports);
83
91
  __exportStar(require("./provider"), exports);
84
92
  // Re-export utility services
85
93
  exports.Utils = utils_1.Utils;
86
- exports.Storage = utils_1.StorageService;
87
94
  exports.Auth = utils_1.AuthService;
88
95
  /**
89
96
  * Main object for the AmplifyQuery library.
@@ -95,7 +102,6 @@ exports.AmplifyQuery = {
95
102
  createSingletonService: singleton_1.createSingletonService,
96
103
  createRelationalHook: utils_1.createRelationalHook,
97
104
  Utils: utils_1.Utils,
98
- Storage: utils_1.StorageService,
99
105
  Auth: utils_1.AuthService,
100
106
  getModelIds: singleton_1.getModelIds,
101
107
  getQueryClient: query_1.getQueryClient,
package/dist/query.js CHANGED
@@ -18,9 +18,8 @@ exports.invalidateModelItem = invalidateModelItem;
18
18
  exports.invalidateModelByField = invalidateModelByField;
19
19
  exports.invalidateAll = invalidateAll;
20
20
  exports.ensureMutationsFlushed = ensureMutationsFlushed;
21
- const react_native_mmkv_1 = require("react-native-mmkv");
22
21
  const react_query_1 = require("@tanstack/react-query");
23
- const react_query_persist_client_1 = require("@tanstack/react-query-persist-client");
22
+ const config_1 = require("./config");
24
23
  // Default configuration
25
24
  const config = {
26
25
  isCachingEnabled: process.env.EXPO_PUBLIC_DISABLE_STORAGE_CACHE !== "true",
@@ -48,17 +47,31 @@ const config = {
48
47
  maxAge: 1000 * 60 * 60 * 24 * 7, // 7 days
49
48
  },
50
49
  };
51
- // MMKV instance
52
- let storageInstance; // Renamed from 'storage' to avoid conflict with ConfigOptions.storage
50
+ let storageInstance = null; // Renamed from 'storage' to avoid conflict with ConfigOptions.storage
51
+ function getOrCreateMmkv(id) {
52
+ if (storageInstance)
53
+ return storageInstance;
54
+ // Support both react-native-mmkv v3 (class MMKV) and v4 (createMMKV factory)
55
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
56
+ const mmkv = require("react-native-mmkv");
57
+ if (typeof mmkv.createMMKV === "function") {
58
+ const created = mmkv.createMMKV({ id });
59
+ storageInstance = created;
60
+ return created;
61
+ }
62
+ if (typeof mmkv.MMKV === "function") {
63
+ const created = new mmkv.MMKV({ id });
64
+ storageInstance = created;
65
+ return created;
66
+ }
67
+ throw new Error("react-native-mmkv is not available in this runtime");
68
+ }
53
69
  // Function to create MMKV persister
54
70
  function createMmkvPersister() {
55
71
  var _a, _b;
56
72
  // Initialize or reuse MMKV instance
57
- if (!storageInstance) {
58
- storageInstance = new react_native_mmkv_1.MMKV({
59
- id: ((_a = config.storage) === null || _a === void 0 ? void 0 : _a.mmkvId) || "mmkv.amplify-query.cache",
60
- });
61
- }
73
+ const mmkvId = ((_a = config.storage) === null || _a === void 0 ? void 0 : _a.mmkvId) || "mmkv.amplify-query.cache";
74
+ const storage = getOrCreateMmkv(mmkvId);
62
75
  // Check cache key
63
76
  const cacheKey = ((_b = config.storage) === null || _b === void 0 ? void 0 : _b.cacheKey) || "REACT_QUERY_OFFLINE_CACHE";
64
77
  return {
@@ -66,7 +79,7 @@ function createMmkvPersister() {
66
79
  try {
67
80
  // Convert object to JSON string
68
81
  const clientStr = JSON.stringify(client);
69
- storageInstance.set(cacheKey, clientStr);
82
+ storage.set(cacheKey, clientStr);
70
83
  }
71
84
  catch (error) {
72
85
  console.error("Error saving cache:", error);
@@ -74,7 +87,7 @@ function createMmkvPersister() {
74
87
  },
75
88
  restoreClient: () => {
76
89
  try {
77
- const clientStr = storageInstance.getString(cacheKey);
90
+ const clientStr = storage.getString(cacheKey);
78
91
  if (!clientStr)
79
92
  return null;
80
93
  // Convert string back to object
@@ -87,7 +100,17 @@ function createMmkvPersister() {
87
100
  },
88
101
  removeClient: () => {
89
102
  try {
90
- storageInstance.delete(cacheKey);
103
+ // v3 uses delete(), v4 uses remove()
104
+ if (typeof storage.remove === "function") {
105
+ storage.remove(cacheKey);
106
+ }
107
+ else if (typeof storage.delete === "function") {
108
+ storage.delete(cacheKey);
109
+ }
110
+ else if (typeof storage.clearAll === "function") {
111
+ // As a last resort, clear all
112
+ storage.clearAll();
113
+ }
91
114
  }
92
115
  catch (error) {
93
116
  console.error("Error removing cache:", error);
@@ -124,28 +147,36 @@ function configure(options = {}) {
124
147
  }
125
148
  // Apply caching config
126
149
  if (config.isCachingEnabled) {
127
- console.log("🏃‍♀️ React Query offline cache is enabled with MMKV.");
150
+ (0, config_1.debugLog)("🏃‍♀️ React Query offline cache is enabled with MMKV.");
128
151
  // Create new persister if config changed
129
152
  const mmkvPersister = createMmkvPersister();
130
- (0, react_query_persist_client_1.persistQueryClient)({
131
- queryClient: exports.queryClient,
132
- persister: mmkvPersister,
133
- // Additional options
134
- maxAge: ((_a = config.storage) === null || _a === void 0 ? void 0 : _a.maxAge) || 1000 * 60 * 60 * 24 * 7, // Default 7 days
135
- dehydrateOptions: {
136
- shouldDehydrateQuery: (query) => {
137
- var _a;
138
- // Only persist successful queries to reduce hydration cancellation noise
139
- if (((_a = query.state) === null || _a === void 0 ? void 0 : _a.status) !== "success")
140
- return false;
141
- // Avoid persisting mutation-like or transient keys if needed later
142
- return true;
153
+ try {
154
+ // Lazy-load persistQueryClient to avoid hard dependency resolution at build time.
155
+ // eslint-disable-next-line @typescript-eslint/no-var-requires
156
+ const { persistQueryClient } = require("@tanstack/react-query-persist-client");
157
+ persistQueryClient({
158
+ queryClient: exports.queryClient,
159
+ persister: mmkvPersister,
160
+ // Additional options
161
+ maxAge: ((_a = config.storage) === null || _a === void 0 ? void 0 : _a.maxAge) || 1000 * 60 * 60 * 24 * 7, // Default 7 days
162
+ dehydrateOptions: {
163
+ shouldDehydrateQuery: (query) => {
164
+ var _a;
165
+ // Only persist successful queries to reduce hydration cancellation noise
166
+ if (((_a = query.state) === null || _a === void 0 ? void 0 : _a.status) !== "success")
167
+ return false;
168
+ // Avoid persisting mutation-like or transient keys if needed later
169
+ return true;
170
+ },
143
171
  },
144
- },
145
- });
172
+ });
173
+ }
174
+ catch (e) {
175
+ console.error("Error setting up React Query persistence:", e);
176
+ }
146
177
  }
147
178
  else {
148
- console.log("🏃‍♀️ React Query offline cache is disabled via flag.");
179
+ (0, config_1.debugLog)("🏃‍♀️ React Query offline cache is disabled via flag.");
149
180
  }
150
181
  }
151
182
  /**