本文整理汇总了TypeScript中firebase-admin.database函数的典型用法代码示例。如果您正苦于以下问题:TypeScript database函数的具体用法?TypeScript database怎么用?TypeScript database使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了database函数的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的TypeScript代码示例。
示例1: copyTestImagesToGoldens
export function copyTestImagesToGoldens(prNumber: string) {
return firebaseAdmin.database().ref(`screenshot/reports/${prNumber}/results`).once('value')
.then((snapshot: firebaseAdmin.database.DataSnapshot) => {
const failedFilenames: string[] = [];
let counter = 0;
snapshot.forEach(childSnapshot => {
if (childSnapshot.key && childSnapshot.val() === false) {
failedFilenames.push(childSnapshot.key);
}
counter++;
return counter === snapshot.numChildren();
});
return failedFilenames;
}).then((failedFilenames: string[]) => {
return bucket.getFiles({prefix: `screenshots/${prNumber}/test`}).then((data: any) => {
return Promise.all(data[0]
.filter((file: any) => failedFilenames.includes(
path.basename(file.name, '.screenshot.png')))
.map((file: any) => file.copy(`goldens/${path.basename(file.name)}`)));
});
});
}
示例2: selectFcmTokens
export const statusMessager = functions.database.ref('/status').onWrite(async event => {
const tokens = await selectFcmTokens(admin.database())
const newStatus = event.data.val()
const title = 'Mailbox status changed'
const body = logMessageMap(newStatus)
await sendCloudMessage(title, body, tokens, admin.messaging())
})
示例3: if
await gameRef.once('value').then(async (tables: admin.database.DataSnapshot) => {
console.log("searching for matches")
for (let i = 0; i < tables.numChildren(); i++) {
console.log(`searching table ${i}`)
const result = await admin.database().ref(`games/table${i}/`).transaction((players) => {
if (players === null) {
return 0;
}
if (players.player1 === "") {
console.log("adding as player1");
players.player1 = uid;
matched = true;
return players
} else if (players.player2 === "") {
console.log("adding as player2");
players.player2 = uid;
matched = true;
return players
}
}).catch((reason) => {
console.warn("transaction failed!");
return {
committed: false,
snapshot: undefined
}
})
if (result.committed === true) {
if (matched) {
break;
}
}
}
}).catch(() => {
示例4: copyGoldImagesToDatabase
export function copyGoldImagesToDatabase(name: string, resourceState: string, fileBucket: any) {
// The name should always look like "goldens/xxx.png"
let parsedPath = path.parse(name);
// Get the file name.
if (parsedPath.root != '' ||
parsedPath.dir != 'goldens' ||
parsedPath.ext.toLowerCase() != '.png') {
return;
}
let filenameKey = path.basename(parsedPath.name, '.screenshot');
let databaseRef = firebaseAdmin.database().ref(FIREBASE_DATA_GOLDENS).child(filenameKey);
// When a gold image is deleted, also delete the corresponding record in the firebase database.
if (resourceState === 'not_exists') {
return databaseRef.set(null);
}
let tempFilePath = `/tmp/${parsedPath.base}`;
let bucket = gcs.bucket(fileBucket);
// Download file from bucket.
return bucket.file(name).download({destination: tempFilePath})
.then(() => {
let data = readFileSync(tempFilePath);
return databaseRef.set(data);
}).catch((error: any) => console.error(`${filenameKey} ${error}`));
}
示例5: Date
}, (error: any, response: any, body: string) => {
if (error) {
return res.status(error.code).send(error.message);
}
try {
let parsed = JSON.parse(body);
if (parsed.error) {
return res.status(parsed.error.code).send(parsed.error.message);
}
parsed = parsed.map((rep: any) => {
return {
title: rep.name,
description: rep.description,
url: rep.html_url,
createdOn: new Date(rep.pushed_at).getTime()
};
})
.sort((a, b) => b.createdOn - a.createdOn)
.slice(0, 12)
const result = {
results: parsed,
expires: tomorrow
};
admin.database().ref('github').set(result).then(() => res.send(JSON.stringify(result.results)));
} catch (error) {
return res.status(error.code).send(error.message);
}
});
示例6: cors
cors(req, res, () => {
admin.database().ref('linkedin').once('value', (snapshot: admin.database.DataSnapshot) => {
if (snapshot.exists() && snapshot.val().expires > new Date().getTime()) {
return res.status(400).send('Functions only update data that\'s older than 24 hours.');
} else {
const tomorrow: number = new Date().getTime() + 86400000;
admin.storage().bucket().file('linkedin.html', {})
.getSignedUrl({ action: 'read', expires: tomorrow })
.then((url: string[]) => {
if (url.length !== 1) return res.status(400).send('No profile found');
httpRequest({ method: 'GET', url: url[0] }, (error: any, response: any, body: string) => {
if (error) { return res.status(error.code).send(error.message); }
inScrape.getProfile(body)
.then((profile) => {
const regex: RegExp = RegExp('<img.*?alt="' + profile.name + '".*?src=".\/linkedin_files\/(.*?)"', 'g');
const images: string[] = regex.exec(body);
if (Array.isArray(images) && images.length > 0) { profile.photo = 'https://media.licdn.com/mpr/mpr/' + images[1]; }
if (profile.experience && Array.isArray(profile.experience)) { profile.experience = orderExperience(profile.experience); }
const result = {
results: profile,
expires: tomorrow
};
admin.database().ref('linkedin').set(result).then(() => res.send(JSON.stringify(result.results)));
}).catch(err => res.status(400).send(err));
});
})
.catch(error => res.status(400).send(error));
}
});
});
示例7: Date
.onCreate(async (snap: any, ctx: any) => {
admin
.database()
.ref("write-test")
.set({
date: new Date(),
});
}),
示例8:
.ref('/conversations/{conversationId}/messages/{messageId}').onCreate(event => {
const value = event.data.val();
const timestamp = Date.now() * -1;
console.log(`server timestamp ${timestamp}`);
value['.priority'] = timestamp;
return admin.database()
.ref(`/conversations/${event.params.conversationId}/messages/${event.data.key}`)
.update(value);
});
示例9: RegExp
.then((profile) => {
const regex: RegExp = RegExp('<img.*?alt="' + profile.name + '".*?src=".\/linkedin_files\/(.*?)"', 'g');
const images: string[] = regex.exec(body);
if (Array.isArray(images) && images.length > 0) { profile.photo = 'https://media.licdn.com/mpr/mpr/' + images[1]; }
if (profile.experience && Array.isArray(profile.experience)) { profile.experience = orderExperience(profile.experience); }
const result = {
results: profile,
expires: tomorrow
};
admin.database().ref('linkedin').set(result).then(() => res.send(JSON.stringify(result.results)));
}).catch(err => res.status(400).send(err));