amplifyquery
Version:
145 lines (144 loc) • 7.81 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getModelIds = void 0;
exports.createSingletonService = createSingletonService;
const client_1 = require("./client");
const auth_1 = require("aws-amplify/auth");
/**
* Function to create an extension service for singleton models
* @param baseService Base service
* @param getModelId Function to get the unique ID of the model
* @returns Extended service supporting singleton pattern
*/
function createSingletonService(baseService, getModelId) {
// Extract modelName from baseService
const { modelName } = baseService;
// Create singleton service object
const singletonService = Object.assign(Object.assign({}, baseService), {
// Add singleton instance management methods
getCurrent: (options) => __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
try {
const modelId = yield getModelId();
return baseService.get(modelId, options);
}
catch (error) {
console.error(`${modelName} singleton instance lookup error:`, error);
// Safely call getStore
try {
(_c = (_b = (_a = baseService
.getStore) === null || _a === void 0 ? void 0 : _a.call(baseService)) === null || _b === void 0 ? void 0 : _b.setError) === null || _c === void 0 ? void 0 : _c.call(_b, error instanceof Error ? error : new Error(String(error)));
}
catch (storeError) {
// Ignore if getStore doesn't exist or call fails
}
return null;
}
}), updateCurrent: (data) => __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c;
try {
const modelId = yield getModelId();
return baseService.update(Object.assign(Object.assign({}, data), { id: modelId }));
}
catch (error) {
console.error(`${modelName} singleton instance update error:`, error);
// Safely call getStore
try {
(_c = (_b = (_a = baseService
.getStore) === null || _a === void 0 ? void 0 : _a.call(baseService)) === null || _b === void 0 ? void 0 : _b.setError) === null || _c === void 0 ? void 0 : _c.call(_b, error instanceof Error ? error : new Error(String(error)));
}
catch (storeError) {
// Ignore if getStore doesn't exist or call fails
}
return null;
}
}), upsertCurrent: (data) => __awaiter(this, void 0, void 0, function* () {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q;
try {
const modelId = yield getModelId();
// Check the latest status by forced refresh
const existingItem = yield baseService.get(modelId, {
forceRefresh: true,
});
if (existingItem) {
// Update
return baseService.update(Object.assign(Object.assign({}, data), { id: modelId }));
}
else {
// Create (Direct call to Amplify Client - prevents random ID generation of generic create)
const modelData = Object.assign(Object.assign({}, data), { id: modelId });
// Safely call getStore
try {
(_c = (_b = (_a = baseService.getStore) === null || _a === void 0 ? void 0 : _a.call(baseService)) === null || _b === void 0 ? void 0 : _b.setLoading) === null || _c === void 0 ? void 0 : _c.call(_b, true);
}
catch (storeError) {
// Ignore if getStore doesn't exist or call fails
}
try {
// Call appropriate model from Amplify Models
const { data: createdItem } = yield (0, client_1.getClient)().models[modelName].create(modelData);
if (createdItem) {
try {
(_f = (_e = (_d = baseService.getStore) === null || _d === void 0 ? void 0 : _d.call(baseService)) === null || _e === void 0 ? void 0 : _e.setItem) === null || _f === void 0 ? void 0 : _f.call(_e, createdItem);
}
catch (storeError) {
// Ignore if getStore doesn't exist or call fails
}
}
try {
(_j = (_h = (_g = baseService.getStore) === null || _g === void 0 ? void 0 : _g.call(baseService)) === null || _h === void 0 ? void 0 : _h.setLoading) === null || _j === void 0 ? void 0 : _j.call(_h, false);
}
catch (storeError) {
// Ignore if getStore doesn't exist or call fails
}
return createdItem !== null && createdItem !== void 0 ? createdItem : null;
}
catch (apiError) {
try {
(_m = (_l = (_k = baseService.getStore) === null || _k === void 0 ? void 0 : _k.call(baseService)) === null || _l === void 0 ? void 0 : _l.setLoading) === null || _m === void 0 ? void 0 : _m.call(_l, false);
(_q = (_p = (_o = baseService
.getStore) === null || _o === void 0 ? void 0 : _o.call(baseService)) === null || _p === void 0 ? void 0 : _p.setError) === null || _q === void 0 ? void 0 : _q.call(_p, apiError instanceof Error
? apiError
: new Error(String(apiError)));
}
catch (storeError) {
// Ignore if getStore doesn't exist or call fails
}
console.error(`${modelName} singleton instance Upsert error:`, apiError);
throw apiError; // Propagate error upwards
}
}
}
catch (error) {
console.error(`${modelName} singleton instance final Upsert error:`, error);
return null;
}
}) });
return singletonService;
}
/**
* Define functions to get model IDs by model
*/
exports.getModelIds = {
// Get user ID
User: () => __awaiter(void 0, void 0, void 0, function* () {
try {
const { userId } = yield (0, auth_1.getCurrentUser)();
return userId;
}
catch (e) {
console.error("Failed to get user ID:", e);
throw new Error("Unable to retrieve user authentication information.");
}
}),
// Users can add their own functions to get singleton model IDs here as needed.
};