integration with mongo?
ghatul opened this issue · comments
i try to integrate project with mongo but it not working ,can u please tell me
@ghatul , I was thinking about it, when I will have a free time I will implement integration with mongo :)
@ghatul , can you provide more details what are not working for you ?
i use https://github.com/mgechev/angular2-seed this seed project and build my client and server in nodejs with typescript with mongo , i try to integrate client and server as per your seed ,i suceesfully integrate client but i getting confused for integration of server ...
@ghatul , any errors? Have you read the documentation in README.md
?
yes , i read that
@ghatul , can you show your .ts
or .js
code to connect with MongoDB
?
`import Mongoose = require("mongoose");
class DataAccess {
static mongooseInstance:any;
static mongooseConnection:Mongoose.Connection;
constructor() {
DataAccess.connect();
}
static connect():Mongoose.Connection {
if (this.mongooseInstance) return this.mongooseInstance;
this.mongooseConnection = Mongoose.connection;
this.mongooseConnection.once("open", () => {
console.log("Connected to mongodb.");
});
this.mongooseInstance = Mongoose.connect('mongodb://127.0.0.1/seed-backend');
return this.mongooseInstance;
}
}
DataAccess.connect();
export = DataAccess;
`
@ghatul , have you installed typings install dt~mongoose --save --global
?
You MongoDB
is running ? What error
do you have? Maybe you have some debug
log ?
Code in ts
or js
?
Hi, i'm using mongoose in my project with this seed and it's working as expected...
typings.json
"mongoose": "registry:dt/mongoose#4.5.4+20160807120805",
db.conf.ts
'use strict';
import * as mongoose from 'mongoose';
export class DBConfig {
static init(): void {
const URL = 'mongodb://xxxxx/database';
mongoose.connect(URL, (err) => {
if (err) {
console.log(err.message);
console.log(err);
} else {
console.log('Connected to MongoDb');
}
});
}
}
then in server/index.ts we just need to init the DBConfig
import { DBConfig } from './configs/db.conf';
var _clientDir = '../client';
var app = express();
export function init(port: number, mode: string) {
app.use(bodyParser.urlencoded({extended: false}));
app.use(bodyParser.json());
app.use(bodyParser.text());
app.use(compression());
// DB Init
DBConfig.init();
...
}
and this is one example of mongoose model
import * as mongoose from 'mongoose';
export class ExportState {
static Processing: string = 'Processing';
static Finished: string = 'Finished';
static Failed: string = 'Failed';
static Deleted: string = 'Deleted';
}
export interface FiltersInterface {
startDate: Date;
endDate: Date;
users: string[];
dataTypes: string[];
lowQualityValues: boolean;
}
export interface ExportResult {
medableExportDuration: number; // in miliseconds
internalExportDuration: number; // in miliseconds
numberOfLines: number;
fileSize: number; // in bytes
}
export interface ExportInterface {
userId: string;
name: string;
filters: FiltersInterface;
state: string;
fileUrl: string;
fileName: string;
exportResultDetails: ExportResult;
creationTime: Date;
deletionTime: Date;
}
export const DataExportSchema = new mongoose.Schema({
userId: String,
name: String,
filters: {
startDate: Date,
endDate: Date,
users: [String],
dataTypes: [String],
lowQualityValues: Boolean
},
state: String,
fileUrl: String,
fileName: String,
exportResultDetails: {
medableExportDuration: Number,
internalExportDuration: Number,
numberOfLines: Number,
fileSize: Number
},
creationTime: Date,
deletionTime: Date
}, {versionKey: false});
export interface DataExportType extends ExportInterface, mongoose.Document {
}
export const DataExport = mongoose.model < DataExportType >('DataExport', DataExportSchema);
thanks !
typings install dt~mongoose --save --global work for me