nestjs-express-cassandra
Version:
Express-cassandra module for Nest framework
48 lines (47 loc) • 2.9 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.BaseRepository = void 0;
const rxjs_1 = require("rxjs");
class BaseRepository extends Function {
create(entity) {
return Object.assign(new this.target(), entity);
}
save(entity, options = {}) {
return (0, rxjs_1.defer)(() => __awaiter(this, void 0, void 0, function* () {
const model = new this.model(entity);
yield model.saveAsync(options);
return Object.assign(new this.target(), model.toJSON());
})).pipe((0, rxjs_1.catchError)((error) => (0, rxjs_1.of)(error)));
}
bulkSave(entities, options = {}) {
return (0, rxjs_1.defer)(() => __awaiter(this, void 0, void 0, function* () {
return yield Promise.all(entities.map((entity) => __awaiter(this, void 0, void 0, function* () {
const model = new this.model(entity);
yield model.saveAsync(options);
return Object.assign(new this.target(), model.toJSON());
})));
})).pipe((0, rxjs_1.catchError)((error) => (0, rxjs_1.of)(error)));
}
find(query, options = {}) {
return (0, rxjs_1.defer)(() => this.model.findAsync(query, Object.assign({ raw: true }, options))).pipe((0, rxjs_1.map)((entities) => entities.map((e) => Object.assign(new this.target(), e))), (0, rxjs_1.catchError)((error) => (0, rxjs_1.of)(error)));
}
findOne(query, options = {}) {
return (0, rxjs_1.defer)(() => this.model.findOneAsync(query, Object.assign({ raw: true }, options))).pipe((0, rxjs_1.map)((e) => e && Object.assign(new this.target(), e)), (0, rxjs_1.catchError)((error) => (0, rxjs_1.of)(error)));
}
update(query, updateValue, options = {}) {
return (0, rxjs_1.defer)(() => this.model.updateAsync(query, updateValue, Object.assign({ if_exists: true }, options))).pipe((0, rxjs_1.catchError)((error) => (0, rxjs_1.of)(error)));
}
delete(query, options) {
return (0, rxjs_1.defer)(() => this.model.deleteAsync(query, Object.assign({ if_exists: true }, options))).pipe((0, rxjs_1.catchError)((error) => (0, rxjs_1.of)(error)));
}
}
exports.BaseRepository = BaseRepository;