@akanjs/document 0.0.45 → 0.0.47
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/index.js +2 -2073
- package/package.json +2 -30
- package/src/dataLoader.js +139 -0
- package/src/database.js +308 -0
- package/src/dbDecorators.js +108 -0
- package/src/index.js +21 -0
- package/src/schema.js +123 -0
- package/src/types.js +15 -0
package/src/schema.js
ADDED
|
@@ -0,0 +1,123 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __export = (target, all) => {
|
|
6
|
+
for (var name in all)
|
|
7
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
8
|
+
};
|
|
9
|
+
var __copyProps = (to, from, except, desc) => {
|
|
10
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
11
|
+
for (let key of __getOwnPropNames(from))
|
|
12
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
13
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
14
|
+
}
|
|
15
|
+
return to;
|
|
16
|
+
};
|
|
17
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
18
|
+
var schema_exports = {};
|
|
19
|
+
__export(schema_exports, {
|
|
20
|
+
convertAggregateMatch: () => convertAggregateMatch,
|
|
21
|
+
getDefaultSchemaOptions: () => getDefaultSchemaOptions
|
|
22
|
+
});
|
|
23
|
+
module.exports = __toCommonJS(schema_exports);
|
|
24
|
+
var import_base = require("@akanjs/base");
|
|
25
|
+
var import_common = require("@akanjs/common");
|
|
26
|
+
var import_mongoose = require("mongoose");
|
|
27
|
+
const getDefaultSchemaOptions = () => ({
|
|
28
|
+
toJSON: { getters: false, virtuals: true },
|
|
29
|
+
toObject: { getters: false, virtuals: true },
|
|
30
|
+
_id: true,
|
|
31
|
+
id: true,
|
|
32
|
+
timestamps: true,
|
|
33
|
+
methods: {
|
|
34
|
+
refresh: async function() {
|
|
35
|
+
Object.assign(this, await this.constructor.findById(this._id));
|
|
36
|
+
return this;
|
|
37
|
+
}
|
|
38
|
+
},
|
|
39
|
+
statics: {
|
|
40
|
+
pickOne: async function(query, projection) {
|
|
41
|
+
const doc = await this.findOne(query, projection);
|
|
42
|
+
if (!doc)
|
|
43
|
+
throw new Error("No Document");
|
|
44
|
+
return doc;
|
|
45
|
+
},
|
|
46
|
+
pickById: async function(docId, projection) {
|
|
47
|
+
if (!docId)
|
|
48
|
+
throw new Error("No Document ID");
|
|
49
|
+
const doc = await this.findById(docId, projection);
|
|
50
|
+
if (!doc)
|
|
51
|
+
throw new Error("No Document");
|
|
52
|
+
return doc;
|
|
53
|
+
},
|
|
54
|
+
sample: async function(query, size = 1, aggregations = []) {
|
|
55
|
+
const objs = await this.aggregate([
|
|
56
|
+
{ $match: convertAggregateMatch(query) },
|
|
57
|
+
{ $sample: { size } },
|
|
58
|
+
...aggregations
|
|
59
|
+
]);
|
|
60
|
+
return objs.map((obj) => new this(obj));
|
|
61
|
+
},
|
|
62
|
+
sampleOne: async function(query, aggregations = []) {
|
|
63
|
+
const obj = await this.aggregate([
|
|
64
|
+
{ $match: convertAggregateMatch(query) },
|
|
65
|
+
{ $sample: { size: 1 } },
|
|
66
|
+
...aggregations
|
|
67
|
+
]);
|
|
68
|
+
return obj.length ? new this(obj[0]) : null;
|
|
69
|
+
},
|
|
70
|
+
addSummary: async function(prefix = "total", num = 1) {
|
|
71
|
+
const update = Array.isArray(prefix) ? {
|
|
72
|
+
$inc: {
|
|
73
|
+
...prefix.reduce((acc, cur) => ({ ...acc, [`${cur}${this.modelName}`]: num }), {})
|
|
74
|
+
}
|
|
75
|
+
} : { $inc: { [`${prefix}${this.modelName}`]: num } };
|
|
76
|
+
await this.db.collection("summaries").updateOne({ status: "active" }, update);
|
|
77
|
+
},
|
|
78
|
+
moveSummary: async function(prev, next, num = 1) {
|
|
79
|
+
await this.db.collection("summaries").updateOne(
|
|
80
|
+
{ status: "active" },
|
|
81
|
+
{
|
|
82
|
+
$inc: {
|
|
83
|
+
[`${prev}${this.modelName}`]: -num,
|
|
84
|
+
[`${next}${this.modelName}`]: num
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
);
|
|
88
|
+
},
|
|
89
|
+
subSummary: async function(prefix = "total", num = 1) {
|
|
90
|
+
const update = Array.isArray(prefix) ? {
|
|
91
|
+
$inc: {
|
|
92
|
+
...prefix.reduce((acc, cur) => ({ ...acc, [`${cur}${this.modelName}`]: -num }), {})
|
|
93
|
+
}
|
|
94
|
+
} : { $inc: { [`${prefix}${this.modelName}`]: -num } };
|
|
95
|
+
await this.db.collection("summaries").updateOne({ status: "active" }, update);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
const convertOperatorValue = (value) => {
|
|
100
|
+
if (Array.isArray(value))
|
|
101
|
+
return value.map((v) => convertOperatorValue(v));
|
|
102
|
+
else if (!value)
|
|
103
|
+
return value;
|
|
104
|
+
else if ((0, import_mongoose.isValidObjectId)(value))
|
|
105
|
+
return new import_mongoose.Types.ObjectId(value);
|
|
106
|
+
else if ((0, import_common.isValidDate)(value))
|
|
107
|
+
return (0, import_base.dayjs)(value).toDate();
|
|
108
|
+
else if (value.constructor !== Object)
|
|
109
|
+
return value;
|
|
110
|
+
else if (typeof value !== "object")
|
|
111
|
+
return value;
|
|
112
|
+
else
|
|
113
|
+
return Object.fromEntries(
|
|
114
|
+
Object.entries(value).map(([key, value2]) => [key, convertOperatorValue(value2)])
|
|
115
|
+
);
|
|
116
|
+
};
|
|
117
|
+
const convertAggregateMatch = (query) => {
|
|
118
|
+
return Object.fromEntries(
|
|
119
|
+
Object.entries(query).map(([key, value]) => {
|
|
120
|
+
return [key, convertOperatorValue(value)];
|
|
121
|
+
})
|
|
122
|
+
);
|
|
123
|
+
};
|
package/src/types.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
var __defProp = Object.defineProperty;
|
|
2
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
+
var __copyProps = (to, from, except, desc) => {
|
|
6
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
7
|
+
for (let key of __getOwnPropNames(from))
|
|
8
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
9
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
10
|
+
}
|
|
11
|
+
return to;
|
|
12
|
+
};
|
|
13
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
14
|
+
var types_exports = {};
|
|
15
|
+
module.exports = __toCommonJS(types_exports);
|