Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ubugeeei/firegen
Generator of Firestore rules and type safe client code.
https://github.com/ubugeeei/firegen
code-generation firebase firestore rust rust-lang
Last synced: 27 days ago
JSON representation
Generator of Firestore rules and type safe client code.
- Host: GitHub
- URL: https://github.com/ubugeeei/firegen
- Owner: ubugeeei
- Created: 2021-09-22T01:24:49.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2021-09-26T08:31:18.000Z (over 3 years ago)
- Last Synced: 2024-12-16T15:15:33.343Z (about 1 month ago)
- Topics: code-generation, firebase, firestore, rust, rust-lang
- Language: Rust
- Homepage:
- Size: 28.3 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 13
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Usage [WIP]
## Install from npm or curl.
```sh
$ npm install -g firegen
```
## Setting your yml.```yml
# firegen.yml
schemaPath: ./schema/**/**.fireSchema
export:
rulesPath: ./generated/firestore.rules
clientCodePath: ./generated/firestoreClient.ts```
## Create firestore schema files.
```ts
// User.fireSchema/**
* definition schema
*/
Document User {
username: Text
mail?: Text
age: Int
todos: Todo
}
/**
* [WIP] Rules schema
*/
//ex1
Document Todo rules TodoRules {
id: Int
description: Text
memo?: Text
}
Rule TodoRules {
get: request.auth != null
list: true
create: request.auth.uid == userKey
update: request.auth.uid == userKey
delete: false
}//ex2 normalization rules
Document Todo rules AllowReadOnlyLoginUser & AllowWriteOriginalUser {
id: number
description: string
memo?: string
}
Rule AllowReadOnlyLoginUser {
get: request.auth != null
list: request.auth != null
}
Rule AllowWriteOriginalUser {
create: request.auth.uid == userId
update: request.auth.uid == userId
delete: request.auth.uid == userId
}```
## Run command to generate files.
```sh
$ firegen generate
```
exort as
```ts
// types
export interface User {
id: number
username: string
mail?: string
age: number
todos: Todo[]
}
export interface UserCreateInput {
username: string
mail?: string
age: number
todos: Todo[]
}
export interface UserUpdateInput {
username?: string
mail?: string
age?: number
}
export interface Todo {
id: number
description: string
memo?: string
createdAt?: string
isDone: boolean
}
export interface TodoCreateInput {
description: string
memo?: string
createdAt?: string
isDone: boolean
}
export interface TodoUpdateInput {
description: string
memo?: string
createdAt?: string
isDone: boolean
}
``````ts
// client code
import firebase from "firebase"
const db = firebase.firestore()
export const firestoreClient = {
getUsers: async (): Promise =>
await db.collection('users').get(),getUser: async (id: number): Promise =>
await db.collection('users').doc(id).get(),createUser: async (input: UserCreateInput) =>
await db.collection('users').add(input),updateUser: async (userId: number, input: UserUpdateInput) =>
await db.collection('users').doc(userId).update(input),deleteUser: async (id: number) =>
await db.collection('users').doc(id).delete(),getTodosByUser: async (id: number): Promise =>
await db.collection('users').doc(id).collection('todos').get(),getTodoByUser: async (userId: number, todoId: number): Promise =>
await db.collection('users').doc(userId).collection('todos').doc(todoId).get(),createTodo: async (userId: number, input: TodoCreateInput) =>
await db.collection('users').doc(userId).collection('todos').add(input),updateTodo: async (userId: number, todoId: number, input: TodoUpdateInput) =>
await db.collection('users').doc(userId).collection('todos').doc(todoId).update(input),deleteTodo: async (userId: number, todoId: number) =>
await db.collection('users').doc(userId).collection('todos').doc(todoId).delete()
}
``````ts
// rules
rules_version = '2';service cloud.firestore {
match /databases/{database}/documents {
match /users/{userId} {
match /todos/{todoId} {
allow get: if request.auth != null
allow create: if request.auth.uid == userId
allow update: if request.auth.uid == userId
allow delete: if request.auth.uid == userId
}
}
}
}
``````ts
/**
* use in your project!
*/
const userId = store.user.id
const user = await firestoreClient.getUser(userId)
const todos = await firestoreClient.getTodosByUser(userId)const todoId = 22
await firestoreClient.deleteTodo(userId, todoId)```