MCPcopy Create free account
hub / github.com/firebase/quickstart-js / writeNewPost

Function writeNewPost

database/scripts/main.ts:123–149  ·  view source on GitHub ↗

* Saves a new post to the Firebase DB.

(
  uid: string,
  username: string | null,
  picture: string | null,
  title: string,
  body: string,
)

Source from the content-addressed store, hash-verified

121 * Saves a new post to the Firebase DB.
122 */
123function writeNewPost(
124 uid: string,
125 username: string | null,
126 picture: string | null,
127 title: string,
128 body: string,
129) {
130 // A post entry.
131 const postData: Post = {
132 author: username,
133 uid: uid,
134 body: body,
135 title: title,
136 starCount: 0,
137 authorPic: picture,
138 };
139
140 // Get a key for a new Post.
141 const newPostKey = push(child(ref(database), 'posts')).key;
142
143 // Write the new post's data simultaneously in the posts list and the user's post list.
144 const updates: Record<string, Post> = {};
145 updates['/posts/' + newPostKey] = postData;
146 updates['/user-posts/' + uid + '/' + newPostKey] = postData;
147
148 return update(ref(database), updates);
149}
150
151/**
152 * Star/unstar post.

Callers 1

newPostForCurrentUserFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected