有没有办法使用SimpleSchema和Typescript进行DRY?

时间:2017-03-06 03:28:22

标签: mongodb meteor typescript dry

您可以将Typescript接口用作数据库的基本架构,但使用SimpleSchema和Collection2可以真正控制数据库架构。

有没有办法更好地将Typescript 界面 SimpleSchema 集成(为了坚持DRY原则)?

在此示例代码中检查演示界面架构之间的重复性:

demo.collection.ts

import { MongoObservable } from "meteor-rxjs";
import { Meteor } from 'meteor/meteor';
import SimpleSchema from 'simpl-schema';

export interface Demo {
  name: string;
  age: number;
  location?: string;
  owner?: string;
}

const schema = new SimpleSchema({
  name: String,
  age: SimpleSchema.Integer,
  location: { type: String, optional: true},
  owner:  { type: String, optional: true}
});

let demoCollection = new Mongo.Collection<Demo>('demo-collection');
let demoObservable = new MongoObservable.Collection<Demo>(demoCollection);

demoCollection.attachSchema(schema);

export const DemoCollection = demoObservable;

演示data.services.ts

import { Injectable } from "@angular/core";
import { ObservableCursor } from "meteor-rxjs";

import { Demo, DemoCollection } from "../../../../both/collections/demo.collection";

@Injectable()
export class DemoDataService {
  private data: ObservableCursor<Demo>;

  constructor() {
    this.data = DemoCollection.find({});
  }

  public getData(): ObservableCursor<Demo> {
    return this.data;
  }

  public insertData(value: Demo): any {
    return DemoCollection.insert(value);
  }
}

0 个答案:

没有答案
相关问题