-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfirebase.js
More file actions
89 lines (75 loc) · 2.52 KB
/
firebase.js
File metadata and controls
89 lines (75 loc) · 2.52 KB
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
import alt from 'alt-server';
import chalk from 'chalk';
import {createRequire} from 'module';
const require = createRequire(import.meta.url);
var admin = require('firebase-admin');
var key = require('./key.json');
export class Firebase{ // FIREBASE
constructor(url){
this.credential = admin.credential.cert(key) // Ключ // Key
this.databaseURL = url; // url database // ссылка на датабазу
// Connect // Подключение
try{
admin.initializeApp({
credential: this.credential,
databaseURL: this.databaseURL
})
console.log(chalk.cyanBright('Firebase Connect!'));
} catch (err) {
console.log(err)
}
this.database = admin.firestore();
}
addDocument(collection, document, data){ // Добавление документа
try{
this.database.collection(collection).doc(String(document)).set(data);
}
catch(err){
console.log(err)
}
}
updateDocument(collection, document, data){ // Обновление документа
try {
this.database.collection(collection).doc(String(document)).update(data)
}
catch (err) {
console.log(err)
}
}
async getAllDocuments(collection){ // Получение всех документов
try {
let data = [];
await (await this.database.collection(collection).get()).docs.map(value => {
data.push(value.data())
})
return data
}
catch (err) {
console.log(err)
}
}
async getDocument(collection, document) { // Получение данных из документа
try {
return new Promise((resolve, rejects) => {
const result = this.database.collection(collection).doc(document).get()
result.then(res => {
return resolve(res.data())
})
.catch(err => {
return rejects(undefined)
})
})
}
catch (err) {
console.log(err)
}
}
deleteDocument(collection, document) { // Удаление документа
try {
this.database.collection(collection).doc(document).delete()
}
catch (err) {
console.log(err)
}
}
}