Skip to content

Instantly share code, notes, and snippets.

@taxilian
Created September 14, 2020 15:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save taxilian/b46dab394cd063f472ad1ef21b59a577 to your computer and use it in GitHub Desktop.
Save taxilian/b46dab394cd063f472ad1ef21b59a577 to your computer and use it in GitHub Desktop.
Example typescript pattern for defining a view using 'mongoose-decorators-ts'
export const readOnlyExcludeKeysModel = <const>[
'watch', 'bulkWrite', 'create', 'createCollection',
'syncIndexes', 'listIndexes', 'ensureIndexes',
'createIndexes', 'findByIdAndRemove', 'findByIdAndDelete',
'findByIdAndUpdate', 'findOneAndRemove', 'findOneAndDelete',
'findOneAndUpdate', 'insertMany', 'remove', 'deleteOne',
'deleteMany', 'replaceOne', 'update', 'updateMany', 'updateOne',
];
export type readOnlyExcludeKeysModel = typeof readOnlyExcludeKeysModel[number];
export const readOnlyExcludeKeysDoc = <const>[
'remove', 'save', 'set', 'overwrite', 'isModified', 'isDirectModified',
];
export type readOnlyExcludeKeysDoc = typeof readOnlyExcludeKeysDoc[number];
import { Db } from "mongodb";
import mongoose from "mongoose";
import { schemaDef, ModelFromSchemaDef, field } from "mongoose-decorators-ts";
import { ULSEntityModel, ULSLicenseModel, ULSAmateurModel } from "../uls";
import { readOnlyExcludeKeysModel } from "./common";
// Pipeline
const pipeline = [
{ $match: { applicant_type: 'Individual', } },
{ $project: {
"u_id" : 1,
"licensee_id" : 1,
"callsign": 1,
"first_name" : 1,
"middle_initial" : 1,
"last_name" : 1,
"suffix" : 1,
"address": 1,
"city" : 1,
"state" : 1,
"zip" : 1,
"frn" : 1,
} },
{ $lookup: {
"from" : "ulslicenses",
"let" : { "uid" : "$u_id" },
"pipeline" : [ {
"$match" : {
"$expr" : { "$eq" : [ "$u_id", "$$uid" ] },
"license_status" : "Active"
}
}, {
"$project" : {
"_id": 0,
"license_status" : 1,
"grant_date" : 1,
"expired_date" : 1,
"effective_date" : 1,
"cancellation_date" : 1,
}
}
], "as" : "ulsLicense"
} },
{ $lookup: {
"from" : "ulsamateurs",
"let" : { "uid" : "$u_id" },
"pipeline" : [ {
"$match" : {
"$expr" : { "$eq" : [ "$u_id", "$$uid" ] }
}
}, {
"$project" : {
"_id": 0,
"license_class" : 1,
"prev_license_class": 1,
}
}
],
"as" : "ulsAmateur"
} },
{ $unwind: { path : "$ulsAmateur", } },
{ $unwind: { path : "$ulsLicense", } },
{
$addFields: {
"license_status" : "$ulsLicense.license_status",
"grant_date" : "$ulsLicense.grant_date",
"expired_date" : "$ulsLicense.expired_date",
"effective_date" : "$ulsLicense.effective_date",
"license_class" : "$ulsAmateur.license_class",
"prev_license_class" : "$ulsAmateur.prev_license_class"
}
},
{
$unset: ["ulsAmateur", "ulsLicense"],
}
];
export async function register() {
try {
return await UlsActiveCallsignsModel.db.createCollection('ulsActiveCallsigns', {
viewOn: 'ulsentities',
pipeline,
});
} catch (err) {
console.warn("Could not create ulsActiveCallsigns view:", err);
}
}
@schemaDef({
schemaOpts: {
collection: 'ulsActiveCallsigns',
id: false,
autoIndex: false,
}
})
class UlsActiveCallsigns {
@field() u_id: ULSEntityModel['u_id'];
@field() callsign: ULSEntityModel['callsign'];
@field() licensee_id: ULSEntityModel['licensee_id'];
@field() first_name: ULSEntityModel['first_name'];
@field() middle_initial: ULSEntityModel['middle_initial'];
@field() last_name: ULSEntityModel['last_name'];
@field() suffix: ULSEntityModel['suffix'];
@field() address: ULSEntityModel['address'];
@field() city: ULSEntityModel['city'];
@field() state: ULSEntityModel['state'];
@field() zip: ULSEntityModel['zip'];
@field() frn: ULSEntityModel['frn'];
@field() license_status: ULSLicenseModel['license_status'];
@field() grant_date: ULSLicenseModel['grant_date'];
@field() expired_date: ULSLicenseModel['expired_date'];
@field() effective_date: ULSLicenseModel['effective_date'];
@field() license_class: ULSAmateurModel['license_class'];
@field() prev_license_class: ULSAmateurModel['prev_license_class'];
}
const fullModel = ModelFromSchemaDef(UlsActiveCallsigns);
export const UlsActiveCallsignsModel: Omit<typeof fullModel, typeof readOnlyExcludeKeysModel[number]> = fullModel;
export type UlsActiveCallsignsModel = Omit<mongoose.Document<UlsActiveCallsigns>, typeof readOnlyExcludeKeysModel[number]>;
export default UlsActiveCallsignsModel;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment