Giter Club home page Giter Club logo

firegen's Introduction

Rust CI

Generator of Firestore rules and type safe client code.


Usage [WIP]

Install from npm or curl.

$ npm install -g firegen

Setting your yml.

# firegen.yml
schemaPath: ./schema/**/**.fireSchema
export:
  rulesPath: ./generated/firestore.rules
  clientCodePath: ./generated/firestoreClient.ts

Create firestore schema files.

// 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.

$ firegen generate

exort as

// 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
}
// client code
import firebase from "firebase"
const db = firebase.firestore()
export const firestoreClient = {
  getUsers: async (): Promise<User[]> =>
    await db.collection('users').get(),

  getUser: async (id: number): Promise<User> =>
    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<Todo[]> =>
    await db.collection('users').doc(id).collection('todos').get(),

  getTodoByUser: async (userId: number, todoId: number): Promise<Todo> =>
    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()
}
// 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
             }
             
         }
     }
}
/**
 * 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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.