This repository was archived by the owner on Oct 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathdb.ts
110 lines (100 loc) · 3.55 KB
/
db.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Mongo
import mongodb = require('mongodb');
var server = new mongodb.Server('localhost', 27017, {auto_reconnect: true});
var db = new mongodb.Db('mydb', server, { w: 1 });
db.open(function() {});
export interface User {
_id: string;
email: string;
first_name: string;
last_name: string;
fbId: number;
boards: Board[];
}
export interface Board {
title: string;
description: string;
images: mongodb.ObjectID[];
}
export interface Image {
_id: mongodb.ObjectID;
user: string;
caption: string;
imageUri: string;
link: string;
board: string;
comments: {text: string; user: string;}[];
}
export function getUser(id: string, callback: (user: User) => void) {
db.collection('users', function(error, users) {
if(error) { console.error(error); return; }
users.find({_id: id}).batchSize(10).nextObject(function(error, user) {
if(error) { console.error(error); return; }
callback(user);
});
});
}
export function getUsers(callback: (users: User[]) => void) {
db.collection('users', function(error, users_collection) {
if(error) { console.error(error); return; }
users_collection.find({}, { '_id': 1 }).toArray(function(error, userobjs) {
if(error) { console.error(error); return; }
callback(userobjs);
});
});
}
export function getImage(imageId: string, callback: (image: Image) => void) {
db.collection('images', function(error, images_collection) {
if(error) { console.error(error); return; }
images_collection.find({_id: new mongodb.ObjectID(imageId)}).batchSize(10).nextObject(function(error, image) {
if(error) { console.error(error); return; }
callback(image);
});
});
}
export function getImages(imageIds: mongodb.ObjectID[], callback: (images: Image[]) => void) {
db.collection('images', function(error, images_collection) {
if(error) { console.error(error); return; }
images_collection.find({_id: {$in: imageIds}}).toArray(function(error, images) {
callback(images);
});
});
}
export function addBoard(userid: any, title: string, description: string, callback: (user: User) => void) {
db.collection('users', function(error, users) {
if(error) { console.error(error); return; }
users.update(
{_id: userid},
{"$push": {boards: { title: title, description: description, images: []}}},
function(error, user) {
if(error) { console.error(error); return; }
callback(user);
}
);
});
}
export function addPin(userid: string, boardid: string, imageUri: string, link: string, caption: string, callback: (user: User) => void) {
db.collection('images', function(error, images_collection) {
if(error) { console.error(error); return; }
images_collection.insert({
user: userid,
caption: caption,
imageUri: imageUri,
link: link,
board: boardid,
comments: []
}, function(error, image) {
console.log(image);
db.collection('users', function(error, users) {
if(error) { console.error(error); return; }
users.update(
{_id: userid, "boards.title": boardid},
{"$push": {"boards.$.images": image[0]._id}},
function(error, user) {
callback(user);
}
);
})
})
})
}