-
Notifications
You must be signed in to change notification settings - Fork 0
/
github_vue_db.js
124 lines (93 loc) · 2.92 KB
/
github_vue_db.js
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
"use strict";
/*import * as firebase from 'E:\\nodejs\\node_global\\node_modules\\firebase';*/
/*抽象数据库工厂*/
class AbsDBFactory {
constructor() {
this.db = null;
this.db = this.connect()
}
connect() {
}
db_set_data() {
}
db_get_data() {
}
db_delete_doc(){}
}
/*具体 firebase数据库*/
class FireBaseDBFactory extends AbsDBFactory {
/*数据库*/
connect() {
this._collection = "users";
let firebaseConfig = {
apiKey : "AIzaSyAcuEDPktUr79r2cT0CQDhsN1xVCRNhISs",
authDomain : "github-comment.firebaseapp.com",
databaseURL : "https://github-comment.firebaseio.com",
projectId : "github-comment",
storageBucket : "github-comment.appspot.com",
messagingSenderId: "128561426333",
appId : "1:128561426333:web:b73e218ee736304e4a4974",
measurementId : "G-ZS85H7H49J",
};
/*init*/
firebase.initializeApp(firebaseConfig);
let db = firebase.firestore();
return db
// firebase.analytics();
}
//set
db_set_data(doc, value) {
let _collection = this._collection;
this.db.collection(_collection).doc(doc).set(value);
}
//get
db_get_data(extra) {
let _collection = this._collection;
const {index, query_field, factory} = extra;
let doc = query_field;
let docRef = this.db.collection(_collection).doc(doc);
docRef.get()
.then(function (doc) {
if (doc.exists) {
let data = doc.data()
let comment = data.comment;
console.info("Document data:", data);
// console.info("comment data:", comment);
factory.addDom_TextArea(index, comment, query_field)
}
else {
console.info("No such document!");
let comment = "";
factory.addDom_TextArea(index, comment, query_field)
}
})
.catch(function (error) {
console.error("Error getting document:", error);
});
}
//del
db_delete_doc(doc){
let _collection = this._collection;
let docRef = this.db.collection(_collection).doc(doc);
docRef.delete()
}
}
/*具体 Mongodb数据库*/
class RemoteDBFactory extends AbsDBFactory {
connect() {
}
}
//
class DBManager {
static get_factory(db_type = "firebase") {
let factory = null;
if (db_type === "firebase") {
console.info("db_type:",db_type);
factory = new FireBaseDBFactory()
}
return factory
}
}
export {
DBManager,
}