Skip to content

Instantly share code, notes, and snippets.

@dbousamra
Last active August 29, 2015 14:13
Show Gist options
  • Select an option

  • Save dbousamra/2ed8cefb57372748cff7 to your computer and use it in GitHub Desktop.

Select an option

Save dbousamra/2ed8cefb57372748cff7 to your computer and use it in GitHub Desktop.
// Raw JSON types
interface CameraRaw {
_id: string;
name: string;
}
interface AccountRaw {
_id: string;
email: string;
password: string;
cameras: [CameraRaw]
}
// Domain objects
class Account {
_id: MongoId;
email: Email;
password: Password;
cameras: Array<Camera>
private constructor(accountRaw: AccountRaw) {
this._id = new MongoId(accountRaw._id);
this.password = new Password(accountRaw.password);
this.cameras = accountRaw.cameras.map(function(cameraRaw) {
return new Camera(cameraRaw)
})
}
// new code
public fromRaw(accountRaw: AccountRaw): Either<Account, ValidationError> {
var validation = AccountValidator(accountRaw, version = 3)
if (validaton.isValid()) {
return Left(new Account(accountRaw))
} else {
return Right(validation.error) // or some shit i dunno
}
}
}
class MongoId {
value: string;
constructor(value: string) {
this.value = value;
}
}
class Email {
value: string;
constructor(value: string) {
this.value = value;
}
}
class Password {
value: string;
constructor(value: string) {
this.value = value;
}
}
class Camera {
_id: MongoId;
name: String;
constructor(cameraRaw: CameraRaw) {
this._id = new MongoId(cameraRaw._id);
this.name = cameraRaw.name;
}
}
// Mongo mappers (ignore name - not sure what to call it)
interface MongoMapper<T> {
findOne(value: T): Option<T>
update(value: T): WriteResult<T>
delete(value: T): WriteResult<T>
}
class AccountMongoMapper implements MongoMapper<Account> {
findOne(account: Account): Option<T> {
// find from DB where _id === account._id.value
}
update(account: Account): WriteResult<T> {
// update account in DB
}
delete(account: Account): WriteResult<T> {
// delete account in DB
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment