Created
May 26, 2016 08:00
-
-
Save davidgilbertson/337e8449a934d69a69b3cfab8423c26c to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| // Firebase V3.0.0 | |
| // an example using a single atomic `update()` to create objects in three 'tables' that reference each other. | |
| // one school has many courses and many studens | |
| // one course has many students and is in only one school | |
| // one student is in many courses and only one school | |
| db = firebase.database().ref('test'); | |
| const userId = firebase.auth().currentUser.uid; | |
| const school1Key = db.child('data/schools').push().key; | |
| const course1Key = db.child('data/courses').push().key; | |
| const course2Key = db.child('data/courses').push().key; | |
| const student1Key = db.child('data/students').push().key; | |
| const student2Key = db.child('data/students').push().key; | |
| const school1 = { | |
| name: 'A magnificent school', | |
| courseKeys: { | |
| [course1Key]: true, | |
| [course2Key]: true, | |
| }, | |
| studentKeys: { | |
| [student1Key]: true, | |
| [student2Key]: true, | |
| }, | |
| }; | |
| const course1 = { | |
| name: 'An excellent course', | |
| schoolKey: school1Key, | |
| studentKeys: { | |
| [student1Key]: true, | |
| [student2Key]: true, | |
| }, | |
| }; | |
| const course2 = { | |
| name: 'An second excellent course', | |
| schoolKey: school1Key, | |
| studentKeys: { | |
| [student2Key]: true, | |
| }, | |
| }; | |
| const student1 = { | |
| name: 'Bobby Brown', | |
| schoolKey: school1Key, | |
| courseKeys: { | |
| [course1Key]: true, | |
| }, | |
| }; | |
| const student2 = { | |
| name: 'Sally Mae', | |
| schoolKey: school1Key, | |
| courseKeys: { | |
| [course1Key]: true, | |
| [course2Key]: true, | |
| }, | |
| }; | |
| const newData = { | |
| [`users/${userId}/schoolKeys/${school1Key}`]: true, | |
| [`users/${userId}/mruSchool`]: school1Key, | |
| [`users/${userId}/courseKeys/${course1Key}`]: true, | |
| [`users/${userId}/courseKeys/${course2Key}`]: true, | |
| [`users/${userId}/studentKeys/${student1Key}`]: true, | |
| [`users/${userId}/studentKeys/${student2Key}`]: true, | |
| [`data/schools/${school1Key}`]: school1, | |
| [`data/courses/${course1Key}`]: course1, | |
| [`data/courses/${course2Key}`]: course2, | |
| [`data/students/${student1Key}`]: student1, | |
| [`data/students/${student2Key}`]: student2, | |
| }; | |
| db.update(newData); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment