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

Adr*_*o P 5 dry mongodb meteor typescript

您可以使用 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;
Run Code Online (Sandbox Code Playgroud)

演示-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);
  }
}
Run Code Online (Sandbox Code Playgroud)