Skip to content

Commit de97719

Browse files
committed
feat!: integrate mongoDB
1 parent 39d637a commit de97719

File tree

13 files changed

+337
-85
lines changed

13 files changed

+337
-85
lines changed

app.ts

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,5 @@ app.use(express.errorHandler());
1717
app.configure(services);
1818

1919

20-
// Mock data
21-
app.service('polls').create({
22-
contents: {
23-
left: {
24-
url: 'https://github.com/eug-vs.png',
25-
votes: 10
26-
},
27-
right: {
28-
url: 'https://github.com/ilyayudovin.png',
29-
votes: 15
30-
}
31-
}
32-
});
33-
34-
app.service('users').create({
35-
name: 'John Doe',
36-
age: 20,
37-
avatarUrl: 'https://github.com/ilyayudovin.png'
38-
});
39-
4020
export default app;
4121

index.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,16 @@
11
import app from './app';
2+
import mongoose from 'mongoose';
3+
4+
5+
mongoose.Promise = global.Promise;
6+
7+
mongoose.connect('mongodb://localhost:27017/which', { useNewUrlParser: true });
8+
9+
const db = mongoose.connection;
10+
db.on('error', console.error.bind(console, 'connection error:'));
11+
db.once('open', function() {
12+
console.log('Connection to MongoDB successful');
13+
});
214

315
// Add any new real-time connection to the `everybody` channel
416
app.on('connection', connection =>
@@ -8,7 +20,8 @@ app.on('connection', connection =>
820
app.publish(data => app.channel('everybody'));
921

1022

11-
app.listen(3030).on('listening', () =>
12-
console.log('Feathers server listening on localhost:3030')
23+
const port = 3030
24+
app.listen(port).on('listening', () =>
25+
console.log(`Feathers server listening on localhost:${port}`)
1326
);
1427

models/polls/poll.model.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Model, model } from "mongoose"
2+
import { Poll, PollSchema } from './poll.schema';
3+
4+
export default model<Poll, Model<Poll>>("Poll", PollSchema);
5+

models/polls/poll.schema.ts

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import { Document, Schema, Types } from 'mongoose';
2+
import { User } from '../users/user.schema';
3+
4+
interface ImageData {
5+
url: string;
6+
votes: number;
7+
}
8+
9+
export interface Poll extends Document {
10+
authorId: string;
11+
contents: {
12+
left: ImageData;
13+
right: ImageData;
14+
};
15+
}
16+
17+
18+
const imageDataSchema = {
19+
url: String,
20+
votes: Number
21+
}
22+
23+
export const PollSchema = new Schema({
24+
contents: {
25+
left: imageDataSchema,
26+
right: imageDataSchema
27+
},
28+
authorId: {
29+
type: Types.ObjectId,
30+
ref: 'User'
31+
}
32+
});
33+

models/users/user.model.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { Model, model } from "mongoose"
2+
import { User, UserSchema } from './user.schema';
3+
4+
export default model<User, Model<User>>("User", UserSchema);
5+

models/users/user.schema.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { Document, Schema } from "mongoose"
2+
3+
export interface User extends Document {
4+
name: string;
5+
avatarUrl?: string;
6+
age?: number;
7+
}
8+
9+
export const UserSchema = new Schema({
10+
name: String,
11+
avatarUrl: {
12+
type: String,
13+
required: false
14+
},
15+
age: {
16+
type: Number
17+
}
18+
});
19+

0 commit comments

Comments
 (0)