您可以将Typescript接口用作数据库的基本架构,但使用SimpleSchema和Collection2可以真正控制数据库架构。
有没有办法更好地将Typescript 界面与 SimpleSchema 集成(为了坚持DRY原则)?
在此示例代码中检查演示界面和架构之间的重复性:
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;
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);
}
}