@mayajs/mongo
Advanced tools
Comparing version 0.6.0 to 0.7.0
/// <reference types="mongoose-paginate" /> | ||
import { PaginateModel, ConnectionOptions, Schema, Mongoose } from "mongoose"; | ||
interface ModelDictionary { | ||
[k: string]: PaginateModel<any>; | ||
import { PaginateModel, ConnectionOptions, Schema, Mongoose, Document } from "mongoose"; | ||
export declare type AnyDocument = Document<any, any, any>; | ||
export declare type ModelPaginate<T extends AnyDocument> = PaginateModel<T>; | ||
export interface DatabasesModels { | ||
[k: string]: ModelDictionary<any>; | ||
} | ||
export interface ModelDictionary<T extends AnyDocument> { | ||
[k: string]: ModelPaginate<T>; | ||
} | ||
interface SchemaObject { | ||
@@ -11,6 +16,3 @@ name: string; | ||
} | ||
export interface MongodbOptions extends MongoInstanceProps { | ||
name: string; | ||
} | ||
interface MongoInstanceProps { | ||
interface MongodbConfigOptions { | ||
connectionString: string; | ||
@@ -20,12 +22,14 @@ options?: ConnectionOptions; | ||
} | ||
export interface MongodbOptions extends MongodbConfigOptions { | ||
name: string; | ||
} | ||
interface MongoModelOptions { | ||
discriminators?: DiscriminatorObject[]; | ||
} | ||
export interface MongoInstance extends MongoInstanceProps { | ||
instance: Mongoose; | ||
models: ModelDictionary; | ||
} | ||
export interface MongoDatabases { | ||
[x: string]: MongoInstance; | ||
[x: string]: Mongoose; | ||
} | ||
export interface DatabaseConfig { | ||
[x: string]: MongodbConfigOptions; | ||
} | ||
export declare type DiscriminatorObject = { | ||
@@ -32,0 +36,0 @@ key: string; |
/// <reference types="mongoose-paginate" /> | ||
import { MongodbOptions, MongoInstance } from "./interfaces"; | ||
import mongoose from "mongoose"; | ||
import { MongodbOptions, AnyDocument, ModelPaginate } from "./interfaces"; | ||
import mongoose, { Mongoose } from "mongoose"; | ||
import { Services } from "@mayajs/router"; | ||
export declare class MongoDbServices extends Services { | ||
private list; | ||
private databases; | ||
private config; | ||
private models; | ||
constructor(); | ||
connect(name: string): Promise<typeof mongoose | undefined>; | ||
set options(value: MongodbOptions); | ||
database(name: string): MongoInstance; | ||
mapSchemas(name: string): void; | ||
private addDiscriminatorModel; | ||
database(name: string): Mongoose; | ||
model<T extends AnyDocument>(db: string, name: string): ModelPaginate<T>; | ||
mapSchemas(value: string): void; | ||
} |
@@ -32,10 +32,13 @@ "use strict"; | ||
super(); | ||
this.list = {}; | ||
this.databases = {}; | ||
this.config = {}; | ||
this.models = {}; | ||
} | ||
connect(name) { | ||
return __awaiter(this, void 0, void 0, function* () { | ||
const db = this.list[name]; | ||
const db = this.databases[name]; | ||
const config = this.config[name]; | ||
try { | ||
const instance = yield db.instance.connect(db.connectionString, db.options); | ||
this.list[name].instance = instance; | ||
const instance = yield db.connect(config.connectionString, config.options); | ||
this.databases[name] = instance; | ||
console.log(`\x1b[32m[mayajs] ${name} is connected!\x1b[0m`); | ||
@@ -52,27 +55,31 @@ return instance; | ||
const options = Object.assign({ useNewUrlParser: true, useCreateIndex: true, useUnifiedTopology: true, useFindAndModify: true }, rest.options); | ||
this.list[name] = Object.assign(Object.assign({}, rest), { options, instance: mongoose_1.default, models: {} }); | ||
this.databases[name] = mongoose_1.default; | ||
this.config[name] = Object.assign(Object.assign({}, rest), { options }); | ||
} | ||
database(name) { | ||
return this.list[name]; | ||
return this.databases[name]; | ||
} | ||
mapSchemas(name) { | ||
model(db, name) { | ||
return this.models[db][name]; | ||
} | ||
mapSchemas(value) { | ||
var _a; | ||
const db = this.list[name]; | ||
if (db.schemas && ((_a = db.schemas) === null || _a === void 0 ? void 0 : _a.length) > 0) { | ||
db.schemas.map(({ name, schema, options }) => { | ||
const model = db.instance.model(name, schema); | ||
if (options && (options === null || options === void 0 ? void 0 : options.discriminators) && (options === null || options === void 0 ? void 0 : options.discriminators.length) > 0) { | ||
options.discriminators.map(this.addDiscriminatorModel(db, model)); | ||
const db = this.databases[value]; | ||
const config = this.config[value]; | ||
if (this.models[value]) | ||
this.models[value] = {}; | ||
if (config.schemas && ((_a = config.schemas) === null || _a === void 0 ? void 0 : _a.length) > 0) { | ||
config.schemas.forEach(({ name, schema, options }) => { | ||
var _a; | ||
const model = db.model(name, schema); | ||
if ((options === null || options === void 0 ? void 0 : options.discriminators) && ((_a = options === null || options === void 0 ? void 0 : options.discriminators) === null || _a === void 0 ? void 0 : _a.length) > 0) { | ||
options === null || options === void 0 ? void 0 : options.discriminators.forEach((discriminator) => { | ||
this.models[value][discriminator.key] = model.discriminator(discriminator.key, discriminator.schema); | ||
}); | ||
} | ||
db.models[name] = model; | ||
this.models[value][name] = model; | ||
}); | ||
} | ||
} | ||
addDiscriminatorModel(db, modelInstance) { | ||
return (discriminator) => { | ||
const model = modelInstance.discriminator(discriminator.key, discriminator.schema); | ||
db.models[discriminator.key] = model; | ||
}; | ||
} | ||
} | ||
exports.MongoDbServices = MongoDbServices; |
{ | ||
"name": "@mayajs/mongo", | ||
"version": "0.6.0", | ||
"version": "0.7.0", | ||
"description": "MayaJS Mongo decorators and modules", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -1,2 +0,2 @@ | ||
<p align="center"><img src="https://github.com/mayajs/maya/blob/master/maya.svg"></p> | ||
<p align="center"><img src="https://github.com/mayajs/maya/blob/main/logo.svg"></p> | ||
<h1 align="center">Mongodb Module and Service</h1> | ||
@@ -3,0 +3,0 @@ |
12294
184