Skip to content

Commit

Permalink
Add apollo to project; optimize rated_artists aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
isaldin committed Jan 19, 2020
1 parent 0a18fa2 commit 54e3c7e
Show file tree
Hide file tree
Showing 12 changed files with 1,553 additions and 74 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
node_modules/
.env
.DS_Store
.DS_Store
mongo-volume
16 changes: 10 additions & 6 deletions docker/init-mongo.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
db.createUser({
user: "rootuser",
pwd: "supersecurepassword",
user: 'rootuser',
pwd: 'supersecurepassword',
roles: [
{
role: "readWrite",
db: "usersdb"
}
]
role: 'readWrite',
db: 'usersdb',
},
{
role: 'dbOwner',
db: 'usersdb',
},
],
});
5 changes: 3 additions & 2 deletions docker/mongo-compose.yml
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
# https://medium.com/faun/managing-mongodb-on-docker-with-docker-compose-26bf8a0bbae3

version: "2.4"
version: '2.4'

services:
mongo:
image: mongo:4.2.2
container_name: "mongodb-gateway"
container_name: 'mongodb-gateway'
environment:
- MONGO_INITDB_DATABASE=usersdb
- MONGO_INITDB_ROOT_USERNAME=rootuser
- MONGO_INITDB_ROOT_PASSWORD=supersecurepassword
volumes:
- ./init-mongo.js:/docker-entrypoint-initdb.d/init-mongo.js:ro
- ../mongo-volume:/data/db
ports:
- 27018:27017
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
"dotenv": "8.2.0",
"fastify": "2.11.0",
"fastify-compress": "2.0.0",
"fastify-cors": "3.0.1",
"fastify-gql": "2.2.0",
"graphql": "14.5.8",
"js-base64": "2.5.1",
Expand All @@ -28,6 +29,7 @@
"@types/mongoose": "5.5.35",
"@types/ramda": "0.26.39",
"@types/supertest": "2.0.8",
"apollo": "2.21.3",
"jest": "24.9.0",
"mongodb-memory-server": "6.2.1",
"nodemon": "2.0.2",
Expand Down
4 changes: 3 additions & 1 deletion src/controllers/ratedArtists.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,11 @@ const getRatedArtists = async (input?: IGetRatedArtistsInput): Promise<IRatedArt
...defaultParams,
...input,
};
return RatedArtistModel.find({})

const result = await RatedArtistModel.find({})
.sort(buildSortingParams(params.sort))
.limit(params.limit);
return result;
};

export default { getRatedArtists };
Expand Down
34 changes: 23 additions & 11 deletions src/db/initRatedArtistsView.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,33 @@
import { Connection, Aggregate } from 'mongoose';

import { IRatedArtistModel } from '@app/models/ratedArtist.model';

const initRatedArtistsView = async (connection: Connection): Promise<void> => {
if (connection.collections['rated_artists']) {
try {
await connection.dropCollection('rated_artists');
} catch (error) {
if (error.codeName !== 'NamespaceNotFound') {
throw error;
}
try {
const dropResult = await connection.db.dropCollection('rated_artists');
console.log('rated_artists dropped: ', dropResult);
} catch (error) {
console.error(error);
if (error.codeName !== 'NamespaceNotFound') {
throw error;
}
}
await connection.createCollection('rated_artists', {

await connection.db.createCollection<IRatedArtistModel>('rated_artists', {
viewOn: 'artists',
pipeline: new Aggregate([
{
$addFields: {
rounds: {
$size: '$tracks',
},
},
},
{
$match: {
rounds: { $gt: 2 },
},
},
{
$lookup: {
from: 'tracks',
Expand All @@ -23,9 +38,6 @@ const initRatedArtistsView = async (connection: Connection): Promise<void> => {
},
{
$addFields: {
rounds: {
$size: '$tracks',
},
wins: {
$size: {
$filter: {
Expand Down
5 changes: 2 additions & 3 deletions src/graphql/types/track.type.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ObjectType, Field, ID } from "type-graphql";
import { ObjectType, Field, ID } from 'type-graphql';

@ObjectType()
@ObjectType('Track')
class TrackGQLType {
@Field(type => ID)
id: string;
Expand All @@ -11,7 +11,6 @@ class TrackGQLType {
@Field()
round: number;

@Field(type => ID)
artistID: string;
}

Expand Down
9 changes: 8 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'reflect-metadata';
import dotenv from 'dotenv';
import fastify from 'fastify';
import fastifyGQL from 'fastify-gql';
import fastifyCORS from 'fastify-cors';
import mongoose from 'mongoose';
import { buildSchema } from 'type-graphql';
import fastifyCompress from 'fastify-compress';
Expand All @@ -28,6 +29,10 @@ const buildServer = async (): Promise<FastifyInstanceType> => {
graphiql: !process.env.PRODUCTION,
});

app.register(fastifyCORS, {
origin: true,
});

return app;
};

Expand All @@ -39,9 +44,11 @@ const start = async () => {
useNewUrlParser: true,
useUnifiedTopology: true,
});

mongoose.set('debug', !process.env.PRODUCTION);
await mongoose.connection.db.setProfilingLevel('all');

await initRatedArtistsView(mongoose.connection);
// await initRatedArtistsView(mongoose.connection);

const server = await buildServer();
await server.listen(port, '0.0.0.0');
Expand Down
4 changes: 2 additions & 2 deletions src/models/artist.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Document, model, Model, Schema, Types } from 'mongoose';
import { ITrackModel } from '@app/models/track.model';

interface IArtistModel extends Document {
_id: Types.ObjectId;
artistId: number;
username: string;
name: string | null;
Expand All @@ -16,9 +15,10 @@ const ArtistSchema: Schema = new Schema({
required: true,
type: Number,
unique: true,
index: true,
},
username: { type: String, index: true, required: true },
name: { type: String, index: true },
name: { type: String, index: 'text' },
location: String,
tracks: [{ type: Schema.Types.ObjectId, ref: 'Track' }],
});
Expand Down
14 changes: 7 additions & 7 deletions src/models/ratedArtist.model.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Document, model, Model, Schema, Types } from "mongoose";
import { Document, model, Model, Schema, Types } from 'mongoose';

import { ITrackModel, TrackSchema } from "@app/models/track.model";
import { ITrackModel, TrackSchema } from '@app/models/track.model';

interface IRatedArtistModel extends Document {
_id: Types.ObjectId;
Expand All @@ -20,15 +20,15 @@ const RatedArtistSchema: Schema = new Schema(
name: String,
location: String,
tracks: [TrackSchema],
overallJudgesRating: Number,
overallPopularRating: Number
overallJudgesRating: { type: Number, index: true },
overallPopularRating: { type: Number, index: true },
},
{ collection: "rated_artists" }
{ collection: 'rated_artists' },
);

const RatedArtistModel: Model<IRatedArtistModel> = model<IRatedArtistModel>(
"RatedArtist",
RatedArtistSchema
'RatedArtist',
RatedArtistSchema,
);

export { RatedArtistModel, IRatedArtistModel };
8 changes: 4 additions & 4 deletions src/models/track.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ interface ITrackModel extends Document {
}

const TrackSchema = new Schema({
trackId: { type: Number, unique: true, required: true },
trackId: { type: Number, unique: true, required: true, index: true },
artist: {
ref: 'Artist',
required: true,
type: Schema.Types.ObjectId,
},
pair: { type: Schema.Types.ObjectId, ref: 'Artist' },
path: String,
popular_rating: Number,
judges_rating: Number,
popular_rating: { type: Number, index: true },
judges_rating: { type: Number, index: true },
judges_ratings: [],
round: { type: Number, required: true },
table: { type: String, enum: ['qualifying', 'yin', 'yang'], required: true },
lifebuoy: Boolean,
status: Number,
status: { type: Number, index: true },
});

const TrackModel: Model<ITrackModel> = model<ITrackModel>('Track', TrackSchema);
Expand Down
Loading

0 comments on commit 54e3c7e

Please sign in to comment.