ampatspell / ember-cli-zuglet

🔥 Easiest way to use Google Firebase services in your Ember.js Octane app
https://www.ember-cli-zuglet.com
MIT License
15 stars 0 forks source link

Support for array methods like arrayUnion #295

Open takshch opened 2 years ago

takshch commented 2 years ago

Description

Exports methods like arrayUnion

Change from current to suggested

Current

const { FieldValue } = this.store.firebase.firebase.firestore;
const data = {
    searches: FieldValue.arrayUnion(this.searchValue),
};

const ref = this.store.doc(....);
await ref.save(data);

Suggested

import { arrayUnion } from 'zuglet/firestore';

const data = {
    searches: arrayUnion(this.searchValue),
};

const ref = this.store.doc(....);
await ref.save(data);
takshch commented 2 years ago

@ampatspell I think this is needed. I also want to work on this! So, can you please review this and make some changes to make it better.

Idea is to make zuglet/firestore like firestore package as mentioned in the link https://firebase.google.com/docs/firestore/manage-data/add-data

ampatspell commented 2 years ago

currently there is store.serverTimestamp. how about we deprecate that then too and add:

import { serverTimestamp, arrayUnion } from 'zuglet/store';

let a = serverTimestamp();
let b = arrayUnion(arg);

essentially store is firestore in zuglet.

also arrayUnion will need a case in: https://github.com/ampatspell/ember-cli-zuglet/blob/master/addon/-private/model/properties/object.js#L27-L42

takshch commented 2 years ago

"firebase/firestore" also exports methods like doc, setDoc, addDoc, collection.

// models/message.js
import EmberObject from '@ember/object';
import { inject as service } from '@ember/service';
import { activate } from 'zuglet/decorators';
import { doc } from 'zuglet/store';

export default class Message extends EmberObject {
  @tracked id;

  @activate().content(
     ({ store, id }) => doc(`messages/${id}`).existing()
  )  doc;
}

Injecting a store in models every time is like sticking with something tightly. It's like a boilerplate. How about removing the need of store in model?

ampatspell commented 2 years ago

doc, collection creates references. that is covered by store.doc() and store.collection()

regarding store service, app can have multiple stores, each with separate firebase project configuration. when you interact with a store, you're referencing one particular connection.

https://github.com/ampatspell/ember-cli-zuglet/blob/master/blueprints/ember-cli-zuglet/files/__root__/instance-initializers/__name__-store.js#L7-L21

initialize(app, {
  store: {
    identifier: 'firstStore',
    factory: FirstStore
  }
});

initialize(app, {
  store: {
    identifier: 'secondStore',
    factory: SecondStore
  }
});
takshch commented 2 years ago

I don't think people will use 2 or more firebase projects in their ember app.

What do you think?

takshch commented 2 years ago

Can you please tell me what is <%= classifiedPackageName %> in export default class <%= classifiedPackageName %>Store extends Store and how it is supported in js?

https://github.com/ampatspell/ember-cli-zuglet/blob/master/blueprints/ember-cli-zuglet/files/__root__/store.js#L48

ampatspell commented 2 years ago

I don't think people will use 2 or more firebase projects in their ember app.

that's certainly something I want to support for dashboard-style applications

ampatspell commented 2 years ago

classifiedPackageName

that is ember-cli blueprint variable, those files are used to generate store, application route when you do ember install or ember g ember-cli-zuglet: https://cli.emberjs.com/release/advanced-use/blueprints/

takshch commented 2 years ago

that's certainly something I want to support for dashboard-style applications

What are dashboard-style applications? Why do those applications need multiple stores?

takshch commented 2 years ago

@ampatspell Can you please reply?

ampatspell commented 2 years ago

Oh, sorry, missed your comment.

Let's say you have multiple firebase apps (separate projectIds) and you want to build some kind of overview dashboard app that shows information gathered from all the apps.

takshch commented 2 years ago

It makes sense. Then zuglet/store is fine!

import { serverTimestamp, arrayUnion } from 'zuglet/store';

Do you have anything in mind to refine this?

ampatspell commented 2 years ago

Looks good to me

takshch commented 2 years ago

@ampatspell I will understand the codebase and will send the PR for this issue.

takshch commented 2 years ago

@ampatspell I tried to understand the code but it is really hard to understand it without documentation. I got to know a few little things.

Can you please write the architecture of the library with a UML diagram?

ampatspell commented 2 years ago

That would be way too much effort.

Everything revolves around custom registry registrations: https://github.com/ampatspell/ember-cli-zuglet/blob/master/app/initializers/zuglet-internal.js

Basically there are 2 bigger groups of code: