update to mongodb

This commit is contained in:
2018-07-10 00:02:04 +02:00
parent bdf33ed1ca
commit 191ea5d3c6
19 changed files with 153 additions and 586 deletions

View File

@ -1,3 +1,3 @@
export * from './smartdata.classes.db';
export * from './smartdata.classes.dbtable';
export * from './smartdata.classes.dbdoc';
export * from './smartdata.classes.collection';
export * from './smartdata.classes.doc';

View File

@ -1,6 +1,6 @@
import * as plugins from './smartdata.plugins';
import { SmartdataDb } from './smartdata.classes.db';
import { smartDataDbDoc } from './smartdata.classes.dbdoc';
import { SmartDataDbDoc } from './smartdata.classes.doc';
export interface IFindOptions {
limit?: number;
@ -17,9 +17,9 @@ export interface IDocValidationFunc<T> {
* This is a decorator that will tell the decorated class what dbTable to use
* @param db
*/
export function Table(db: SmartdataDb) {
export function Collection(db: SmartdataDb) {
return function(constructor) {
constructor['mongoDbCollection'] = new SmartdataCollection(constructor, db);
constructor['smartdataCollection'] = new SmartdataCollection(constructor, db);
};
}
@ -32,7 +32,7 @@ export class SmartdataCollection<T> {
collectionName: string;
smartdataDb: SmartdataDb;
constructor(collectedClassArg: T & smartDataDbDoc<T>, smartDataDbArg: SmartdataDb) {
constructor(collectedClassArg: T & SmartDataDbDoc<T>, smartDataDbArg: SmartdataDb) {
// tell the collection where it belongs
this.collectionName = collectedClassArg.name;
this.smartdataDb = smartDataDbArg;
@ -55,6 +55,7 @@ export class SmartdataCollection<T> {
await this.smartdataDb.mongoDb.createCollection(this.collectionName);
}
this.mongoDbCollection = await this.smartdataDb.mongoDb.collection(this.collectionName);
console.log(`Successfully initiated Collection ${this.collectionName}`)
}
}
@ -70,15 +71,18 @@ export class SmartdataCollection<T> {
*/
async find(filterObject: any): Promise<any> {
await this.init();
const result = await this.mongoDbCollection.find(filterObject).toArray();
return result;
}
/**
* create an object in the database
*/
async insert(dbDocArg: T & smartDataDbDoc<T>): Promise<any> {
async insert(dbDocArg: T & SmartDataDbDoc<T>): Promise<any> {
await this.init();
await this.checkDoc(dbDocArg);
const saveableObject = await dbDocArg.createSavableObject();
console.log(saveableObject);
const result = await this.mongoDbCollection.insertOne(saveableObject);
return result;
}
@ -86,7 +90,7 @@ export class SmartdataCollection<T> {
/**
* inserts object into the DbCollection
*/
async update(dbDocArg: T & smartDataDbDoc<T>): Promise<any> {
async update(dbDocArg: T & SmartDataDbDoc<T>): Promise<any> {
await this.init();
await this.checkDoc(dbDocArg);
const saveableObject = await dbDocArg.createSavableObject();

View File

@ -1,7 +1,7 @@
import * as plugins from './smartdata.plugins';
import { Objectmap } from 'lik';
import { SmartdataCollection } from './smartdata.classes.dbtable';
import { SmartdataCollection } from './smartdata.classes.collection';
import * as mongoHelpers from './smartdata.mongohelpers';
@ -48,19 +48,20 @@ export class SmartdataDb {
let finalConnectionUrl = this.smartdataOptions.mongoDbUrl;
if (this.smartdataOptions.mongoDbPass) {
finalConnectionUrl = mongoHelpers.addPassword(
this.smartdataOptions.mongoDbName,
this.smartdataOptions.mongoDbUrl,
this.smartdataOptions.mongoDbPass
);
}
console.log(finalConnectionUrl);
this.mongoDbClient = await plugins.mongodb.MongoClient.connect(
finalConnectionUrl,
{}
{
useNewUrlParser: true
}
);
this.mongoDb = this.mongoDbClient.db(this.smartdataOptions.mongoDbName);
this.status = 'connected';
plugins.smartlog
.getDefaultLogger()
.info(`Connected to database ${this.smartdataOptions.mongoDbName}`);
console.log(`Connected to database ${this.smartdataOptions.mongoDbName}`);
}
/**

View File

@ -3,7 +3,7 @@ import * as plugins from './smartdata.plugins';
import { Objectmap } from 'lik';
import { SmartdataDb } from './smartdata.classes.db';
import { SmartdataCollection } from './smartdata.classes.dbtable';
import { SmartdataCollection } from './smartdata.classes.collection';
export type TDocCreation = 'db' | 'new' | 'mixed';
@ -11,7 +11,7 @@ export type TDocCreation = 'db' | 'new' | 'mixed';
* saveable - saveable decorator to be used on class properties
*/
export function svDb() {
return (target: smartDataDbDoc<any>, key: string) => {
return (target: SmartDataDbDoc<any>, key: string) => {
console.log('called sva');
if (!target.saveableProperties) {
target.saveableProperties = [];
@ -20,7 +20,7 @@ export function svDb() {
};
}
export class smartDataDbDoc<T> {
export class SmartDataDbDoc<T> {
/**
* the collection object an Doc belongs to
*/
@ -51,13 +51,13 @@ export class smartDataDbDoc<T> {
*/
constructor() {
this.name = this.constructor['name'];
this.collection = this.constructor['dbTable'];
this.collection = this.constructor['smartdataCollection'];
}
static async getInstances<T>(filterArg): Promise<T[]> {
let self: any = this; // fool typesystem
let referenceTable: SmartdataCollection<T> = self.dbTable;
const foundDocs = await referenceTable.find(filterArg);
let referenceMongoDBCollection: SmartdataCollection<T> = self.smartdataCollection;
const foundDocs = await referenceMongoDBCollection.find(filterArg);
const returnArray = [];
for (let item of foundDocs) {
let newInstance = new this();
@ -101,15 +101,15 @@ export class smartDataDbDoc<T> {
* also store any referenced objects to DB
* better for data consistency
*/
saveDeep(savedMapArg: Objectmap<smartDataDbDoc<any>> = null) {
saveDeep(savedMapArg: Objectmap<SmartDataDbDoc<any>> = null) {
if (!savedMapArg) {
savedMapArg = new Objectmap<smartDataDbDoc<any>>();
savedMapArg = new Objectmap<SmartDataDbDoc<any>>();
}
savedMapArg.add(this);
this.save();
for (let propertyKey in this) {
let property: any = this[propertyKey];
if (property instanceof smartDataDbDoc && !savedMapArg.checkForObject(property)) {
if (property instanceof SmartDataDbDoc && !savedMapArg.checkForObject(property)) {
property.saveDeep(savedMapArg);
}
}