amplifyquery 1.0.20 → 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 +11 -0
- package/dist/config.d.ts +8 -1
- package/dist/config.js +36 -6
- package/dist/index.d.ts +0 -62
- package/dist/index.js +6 -4
- package/dist/query.js +60 -29
- package/dist/service.js +336 -112
- package/dist/singleton.js +85 -95
- package/dist/types.d.ts +23 -0
- package/dist/utils.d.ts +0 -72
- package/dist/utils.js +20 -262
- package/package.json +14 -17
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
|
|
@@ -36,7 +40,10 @@ export declare function getDefaultAuthMode(): "apiKey" | "iam" | "identityPool"
|
|
|
36
40
|
*/
|
|
37
41
|
export declare function resetConfig(): void;
|
|
38
42
|
/**
|
|
39
|
-
* Enable/disable singleton auto-create behavior for
|
|
43
|
+
* Enable/disable singleton auto-create behavior for singleton hooks.
|
|
44
|
+
*
|
|
45
|
+
* - **New**: `useSigletoneHook()` (recommended)
|
|
46
|
+
* - **Legacy**: `useCurrentHook()` (deprecated alias)
|
|
40
47
|
*/
|
|
41
48
|
export declare function setSingletonAutoCreate(config: {
|
|
42
49
|
enabled?: boolean;
|
package/dist/config.js
CHANGED
|
@@ -3,6 +3,10 @@
|
|
|
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;
|
|
@@ -14,13 +18,35 @@ exports.getSingletonAutoCreate = getSingletonAutoCreate;
|
|
|
14
18
|
exports.isSingletonAutoCreateEnabledForModel = isSingletonAutoCreateEnabledForModel;
|
|
15
19
|
// Global configuration state
|
|
16
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
|
+
}
|
|
17
43
|
/**
|
|
18
44
|
* Set the global model owner query mapping
|
|
19
45
|
* @param queryMap Mapping of model names to their owner query names
|
|
20
46
|
*/
|
|
21
47
|
function setModelOwnerQueryMap(queryMap) {
|
|
22
48
|
globalConfig.modelOwnerQueryMap = Object.assign({}, queryMap);
|
|
23
|
-
|
|
49
|
+
debugLog("🔧 AmplifyQuery: Global model owner query map configured");
|
|
24
50
|
}
|
|
25
51
|
/**
|
|
26
52
|
* Get the global model owner query mapping
|
|
@@ -48,7 +74,7 @@ function getOwnerQueryName(modelName) {
|
|
|
48
74
|
*/
|
|
49
75
|
function setDefaultAuthMode(authMode) {
|
|
50
76
|
globalConfig.defaultAuthMode = authMode;
|
|
51
|
-
|
|
77
|
+
debugLog(`🔧 AmplifyQuery: Default auth mode set to ${authMode}`);
|
|
52
78
|
}
|
|
53
79
|
/**
|
|
54
80
|
* Get the default auth mode
|
|
@@ -62,17 +88,21 @@ function getDefaultAuthMode() {
|
|
|
62
88
|
*/
|
|
63
89
|
function resetConfig() {
|
|
64
90
|
globalConfig = {};
|
|
65
|
-
|
|
91
|
+
debugLog("🔧 AmplifyQuery: Global configuration reset");
|
|
66
92
|
}
|
|
67
93
|
/**
|
|
68
|
-
* Enable/disable singleton auto-create behavior for
|
|
94
|
+
* Enable/disable singleton auto-create behavior for singleton hooks.
|
|
95
|
+
*
|
|
96
|
+
* - **New**: `useSigletoneHook()` (recommended)
|
|
97
|
+
* - **Legacy**: `useCurrentHook()` (deprecated alias)
|
|
69
98
|
*/
|
|
70
99
|
function setSingletonAutoCreate(config) {
|
|
71
100
|
globalConfig.singletonAutoCreate = {
|
|
72
|
-
|
|
101
|
+
// Default: enabled (singleton services are meant to be convenient)
|
|
102
|
+
enabled: config.enabled !== false,
|
|
73
103
|
models: Array.isArray(config.models) ? config.models : undefined,
|
|
74
104
|
};
|
|
75
|
-
|
|
105
|
+
debugLog("🔧 AmplifyQuery: Singleton auto-create configured", globalConfig.singletonAutoCreate);
|
|
76
106
|
}
|
|
77
107
|
function getSingletonAutoCreate() {
|
|
78
108
|
return globalConfig.singletonAutoCreate;
|
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.
|
|
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);
|
|
@@ -79,7 +83,7 @@ function configure(config) {
|
|
|
79
83
|
queryClientConfig: config.queryClientConfig,
|
|
80
84
|
storage: config.storage,
|
|
81
85
|
});
|
|
82
|
-
|
|
86
|
+
(0, config_1.debugLog)("🔌 AmplifyQuery initialized successfully.");
|
|
83
87
|
}
|
|
84
88
|
// Re-export types
|
|
85
89
|
__exportStar(require("./types"), exports);
|
|
@@ -87,7 +91,6 @@ __exportStar(require("./types"), exports);
|
|
|
87
91
|
__exportStar(require("./provider"), exports);
|
|
88
92
|
// Re-export utility services
|
|
89
93
|
exports.Utils = utils_1.Utils;
|
|
90
|
-
exports.Storage = utils_1.StorageService;
|
|
91
94
|
exports.Auth = utils_1.AuthService;
|
|
92
95
|
/**
|
|
93
96
|
* Main object for the AmplifyQuery library.
|
|
@@ -99,7 +102,6 @@ exports.AmplifyQuery = {
|
|
|
99
102
|
createSingletonService: singleton_1.createSingletonService,
|
|
100
103
|
createRelationalHook: utils_1.createRelationalHook,
|
|
101
104
|
Utils: utils_1.Utils,
|
|
102
|
-
Storage: utils_1.StorageService,
|
|
103
105
|
Auth: utils_1.AuthService,
|
|
104
106
|
getModelIds: singleton_1.getModelIds,
|
|
105
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
|
|
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
|
-
//
|
|
52
|
-
|
|
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
|
-
|
|
58
|
-
|
|
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
|
-
|
|
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 =
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
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
|
-
|
|
179
|
+
(0, config_1.debugLog)("🏃♀️ React Query offline cache is disabled via flag.");
|
|
149
180
|
}
|
|
150
181
|
}
|
|
151
182
|
/**
|