0.14.4 • Published 1 year ago

@team-decorate/alc-firestore-relations v0.14.4

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

alc-firestore-relations

Installation

With yarn:

yarn add @team-decorate/alc-firestore-relations

Usage

Model create

import {FModel} from '@team-decorate/alc-firesore-reations'  
import Post from './models/Post'  
import Comment from './models/Comment'  
  
/*
* Only those added to fillable will be sent
*/
const FILLABLE = [  
 'id', 'name', 'email', 'password', 'type'
 ]  
  
class User extends FModel {  

  id: number = 0
  name: string = ''
  email: string = ''
  password: string = ''
  type: number = 0
  posts: Array<Post> = []
  userComments: Array<Comment> = []

  constructor(data?: IIndextable) {  
	 super()         
	 //change access primary key
	 this.primaryKey = 'uid'
	 
	 this.fillable = FILLABLE 
	 //presents is send even if the field is empty 
	 this.presents = ['type']  
	 //sender is allows non-empty value
	 this.sender = this.fillable.filter(x => ['posts', 'userComments'].every(v => v != x))
	 
	 this.arrayMap(  
		 new ArrayMappable(Post), 
		 new ArrayMappable(Comment).bind('userComments')
	) 
	
	if(data) {
	    this.data = data
	}
    }
    
    // 1:n relation
    _posts() {
		return this.hasRelationships.hasMany(Post)
    }
    
    // 1 in subCollection relation
    _userComments() {
		return this.hasRelationships.hasManySub(Comment, 'user_comments')
    }
    
    // 1:1 relation
    _anyHasOne() {
        return this.hasRelationships.hasOne(Any)
    }
    
    // 1:1 relation
    _anyBelongsTo() {
        return this.hasRelationships.beongsTo(Any)
    }
}  

How to use

	#user firestore response
	{
	  "id": 1,
	  "name": "test-user",
	  "email": "test@mail.com",
	  "type": 1,
	  "posts": [
		  {"id": 1, "text": "test post 1"},
		  {"id": 2, "text": "test post 2"}
	  ],
	  "user_comments": [
		  {"id": 1, "text": "test comment 1"},
		  {"id": 2, "text": "test comment 2"}
	  ]
	}
  
export default {  
 methods: {
     async get() {
         this.user = await User.query()
             .with(['_posts', '_userComments'])
             .find(1)

         expect(this.user.id).toBe(1)
         expect(this.user.name).toBe('test-user')
         expect(this.user.posts.length).toBe(2)
         expect(this.user.userComments.length).toBe(2)
         
         const posts = this.user._posts().get()
         expect(posts.length).toBe(2)
     },

     async post() {
         /*
         * Added to the fillable and the one containing the value is sent and saved in firestore
         */
         await new User({
             name: 'A', email: 'test@mail.com',
             posts: [new Post({title: 'A+'})]
         }).save()
     }
	
  }
}  

Overridable Property

propertytype
primaryKeystring

Overridable Function

methodsvaluedescription
beforePostablenullCalled before sending api
afterPostableresCalled after sending api

Model Methods

methodsargsoutputtype
getPostablenullObject
updateObjectnull
savenullthisstatic
querynullAlcQuerystatic
collectionGroupnullAlcQuerystatic
paginatenullIPagenatestatic
hasRelationshipsnullHasRelationships

AlcQuery Methods

methodsargsoutput
findstringFModel?
firstnullFModel?
getnullFModel[]
withArray<string or {key: string, query: any, relation?: string}>AlcQuery
where
limit
orderBy
startAt
startAfter
endAt
endAfter
toQuerynull{log, stack, documentAll}
0.14.0

1 year ago

0.14.1

1 year ago

0.14.2

1 year ago

0.14.3

1 year ago

0.14.4

1 year ago

0.10.0

1 year ago

0.11.0

1 year ago

0.9.0

1 year ago

0.12.0

1 year ago

0.11.1

1 year ago

0.8.0

1 year ago

0.13.0

1 year ago

0.12.1

1 year ago

0.11.2

1 year ago

0.9.1

1 year ago

0.7.0

2 years ago

0.6.0

2 years ago

0.5.1

2 years ago

0.5.0

2 years ago

0.4.0

2 years ago

0.3.0

2 years ago

0.2.0

2 years ago

0.1.0

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago