1 | /**
|
2 | * This file is part of the @egodigital/egoose distribution.
|
3 | * Copyright (c) e.GO Digital GmbH, Aachen, Germany (https://www.e-go-digital.com/)
|
4 | *
|
5 | * @egodigital/egoose is free software: you can redistribute it and/or modify
|
6 | * it under the terms of the GNU Lesser General Public License as
|
7 | * published by the Free Software Foundation, version 3.
|
8 | *
|
9 | * @egodigital/egoose is distributed in the hope that it will be useful, but
|
10 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
12 | * Lesser General Public License for more details.
|
13 | *
|
14 | * You should have received a copy of the GNU Lesser General Public License
|
15 | * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
16 | */
|
17 | import * as mongoose from 'mongoose';
|
18 | /**
|
19 | * A suggestion for a mongo log document.
|
20 | */
|
21 | export interface LogsDocument extends mongoose.Document {
|
22 | /**
|
23 | * The time, the entry has been created.
|
24 | */
|
25 | created: Date;
|
26 | /**
|
27 | * The message.
|
28 | */
|
29 | message: string;
|
30 | /**
|
31 | * The optional payload.
|
32 | */
|
33 | payload?: string;
|
34 | /**
|
35 | * The type.
|
36 | */
|
37 | type: number;
|
38 | /**
|
39 | * The time, the entry has been updated.
|
40 | */
|
41 | updated?: Date;
|
42 | /**
|
43 | * The UUID of the entry.
|
44 | */
|
45 | uuid: string;
|
46 | }
|
47 | /**
|
48 | * Options for connecting to a database.
|
49 | */
|
50 | export interface MongoDatabaseOptions {
|
51 | /**
|
52 | * A custom function, that creates a Mongoose connection
|
53 | * from objects, when executing MongoDatabase.connect().
|
54 | */
|
55 | readonly connector?: (opts: MongoDatabaseOptions) => mongoose.Connection | PromiseLike<mongoose.Connection>;
|
56 | /**
|
57 | * The name of the database.
|
58 | */
|
59 | readonly database: string;
|
60 | /**
|
61 | * The host address.
|
62 | */
|
63 | readonly host: string;
|
64 | /**
|
65 | * Custom options for an underlying (mongoose) connection.
|
66 | */
|
67 | readonly mongooseOptions?: any;
|
68 | /**
|
69 | * Additional options for the connection string.
|
70 | */
|
71 | readonly options?: string;
|
72 | /**
|
73 | * The password for the authentification.
|
74 | */
|
75 | readonly password?: string;
|
76 | /**
|
77 | * The TCP port.
|
78 | */
|
79 | readonly port: number;
|
80 | /**
|
81 | * The user name.
|
82 | */
|
83 | readonly user?: string;
|
84 | }
|
85 | /**
|
86 | * Global repository for MongoDB models.
|
87 | */
|
88 | export declare const MONGO_MODELS: {
|
89 | [name: string]: mongoose.Model<mongoose.Document>;
|
90 | };
|
91 | /**
|
92 | * Global repository for MongoDB schemas.
|
93 | */
|
94 | export declare const MONGO_SCHEMAS: {
|
95 | [name: string]: mongoose.Schema;
|
96 | };
|
97 | /**
|
98 | * A MongoDB connection.
|
99 | */
|
100 | export declare class MongoDatabase {
|
101 | readonly options: MongoDatabaseOptions;
|
102 | private _mongo;
|
103 | /**
|
104 | * Initializes a new instance of that class.
|
105 | *
|
106 | * @param {MongoDatabaseOptions} options Connection options.
|
107 | */
|
108 | constructor(options: MongoDatabaseOptions);
|
109 | /**
|
110 | * Starts a connection to the server.
|
111 | *
|
112 | * @return {Promise<boolean>} The promise, which indicates if operation was successful or not.
|
113 | */
|
114 | connect(): Promise<boolean>;
|
115 | /**
|
116 | * Closes the current connection.
|
117 | *
|
118 | * @return {Promise<boolean>} The promise, which indicates if operation was successful or not.
|
119 | */
|
120 | disconnect(): Promise<boolean>;
|
121 | /**
|
122 | * Gets if there is currently an open database connection or not.
|
123 | */
|
124 | readonly isConnected: boolean;
|
125 | /**
|
126 | * Returns a model by name.
|
127 | *
|
128 | * @param {string} name The name of the model.
|
129 | *
|
130 | * @return {mongoose.Model<T>} The model.
|
131 | */
|
132 | model<T extends mongoose.Document = mongoose.Document>(name: string): mongoose.Model<T>;
|
133 | /**
|
134 | * Gets the underlying database connection.
|
135 | */
|
136 | readonly mongo: mongoose.Connection;
|
137 | /**
|
138 | * Starts a query for a schema and a list of results.
|
139 | *
|
140 | * @param {string} schema The name of the schema.
|
141 | * @param {string} func The name of the initial function.
|
142 | * @param {any[]} [args] One or more argument for the function, like a condition.
|
143 | *
|
144 | * @return {mongoose.DocumentQuery<mongoose.Document[], mongoose.Document>} The query.
|
145 | */
|
146 | query<T extends mongoose.Document = mongoose.Document>(schema: string, func: string, ...args: any[]): mongoose.DocumentQuery<T[], T>;
|
147 | /**
|
148 | * Starts a query for a schema and a single result.
|
149 | *
|
150 | * @param {string} schema The name of the schema.
|
151 | * @param {string} func The name of the initial function.
|
152 | * @param {any[]} [args] One or more argument for the function, like a condition.
|
153 | *
|
154 | * @return {mongoose.DocumentQuery<mongoose.Document, mongoose.Document>} The query.
|
155 | */
|
156 | queryOne<T extends mongoose.Document = mongoose.Document>(schema: string, func: string, ...args: any[]): mongoose.DocumentQuery<T, T>;
|
157 | /**
|
158 | * Returns a schema by name.
|
159 | *
|
160 | * @param {string} name The name of the schema.
|
161 | *
|
162 | * @return {mongoose.Schema} The schema.
|
163 | */
|
164 | schema(name: string): mongoose.Schema;
|
165 | }
|
166 | /**
|
167 | * Initializes the schema for a 'logs' collection.
|
168 | */
|
169 | export declare function initLogsSchema(): void;
|
170 | /**
|
171 | * Checks if a value can be used as Mongo object ID or not.
|
172 | *
|
173 | * @param {any} val The value to check.
|
174 | *
|
175 | * @return {boolean} Can be used as object ID or not.
|
176 | */
|
177 | export declare function isMongoId(val: any): boolean;
|