UNPKG

@qbcart/cosmos

Version:

Azure Cosmos DB access common across the QBCart node ecosystem.

152 lines (151 loc) 5.27 kB
"use strict"; /*********************************************** * @license * Copyright (c) QBCart Inc. All rights reserved. ************************************************/ 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 }); class Base { constructor(container) { this.container = container; } read(id, partitionKey) { return __awaiter(this, void 0, void 0, function* () { try { const { resource: item } = yield this.container .item(id, partitionKey) .read(); return item; } catch (error) { return null; } }); } queryFirst(query, partitionKey) { return __awaiter(this, void 0, void 0, function* () { try { const { resources: items } = yield this.container.items .query(query, { bufferItems: true, partitionKey: partitionKey }) .fetchAll(); return items[0]; } catch (error) { console.error(error); return null; } }); } queryAll(query, partitionKey) { return __awaiter(this, void 0, void 0, function* () { try { const { resources: items } = yield this.container.items .query(query, { bufferItems: true, partitionKey: partitionKey }) .fetchAll(); return items; } catch (error) { console.error(error); return []; } }); } create(object) { return __awaiter(this, void 0, void 0, function* () { try { object.Created = new Date(); const { resource: item } = yield this.container.items.create(object); return item; } catch (error) { return null; } }); } upsert(object) { return __awaiter(this, void 0, void 0, function* () { try { const { resource: item } = yield this.container.items.upsert(object); return item; } catch (error) { return null; } }); } replace(object) { return __awaiter(this, void 0, void 0, function* () { try { const { resource: item } = yield this.container .item(object.id, object.Discriminator) .replace(object); return item; } catch (error) { return null; } }); } safeReplace(object) { return __awaiter(this, void 0, void 0, function* () { let retry = 9; while (retry--) try { const { resource: item } = yield this.container .item(object.id, object.Discriminator) .replace(object, { accessCondition: { type: 'IfMatch', condition: object._etag } }); return item; } catch (error) { if (error.code == 412) { continue; } else { return null; } } }); } delete(object) { return __awaiter(this, void 0, void 0, function* () { try { const { resource: item } = yield this.container .item(object.id, object.Discriminator) .delete(); return item; } catch (error) { return null; } }); } sync(returnProps, filter, partitionKey, lastSynced) { var _a; return __awaiter(this, void 0, void 0, function* () { try { const query = lastSynced ? `SELECT ${returnProps.join()} FROM c WHERE c._ts > ${lastSynced}` : `SELECT ${returnProps.join()} FROM c WHERE ${filter}`; return (_a = (yield this.queryAll(query, partitionKey))) !== null && _a !== void 0 ? _a : []; } catch (error) { console.log(error); return []; } }); } } exports.default = Base;