@akanjs/document 0.0.40 → 0.0.41
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.d.ts +1 -10
- package/index.js +2062 -21
- package/package.json +34 -2
- package/src/dataLoader.d.ts +16 -19
- package/src/database.d.ts +110 -8
- package/src/dbDecorators.d.ts +67 -8
- package/src/index.d.ts +5 -10
- package/src/schema.d.ts +3 -6
- package/src/types.d.ts +1 -3
- package/dbDecorators-B-tzSGvi.d.ts +0 -252
- package/src/dataLoader.js +0 -184
- package/src/database.js +0 -410
- package/src/dbDecorators.js +0 -153
- package/src/index.js +0 -29
- package/src/schema.js +0 -156
- package/src/types.js +0 -15
- package/types-H2HI4YUe.d.ts +0 -3
package/src/dataLoader.js
DELETED
|
@@ -1,184 +0,0 @@
|
|
|
1
|
-
var __create = Object.create;
|
|
2
|
-
var __defProp = Object.defineProperty;
|
|
3
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
-
var __getProtoOf = Object.getPrototypeOf;
|
|
6
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
8
|
-
var __export = (target, all) => {
|
|
9
|
-
for (var name in all)
|
|
10
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
11
|
-
};
|
|
12
|
-
var __copyProps = (to, from, except, desc) => {
|
|
13
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
14
|
-
for (let key of __getOwnPropNames(from))
|
|
15
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
16
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
17
|
-
}
|
|
18
|
-
return to;
|
|
19
|
-
};
|
|
20
|
-
var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(
|
|
21
|
-
// If the importer is in node compatibility mode or this is not an ESM
|
|
22
|
-
// file that has been converted to a CommonJS file using a Babel-
|
|
23
|
-
// compatible transform (i.e. "__esModule" has not been set), then set
|
|
24
|
-
// "default" to the CommonJS "module.exports" for node compatibility.
|
|
25
|
-
isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target,
|
|
26
|
-
mod
|
|
27
|
-
));
|
|
28
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
29
|
-
var dataLoader_exports = {};
|
|
30
|
-
__export(dataLoader_exports, {
|
|
31
|
-
DataLoader: () => import_dataloader.default,
|
|
32
|
-
Id: () => Id,
|
|
33
|
-
Loader: () => Loader,
|
|
34
|
-
Mixed: () => Mixed,
|
|
35
|
-
ObjectId: () => ObjectId,
|
|
36
|
-
createArrayElementLoader: () => createArrayElementLoader,
|
|
37
|
-
createArrayLoader: () => createArrayLoader,
|
|
38
|
-
createLoader: () => createLoader,
|
|
39
|
-
createQueryLoader: () => createQueryLoader,
|
|
40
|
-
getLoaderMetas: () => getLoaderMetas
|
|
41
|
-
});
|
|
42
|
-
module.exports = __toCommonJS(dataLoader_exports);
|
|
43
|
-
var import_dataloader = __toESM(require("dataloader"));
|
|
44
|
-
var import_lodash = require("lodash");
|
|
45
|
-
var import_mongoose = require("mongoose");
|
|
46
|
-
const Id = import_mongoose.Types.ObjectId;
|
|
47
|
-
const ObjectId = import_mongoose.Schema.Types.ObjectId;
|
|
48
|
-
const Mixed = import_mongoose.Schema.Types.Mixed;
|
|
49
|
-
const createLoader = /* @__PURE__ */ __name((model, fieldName = "_id", defaultQuery = {}) => {
|
|
50
|
-
return new import_dataloader.default((fields) => {
|
|
51
|
-
const query = {
|
|
52
|
-
...defaultQuery
|
|
53
|
-
};
|
|
54
|
-
query[fieldName] = {
|
|
55
|
-
$in: fields
|
|
56
|
-
};
|
|
57
|
-
const data = model.find(query).then((list) => {
|
|
58
|
-
const listByKey = (0, import_lodash.keyBy)(list, fieldName);
|
|
59
|
-
return fields.map((id) => (0, import_lodash.get)(listByKey, id, null));
|
|
60
|
-
});
|
|
61
|
-
return data;
|
|
62
|
-
}, {
|
|
63
|
-
name: "dataloader",
|
|
64
|
-
cache: false
|
|
65
|
-
});
|
|
66
|
-
}, "createLoader");
|
|
67
|
-
const createArrayLoader = /* @__PURE__ */ __name((model, fieldName = "_id", defaultQuery = {}) => {
|
|
68
|
-
return new import_dataloader.default((fields) => {
|
|
69
|
-
const query = {
|
|
70
|
-
...defaultQuery
|
|
71
|
-
};
|
|
72
|
-
query[fieldName] = {
|
|
73
|
-
$in: fields
|
|
74
|
-
};
|
|
75
|
-
const data = model.find(query).then((list) => {
|
|
76
|
-
return fields.map((field) => list.filter((item) => field === item[fieldName]));
|
|
77
|
-
});
|
|
78
|
-
return data;
|
|
79
|
-
});
|
|
80
|
-
}, "createArrayLoader");
|
|
81
|
-
const createArrayElementLoader = /* @__PURE__ */ __name((model, fieldName = "_id", defaultQuery = {}) => {
|
|
82
|
-
return new import_dataloader.default((fields) => {
|
|
83
|
-
const query = {
|
|
84
|
-
...defaultQuery
|
|
85
|
-
};
|
|
86
|
-
query[fieldName] = {
|
|
87
|
-
$in: fields
|
|
88
|
-
};
|
|
89
|
-
const data = model.find(query).then((list) => {
|
|
90
|
-
const flat = (0, import_lodash.flatMap)(list, (dat) => dat[fieldName].map((datField) => ({
|
|
91
|
-
...dat.toObject(),
|
|
92
|
-
key: datField
|
|
93
|
-
})));
|
|
94
|
-
const listByKey = (0, import_lodash.groupBy)(flat, (dat) => dat.key);
|
|
95
|
-
return fields.map((id) => (0, import_lodash.get)(listByKey, id, null));
|
|
96
|
-
});
|
|
97
|
-
return data;
|
|
98
|
-
}, {
|
|
99
|
-
name: "dataloader",
|
|
100
|
-
cache: false
|
|
101
|
-
});
|
|
102
|
-
}, "createArrayElementLoader");
|
|
103
|
-
const createQueryLoader = /* @__PURE__ */ __name((model, queryKeys, defaultQuery = {}) => {
|
|
104
|
-
return new import_dataloader.default((queries) => {
|
|
105
|
-
const query = {
|
|
106
|
-
$and: [
|
|
107
|
-
{
|
|
108
|
-
$or: queries
|
|
109
|
-
},
|
|
110
|
-
defaultQuery
|
|
111
|
-
]
|
|
112
|
-
};
|
|
113
|
-
const getQueryKey = /* @__PURE__ */ __name((query2) => queryKeys.map((key) => query2[key].toString()).join(""), "getQueryKey");
|
|
114
|
-
const data = model.find(query).then((list) => {
|
|
115
|
-
const listByKey = (0, import_lodash.keyBy)(list, getQueryKey);
|
|
116
|
-
return queries.map((query2) => (0, import_lodash.get)(listByKey, getQueryKey(query2), null));
|
|
117
|
-
});
|
|
118
|
-
return data;
|
|
119
|
-
}, {
|
|
120
|
-
name: "dataloader",
|
|
121
|
-
cache: false
|
|
122
|
-
});
|
|
123
|
-
}, "createQueryLoader");
|
|
124
|
-
const getLoaderMetaMapByPrototype = /* @__PURE__ */ __name((prototype) => {
|
|
125
|
-
const loaderMetaMap = Reflect.getOwnMetadata("loaders", prototype) ?? /* @__PURE__ */ new Map();
|
|
126
|
-
return loaderMetaMap;
|
|
127
|
-
}, "getLoaderMetaMapByPrototype");
|
|
128
|
-
const getLoaderMetas = /* @__PURE__ */ __name((target) => {
|
|
129
|
-
const metas = [
|
|
130
|
-
...getLoaderMetaMapByPrototype(target.prototype).values()
|
|
131
|
-
];
|
|
132
|
-
return metas;
|
|
133
|
-
}, "getLoaderMetas");
|
|
134
|
-
const Loader = {
|
|
135
|
-
ByField: /* @__PURE__ */ __name((fieldName, defaultQuery = {}) => {
|
|
136
|
-
return function(target, key) {
|
|
137
|
-
const loaderMetaMap = getLoaderMetaMapByPrototype(target);
|
|
138
|
-
loaderMetaMap.set(key, {
|
|
139
|
-
key,
|
|
140
|
-
type: "Field",
|
|
141
|
-
fieldName,
|
|
142
|
-
defaultQuery
|
|
143
|
-
});
|
|
144
|
-
Reflect.defineMetadata("loaders", loaderMetaMap, target);
|
|
145
|
-
};
|
|
146
|
-
}, "ByField"),
|
|
147
|
-
ByArrayField: /* @__PURE__ */ __name((fieldName, defaultQuery = {}) => {
|
|
148
|
-
return function(target, key) {
|
|
149
|
-
const loaderMetaMap = getLoaderMetaMapByPrototype(target);
|
|
150
|
-
loaderMetaMap.set(key, {
|
|
151
|
-
key,
|
|
152
|
-
type: "ArrayField",
|
|
153
|
-
fieldName,
|
|
154
|
-
defaultQuery
|
|
155
|
-
});
|
|
156
|
-
Reflect.defineMetadata("loaders", loaderMetaMap, target);
|
|
157
|
-
};
|
|
158
|
-
}, "ByArrayField"),
|
|
159
|
-
ByQuery: /* @__PURE__ */ __name((queryKeys, defaultQuery = {}) => {
|
|
160
|
-
return function(target, key) {
|
|
161
|
-
const loaderMetaMap = getLoaderMetaMapByPrototype(target);
|
|
162
|
-
loaderMetaMap.set(key, {
|
|
163
|
-
key,
|
|
164
|
-
type: "Query",
|
|
165
|
-
queryKeys,
|
|
166
|
-
defaultQuery
|
|
167
|
-
});
|
|
168
|
-
Reflect.defineMetadata("loaders", loaderMetaMap, target);
|
|
169
|
-
};
|
|
170
|
-
}, "ByQuery")
|
|
171
|
-
};
|
|
172
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
173
|
-
0 && (module.exports = {
|
|
174
|
-
DataLoader,
|
|
175
|
-
Id,
|
|
176
|
-
Loader,
|
|
177
|
-
Mixed,
|
|
178
|
-
ObjectId,
|
|
179
|
-
createArrayElementLoader,
|
|
180
|
-
createArrayLoader,
|
|
181
|
-
createLoader,
|
|
182
|
-
createQueryLoader,
|
|
183
|
-
getLoaderMetas
|
|
184
|
-
});
|
package/src/database.js
DELETED
|
@@ -1,410 +0,0 @@
|
|
|
1
|
-
var __defProp = Object.defineProperty;
|
|
2
|
-
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
3
|
-
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
4
|
-
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
5
|
-
var __name = (target, value) => __defProp(target, "name", { value, configurable: true });
|
|
6
|
-
var __export = (target, all) => {
|
|
7
|
-
for (var name in all)
|
|
8
|
-
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
-
};
|
|
10
|
-
var __copyProps = (to, from, except, desc) => {
|
|
11
|
-
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
-
for (let key of __getOwnPropNames(from))
|
|
13
|
-
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
-
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
-
}
|
|
16
|
-
return to;
|
|
17
|
-
};
|
|
18
|
-
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
-
var database_exports = {};
|
|
20
|
-
__export(database_exports, {
|
|
21
|
-
databaseModelOf: () => databaseModelOf
|
|
22
|
-
});
|
|
23
|
-
module.exports = __toCommonJS(database_exports);
|
|
24
|
-
var import_base = require("@akanjs/base");
|
|
25
|
-
var import_common = require("@akanjs/common");
|
|
26
|
-
var import_constant = require("@akanjs/constant");
|
|
27
|
-
var import_nest = require("@akanjs/nest");
|
|
28
|
-
var import_dataLoader = require("./dataLoader");
|
|
29
|
-
var import_schema = require("./schema");
|
|
30
|
-
let CacheDatabase = class CacheDatabase2 {
|
|
31
|
-
static {
|
|
32
|
-
__name(this, "CacheDatabase");
|
|
33
|
-
}
|
|
34
|
-
refName;
|
|
35
|
-
redis;
|
|
36
|
-
logger;
|
|
37
|
-
constructor(refName, redis) {
|
|
38
|
-
this.refName = refName;
|
|
39
|
-
this.redis = redis;
|
|
40
|
-
this.logger = new import_common.Logger(`${refName}Cache`);
|
|
41
|
-
}
|
|
42
|
-
async set(topic, key, value, option = {}) {
|
|
43
|
-
const setOption = {
|
|
44
|
-
PXAT: option.expireAt?.toDate().getTime()
|
|
45
|
-
};
|
|
46
|
-
await this.redis.set(`${this.refName}:${topic}:${key}`, value, setOption);
|
|
47
|
-
}
|
|
48
|
-
async get(topic, key) {
|
|
49
|
-
const value = await this.redis.get(`${this.refName}:${topic}:${key}`);
|
|
50
|
-
return value;
|
|
51
|
-
}
|
|
52
|
-
async delete(topic, key) {
|
|
53
|
-
await this.redis.del(`${this.refName}:${topic}:${key}`);
|
|
54
|
-
}
|
|
55
|
-
};
|
|
56
|
-
let SearchDatabase = class SearchDatabase2 {
|
|
57
|
-
static {
|
|
58
|
-
__name(this, "SearchDatabase");
|
|
59
|
-
}
|
|
60
|
-
refName;
|
|
61
|
-
meili;
|
|
62
|
-
logger;
|
|
63
|
-
index;
|
|
64
|
-
constructor(refName, meili) {
|
|
65
|
-
this.refName = refName;
|
|
66
|
-
this.meili = meili;
|
|
67
|
-
this.logger = new import_common.Logger(`${refName}Search`);
|
|
68
|
-
this.index = meili.index((0, import_common.lowerlize)(refName));
|
|
69
|
-
}
|
|
70
|
-
async searchIds(searchText, option = {}) {
|
|
71
|
-
const { skip = 0, limit = import_constant.DEFAULT_PAGE_SIZE, sort } = option;
|
|
72
|
-
if (!searchText) {
|
|
73
|
-
const { results, total } = await this.index.getDocuments({
|
|
74
|
-
offset: skip ?? 0,
|
|
75
|
-
limit: limit ?? 0
|
|
76
|
-
});
|
|
77
|
-
return {
|
|
78
|
-
ids: results.map((result) => result.id),
|
|
79
|
-
total
|
|
80
|
-
};
|
|
81
|
-
}
|
|
82
|
-
const { hits, estimatedTotalHits } = await this.index.search(searchText, {
|
|
83
|
-
offset: skip ?? 0,
|
|
84
|
-
limit: limit ?? 0,
|
|
85
|
-
sort: sort ?? [],
|
|
86
|
-
filter: option.filter,
|
|
87
|
-
attributesToRetrieve: [
|
|
88
|
-
"id"
|
|
89
|
-
]
|
|
90
|
-
});
|
|
91
|
-
return {
|
|
92
|
-
ids: hits.map((hit) => hit.id),
|
|
93
|
-
total: estimatedTotalHits
|
|
94
|
-
};
|
|
95
|
-
}
|
|
96
|
-
async count(searchText, option = {}) {
|
|
97
|
-
const { skip = 0, limit = import_constant.DEFAULT_PAGE_SIZE, sort = "" } = option;
|
|
98
|
-
if (!searchText) {
|
|
99
|
-
const { results, total } = await this.index.getDocuments({
|
|
100
|
-
offset: skip ?? 0,
|
|
101
|
-
limit: limit ?? 0
|
|
102
|
-
});
|
|
103
|
-
return total;
|
|
104
|
-
}
|
|
105
|
-
const { hits, estimatedTotalHits } = await this.index.search(searchText, {
|
|
106
|
-
offset: skip ?? 0,
|
|
107
|
-
limit: limit ?? 0,
|
|
108
|
-
filter: option.filter,
|
|
109
|
-
attributesToRetrieve: [
|
|
110
|
-
"id"
|
|
111
|
-
]
|
|
112
|
-
});
|
|
113
|
-
return estimatedTotalHits;
|
|
114
|
-
}
|
|
115
|
-
};
|
|
116
|
-
let DatabaseModelStorage = class DatabaseModelStorage2 {
|
|
117
|
-
static {
|
|
118
|
-
__name(this, "DatabaseModelStorage");
|
|
119
|
-
}
|
|
120
|
-
__ModelType__ = "DatabaseModelStorage";
|
|
121
|
-
};
|
|
122
|
-
const databaseModelOf = /* @__PURE__ */ __name((database, model, redis, meili) => {
|
|
123
|
-
const [modelName, className] = [
|
|
124
|
-
database.refName,
|
|
125
|
-
(0, import_common.capitalize)(database.refName)
|
|
126
|
-
];
|
|
127
|
-
const insightFieldMetas = (0, import_constant.getFieldMetas)(database.Insight);
|
|
128
|
-
const accumulator = Object.fromEntries(insightFieldMetas.map((fieldMeta) => [
|
|
129
|
-
fieldMeta.key,
|
|
130
|
-
fieldMeta.accumulate
|
|
131
|
-
]));
|
|
132
|
-
const defaultInsight = Object.fromEntries(insightFieldMetas.map((fieldMeta) => [
|
|
133
|
-
fieldMeta.key,
|
|
134
|
-
fieldMeta.default
|
|
135
|
-
]));
|
|
136
|
-
const makeSafeQuery = /* @__PURE__ */ __name((query) => ({
|
|
137
|
-
removedAt: {
|
|
138
|
-
$exists: false
|
|
139
|
-
},
|
|
140
|
-
...query ?? {}
|
|
141
|
-
}), "makeSafeQuery");
|
|
142
|
-
const makeSafeMatchStage = /* @__PURE__ */ __name((query) => ({
|
|
143
|
-
$match: {
|
|
144
|
-
removedAt: {
|
|
145
|
-
$exists: false
|
|
146
|
-
},
|
|
147
|
-
...(0, import_schema.convertAggregateMatch)(query)
|
|
148
|
-
}
|
|
149
|
-
}), "makeSafeMatchStage");
|
|
150
|
-
const getListQuery = /* @__PURE__ */ __name((query, queryOption) => {
|
|
151
|
-
const find = makeSafeQuery(query);
|
|
152
|
-
const sort = (0, import_constant.getFilterSort)(database.Filter, queryOption?.sort ?? "latest");
|
|
153
|
-
const skip = queryOption?.skip ?? 0;
|
|
154
|
-
const limit = queryOption?.limit === null ? import_constant.DEFAULT_PAGE_SIZE : queryOption?.limit ?? 0;
|
|
155
|
-
const select = queryOption?.select;
|
|
156
|
-
const sample = queryOption?.sample;
|
|
157
|
-
return {
|
|
158
|
-
find,
|
|
159
|
-
sort,
|
|
160
|
-
skip,
|
|
161
|
-
limit,
|
|
162
|
-
select,
|
|
163
|
-
sample
|
|
164
|
-
};
|
|
165
|
-
}, "getListQuery");
|
|
166
|
-
const getFindQuery = /* @__PURE__ */ __name((query, queryOption) => {
|
|
167
|
-
const find = makeSafeQuery(query);
|
|
168
|
-
const sort = (0, import_constant.getFilterSort)(database.Filter, queryOption?.sort ?? "latest");
|
|
169
|
-
const skip = queryOption?.skip ?? 0;
|
|
170
|
-
const select = queryOption?.select;
|
|
171
|
-
const sample = queryOption?.sample ?? false;
|
|
172
|
-
return {
|
|
173
|
-
find,
|
|
174
|
-
sort,
|
|
175
|
-
skip,
|
|
176
|
-
select,
|
|
177
|
-
sample
|
|
178
|
-
};
|
|
179
|
-
}, "getFindQuery");
|
|
180
|
-
const getSearchSort = /* @__PURE__ */ __name((sortKey) => {
|
|
181
|
-
const sort = (0, import_constant.getFilterSort)(database.Filter, sortKey ?? "latest");
|
|
182
|
-
return Object.entries(sort).map(([key, value]) => `${key}:${value === 1 ? "asc" : "desc"}`);
|
|
183
|
-
}, "getSearchSort");
|
|
184
|
-
const loader = (0, import_dataLoader.createLoader)(model);
|
|
185
|
-
const cacheDatabase = new CacheDatabase(database.refName, redis);
|
|
186
|
-
const searchDatabase = new SearchDatabase(database.refName, meili);
|
|
187
|
-
const DatabaseModel = Reflect.getMetadata(database.refName, DatabaseModelStorage.prototype) ?? class DatabaseModel {
|
|
188
|
-
static {
|
|
189
|
-
__name(this, "DatabaseModel");
|
|
190
|
-
}
|
|
191
|
-
logger = new import_common.Logger(`${modelName}Model`);
|
|
192
|
-
__model = model;
|
|
193
|
-
__cache = cacheDatabase;
|
|
194
|
-
__search = searchDatabase;
|
|
195
|
-
__loader = loader;
|
|
196
|
-
async __list(query, queryOption) {
|
|
197
|
-
const { find, sort, skip, limit, select, sample } = getListQuery(query, queryOption);
|
|
198
|
-
return sample ? await this.__model.sample(find, limit) : await this.__model.find(find, select).sort(sort).skip(skip).limit(limit);
|
|
199
|
-
}
|
|
200
|
-
async __listIds(query, queryOption) {
|
|
201
|
-
const { find, sort, skip, limit, sample } = getListQuery(query, queryOption);
|
|
202
|
-
return (sample ? await this.__model.sample(find, limit, [
|
|
203
|
-
{
|
|
204
|
-
$project: {
|
|
205
|
-
_id: 1
|
|
206
|
-
}
|
|
207
|
-
}
|
|
208
|
-
]) : await this.__model.find(find).sort(sort).skip(skip).limit(limit).select("_id")).map(({ _id }) => _id.toString());
|
|
209
|
-
}
|
|
210
|
-
async __find(query, queryOption) {
|
|
211
|
-
const { find, sort, skip, select, sample } = getFindQuery(query, queryOption);
|
|
212
|
-
const doc = sample ? await this.__model.sampleOne(find) : await this.__model.findOne(find, select).sort(sort).skip(skip);
|
|
213
|
-
if (!doc) return null;
|
|
214
|
-
return doc;
|
|
215
|
-
}
|
|
216
|
-
async __findId(query, queryOption) {
|
|
217
|
-
const { find, sort, skip, sample } = getFindQuery(query, queryOption);
|
|
218
|
-
const doc = sample ? await this.__model.sampleOne(find, [
|
|
219
|
-
{
|
|
220
|
-
$project: {
|
|
221
|
-
_id: 1
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
]) : await this.__model.findOne(find).sort(sort).skip(skip).select("_id");
|
|
225
|
-
if (!doc) return null;
|
|
226
|
-
return doc._id.toString();
|
|
227
|
-
}
|
|
228
|
-
async __pick(query, queryOption) {
|
|
229
|
-
const { find, sort, skip, select, sample } = getFindQuery(query, queryOption);
|
|
230
|
-
const doc = sample ? await this.__model.sampleOne(find) : await this.__model.findOne(find, select).sort(sort).skip(skip);
|
|
231
|
-
if (!doc) throw new Error(`No Document (${database.refName}): ${JSON.stringify(query)}`);
|
|
232
|
-
return doc;
|
|
233
|
-
}
|
|
234
|
-
async __pickId(query, queryOption) {
|
|
235
|
-
const { find, sort, skip, sample } = getFindQuery(query, queryOption);
|
|
236
|
-
const doc = sample ? await this.__model.sampleOne(find, [
|
|
237
|
-
{
|
|
238
|
-
$project: {
|
|
239
|
-
_id: 1
|
|
240
|
-
}
|
|
241
|
-
}
|
|
242
|
-
]) : await this.__model.findOne(find).sort(sort).skip(skip).select("_id");
|
|
243
|
-
if (!doc) throw new Error(`No Document (${database.refName}): ${JSON.stringify(query)}`);
|
|
244
|
-
return doc._id.toString();
|
|
245
|
-
}
|
|
246
|
-
async __exists(query) {
|
|
247
|
-
const find = makeSafeQuery(query);
|
|
248
|
-
const existingId = await this.__model.exists(find);
|
|
249
|
-
return existingId?.toString() ?? null;
|
|
250
|
-
}
|
|
251
|
-
async __count(query) {
|
|
252
|
-
const find = makeSafeQuery(query);
|
|
253
|
-
return await this.__model.countDocuments(find);
|
|
254
|
-
}
|
|
255
|
-
async __insight(query) {
|
|
256
|
-
if (!accumulator) throw new Error(`No Insight (${database.refName})`);
|
|
257
|
-
const res = await this.__model.aggregate([
|
|
258
|
-
makeSafeMatchStage(query),
|
|
259
|
-
{
|
|
260
|
-
$group: {
|
|
261
|
-
_id: null,
|
|
262
|
-
...accumulator
|
|
263
|
-
}
|
|
264
|
-
}
|
|
265
|
-
]);
|
|
266
|
-
const data = res[0];
|
|
267
|
-
return data ?? defaultInsight;
|
|
268
|
-
}
|
|
269
|
-
async getDefaultSummary(addQuery = {}) {
|
|
270
|
-
if (!database.Summary) return {};
|
|
271
|
-
const fieldMetas = (0, import_constant.getFieldMetas)(database.Summary);
|
|
272
|
-
const summary = {};
|
|
273
|
-
await Promise.all(fieldMetas.filter((fieldMeta) => !!fieldMeta.query).map(async (fieldMeta) => {
|
|
274
|
-
const query = typeof fieldMeta.query === "function" ? fieldMeta.query() : fieldMeta.query;
|
|
275
|
-
summary[fieldMeta.key] = query ? await model.countDocuments({
|
|
276
|
-
...query,
|
|
277
|
-
...addQuery
|
|
278
|
-
}) : fieldMeta.default;
|
|
279
|
-
}));
|
|
280
|
-
return summary;
|
|
281
|
-
}
|
|
282
|
-
async [`get${className}`](id) {
|
|
283
|
-
const doc = await this.__loader.load(id);
|
|
284
|
-
if (!doc) throw new Error(`No Document (${database.refName}): ${id}`);
|
|
285
|
-
return doc;
|
|
286
|
-
}
|
|
287
|
-
async [`load${className}`](id) {
|
|
288
|
-
return id ? await this.__loader.load(id) : null;
|
|
289
|
-
}
|
|
290
|
-
async [`load${className}Many`](ids) {
|
|
291
|
-
return await this.__loader.loadMany(ids);
|
|
292
|
-
}
|
|
293
|
-
async [`create${className}`](data) {
|
|
294
|
-
return await new this.__model(data).save();
|
|
295
|
-
}
|
|
296
|
-
async [`update${className}`](id, data) {
|
|
297
|
-
const doc = await this.__model.pickById(id);
|
|
298
|
-
return await doc.set(data).save();
|
|
299
|
-
}
|
|
300
|
-
async [`remove${className}`](id) {
|
|
301
|
-
const doc = await this.__model.pickById(id);
|
|
302
|
-
return await doc.set({
|
|
303
|
-
removedAt: (0, import_base.dayjs)()
|
|
304
|
-
}).save();
|
|
305
|
-
}
|
|
306
|
-
async [`search${className}`](searchText, queryOption = {}) {
|
|
307
|
-
const { skip, limit, sort } = queryOption;
|
|
308
|
-
const { ids, total } = await this.__search.searchIds(searchText, {
|
|
309
|
-
skip,
|
|
310
|
-
limit,
|
|
311
|
-
sort: getSearchSort(sort)
|
|
312
|
-
});
|
|
313
|
-
const docs = await this.__loader.loadMany(ids);
|
|
314
|
-
return {
|
|
315
|
-
docs,
|
|
316
|
-
count: total
|
|
317
|
-
};
|
|
318
|
-
}
|
|
319
|
-
async [`searchDocs${className}`](searchText, queryOption = {}) {
|
|
320
|
-
const { skip, limit, sort } = queryOption;
|
|
321
|
-
const { ids } = await this.__search.searchIds(searchText, {
|
|
322
|
-
skip,
|
|
323
|
-
limit,
|
|
324
|
-
sort: getSearchSort(sort)
|
|
325
|
-
});
|
|
326
|
-
return await this.__loader.loadMany(ids);
|
|
327
|
-
}
|
|
328
|
-
async [`searchCount${className}`](searchText) {
|
|
329
|
-
return await this.__search.count(searchText);
|
|
330
|
-
}
|
|
331
|
-
};
|
|
332
|
-
DatabaseModel.prototype[className] = model;
|
|
333
|
-
DatabaseModel.prototype[`${modelName}Loader`] = loader;
|
|
334
|
-
DatabaseModel.prototype[`${modelName}Cache`] = cacheDatabase;
|
|
335
|
-
DatabaseModel.prototype[`${modelName}Search`] = searchDatabase;
|
|
336
|
-
const getQueryDataFromKey = /* @__PURE__ */ __name((queryKey, args) => {
|
|
337
|
-
const lastArg = args.at(-1);
|
|
338
|
-
const hasQueryOption = lastArg && typeof lastArg === "object" && (typeof lastArg.select === "object" || typeof lastArg.skip === "number" || typeof lastArg.limit === "number" || typeof lastArg.sort === "string");
|
|
339
|
-
const queryFn = (0, import_constant.getFilterQuery)(database.Filter, queryKey);
|
|
340
|
-
const query = queryFn(...hasQueryOption ? args.slice(0, -1) : args);
|
|
341
|
-
const queryOption = hasQueryOption ? lastArg : {};
|
|
342
|
-
return {
|
|
343
|
-
query,
|
|
344
|
-
queryOption
|
|
345
|
-
};
|
|
346
|
-
}, "getQueryDataFromKey");
|
|
347
|
-
const filterKeyMetaMap = (0, import_constant.getFilterKeyMetaMapOnPrototype)(database.Filter.prototype);
|
|
348
|
-
const queryKeys = [
|
|
349
|
-
...filterKeyMetaMap.keys()
|
|
350
|
-
];
|
|
351
|
-
queryKeys.forEach((queryKey) => {
|
|
352
|
-
const queryFn = (0, import_constant.getFilterQuery)(database.Filter, queryKey);
|
|
353
|
-
DatabaseModel.prototype[`list${(0, import_common.capitalize)(queryKey)}`] = async function(...args) {
|
|
354
|
-
const { query, queryOption } = getQueryDataFromKey(queryKey, args);
|
|
355
|
-
return this.__list(query, queryOption);
|
|
356
|
-
};
|
|
357
|
-
DatabaseModel.prototype[`listIds${(0, import_common.capitalize)(queryKey)}`] = async function(...args) {
|
|
358
|
-
const { query, queryOption } = getQueryDataFromKey(queryKey, args);
|
|
359
|
-
return this.__listIds(query, queryOption);
|
|
360
|
-
};
|
|
361
|
-
DatabaseModel.prototype[`find${(0, import_common.capitalize)(queryKey)}`] = async function(...args) {
|
|
362
|
-
const { query, queryOption } = getQueryDataFromKey(queryKey, args);
|
|
363
|
-
return this.__find(query, queryOption);
|
|
364
|
-
};
|
|
365
|
-
DatabaseModel.prototype[`findId${(0, import_common.capitalize)(queryKey)}`] = async function(...args) {
|
|
366
|
-
const { query, queryOption } = getQueryDataFromKey(queryKey, args);
|
|
367
|
-
return this.__findId(query, queryOption);
|
|
368
|
-
};
|
|
369
|
-
DatabaseModel.prototype[`pick${(0, import_common.capitalize)(queryKey)}`] = async function(...args) {
|
|
370
|
-
const { query, queryOption } = getQueryDataFromKey(queryKey, args);
|
|
371
|
-
return this.__pick(query, queryOption);
|
|
372
|
-
};
|
|
373
|
-
DatabaseModel.prototype[`pickId${(0, import_common.capitalize)(queryKey)}`] = async function(...args) {
|
|
374
|
-
const { query, queryOption } = getQueryDataFromKey(queryKey, args);
|
|
375
|
-
return this.__pickId(query, queryOption);
|
|
376
|
-
};
|
|
377
|
-
DatabaseModel.prototype[`exists${(0, import_common.capitalize)(queryKey)}`] = async function(...args) {
|
|
378
|
-
const query = queryFn(...args);
|
|
379
|
-
return this.__exists(query);
|
|
380
|
-
};
|
|
381
|
-
DatabaseModel.prototype[`count${(0, import_common.capitalize)(queryKey)}`] = async function(...args) {
|
|
382
|
-
const query = queryFn(...args);
|
|
383
|
-
return this.__count(query);
|
|
384
|
-
};
|
|
385
|
-
DatabaseModel.prototype[`insight${(0, import_common.capitalize)(queryKey)}`] = async function(...args) {
|
|
386
|
-
const query = queryFn(...args);
|
|
387
|
-
return this.__insight(query);
|
|
388
|
-
};
|
|
389
|
-
});
|
|
390
|
-
const loaderMetas = (0, import_dataLoader.getLoaderMetas)(database.Model);
|
|
391
|
-
loaderMetas.forEach((loaderMeta) => {
|
|
392
|
-
const loader2 = loaderMeta.type === "Field" ? (0, import_dataLoader.createLoader)(model, loaderMeta.fieldName) : loaderMeta.type === "ArrayField" ? (0, import_dataLoader.createArrayLoader)(model, loaderMeta.fieldName) : loaderMeta.type === "Query" ? (0, import_dataLoader.createQueryLoader)(model, loaderMeta.queryKeys ?? []) : null;
|
|
393
|
-
DatabaseModel.prototype[loaderMeta.key] = loader2;
|
|
394
|
-
});
|
|
395
|
-
Object.getOwnPropertyNames(database.Model.prototype).forEach((key) => {
|
|
396
|
-
DatabaseModel.prototype[key] = database.Model.prototype[key];
|
|
397
|
-
});
|
|
398
|
-
const createDescriptor = Object.getOwnPropertyDescriptor(DatabaseModel.prototype, `create${className}`);
|
|
399
|
-
if (createDescriptor) (0, import_nest.Transaction)()(DatabaseModel.prototype, `create${className}`, createDescriptor);
|
|
400
|
-
const updateDescriptor = Object.getOwnPropertyDescriptor(DatabaseModel.prototype, `update${className}`);
|
|
401
|
-
if (updateDescriptor) (0, import_nest.Transaction)()(DatabaseModel.prototype, `update${className}`, updateDescriptor);
|
|
402
|
-
const removeDescriptor = Object.getOwnPropertyDescriptor(DatabaseModel.prototype, `remove${className}`);
|
|
403
|
-
if (removeDescriptor) (0, import_nest.Transaction)()(DatabaseModel.prototype, `remove${className}`, removeDescriptor);
|
|
404
|
-
Reflect.defineMetadata(database.refName, DatabaseModel, DatabaseModelStorage.prototype);
|
|
405
|
-
return new DatabaseModel();
|
|
406
|
-
}, "databaseModelOf");
|
|
407
|
-
// Annotate the CommonJS export names for ESM import in node:
|
|
408
|
-
0 && (module.exports = {
|
|
409
|
-
databaseModelOf
|
|
410
|
-
});
|