-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #9 from samuraikun/data_modeling_between_users_and…
…_videos Users と Videos のDB設計を変更
- Loading branch information
Showing
5 changed files
with
98 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,29 @@ | ||
service cloud.firestore { | ||
match /databases/{database}/documents { | ||
function isAuthenticated() { | ||
return request.auth.uid != null; | ||
} | ||
|
||
function isUserAuthenticated(userId) { | ||
return request.auth.uid == userId; | ||
} | ||
|
||
function isAdmin() { | ||
return exists(/databases/$(database)/documents/admins/$(request.auth.uid)); | ||
} | ||
|
||
match /users/{userId} { | ||
allow get: if isAuthenticated(); | ||
allow create, update, delete: if isUserAuthenticated(userId) || isAdmin(); | ||
|
||
match /videos/{videoId} { | ||
allow read: if isAuthenticated(); | ||
allow create, update, delete: if isUserAuthenticated(userId) || isAdmin(); | ||
} | ||
} | ||
|
||
match /videos/{video} { | ||
allow read, write: if request.auth.uid != null; | ||
allow read: if isAuthenticated(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
const functions = require('firebase-functions'); | ||
const serviceAccount = require('./config/service_account.json'); | ||
const admin = require('firebase-admin'); | ||
try { | ||
admin.initializeApp({ | ||
credential: admin.credential.cert(serviceAccount), | ||
databaseURL: "https://fir-reacty-videos.firebaseio.com" | ||
}); | ||
admin.firestore().settings({timestampsInSnapshots: true}); | ||
} catch (error) { | ||
console.log(error); | ||
} | ||
const firestore = admin.firestore(); | ||
|
||
exports.onUsersVideoCreate = functions.firestore.document('users/{userId}/videos/{videoId}').onCreate(async (snapshot, context) => { | ||
await copyToRootWithUsersVideoSnapshot(snapshot, context); | ||
}); | ||
|
||
exports.onUsersVideoUpdate = functions.firestore.document('users/{userId}/videos/{videoId}').onUpdate(async (change, context) => { | ||
await copyToRootWithUsersVideoSnapshot(change.after, context); | ||
}); | ||
|
||
async function copyToRootWithUsersVideoSnapshot(snapshot, context) { | ||
const userId = context.params.userId; | ||
const videoId = context.params.videoId; | ||
const video = snapshot.data(); | ||
video.userRef = firestore.collection('users').doc(userId); | ||
|
||
await firestore.collection('videos').doc(videoId).set(video, { merge: true }); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters