@akanjs/document 0.0.39 → 0.0.40

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.
@@ -0,0 +1,153 @@
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 dbDecorators_exports = {};
20
+ __export(dbDecorators_exports, {
21
+ Database: () => Database,
22
+ DocumentDatabaseStorage: () => DocumentDatabaseStorage,
23
+ InputDatabaseStorage: () => InputDatabaseStorage,
24
+ MiddlewareDatabaseStorage: () => MiddlewareDatabaseStorage,
25
+ ModelDatabaseStorage: () => ModelDatabaseStorage,
26
+ beyond: () => beyond,
27
+ by: () => by,
28
+ dbOf: () => dbOf,
29
+ getAllDatabaseModelNames: () => getAllDatabaseModelNames,
30
+ inside: () => inside,
31
+ into: () => into
32
+ });
33
+ module.exports = __toCommonJS(dbDecorators_exports);
34
+ var import_constant = require("@akanjs/constant");
35
+ const dbOf = /* @__PURE__ */ __name((refName, Input, Doc, Model2, Middleware, Obj, Insight, Filter, Summary) => {
36
+ return {
37
+ refName,
38
+ Input,
39
+ Doc,
40
+ Model: Model2,
41
+ Middleware,
42
+ Obj,
43
+ Insight,
44
+ Filter,
45
+ Summary
46
+ };
47
+ }, "dbOf");
48
+ class InputDatabaseStorage {
49
+ static {
50
+ __name(this, "InputDatabaseStorage");
51
+ }
52
+ }
53
+ class DocumentDatabaseStorage {
54
+ static {
55
+ __name(this, "DocumentDatabaseStorage");
56
+ }
57
+ }
58
+ class ModelDatabaseStorage {
59
+ static {
60
+ __name(this, "ModelDatabaseStorage");
61
+ }
62
+ }
63
+ class MiddlewareDatabaseStorage {
64
+ static {
65
+ __name(this, "MiddlewareDatabaseStorage");
66
+ }
67
+ }
68
+ const getAllDatabaseModelNames = /* @__PURE__ */ __name(() => {
69
+ const modelNames = Reflect.getMetadataKeys(ModelDatabaseStorage.prototype) ?? [];
70
+ return modelNames;
71
+ }, "getAllDatabaseModelNames");
72
+ const Database = {
73
+ Input: /* @__PURE__ */ __name((returns) => {
74
+ return function(target) {
75
+ const modelRef = returns();
76
+ const classMeta = (0, import_constant.getClassMeta)(modelRef);
77
+ Reflect.defineMetadata(classMeta.refName, target, InputDatabaseStorage.prototype);
78
+ };
79
+ }, "Input"),
80
+ Document: /* @__PURE__ */ __name((returns) => {
81
+ return function(target) {
82
+ const modelRef = returns();
83
+ const classMeta = (0, import_constant.getClassMeta)(modelRef);
84
+ Reflect.defineMetadata(classMeta.refName, target, DocumentDatabaseStorage.prototype);
85
+ };
86
+ }, "Document"),
87
+ Model: /* @__PURE__ */ __name((returns) => {
88
+ return function(target) {
89
+ const modelRef = returns();
90
+ const classMeta = (0, import_constant.getClassMeta)(modelRef);
91
+ Reflect.defineMetadata(classMeta.refName, target, ModelDatabaseStorage.prototype);
92
+ };
93
+ }, "Model"),
94
+ Middleware: /* @__PURE__ */ __name((returns) => {
95
+ return function(target) {
96
+ const modelRef = returns();
97
+ const classMeta = (0, import_constant.getClassMeta)(modelRef);
98
+ Reflect.defineMetadata(classMeta.refName, target, MiddlewareDatabaseStorage.prototype);
99
+ };
100
+ }, "Middleware")
101
+ };
102
+ const Model = /* @__PURE__ */ __name((docRef, cnst) => {
103
+ let DefaultModel = class DefaultModel {
104
+ static {
105
+ __name(this, "DefaultModel");
106
+ }
107
+ };
108
+ return DefaultModel;
109
+ }, "Model");
110
+ const into = Model;
111
+ const AddModel = /* @__PURE__ */ __name((modelRef, cnst) => {
112
+ return modelRef;
113
+ }, "AddModel");
114
+ const inside = AddModel;
115
+ const by = /* @__PURE__ */ __name((modelRef, addRef) => {
116
+ if (!addRef) return InputOrDocument(modelRef);
117
+ return AddInputOrDocument(modelRef, addRef);
118
+ }, "by");
119
+ const InputOrDocument = /* @__PURE__ */ __name((inputRef) => {
120
+ return inputRef;
121
+ }, "InputOrDocument");
122
+ const AddInputOrDocument = /* @__PURE__ */ __name((modelRef, addRef) => {
123
+ const fieldMetaMap = (0, import_constant.getFieldMetaMap)(modelRef);
124
+ const addFieldMetas = (0, import_constant.getFieldMetaMap)(addRef);
125
+ (0, import_constant.setFieldMetaMap)(modelRef, new Map([
126
+ ...fieldMetaMap,
127
+ ...addFieldMetas
128
+ ]));
129
+ return modelRef;
130
+ }, "AddInputOrDocument");
131
+ const beyond = /* @__PURE__ */ __name((model, doc) => {
132
+ return class Middleware {
133
+ static {
134
+ __name(this, "Middleware");
135
+ }
136
+ onSchema(schema) {
137
+ }
138
+ };
139
+ }, "beyond");
140
+ // Annotate the CommonJS export names for ESM import in node:
141
+ 0 && (module.exports = {
142
+ Database,
143
+ DocumentDatabaseStorage,
144
+ InputDatabaseStorage,
145
+ MiddlewareDatabaseStorage,
146
+ ModelDatabaseStorage,
147
+ beyond,
148
+ by,
149
+ dbOf,
150
+ getAllDatabaseModelNames,
151
+ inside,
152
+ into
153
+ });
package/src/index.d.ts ADDED
@@ -0,0 +1,10 @@
1
+ export { B as BaseMiddleware, a as Database, k as DatabaseModel, D as Doc, b as DocumentDatabaseStorage, I as InputDatabaseStorage, M as Mdl, e as MiddlewareDatabaseStorage, c as ModelDatabaseStorage, S as SchemaOf, j as beyond, h as by, l as databaseModelOf, d as dbOf, g as getAllDatabaseModelNames, f as inside, i as into } from '../dbDecorators-B-tzSGvi.js';
2
+ export { convertAggregateMatch, getDefaultSchemaOptions } from './schema.js';
3
+ export { DataInputOf } from './types.js';
4
+ export { Id, Loader, LoaderMeta, Mixed, ObjectId, createArrayElementLoader, createArrayLoader, createLoader, createQueryLoader, getLoaderMetas } from './dataLoader.js';
5
+ export { FilterQuery as QueryOf } from 'mongoose';
6
+ export { default as DataLoader } from 'dataloader';
7
+ import 'dayjs';
8
+ import '../types-H2HI4YUe.js';
9
+ import 'meilisearch';
10
+ import 'redis';
package/src/index.js ADDED
@@ -0,0 +1,29 @@
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 __reExport = (target, mod, secondTarget) => (__copyProps(target, mod, "default"), secondTarget && __copyProps(secondTarget, mod, "default"));
14
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
15
+ var src_exports = {};
16
+ module.exports = __toCommonJS(src_exports);
17
+ __reExport(src_exports, require("./dbDecorators"), module.exports);
18
+ __reExport(src_exports, require("./schema"), module.exports);
19
+ __reExport(src_exports, require("./types"), module.exports);
20
+ __reExport(src_exports, require("./database"), module.exports);
21
+ __reExport(src_exports, require("./dataLoader"), module.exports);
22
+ // Annotate the CommonJS export names for ESM import in node:
23
+ 0 && (module.exports = {
24
+ ...require("./dbDecorators"),
25
+ ...require("./schema"),
26
+ ...require("./types"),
27
+ ...require("./database"),
28
+ ...require("./dataLoader")
29
+ });
@@ -0,0 +1,32 @@
1
+ import { FilterQuery, ProjectionType, PipelineStage } from 'mongoose';
2
+
3
+ declare const getDefaultSchemaOptions: <TSchema, TDocument>() => {
4
+ toJSON: {
5
+ getters: boolean;
6
+ virtuals: boolean;
7
+ };
8
+ toObject: {
9
+ getters: boolean;
10
+ virtuals: boolean;
11
+ };
12
+ _id: boolean;
13
+ id: boolean;
14
+ timestamps: boolean;
15
+ methods: {
16
+ refresh: () => Promise<any>;
17
+ };
18
+ statics: {
19
+ pickOne: (query: FilterQuery<TSchema>, projection?: ProjectionType<TSchema>) => Promise<TDocument>;
20
+ pickById: (docId: string | undefined, projection?: ProjectionType<TSchema>) => Promise<TDocument>;
21
+ sample: (query: FilterQuery<TSchema>, size?: number, aggregations?: PipelineStage[]) => Promise<TDocument[]>;
22
+ sampleOne: (query: FilterQuery<TSchema>, aggregations?: PipelineStage[]) => Promise<TDocument | null>;
23
+ addSummary: (prefix?: string, num?: number) => Promise<void>;
24
+ moveSummary: (prev: string, next: string, num?: number) => Promise<void>;
25
+ subSummary: (prefix?: string, num?: number) => Promise<void>;
26
+ };
27
+ };
28
+ declare const convertAggregateMatch: (query: any) => {
29
+ [k: string]: any;
30
+ };
31
+
32
+ export { convertAggregateMatch, getDefaultSchemaOptions };
package/src/schema.js ADDED
@@ -0,0 +1,156 @@
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 schema_exports = {};
20
+ __export(schema_exports, {
21
+ convertAggregateMatch: () => convertAggregateMatch,
22
+ getDefaultSchemaOptions: () => getDefaultSchemaOptions
23
+ });
24
+ module.exports = __toCommonJS(schema_exports);
25
+ var import_base = require("@akanjs/base");
26
+ var import_common = require("@akanjs/common");
27
+ var import_mongoose = require("mongoose");
28
+ const getDefaultSchemaOptions = /* @__PURE__ */ __name(() => ({
29
+ toJSON: {
30
+ getters: false,
31
+ virtuals: true
32
+ },
33
+ toObject: {
34
+ getters: false,
35
+ virtuals: true
36
+ },
37
+ _id: true,
38
+ id: true,
39
+ timestamps: true,
40
+ methods: {
41
+ refresh: /* @__PURE__ */ __name(async function() {
42
+ Object.assign(this, await this.constructor.findById(this._id));
43
+ return this;
44
+ }, "refresh")
45
+ },
46
+ statics: {
47
+ pickOne: /* @__PURE__ */ __name(async function(query, projection) {
48
+ const doc = await this.findOne(query, projection);
49
+ if (!doc) throw new Error("No Document");
50
+ return doc;
51
+ }, "pickOne"),
52
+ pickById: /* @__PURE__ */ __name(async function(docId, projection) {
53
+ if (!docId) throw new Error("No Document ID");
54
+ const doc = await this.findById(docId, projection);
55
+ if (!doc) throw new Error("No Document");
56
+ return doc;
57
+ }, "pickById"),
58
+ sample: /* @__PURE__ */ __name(async function(query, size = 1, aggregations = []) {
59
+ const objs = await this.aggregate([
60
+ {
61
+ $match: convertAggregateMatch(query)
62
+ },
63
+ {
64
+ $sample: {
65
+ size
66
+ }
67
+ },
68
+ ...aggregations
69
+ ]);
70
+ return objs.map((obj) => new this(obj));
71
+ }, "sample"),
72
+ sampleOne: /* @__PURE__ */ __name(async function(query, aggregations = []) {
73
+ const obj = await this.aggregate([
74
+ {
75
+ $match: convertAggregateMatch(query)
76
+ },
77
+ {
78
+ $sample: {
79
+ size: 1
80
+ }
81
+ },
82
+ ...aggregations
83
+ ]);
84
+ return obj.length ? new this(obj[0]) : null;
85
+ }, "sampleOne"),
86
+ addSummary: /* @__PURE__ */ __name(async function(prefix = "total", num = 1) {
87
+ const update = Array.isArray(prefix) ? {
88
+ $inc: {
89
+ ...prefix.reduce((acc, cur) => ({
90
+ ...acc,
91
+ [`${cur}${this.modelName}`]: num
92
+ }), {})
93
+ }
94
+ } : {
95
+ $inc: {
96
+ [`${prefix}${this.modelName}`]: num
97
+ }
98
+ };
99
+ await this.db.collection("summaries").updateOne({
100
+ status: "active"
101
+ }, update);
102
+ }, "addSummary"),
103
+ moveSummary: /* @__PURE__ */ __name(async function(prev, next, num = 1) {
104
+ await this.db.collection("summaries").updateOne({
105
+ status: "active"
106
+ }, {
107
+ $inc: {
108
+ [`${prev}${this.modelName}`]: -num,
109
+ [`${next}${this.modelName}`]: num
110
+ }
111
+ });
112
+ }, "moveSummary"),
113
+ subSummary: /* @__PURE__ */ __name(async function(prefix = "total", num = 1) {
114
+ const update = Array.isArray(prefix) ? {
115
+ $inc: {
116
+ ...prefix.reduce((acc, cur) => ({
117
+ ...acc,
118
+ [`${cur}${this.modelName}`]: -num
119
+ }), {})
120
+ }
121
+ } : {
122
+ $inc: {
123
+ [`${prefix}${this.modelName}`]: -num
124
+ }
125
+ };
126
+ await this.db.collection("summaries").updateOne({
127
+ status: "active"
128
+ }, update);
129
+ }, "subSummary")
130
+ }
131
+ }), "getDefaultSchemaOptions");
132
+ const convertOperatorValue = /* @__PURE__ */ __name((value) => {
133
+ if (Array.isArray(value)) return value.map((v) => convertOperatorValue(v));
134
+ else if (!value) return value;
135
+ else if ((0, import_mongoose.isValidObjectId)(value)) return new import_mongoose.Types.ObjectId(value);
136
+ else if ((0, import_common.isValidDate)(value)) return (0, import_base.dayjs)(value).toDate();
137
+ else if (value.constructor !== Object) return value;
138
+ else if (typeof value !== "object") return value;
139
+ else return Object.fromEntries(Object.entries(value).map(([key, value2]) => [
140
+ key,
141
+ convertOperatorValue(value2)
142
+ ]));
143
+ }, "convertOperatorValue");
144
+ const convertAggregateMatch = /* @__PURE__ */ __name((query) => {
145
+ return Object.fromEntries(Object.entries(query).map(([key, value]) => {
146
+ return [
147
+ key,
148
+ convertOperatorValue(value)
149
+ ];
150
+ }));
151
+ }, "convertAggregateMatch");
152
+ // Annotate the CommonJS export names for ESM import in node:
153
+ 0 && (module.exports = {
154
+ convertAggregateMatch,
155
+ getDefaultSchemaOptions
156
+ });
package/src/types.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ type DataInputOf<Input, Obj> = {
2
+ [K in keyof Input as Input[K] extends any[] ? never : K]: Input[K];
3
+ } & {
4
+ [K in keyof Input as Input[K] extends any[] ? K : never]?: Input[K];
5
+ } & Partial<Obj>;
6
+
7
+ export type { DataInputOf };
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);
@@ -0,0 +1,3 @@
1
+ type Type<T = any> = new (...args: any[]) => T;
2
+
3
+ export type { Type as T };