@chevre/domain
Version:
Chevre Domain Library for Node.js
175 lines (174 loc) • 7.33 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());
});
};
var __rest = (this && this.__rest) || function (s, e) {
var t = {};
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
t[p] = s[p];
if (s != null && typeof Object.getOwnPropertySymbols === "function")
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
t[p[i]] = s[p[i]];
}
return t;
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.IssuerRepo = void 0;
const factory = require("../factory");
const settings_1 = require("../settings");
const issuer_1 = require("./mongoose/schemas/issuer");
/**
* 発行者リポジトリ
*/
class IssuerRepo {
constructor(connection) {
this.issuerModel = connection.model(issuer_1.modelName, (0, issuer_1.createSchema)());
}
static CREATE_MONGO_CONDITIONS(params) {
var _a, _b, _c, _d;
const andConditions = [];
const projectIdEq = (_b = (_a = params.project) === null || _a === void 0 ? void 0 : _a.id) === null || _b === void 0 ? void 0 : _b.$eq;
if (typeof projectIdEq === 'string') {
andConditions.push({ 'project.id': { $eq: projectIdEq } });
}
const identifiereEq = (_c = params.identifier) === null || _c === void 0 ? void 0 : _c.$eq;
if (typeof identifiereEq === 'string') {
andConditions.push({ identifier: { $eq: identifiereEq } });
}
const idEq = (_d = params.id) === null || _d === void 0 ? void 0 : _d.$eq;
if (typeof idEq === 'string') {
andConditions.push({ _id: { $eq: idEq } });
}
return andConditions;
}
saveIssuer(params) {
return __awaiter(this, void 0, void 0, function* () {
let savedId;
let doc;
if (params.id === '') {
const { id } = params, creatingParams = __rest(params, ["id"]);
doc = yield this.issuerModel.create(creatingParams);
savedId = doc.id;
}
else {
const { id, identifier, project } = params, updateFields = __rest(params, ["id", "identifier", "project"]);
doc = yield this.issuerModel.findOneAndUpdate({ _id: { $eq: params.id } }, { $set: updateFields }, { upsert: false, new: true, projection: { _id: 1 } })
.exec();
savedId = params.id;
}
if (doc === null) {
throw new factory.errors.NotFound(this.issuerModel.modelName);
}
return { id: savedId };
});
}
renameIssuerIdentifier(params) {
return __awaiter(this, void 0, void 0, function* () {
let savedId;
let doc;
const { id } = params, updateFields = __rest(params, ["id"]);
doc = yield this.issuerModel.findOneAndUpdate({ _id: { $eq: params.id } }, { $set: updateFields }, { upsert: false, new: true, projection: { _id: 1 } })
.exec();
savedId = params.id;
if (doc === null) {
throw new factory.errors.NotFound(this.issuerModel.modelName);
}
return { id: savedId };
});
}
projectPublicFields(params) {
return __awaiter(this, void 0, void 0, function* () {
var _a;
const conditions = IssuerRepo.CREATE_MONGO_CONDITIONS(params);
const projection = {
_id: 0,
id: { $toString: '$_id' },
identifier: 1,
project: 1,
name: 1,
url: 1
};
const query = this.issuerModel.find((conditions.length > 0) ? { $and: conditions } : {}, projection);
if (typeof params.limit === 'number' && params.limit > 0) {
const page = (typeof params.page === 'number' && params.page > 0) ? params.page : 1;
query.limit(params.limit)
.skip(params.limit * (page - 1));
}
if (typeof ((_a = params.sort) === null || _a === void 0 ? void 0 : _a.identifier) === 'number') {
query.sort({ identifier: params.sort.identifier });
}
return query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
.lean()
.exec();
});
}
findById(params) {
return __awaiter(this, void 0, void 0, function* () {
const projection = {
_id: 0,
id: { $toString: '$_id' },
identifier: 1,
project: 1,
tokenSecret: 1,
name: 1,
url: 1
};
const query = this.issuerModel.findOne({
_id: { $eq: params.id },
'project.id': { $eq: params.project.id }
}, projection);
const issuer = yield query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
.lean()
.exec();
if (issuer === null) {
throw new factory.errors.NotFound('Issuer');
}
return issuer;
});
}
findByIdentifier(params) {
return __awaiter(this, void 0, void 0, function* () {
const projection = {
_id: 0,
id: { $toString: '$_id' },
identifier: 1,
project: 1,
tokenSecret: 1,
url: 1
};
const query = this.issuerModel.findOne({
identifier: { $eq: params.identifier },
'project.id': { $eq: params.project.id }
}, projection);
const issuer = yield query.setOptions({ maxTimeMS: settings_1.MONGO_MAX_TIME_MS })
.lean()
.exec();
if (issuer === null) {
throw new factory.errors.NotFound('Issuer');
}
return issuer;
});
}
deleteById(params) {
return __awaiter(this, void 0, void 0, function* () {
yield this.issuerModel.findOneAndDelete({
_id: { $eq: params.id },
'project.id': { $eq: params.project.id }
}, { projection: { _id: 1 } })
.exec();
});
}
getCursor(conditions, projection) {
return this.issuerModel.find(conditions, projection)
.sort({ identifier: factory.sortType.Ascending })
.cursor();
}
}
exports.IssuerRepo = IssuerRepo;
;