pebblebed
Version:
Simplified interactions with Google Datastore for NodeJS
40 lines • 1.95 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 });
const Core_1 = require("../Core");
function replaceIncompleteWithAllocatedIds(entities, transaction = null) {
return __awaiter(this, void 0, void 0, function* () {
let allocateAmount = 0;
let incompleteKey = null;
for (const entity of entities) {
if (entity.generated) {
allocateAmount += 1;
if (incompleteKey == null) {
incompleteKey = entity.key;
}
}
}
const allocatedKeys = transaction ? yield transaction.allocateIds(incompleteKey, allocateAmount) : yield Core_1.default.Instance.dsModule.allocateIds(incompleteKey, allocateAmount);
let ids = [];
for (let i = 0; i < entities.length; i += 1) {
if (entities[i].generated) {
entities[i].key = allocatedKeys[0].shift();
ids.push(entities[i].key.id);
}
else {
ids.push(null);
}
}
return { ids, newEntities: entities };
});
}
exports.default = replaceIncompleteWithAllocatedIds;
//# sourceMappingURL=replaceIncompleteWithAllocatedIds.js.map