aurora-mongo
Version:
MongoDBをKeyvのように扱うことができるパッケージです。
126 lines • 4.24 kB
JavaScript
"use strict";
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 __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.Database = exports.connect = void 0;
const mongoose_1 = __importDefault(require("mongoose"));
const json_bigint_1 = __importDefault(require("json-bigint"));
const mongoose_2 = require("mongoose");
/**
*
* @param {string} uri MongoDBとの接続で使用します。
* @example mongo.connect(`mongodb+srv://username:password@hoge.rhaqe.mongodb.net/myFirstDatabase?retryWrites=true&w=majority`)
*/
function connect(uri) {
return __awaiter(this, void 0, void 0, function* () {
mongoose_1.default.connect(uri);
});
}
exports.connect = connect;
;
const schemaFields = {
key: String,
value: String
};
const schema = new mongoose_2.Schema({
key: String,
value: String
});
//class
class Database {
/**
*
* @param {string} name MongoDBのモデルの名前。
*/
constructor(name) {
this.name = name;
this.model = (0, mongoose_2.model)(name, schema);
}
dget(key) {
return __awaiter(this, void 0, void 0, function* () {
const data = yield this.model.findOne({ key });
return data;
});
}
get(key) {
return __awaiter(this, void 0, void 0, function* () {
const data = yield this.dget(key);
if (!data)
return null;
return json_bigint_1.default.parse(data.value);
});
}
set(key, value) {
return __awaiter(this, void 0, void 0, function* () {
const data = (yield this.dget(key)) || new this.model();
data.key = key;
data.value = json_bigint_1.default.stringify(value);
yield data.save();
return null;
});
}
has(key) {
return __awaiter(this, void 0, void 0, function* () {
const data = yield this.dget(key);
if (!data)
return false;
return true;
});
}
delete(key) {
return __awaiter(this, void 0, void 0, function* () {
const data = yield this.dget(key);
if (!data)
return null;
yield data.remove();
return;
});
}
clear() {
return __awaiter(this, void 0, void 0, function* () {
yield this.model.remove();
return;
});
}
/**
*
* @returns {Promise<string[]>} キーの配列が返ってきます。
*/
keys() {
return __awaiter(this, void 0, void 0, function* () {
const data = yield this.model.find();
const filteredData = data.map(v => v.key);
return filteredData;
});
}
/**
*
* @returns {Promise<any[]>} 値の配列が返ってきます。
*/
values() {
return __awaiter(this, void 0, void 0, function* () {
const data = yield this.model.find();
const filteredData = data.map(v => json_bigint_1.default.parse(v.value));
return filteredData;
});
}
entries() {
return __awaiter(this, void 0, void 0, function* () {
const data = yield this.model.find();
const filteredData = data.map(v => { return [v.key, json_bigint_1.default.parse(v.value)]; });
return filteredData;
});
}
}
exports.Database = Database;
//# sourceMappingURL=index.js.map