amplifyquery 1.0.0
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 +203 -0
- package/dist/client.d.ts +11 -0
- package/dist/client.js +26 -0
- package/dist/index.d.ts +133 -0
- package/dist/index.js +89 -0
- package/dist/query.d.ts +52 -0
- package/dist/query.js +197 -0
- package/dist/service.d.ts +9 -0
- package/dist/service.js +1236 -0
- package/dist/singleton.d.ts +14 -0
- package/dist/singleton.js +144 -0
- package/dist/store.d.ts +5 -0
- package/dist/store.js +67 -0
- package/dist/types.d.ts +138 -0
- package/dist/types.js +2 -0
- package/dist/utils.d.ts +130 -0
- package/dist/utils.js +437 -0
- package/package.json +53 -0
package/dist/query.js
ADDED
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.queryClient = void 0;
|
|
13
|
+
exports.configure = configure;
|
|
14
|
+
exports.createQueryKeys = createQueryKeys;
|
|
15
|
+
exports.invalidateModel = invalidateModel;
|
|
16
|
+
exports.invalidateModelItem = invalidateModelItem;
|
|
17
|
+
exports.invalidateModelByField = invalidateModelByField;
|
|
18
|
+
exports.invalidateAll = invalidateAll;
|
|
19
|
+
exports.ensureMutationsFlushed = ensureMutationsFlushed;
|
|
20
|
+
const react_native_mmkv_1 = require("react-native-mmkv");
|
|
21
|
+
const react_query_1 = require("@tanstack/react-query");
|
|
22
|
+
const react_query_persist_client_1 = require("@tanstack/react-query-persist-client");
|
|
23
|
+
// Default configuration
|
|
24
|
+
const config = {
|
|
25
|
+
isCachingEnabled: process.env.EXPO_PUBLIC_DISABLE_STORAGE_CACHE !== "true",
|
|
26
|
+
queryClientConfig: {
|
|
27
|
+
defaultOptions: {
|
|
28
|
+
queries: {
|
|
29
|
+
// Default cache settings - increased from defaults
|
|
30
|
+
staleTime: 5 * 60 * 1000, // Data remains "fresh" for 5 minutes
|
|
31
|
+
gcTime: 30 * 60 * 1000, // Inactive query cache kept for 30 minutes (formerly cacheTime)
|
|
32
|
+
retry: 1, // Retry only once on failure
|
|
33
|
+
refetchOnWindowFocus: false, // Disable refetch on window focus (important for mobile)
|
|
34
|
+
refetchOnReconnect: true, // Refetch on network reconnect
|
|
35
|
+
},
|
|
36
|
+
mutations: {
|
|
37
|
+
retry: 1, // Retry only once on failure
|
|
38
|
+
onError: (error) => {
|
|
39
|
+
console.error("Mutation error:", error);
|
|
40
|
+
},
|
|
41
|
+
},
|
|
42
|
+
},
|
|
43
|
+
},
|
|
44
|
+
storage: {
|
|
45
|
+
mmkvId: "mmkv.amplify-query.cache",
|
|
46
|
+
cacheKey: "REACT_QUERY_OFFLINE_CACHE",
|
|
47
|
+
maxAge: 1000 * 60 * 60 * 24 * 7, // 7 days
|
|
48
|
+
},
|
|
49
|
+
};
|
|
50
|
+
// MMKV instance
|
|
51
|
+
let storageInstance; // Renamed from 'storage' to avoid conflict with ConfigOptions.storage
|
|
52
|
+
// Function to create MMKV persister
|
|
53
|
+
function createMmkvPersister() {
|
|
54
|
+
var _a, _b;
|
|
55
|
+
// Initialize or reuse MMKV instance
|
|
56
|
+
if (!storageInstance) {
|
|
57
|
+
storageInstance = new react_native_mmkv_1.MMKV({
|
|
58
|
+
id: ((_a = config.storage) === null || _a === void 0 ? void 0 : _a.mmkvId) || "mmkv.amplify-query.cache",
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
// Check cache key
|
|
62
|
+
const cacheKey = ((_b = config.storage) === null || _b === void 0 ? void 0 : _b.cacheKey) || "REACT_QUERY_OFFLINE_CACHE";
|
|
63
|
+
return {
|
|
64
|
+
persistClient: (client) => {
|
|
65
|
+
try {
|
|
66
|
+
// Convert object to JSON string
|
|
67
|
+
const clientStr = JSON.stringify(client);
|
|
68
|
+
storageInstance.set(cacheKey, clientStr);
|
|
69
|
+
}
|
|
70
|
+
catch (error) {
|
|
71
|
+
console.error("Error saving cache:", error);
|
|
72
|
+
}
|
|
73
|
+
},
|
|
74
|
+
restoreClient: () => {
|
|
75
|
+
try {
|
|
76
|
+
const clientStr = storageInstance.getString(cacheKey);
|
|
77
|
+
if (!clientStr)
|
|
78
|
+
return null;
|
|
79
|
+
// Convert string back to object
|
|
80
|
+
return JSON.parse(clientStr);
|
|
81
|
+
}
|
|
82
|
+
catch (error) {
|
|
83
|
+
console.error("Error restoring cache:", error);
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
},
|
|
87
|
+
removeClient: () => {
|
|
88
|
+
try {
|
|
89
|
+
storageInstance.delete(cacheKey);
|
|
90
|
+
}
|
|
91
|
+
catch (error) {
|
|
92
|
+
console.error("Error removing cache:", error);
|
|
93
|
+
}
|
|
94
|
+
},
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
/**
|
|
98
|
+
* TanStack Query client
|
|
99
|
+
*/
|
|
100
|
+
exports.queryClient = new react_query_1.QueryClient(config.queryClientConfig);
|
|
101
|
+
/**
|
|
102
|
+
* AmplifyQuery configuration
|
|
103
|
+
* @param options Configuration options
|
|
104
|
+
*/
|
|
105
|
+
function configure(options = {}) {
|
|
106
|
+
var _a;
|
|
107
|
+
// Backup previous config
|
|
108
|
+
const prevConfig = Object.assign({}, config);
|
|
109
|
+
// Apply new config
|
|
110
|
+
Object.assign(config, options);
|
|
111
|
+
// Recreate client if QueryClient config changed
|
|
112
|
+
if (options.queryClientConfig &&
|
|
113
|
+
JSON.stringify(options.queryClientConfig) !==
|
|
114
|
+
JSON.stringify(prevConfig.queryClientConfig)) {
|
|
115
|
+
exports.queryClient = new react_query_1.QueryClient(config.queryClientConfig);
|
|
116
|
+
}
|
|
117
|
+
// Apply caching config
|
|
118
|
+
if (config.isCachingEnabled) {
|
|
119
|
+
console.log("🏃♀️ React Query offline cache is enabled with MMKV.");
|
|
120
|
+
// Create new persister if config changed
|
|
121
|
+
const mmkvPersister = createMmkvPersister();
|
|
122
|
+
(0, react_query_persist_client_1.persistQueryClient)({
|
|
123
|
+
queryClient: exports.queryClient,
|
|
124
|
+
persister: mmkvPersister,
|
|
125
|
+
// Additional options
|
|
126
|
+
maxAge: ((_a = config.storage) === null || _a === void 0 ? void 0 : _a.maxAge) || 1000 * 60 * 60 * 24 * 7, // Default 7 days
|
|
127
|
+
dehydrateOptions: {
|
|
128
|
+
shouldDehydrateQuery: (query) => {
|
|
129
|
+
// Filter for queries not to cache (implement if needed)
|
|
130
|
+
return true;
|
|
131
|
+
},
|
|
132
|
+
},
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
else {
|
|
136
|
+
console.log("🏃♀️ React Query offline cache is disabled via flag.");
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Create query keys from model names
|
|
141
|
+
* @param modelNames Array of model names
|
|
142
|
+
* @returns Object of query keys per model
|
|
143
|
+
*/
|
|
144
|
+
function createQueryKeys(modelNames) {
|
|
145
|
+
const queryKeys = {};
|
|
146
|
+
modelNames.forEach((modelName) => {
|
|
147
|
+
queryKeys[modelName] = [modelName];
|
|
148
|
+
});
|
|
149
|
+
return queryKeys;
|
|
150
|
+
}
|
|
151
|
+
/**
|
|
152
|
+
* Invalidate all queries for a model
|
|
153
|
+
* @param modelName Model name
|
|
154
|
+
*/
|
|
155
|
+
function invalidateModel(modelName) {
|
|
156
|
+
exports.queryClient.invalidateQueries({ queryKey: [modelName] });
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* Invalidate a model item with a specific ID
|
|
160
|
+
* @param modelName Model name
|
|
161
|
+
* @param id Item ID
|
|
162
|
+
*/
|
|
163
|
+
function invalidateModelItem(modelName, id) {
|
|
164
|
+
exports.queryClient.invalidateQueries({ queryKey: [modelName, id] });
|
|
165
|
+
}
|
|
166
|
+
/**
|
|
167
|
+
* Invalidate model items with a specific field value
|
|
168
|
+
* @param modelName Model name
|
|
169
|
+
* @param field Field name
|
|
170
|
+
* @param value Field value
|
|
171
|
+
*/
|
|
172
|
+
function invalidateModelByField(modelName, field, value) {
|
|
173
|
+
exports.queryClient.invalidateQueries({ queryKey: [modelName, "by", field, value] });
|
|
174
|
+
}
|
|
175
|
+
/**
|
|
176
|
+
* Invalidate all query caches (full app reset)
|
|
177
|
+
*/
|
|
178
|
+
function invalidateAll() {
|
|
179
|
+
exports.queryClient.invalidateQueries();
|
|
180
|
+
}
|
|
181
|
+
/**
|
|
182
|
+
* Ensure important changes are synced to server before app closes
|
|
183
|
+
*/
|
|
184
|
+
function ensureMutationsFlushed() {
|
|
185
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
186
|
+
return exports.queryClient.isMutating()
|
|
187
|
+
? new Promise((resolve) => {
|
|
188
|
+
const unsubscribe = exports.queryClient.getMutationCache().subscribe(() => {
|
|
189
|
+
if (!exports.queryClient.isMutating()) {
|
|
190
|
+
unsubscribe();
|
|
191
|
+
resolve(true);
|
|
192
|
+
}
|
|
193
|
+
});
|
|
194
|
+
})
|
|
195
|
+
: Promise.resolve(true);
|
|
196
|
+
});
|
|
197
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { AmplifyDataService, AuthMode, BaseModel } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Create model-specific Amplify service
|
|
4
|
+
* @param modelName Model name
|
|
5
|
+
* @param queryMap Model-specific query name mapping (optional)
|
|
6
|
+
* @param defaultAuthMode Default authentication mode (default: 'userPool')
|
|
7
|
+
* @returns AmplifyDataService instance for the model
|
|
8
|
+
*/
|
|
9
|
+
export declare function createAmplifyService<T extends BaseModel>(modelName: string, queryMap?: Record<string, string>, defaultAuthMode?: AuthMode): AmplifyDataService<T>;
|