0.1.12 • Published 2 years ago

ts-firebase-rules v0.1.12

Weekly downloads
-
License
MIT
Repository
github
Last release
2 years ago

✨✨TSFR✨✨

TSFR is the lost piece of firebase ".rules" files .
with TSFR you can use power of typescript to overcome firebase rules-engine limitations and scale your app so much easier without worring about your app crashes just for simple typo errors and also better managing/debugging with spliting the .rules file into smaller files.

Features

  • type-checking
    • catch bugs on-the-fly
  • Cli
    • a good cli always makes our life easier (e.g: create, watch, ...)
  • Live reload / watch-mode
    • npx tsfr watch
  • import/export
    • split your project into smaller peaces and have better debugging
  • additional helper types and functions
    • TSFR comes with helpers ,you can import them and use them
  • write additional native modules
    • you can write additinal native helper functions or use other people functions
  • globalVariables
    • e.g: can be used as Configs that are going to be used in many functions ...

Quick Start

npm i -D ts-firebase-rules
npx tsfr create
#use npx tsfr create --help for more complex commands

this will ask you to choose a template and that's it. Just cd to tsfr folder , run build or watch command and start editing tsfr/index.ts!

cd tsfr
npx tsfr watch
#npx tsfr build

note:in case you want to change output file or manipulate paths of source files from your_project/tsfr.config.json


basic-example

lets create a simple todo list:

//index.ts
import { setRuleStructure } from 'ts-firebase-rules'
import { create_todo, update_todo, read_todo } from './functions/todos'

setRuleStructure(() => ({
    'todos': (id) => ({
        create: create_todo(id),
        //other fns e.g update ...
    }),
}))
//functions/todos.ts
import { FrString, request, isString, getReq,  FrBoolean} from "ts-firebase-rules" 

import * as mt from '../../modelsTypes'
export function create_todo(id: FrString): FrBoolean {
    const reqData = getReq<mt.create_todo>()
    return (
        request.auth != null && // user has logged in 
        reqData.createdBy === request.auth.uid &&  
        isString(reqData.comments) && 
        titleIsValid(reqData.title)
    )
} 
//functions/helpers.ts
import { _globalVariables_ } from "../_globalVariables_" 
export function titleIsValid(title: FrString): FrBoolean {
    return (
        title.matches('[A-Za-z][A-Za-z0-9]') &&
        title.size() >= _globalVariables_.minimumCharsInTitle &&
        title.size() <= _globalVariables_.maximumCharsInTitle)
}

compiles to:

match /groups/{id} {
    allow create: if create_groups(id);
    ...
}

//other imported functions will get compiled here 
function create_todo(id) {
    let reqData = getReq();
    return (request.auth != null &&
        titleIsValid(reqData.title) &&
        reqData.createdBy == request.auth.uid &&
        isString(reqData.comments) && 
        );
}

function titleIsValid(title) {
        return (title.matches('[A-Za-z][A-Za-z0-9]') &&
            title.size() >= minimumCharsInTitle()  &&
            title.size() <= maximumCharsInTitle() );
} 

check react-native-todoList-boilerplate for a complete example.


adding-native-modules(optional)

for adding native modules you can simply modify native/functions/nativeFunctions.rules then add a decleration type for it. for example:

//your_project/tsfr/native/functions/nativeFunctions.rules
function docExists2(id, table) {
   return ( 
       exists(/databases/$(database)/documents/$(table)/$(id)) 
   )
}
 //your_project/tsfr/additionalFns.d.ts
import { FrStringAsParam} from 'ts-firebase-rules'
 
 export const docExists2: <tables>(id: FrStringAsParam, table: tables) => FrBoolean

and nowyou can use docExists2 function in your tsfr project.


Notes

⚠️ *Note:this project is still in its first days ,personally i used this for some of my projects(on production) and so far there have been no problem but we use multiple compiler and transitions so unexpected bugs might happen ,so (for now) you should check the final file and do the testing*

Note: we still going to have Firebase rules language limitations like lack of Loops and etc so make sure you have read their documentions.


✨✨✨✨✨✨

Author

License

MIT