Skip to content

Commit 40b804f

Browse files
authored
Merge pull request #3 from eug-vs/hooks
Expand authorId field in polls
2 parents de97719 + d40f6e6 commit 40b804f

File tree

10 files changed

+113
-17
lines changed

10 files changed

+113
-17
lines changed

app.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import feathers from '@feathersjs/feathers';
22
import express from '@feathersjs/express';
33
import socketio from '@feathersjs/socketio';
44
import '@feathersjs/transport-commons';
5+
import cors from 'cors';
56

67
import services from './services';
78

@@ -11,9 +12,11 @@ const app = express(feathers());
1112
app.use(express.json());
1213
app.use(express.urlencoded({ extended: true }));
1314
app.use(express.static(__dirname));
15+
app.use(express.errorHandler());
16+
app.use(cors());
17+
1418
app.configure(express.rest());
1519
app.configure(socketio());
16-
app.use(express.errorHandler());
1720
app.configure(services);
1821

1922

index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
import app from './app';
22
import mongoose from 'mongoose';
3+
import Promise from 'bluebird';
34

4-
5-
mongoose.Promise = global.Promise;
5+
mongoose.Promise = Promise;
66

77
mongoose.connect('mongodb://localhost:27017/which', { useNewUrlParser: true });
88

models/polls/poll.model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Model, model } from "mongoose"
2-
import { Poll, PollSchema } from './poll.schema';
2+
import { PollSchema, pollSchema } from './poll.schema';
33

4-
export default model<Poll, Model<Poll>>("Poll", PollSchema);
4+
export default model<PollSchema, Model<PollSchema>>("Poll", pollSchema);
55

models/polls/poll.schema.ts

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,30 @@
11
import { Document, Schema, Types } from 'mongoose';
2-
import { User } from '../users/user.schema';
2+
import { User } from '../users/user.schema'
33

44
interface ImageData {
55
url: string;
66
votes: number;
77
}
88

9-
export interface Poll extends Document {
10-
authorId: string;
9+
export interface Poll {
10+
author: User;
1111
contents: {
1212
left: ImageData;
1313
right: ImageData;
1414
};
1515
}
1616

17+
export interface PollSchema extends Document, Omit<Poll, 'author'> {
18+
authorId: string;
19+
}
20+
1721

1822
const imageDataSchema = {
1923
url: String,
2024
votes: Number
2125
}
2226

23-
export const PollSchema = new Schema({
27+
export const pollSchema = new Schema({
2428
contents: {
2529
left: imageDataSchema,
2630
right: imageDataSchema

models/users/user.model.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Model, model } from "mongoose"
2-
import { User, UserSchema } from './user.schema';
2+
import { UserSchema, userSchema } from './user.schema';
33

4-
export default model<User, Model<User>>("User", UserSchema);
4+
export default model<UserSchema, Model<UserSchema>>("User", userSchema);
55

models/users/user.schema.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
import { Document, Schema } from "mongoose"
22

3-
export interface User extends Document {
3+
export interface User {
44
name: string;
55
avatarUrl?: string;
66
age?: number;
77
}
88

9-
export const UserSchema = new Schema({
9+
export interface UserSchema extends Document, User {}
10+
11+
export const userSchema = new Schema({
1012
name: String,
1113
avatarUrl: {
1214
type: String,

package-lock.json

Lines changed: 38 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
2-
"name": "@oneflow/which-api",
2+
"name": "which-api",
33
"version": "1.0.0",
4-
"main": "index.js",
4+
"main": "index.ts",
55
"scripts": {
66
"test": "echo \"Error: no test specified\" && exit 1"
77
},
@@ -13,8 +13,9 @@
1313
"@feathersjs/feathers": "^4.5.3",
1414
"@feathersjs/socketio": "^4.5.4",
1515
"@feathersjs/transport-commons": "^4.5.3",
16-
"@types/mongoose": "^5.7.23",
16+
"cors": "^2.8.5",
1717
"feathers-mongoose": "^8.3.0",
18+
"lodash": "^4.17.15",
1819
"mongoose": "^5.9.18"
1920
},
2021
"repository": {
@@ -25,5 +26,11 @@
2526
"url": "https://github.com/eug-vs/which-api/issues"
2627
},
2728
"homepage": "https://github.com/eug-vs/which-api#readme",
28-
"description": ""
29+
"description": "",
30+
"devDependencies": {
31+
"@types/bluebird": "^3.5.32",
32+
"@types/cors": "^2.8.6",
33+
"@types/lodash": "^4.14.155",
34+
"@types/mongoose": "^5.7.23"
35+
}
2936
}

services/polls/polls.hooks.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { HookContext } from '@feathersjs/feathers';
2+
import bluebird from 'bluebird';
3+
import _ from 'lodash';
4+
5+
import { Poll, PollSchema } from '../../models/polls/poll.schema';
6+
import { User } from '../../models/users/user.schema';
7+
import UserModel from '../../models/users/user.model';
8+
9+
10+
const expandAuthor = async (poll: PollSchema): Promise<Poll | null> => {
11+
return UserModel.findById(poll.authorId)
12+
.lean<User>()
13+
.exec()
14+
.then((author: User | null): Poll | null => {
15+
return author && _.merge(_.omit(poll, 'authorId'), { author });
16+
})
17+
.catch(err => {
18+
console.error(err);
19+
return err;
20+
});
21+
};
22+
23+
const expandAuthorHook = async (context: HookContext): Promise<HookContext> => {
24+
context.result = await expandAuthor(context.result);
25+
return context;
26+
};
27+
28+
const expandAuthorManyHook = async (context: HookContext): Promise<HookContext> => {
29+
const polls = await bluebird.map(context.result, (poll: any) => expandAuthor(poll));
30+
context.result = _.compact(polls);
31+
return context;
32+
};
33+
34+
35+
export default {
36+
after: {
37+
get: [expandAuthorHook],
38+
find: [expandAuthorManyHook]
39+
}
40+
}

services/polls/polls.service.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import { Application } from '@feathersjs/express';
22
import Model from '../../models/polls/poll.model';
33
import service from 'feathers-mongoose';
4+
import hooks from './polls.hooks'
45

56
const PollService = service({ Model });
67

78
export default (app: Application): void => {
89
app.use('/polls', PollService);
10+
app.service('polls').hooks(hooks);
911
};
1012

0 commit comments

Comments
 (0)