shyft
Version:
Model driven GraphQL API framework
106 lines (105 loc) • 6.29 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.isStorageType = exports.StorageType = void 0;
var util_1 = require("../util");
var DataType_1 = require("../datatype/DataType");
var StorageDataType_1 = require("./StorageDataType");
var StorageConfiguration_1 = require("./StorageConfiguration");
var StorageType = /** @class */ (function () {
function StorageType(setup) {
if (setup === void 0) { setup = {}; }
var name = setup.name, description = setup.description, findOne = setup.findOne, findOneByValues = setup.findOneByValues, find = setup.find, count = setup.count, mutate = setup.mutate, checkLookupPermission = setup.checkLookupPermission;
util_1.passOrThrow(name, function () { return 'Missing storage type name'; });
util_1.passOrThrow(description, function () { return "Missing description for storage type '" + name + "'"; });
util_1.passOrThrow(util_1.isFunction(findOne), function () { return "Storage type '" + name + "' needs to implement findOne()"; });
util_1.passOrThrow(util_1.isFunction(findOneByValues), function () { return "Storage type '" + name + "' needs to implement findOneByValues()"; });
util_1.passOrThrow(util_1.isFunction(find), function () { return "Storage type '" + name + "' needs to implement find()"; });
util_1.passOrThrow(util_1.isFunction(count), function () { return "Storage type '" + name + "' needs to implement count()"; });
util_1.passOrThrow(util_1.isFunction(mutate), function () { return "Storage type '" + name + "' needs to implement mutate()"; });
util_1.passOrThrow(util_1.isFunction(checkLookupPermission), function () { return "Storage type '" + name + "' needs to implement checkLookupPermission()"; });
this.name = name;
this.description = description;
this.findOne = findOne;
this.findOneByValues = findOneByValues;
this.find = find;
this.count = count;
this.mutate = mutate;
this.checkLookupPermission = checkLookupPermission;
this._dataTypeMap = {};
this._dynamicDataTypeMap = [];
}
StorageType.prototype.addDataTypeMap = function (schemaDataType, storageDataType) {
var _this = this;
util_1.passOrThrow(DataType_1.isDataType(schemaDataType), function () {
return "Provided schemaDataType is not a valid data type in '" + _this.name + "', " +
("got this instead: " + String(schemaDataType));
});
util_1.passOrThrow(StorageDataType_1.isStorageDataType(storageDataType), function () {
return "Provided storageDataType for '" + String(schemaDataType) + "' is not a valid storage data type in '" + _this.name + "', " +
("got this instead: " + String(storageDataType));
});
util_1.passOrThrow(!this._dataTypeMap[schemaDataType.name], function () {
return "Data type mapping for '" + schemaDataType.name + "' already registered with storage type '" + _this.name + "'";
});
this._dataTypeMap[schemaDataType.name] = storageDataType;
};
StorageType.prototype.addDynamicDataTypeMap = function (schemaDataTypeDetector, storageDataType) {
var _this = this;
util_1.passOrThrow(util_1.isFunction(schemaDataTypeDetector), function () {
return "Provided schemaDataTypeDetector is not a valid function in '" + _this.name + "', " +
("got this instead: " + String(schemaDataTypeDetector));
});
util_1.passOrThrow(StorageDataType_1.isStorageDataType(storageDataType) || util_1.isFunction(storageDataType), function () {
return "Provided storageDataType for '" + String(schemaDataTypeDetector) + "' is not a valid storage data type or function in '" + _this.name + "', " +
("got this instead: " + String(storageDataType));
});
this._dynamicDataTypeMap.push({
schemaDataTypeDetector: schemaDataTypeDetector,
storageDataType: storageDataType,
});
};
StorageType.prototype.convertToStorageDataType = function (schemaDataType) {
var _this = this;
var foundDynamicDataType = this._dynamicDataTypeMap.find(function (_a) {
var schemaDataTypeDetector = _a.schemaDataTypeDetector;
return schemaDataTypeDetector(schemaDataType);
});
if (foundDynamicDataType) {
var storageDataType = foundDynamicDataType.storageDataType;
if (util_1.isFunction(storageDataType)) {
return storageDataType(schemaDataType);
}
return storageDataType;
}
if (!DataType_1.isDataType(schemaDataType)) {
throw new Error("Provided schemaDataType is not a valid data type in storage type '" + this.name + "', got this instead: " + String(schemaDataType));
}
util_1.passOrThrow(this._dataTypeMap[schemaDataType.name], function () {
return "No data type mapping found for '" + schemaDataType.name + "' in storage type '" + _this.name + "'";
});
return this._dataTypeMap[schemaDataType.name];
};
StorageType.prototype.setStorageConfiguration = function (storageConfiguration) {
util_1.passOrThrow(StorageConfiguration_1.isStorageConfiguration(storageConfiguration), function () { return 'StorageType expects a valid storageConfiguration'; });
this.storageConfiguration = storageConfiguration;
};
StorageType.prototype.getStorageConfiguration = function () {
util_1.passOrThrow(this.storageConfiguration, function () { return 'StorageType is missing a valid storageConfiguration'; });
return this.storageConfiguration;
};
StorageType.prototype.getStorageInstance = function () {
return this.getStorageConfiguration().getStorageInstance();
};
StorageType.prototype.getStorageModels = function () {
return this.getStorageConfiguration().getStorageModels();
};
StorageType.prototype.toString = function () {
return this.name;
};
return StorageType;
}());
exports.StorageType = StorageType;
var isStorageType = function (obj) {
return obj instanceof StorageType;
};
exports.isStorageType = isStorageType;