amplifyquery 1.0.2 → 1.0.4
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 +122 -6
- package/dist/config.d.ts +33 -0
- package/dist/config.js +59 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.js +15 -0
- package/dist/service.d.ts +2 -3
- package/dist/service.js +7 -7
- package/dist/types.d.ts +1 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -9,6 +9,7 @@ A library that combines AWS Amplify and React Query, making it easier to manage
|
|
|
9
9
|
- 📱 **Offline Support**: Persistent query caching via MMKV for fast data loading even offline.
|
|
10
10
|
- 🪝 **Convenient Hooks API**: Abstract complex data synchronization into simple Hooks.
|
|
11
11
|
- 🛡 **Auth Mode Support**: Supports various AWS Amplify authentication modes (API Key, IAM, Cognito, etc.).
|
|
12
|
+
- ⚙️ **Global Configuration**: Set model mappings and auth modes once - no more repetitive configuration.
|
|
12
13
|
- ⚡ **Performance Optimized**: Maximize performance with request batching and intelligent caching.
|
|
13
14
|
|
|
14
15
|
## Installation
|
|
@@ -31,8 +32,23 @@ import { generateClient } from "aws-amplify/api";
|
|
|
31
32
|
const client = generateClient();
|
|
32
33
|
AmplifyQuery.configure({
|
|
33
34
|
client,
|
|
35
|
+
|
|
36
|
+
// Global model owner query mapping (optional)
|
|
37
|
+
// Set once and all services will use it automatically
|
|
38
|
+
modelOwnerQueryMap: {
|
|
39
|
+
User: "listUserByOwner",
|
|
40
|
+
Project: "listProjectByOwner",
|
|
41
|
+
Todo: "listTodoByOwner",
|
|
42
|
+
Comment: "listCommentByOwner",
|
|
43
|
+
// Add your model mappings here
|
|
44
|
+
},
|
|
45
|
+
|
|
46
|
+
// Default authentication mode (optional)
|
|
47
|
+
defaultAuthMode: "userPool",
|
|
48
|
+
|
|
34
49
|
// Caching options (optional)
|
|
35
50
|
isCachingEnabled: true,
|
|
51
|
+
|
|
36
52
|
// Customize Query Client configuration (optional)
|
|
37
53
|
queryClientConfig: {
|
|
38
54
|
defaultOptions: {
|
|
@@ -41,6 +57,7 @@ AmplifyQuery.configure({
|
|
|
41
57
|
},
|
|
42
58
|
},
|
|
43
59
|
},
|
|
60
|
+
|
|
44
61
|
// Storage configuration (optional)
|
|
45
62
|
storage: {
|
|
46
63
|
mmkvId: "my-app.cache", // MMKV store ID
|
|
@@ -125,12 +142,21 @@ interface TodoModel {
|
|
|
125
142
|
updatedAt: string;
|
|
126
143
|
}
|
|
127
144
|
|
|
128
|
-
// Create Todo service
|
|
145
|
+
// Create Todo service - automatically uses global modelOwnerQueryMap
|
|
129
146
|
const TodoService = AmplifyQuery.createAmplifyService<TodoModel>("Todo");
|
|
130
147
|
|
|
131
|
-
// Create
|
|
148
|
+
// Create service with custom auth mode (optional)
|
|
149
|
+
const AdminTodoService = AmplifyQuery.createAmplifyService<TodoModel>(
|
|
150
|
+
"Todo",
|
|
151
|
+
"iam"
|
|
152
|
+
);
|
|
153
|
+
|
|
154
|
+
// Create Singleton service (for single-instance models like user settings)
|
|
132
155
|
const UserSettingsService =
|
|
133
|
-
AmplifyQuery.createSingletonService<UserSettingsModel>(
|
|
156
|
+
AmplifyQuery.createSingletonService<UserSettingsModel>(
|
|
157
|
+
AmplifyQuery.createAmplifyService<UserSettingsModel>("UserSettings"),
|
|
158
|
+
AmplifyQuery.getModelIds.UserSettings
|
|
159
|
+
);
|
|
134
160
|
```
|
|
135
161
|
|
|
136
162
|
### 4. Data Fetching and Saving
|
|
@@ -213,6 +239,84 @@ function TodoScreen() {
|
|
|
213
239
|
|
|
214
240
|
## Advanced Features
|
|
215
241
|
|
|
242
|
+
### Global Configuration
|
|
243
|
+
|
|
244
|
+
AmplifyQuery supports global configuration to reduce code duplication and simplify service creation.
|
|
245
|
+
|
|
246
|
+
```typescript
|
|
247
|
+
// Set global model owner query mapping (can be done separately from configure)
|
|
248
|
+
AmplifyQuery.setModelOwnerQueryMap({
|
|
249
|
+
User: "listUserByOwner",
|
|
250
|
+
Project: "listProjectByOwner",
|
|
251
|
+
Todo: "listTodoByOwner",
|
|
252
|
+
Comment: "listCommentByOwner",
|
|
253
|
+
Like: "listLikeByOwner",
|
|
254
|
+
// Add all your model mappings here
|
|
255
|
+
});
|
|
256
|
+
|
|
257
|
+
// Set global default auth mode
|
|
258
|
+
AmplifyQuery.setDefaultAuthMode("userPool");
|
|
259
|
+
|
|
260
|
+
// Now all services created will automatically use these settings
|
|
261
|
+
const UserService = AmplifyQuery.createAmplifyService<User>("User");
|
|
262
|
+
const ProjectService = AmplifyQuery.createAmplifyService<Project>("Project");
|
|
263
|
+
const TodoService = AmplifyQuery.createAmplifyService<Todo>("Todo");
|
|
264
|
+
|
|
265
|
+
// Get current global settings
|
|
266
|
+
const currentQueryMap = AmplifyQuery.getModelOwnerQueryMap();
|
|
267
|
+
const currentAuthMode = AmplifyQuery.getDefaultAuthMode();
|
|
268
|
+
|
|
269
|
+
// Reset configuration (useful for testing)
|
|
270
|
+
AmplifyQuery.resetConfig();
|
|
271
|
+
```
|
|
272
|
+
|
|
273
|
+
#### Migration from Previous Versions
|
|
274
|
+
|
|
275
|
+
If you were previously passing `modelOwnerQueryMap` to each service, you can now simplify your code:
|
|
276
|
+
|
|
277
|
+
**Before (repetitive):**
|
|
278
|
+
|
|
279
|
+
```typescript
|
|
280
|
+
const modelOwnerQueryMap = {
|
|
281
|
+
User: "listUserByOwner",
|
|
282
|
+
Project: "listProjectByOwner",
|
|
283
|
+
Todo: "listTodoByOwner",
|
|
284
|
+
};
|
|
285
|
+
|
|
286
|
+
// Had to pass queryMap to every service
|
|
287
|
+
const UserService = AmplifyQuery.createAmplifyService<User>(
|
|
288
|
+
"User",
|
|
289
|
+
modelOwnerQueryMap
|
|
290
|
+
);
|
|
291
|
+
const ProjectService = AmplifyQuery.createAmplifyService<Project>(
|
|
292
|
+
"Project",
|
|
293
|
+
modelOwnerQueryMap
|
|
294
|
+
);
|
|
295
|
+
const TodoService = AmplifyQuery.createAmplifyService<Todo>(
|
|
296
|
+
"Todo",
|
|
297
|
+
modelOwnerQueryMap
|
|
298
|
+
);
|
|
299
|
+
```
|
|
300
|
+
|
|
301
|
+
**After (clean):**
|
|
302
|
+
|
|
303
|
+
```typescript
|
|
304
|
+
// Set once globally
|
|
305
|
+
AmplifyQuery.configure({
|
|
306
|
+
client,
|
|
307
|
+
modelOwnerQueryMap: {
|
|
308
|
+
User: "listUserByOwner",
|
|
309
|
+
Project: "listProjectByOwner",
|
|
310
|
+
Todo: "listTodoByOwner",
|
|
311
|
+
},
|
|
312
|
+
});
|
|
313
|
+
|
|
314
|
+
// Create services without repetition
|
|
315
|
+
const UserService = AmplifyQuery.createAmplifyService<User>("User");
|
|
316
|
+
const ProjectService = AmplifyQuery.createAmplifyService<Project>("Project");
|
|
317
|
+
const TodoService = AmplifyQuery.createAmplifyService<Todo>("Todo");
|
|
318
|
+
```
|
|
319
|
+
|
|
216
320
|
### Caching
|
|
217
321
|
|
|
218
322
|
AmplifyQuery uses MMKV to persistently cache query results. This allows the app to display previous data immediately upon restart.
|
|
@@ -239,10 +343,22 @@ const todos = await TodoService.list({ forceRefresh: true });
|
|
|
239
343
|
Access data with various authentication methods.
|
|
240
344
|
|
|
241
345
|
```typescript
|
|
242
|
-
// Set global default authentication mode
|
|
243
|
-
AmplifyQuery.configure({
|
|
346
|
+
// Set global default authentication mode via configure
|
|
347
|
+
AmplifyQuery.configure({
|
|
348
|
+
client,
|
|
349
|
+
defaultAuthMode: "userPool",
|
|
350
|
+
});
|
|
351
|
+
|
|
352
|
+
// Or set it separately
|
|
353
|
+
AmplifyQuery.setDefaultAuthMode("userPool");
|
|
354
|
+
|
|
355
|
+
// Create service with custom auth mode
|
|
356
|
+
const AdminTodoService = AmplifyQuery.createAmplifyService<TodoModel>(
|
|
357
|
+
"Todo",
|
|
358
|
+
"iam"
|
|
359
|
+
);
|
|
244
360
|
|
|
245
|
-
// Set authentication mode for
|
|
361
|
+
// Set authentication mode for an existing service
|
|
246
362
|
TodoService.setAuthMode("apiKey");
|
|
247
363
|
|
|
248
364
|
// Apply authentication mode to a specific request
|
package/dist/config.d.ts
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Global configuration for AmplifyQuery library
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Set the global model owner query mapping
|
|
6
|
+
* @param queryMap Mapping of model names to their owner query names
|
|
7
|
+
*/
|
|
8
|
+
export declare function setModelOwnerQueryMap(queryMap: Record<string, string>): void;
|
|
9
|
+
/**
|
|
10
|
+
* Get the global model owner query mapping
|
|
11
|
+
* @returns The global model owner query mapping or undefined if not set
|
|
12
|
+
*/
|
|
13
|
+
export declare function getModelOwnerQueryMap(): Record<string, string> | undefined;
|
|
14
|
+
/**
|
|
15
|
+
* Get owner query name for a specific model
|
|
16
|
+
* @param modelName The model name
|
|
17
|
+
* @returns The owner query name or default format if not found
|
|
18
|
+
*/
|
|
19
|
+
export declare function getOwnerQueryName(modelName: string): string;
|
|
20
|
+
/**
|
|
21
|
+
* Set the default auth mode
|
|
22
|
+
* @param authMode Default authentication mode
|
|
23
|
+
*/
|
|
24
|
+
export declare function setDefaultAuthMode(authMode: "apiKey" | "iam" | "identityPool" | "oidc" | "userPool" | "lambda" | "none"): void;
|
|
25
|
+
/**
|
|
26
|
+
* Get the default auth mode
|
|
27
|
+
* @returns The default auth mode or 'userPool' if not set
|
|
28
|
+
*/
|
|
29
|
+
export declare function getDefaultAuthMode(): "apiKey" | "iam" | "identityPool" | "oidc" | "userPool" | "lambda" | "none";
|
|
30
|
+
/**
|
|
31
|
+
* Reset global configuration (mainly for testing)
|
|
32
|
+
*/
|
|
33
|
+
export declare function resetConfig(): void;
|
package/dist/config.js
ADDED
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Global configuration for AmplifyQuery library
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.setModelOwnerQueryMap = setModelOwnerQueryMap;
|
|
7
|
+
exports.getModelOwnerQueryMap = getModelOwnerQueryMap;
|
|
8
|
+
exports.getOwnerQueryName = getOwnerQueryName;
|
|
9
|
+
exports.setDefaultAuthMode = setDefaultAuthMode;
|
|
10
|
+
exports.getDefaultAuthMode = getDefaultAuthMode;
|
|
11
|
+
exports.resetConfig = resetConfig;
|
|
12
|
+
// Global configuration state
|
|
13
|
+
let globalConfig = {};
|
|
14
|
+
/**
|
|
15
|
+
* Set the global model owner query mapping
|
|
16
|
+
* @param queryMap Mapping of model names to their owner query names
|
|
17
|
+
*/
|
|
18
|
+
function setModelOwnerQueryMap(queryMap) {
|
|
19
|
+
globalConfig.modelOwnerQueryMap = Object.assign({}, queryMap);
|
|
20
|
+
console.log("🔧 AmplifyQuery: Global model owner query map configured");
|
|
21
|
+
}
|
|
22
|
+
/**
|
|
23
|
+
* Get the global model owner query mapping
|
|
24
|
+
* @returns The global model owner query mapping or undefined if not set
|
|
25
|
+
*/
|
|
26
|
+
function getModelOwnerQueryMap() {
|
|
27
|
+
return globalConfig.modelOwnerQueryMap;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* Get owner query name for a specific model
|
|
31
|
+
* @param modelName The model name
|
|
32
|
+
* @returns The owner query name or default format if not found
|
|
33
|
+
*/
|
|
34
|
+
function getOwnerQueryName(modelName) {
|
|
35
|
+
const queryMap = getModelOwnerQueryMap();
|
|
36
|
+
return (queryMap === null || queryMap === void 0 ? void 0 : queryMap[modelName]) || `list${modelName}sByOwner`;
|
|
37
|
+
}
|
|
38
|
+
/**
|
|
39
|
+
* Set the default auth mode
|
|
40
|
+
* @param authMode Default authentication mode
|
|
41
|
+
*/
|
|
42
|
+
function setDefaultAuthMode(authMode) {
|
|
43
|
+
globalConfig.defaultAuthMode = authMode;
|
|
44
|
+
console.log(`🔧 AmplifyQuery: Default auth mode set to ${authMode}`);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Get the default auth mode
|
|
48
|
+
* @returns The default auth mode or 'userPool' if not set
|
|
49
|
+
*/
|
|
50
|
+
function getDefaultAuthMode() {
|
|
51
|
+
return globalConfig.defaultAuthMode || "userPool";
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Reset global configuration (mainly for testing)
|
|
55
|
+
*/
|
|
56
|
+
function resetConfig() {
|
|
57
|
+
globalConfig = {};
|
|
58
|
+
console.log("🔧 AmplifyQuery: Global configuration reset");
|
|
59
|
+
}
|
package/dist/index.d.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { createAmplifyService } from "./service";
|
|
|
4
4
|
import { createSingletonService } from "./singleton";
|
|
5
5
|
import { AmplifyQueryConfig } from "./types";
|
|
6
6
|
import { createRelationalHook, setAppUrl, getAppUrl } from "./utils";
|
|
7
|
+
import { setModelOwnerQueryMap, setDefaultAuthMode, getModelOwnerQueryMap, getDefaultAuthMode, resetConfig } from "./config";
|
|
7
8
|
/**
|
|
8
9
|
* Initialization function for the AmplifyQuery library.
|
|
9
10
|
*
|
|
@@ -132,4 +133,9 @@ export declare const AmplifyQuery: {
|
|
|
132
133
|
User: () => Promise<string>;
|
|
133
134
|
};
|
|
134
135
|
getQueryClient: typeof getQueryClient;
|
|
136
|
+
setModelOwnerQueryMap: typeof setModelOwnerQueryMap;
|
|
137
|
+
getModelOwnerQueryMap: typeof getModelOwnerQueryMap;
|
|
138
|
+
setDefaultAuthMode: typeof setDefaultAuthMode;
|
|
139
|
+
getDefaultAuthMode: typeof getDefaultAuthMode;
|
|
140
|
+
resetConfig: typeof resetConfig;
|
|
135
141
|
};
|
package/dist/index.js
CHANGED
|
@@ -32,6 +32,7 @@ const singleton_1 = require("./singleton");
|
|
|
32
32
|
const utils_1 = require("./utils");
|
|
33
33
|
Object.defineProperty(exports, "setAppUrl", { enumerable: true, get: function () { return utils_1.setAppUrl; } });
|
|
34
34
|
Object.defineProperty(exports, "getAppUrl", { enumerable: true, get: function () { return utils_1.getAppUrl; } });
|
|
35
|
+
const config_1 = require("./config");
|
|
35
36
|
/**
|
|
36
37
|
* Initialization function for the AmplifyQuery library.
|
|
37
38
|
*
|
|
@@ -60,6 +61,14 @@ function configure(config) {
|
|
|
60
61
|
if (config.client) {
|
|
61
62
|
(0, client_1.setClient)(config.client);
|
|
62
63
|
}
|
|
64
|
+
// Set global model owner query mapping
|
|
65
|
+
if (config.modelOwnerQueryMap) {
|
|
66
|
+
(0, config_1.setModelOwnerQueryMap)(config.modelOwnerQueryMap);
|
|
67
|
+
}
|
|
68
|
+
// Set default auth mode
|
|
69
|
+
if (config.defaultAuthMode) {
|
|
70
|
+
(0, config_1.setDefaultAuthMode)(config.defaultAuthMode);
|
|
71
|
+
}
|
|
63
72
|
// Apply React Query settings
|
|
64
73
|
(0, query_1.configure)({
|
|
65
74
|
isCachingEnabled: config.isCachingEnabled,
|
|
@@ -90,4 +99,10 @@ exports.AmplifyQuery = {
|
|
|
90
99
|
Auth: utils_1.AuthService,
|
|
91
100
|
getModelIds: singleton_1.getModelIds,
|
|
92
101
|
getQueryClient: query_1.getQueryClient,
|
|
102
|
+
// Global configuration functions
|
|
103
|
+
setModelOwnerQueryMap: config_1.setModelOwnerQueryMap,
|
|
104
|
+
getModelOwnerQueryMap: config_1.getModelOwnerQueryMap,
|
|
105
|
+
setDefaultAuthMode: config_1.setDefaultAuthMode,
|
|
106
|
+
getDefaultAuthMode: config_1.getDefaultAuthMode,
|
|
107
|
+
resetConfig: config_1.resetConfig,
|
|
93
108
|
};
|
package/dist/service.d.ts
CHANGED
|
@@ -2,8 +2,7 @@ import { AmplifyDataService, AuthMode, BaseModel } from "./types";
|
|
|
2
2
|
/**
|
|
3
3
|
* Create model-specific Amplify service
|
|
4
4
|
* @param modelName Model name
|
|
5
|
-
* @param
|
|
6
|
-
* @param defaultAuthMode Default authentication mode (default: 'userPool')
|
|
5
|
+
* @param defaultAuthMode Default authentication mode (optional, uses global config if not provided)
|
|
7
6
|
* @returns AmplifyDataService instance for the model
|
|
8
7
|
*/
|
|
9
|
-
export declare function createAmplifyService<T extends BaseModel>(modelName: string,
|
|
8
|
+
export declare function createAmplifyService<T extends BaseModel>(modelName: string, defaultAuthMode?: AuthMode): AmplifyDataService<T>;
|
package/dist/service.js
CHANGED
|
@@ -13,6 +13,7 @@ exports.createAmplifyService = createAmplifyService;
|
|
|
13
13
|
const client_1 = require("./client");
|
|
14
14
|
const query_1 = require("./query");
|
|
15
15
|
const utils_1 = require("./utils");
|
|
16
|
+
const config_1 = require("./config");
|
|
16
17
|
const react_query_1 = require("@tanstack/react-query");
|
|
17
18
|
const auth_1 = require("aws-amplify/auth");
|
|
18
19
|
const expo_crypto_1 = require("expo-crypto");
|
|
@@ -169,13 +170,12 @@ function rollbackCache(queryClient, previousDataMap) {
|
|
|
169
170
|
/**
|
|
170
171
|
* Create model-specific Amplify service
|
|
171
172
|
* @param modelName Model name
|
|
172
|
-
* @param
|
|
173
|
-
* @param defaultAuthMode Default authentication mode (default: 'userPool')
|
|
173
|
+
* @param defaultAuthMode Default authentication mode (optional, uses global config if not provided)
|
|
174
174
|
* @returns AmplifyDataService instance for the model
|
|
175
175
|
*/
|
|
176
|
-
function createAmplifyService(modelName,
|
|
177
|
-
// Track current authentication mode state
|
|
178
|
-
let currentAuthMode = defaultAuthMode;
|
|
176
|
+
function createAmplifyService(modelName, defaultAuthMode) {
|
|
177
|
+
// Track current authentication mode state - use global config if not provided
|
|
178
|
+
let currentAuthMode = defaultAuthMode || (0, config_1.getDefaultAuthMode)();
|
|
179
179
|
// Create service object
|
|
180
180
|
const service = {
|
|
181
181
|
// Add model name (needed for singleton services)
|
|
@@ -506,8 +506,8 @@ function createAmplifyService(modelName, queryMap, defaultAuthMode = "userPool")
|
|
|
506
506
|
const authMode = (options === null || options === void 0 ? void 0 : options.authMode) || currentAuthMode;
|
|
507
507
|
// Get owner and parameters based on auth mode
|
|
508
508
|
const { owner, authModeParams } = yield getOwnerByAuthMode(authMode);
|
|
509
|
-
// Get owner-based query name
|
|
510
|
-
const ownerQueryName = (
|
|
509
|
+
// Get owner-based query name from global config
|
|
510
|
+
const ownerQueryName = (0, config_1.getOwnerQueryName)(modelName);
|
|
511
511
|
// Try query call
|
|
512
512
|
try {
|
|
513
513
|
console.log(`🍬 ${modelName} list API call`, queryKey, `by ${ownerQueryName}`, `[Auth: ${authMode}]`);
|
package/dist/types.d.ts
CHANGED
|
@@ -128,6 +128,7 @@ import { QueryClientConfig } from "@tanstack/react-query";
|
|
|
128
128
|
export interface AmplifyQueryConfig {
|
|
129
129
|
client: GraphQLClient;
|
|
130
130
|
defaultAuthMode?: AuthMode;
|
|
131
|
+
modelOwnerQueryMap?: Record<string, string>;
|
|
131
132
|
isCachingEnabled?: boolean;
|
|
132
133
|
queryClientConfig?: QueryClientConfig;
|
|
133
134
|
storage?: {
|