@configurator/ravendb
Version:
RavenDB client for Node.js
69 lines • 2.65 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.GenerateEntityIdOnTheClient = void 0;
const Exceptions_1 = require("../../Exceptions");
const TypeUtil_1 = require("../../Utility/TypeUtil");
class GenerateEntityIdOnTheClient {
constructor(conventions, generateId) {
this._conventions = conventions;
this._generateId = generateId;
}
_getIdentityProperty(entityType) {
return this._conventions.getIdentityProperty(entityType);
}
tryGetIdFromInstance(entity, idCallback) {
if (!entity) {
(0, Exceptions_1.throwError)("InvalidArgumentException", "Entity cannot be null or undefined.");
}
const resultCallback = (result) => {
if (idCallback) {
idCallback(result);
}
};
try {
const docType = TypeUtil_1.TypeUtil.findType(entity, this._conventions.knownEntityTypes);
const identityProperty = this._getIdentityProperty(docType);
if (identityProperty) {
const value = entity[identityProperty];
if (typeof (value) === "string") {
resultCallback(value);
return true;
}
}
resultCallback(null);
return false;
}
catch (e) {
(0, Exceptions_1.throwError)("InvalidOperationException", "Error trying to get ID from instance.");
}
}
async getOrGenerateDocumentId(entity) {
let id;
this.tryGetIdFromInstance(entity, (idVal) => id = idVal);
if (!id) {
id = await this._generateId(entity);
}
if (id && id.startsWith("/")) {
(0, Exceptions_1.throwError)("InvalidOperationException", "Cannot use value '" + id + "' as a document id because it begins with a '/'");
}
return id;
}
async generateDocumentKeyForStorage(entity) {
const id = await this.getOrGenerateDocumentId(entity);
this.trySetIdentity(entity, id);
return id;
}
trySetIdentity(entity, id, isProjection = false) {
const docType = TypeUtil_1.TypeUtil.findType(entity, this._conventions.knownEntityTypes);
const identityProperty = this._conventions.getIdentityProperty(docType);
if (!identityProperty) {
return;
}
if (isProjection && entity[identityProperty]) {
return;
}
entity[identityProperty] = id;
}
}
exports.GenerateEntityIdOnTheClient = GenerateEntityIdOnTheClient;
//# sourceMappingURL=GenerateEntityIdOnTheClient.js.map