2016-06-22 1 views
3

私は、TypeScriptで書かれたAngular 2を使って簡単なWebアプリケーションを書いています。 MongoDBは、Mongooseフレームワーク上の私のデータベースであり、Expressフレームワーク上のNodeサーバー上で動作します。私のMongoDBとNodeコードはvanilla JSで書かれています。MongooseモデルをTypescriptクラスとして表現する

今、私は次のように国のためにマングースモデルを作成:

"use strict"; 
const Schema = require('mongoose').Schema, 
     db = require('../../config/database'); 

let countrySchema = new Schema({ 
    countryName: { type: String, index : { unique : true } } 
}); 

let Country = db.model('Country', countrySchema); 

module.exports = Country; 

は今、国は、私は私の物になりたいものです。私のアプリコンポーネントでは、私は持っています:

import { Component } from '@angular/core'; 
import { CountryService } from '../services/country.service'; 
import { Country } from '../models/country.model'; 

@Component({ 
    selector: 'my-app', 
    templateUrl: 'app/views/app.component.html', 
    providers: [ CountryService ] 
}) 

export class AppComponent { 
    originCountries: Country[]; 
    destinationCountries: Country[]; 
    constructor(private countryService: CountryService) { }; 

    ngOnInit() { 
    this.getCountries(); 
    } 

    getCountries() { 
    this.countryService.getCountries() 
     .then(countries => { 
     this.originCountries = countries; 
     this.destinationCountries = countries; 
     }); 
    } 
} 

countriesCountriesとdestinationCountriesはどのように国の配列になるのですか?私はカントリーモデルからカントリーをインポートすることはできません(それは私の頭の中で当時のように聞こえましたが)。

モンゴースモデルに基づくカントリークラスを作成する最適な方法は何ですか?

+1

私はTypeScriptについてよく知りませんが、このようなものはありません:https://github.com/Appsilon/styleguide/wiki/mongoose-typescript-models? –

答えて

4

あなたはこのICountryようなインターフェイスを使用します。

export interface ICountry { 
    _id: string; 
    name: string; 
} 

あなたは今、あなたのマングースのセットアップでこのインタフェースを使用することができます。

import mongoose = require('mongoose'); 
import { ICountry } from './interfaces'; 

var _schema: mongoose.Schema = new mongoose.Schema({ 
    name: { type: String, required: true, index: { unique: true } } 
}); 

type CountryType = ICountry & mongoose.Document; 

var _model = mongoose.model <CountryType> ('Country', _schema); 

export class Country { 

    static getAll(): Promise<Array<ICountry>> { 
    return new Promise<ICountry> ((resolve, reject) => { 
     _model.find((err, counties) => { 
     err ? reject(err) : resolve(counties); 
     }); 
    }); 
    } 
} 

とルート設定:

var router = express.Router(); 
router.get('/api/countries', (req, res) => { 
     Country.getAll().then(c => { 
     return res.json(c); 
     }); 
    }); 

そして、それを実装いくつかの方法が必要な場合、またはサービスクラスでインターフェイスを直接インポートする場合は、Angularアプリケーションで

import { ICountry } from '../../interfaces'; 
... 
countries: Array<ICountry> 
0

これは私が私のプロジェクトでそれを行う方法です。

私のスキーマファイルで:サーバー側のコードで

///<reference path="../typings/mongoose/mongoose.d.ts"/> 

import * as mongoose from 'mongoose'; 

var UserSchema = new mongoose.Schema({ 
    name: String, 
    // ... 
}); 

export interface IUser extends mongoose.Document { 
    _id: string; 
    name: string; 
    // ... 
} 

export interface IUserModel extends mongoose.Model<IUser> { } 

export var User: IUserModel = <IUserModel>mongoose.model<IUser>('User', UserSchema); 

:クライアント側のコードで

import {User, IUser, IUserModel} from '../schemas/user.schema'; 

// ... 

User.findOne({ ... }); 

I今IUserインターフェイスを使用できます。

import {IUser} from '../---/schemas/user.schema'; 

// ... 

userCache: Array<IUser>; 
+0

ほとんど同じですが、私はDocumentクラスからインタフェースを切り離したいと思っています。 – DNRN

+0

@DNRNの答えを見てください!彼はインターフェイスとドキュメントタイプを新しいタイプに組み合わせる方法を示しました。 – rinukkusu

関連する問題