question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Suggestion: BaseModel configuration prop

See original GitHub issue

Proposal

Add a BaseModel to host static methods for models

How it could work

  • Add optional config BaseModel
  • If BaseModel is declared, as soon as the database has a handle, it would BaseModel.database = database, even before declaring collectionMap

Example

I have 32 model classes atm, and for collection methods I decided to use Model’s static methods. For example:

Post.find() is basically: database.collections.get('posts').find() but In a way nicer way.

Instead of just adding it to each class, I created a class BaseModel extends Model and all my classes extend this BaseModel.

So I added in Base Model methods like:

static getCollection<T extends typeof Model>(
    this: T,
  ): Collection<InstanceType<T>> {
    const database = ((this as unknown) as BaseModel).database;
    return database.get<InstanceType<T>>(this.table);
  }

static async find<T extends typeof BaseModel>(this: T, id: string) {
    let item: InstanceType<T> | undefined;

    try {
      item = await this.getCollection().find(id);
    } catch (e) {
      this.registerNotFound(id);
    }

    return item;
  }

  static async findBy<T extends typeof BaseModel>(
    this: T,
    fieldName: string,
    value: string,
  ) {
    let item: InstanceType<T> | undefined;
    let itemArray = await this.fetchBy(fieldName, value, Q.experimentalTake(1));

    try {
      item = itemArray[0];
    } catch (e) {
      this.registerNotFoundBy(fieldName, value);
    }

    return item;
  }

  static findAndObserve<T extends typeof BaseModel>(this: T, id: string) {
    return this.getCollection().findAndObserve(id);
  }

This allows me to write cleaner code, for example in observables:

export const withPostsForHome = withObservables([], () => ({
  posts: Posts.postsForHome(),
}));

The only issue is that I set the BaseModel database way after the database is ready, having some errors once in a while, especially when hot reloading a model. So I’m suggesting this change in the hope I reduce (or zero) the current issues.

If agreed, I would love to write the changes but I will need help with typing

Issue Analytics

  • State:open
  • Created 2 years ago
  • Reactions:1
  • Comments:9 (7 by maintainers)

github_iconTop GitHub Comments

2reactions
radexcommented, May 11, 2021

sorry, but it’s a no from me. This was considered and rejected, oh, like 4 years ago. Posts.foo() is nice, but it’s not clean. I refuse to promote a coding practice that implicitly make a Database a singleton. You should be able to have multiple instances of a Database, of a Collection, etc., and this proposal prevents that.

What we practice at Nozbe is that there’s a ModelRoot instance that contains methods that act on the whole database (when there is no Model instance where the method fits) - e.g. your postsForHome would be there. This object is available the same way as a Database:

const { modelRoot, db } = useServices()
0reactions
stale[bot]commented, Apr 16, 2022

Is this still relevant? If so, what is blocking it? Is there anything you can do to help move it forward?

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Configure the base module - Android Developers
This property is set to true by default. // You can specify `false` to turn off // generating configuration APKs for language resources....
Read more >
Pydantic, allow property to be parsed or passed to constructor ...
I am trying to make a Pydantic model which for the most part is mutable BUT I want one member to be immutable....
Read more >
Model Config - pydantic
Model Config. Behaviour of pydantic can be controlled via the Config class on a model or a pydantic dataclass. Python 3.7 and above....
Read more >
The Beginner's Guide to Pydantic. A Python package to parse ...
The topic for today is on data validation and settings management using Python type hinting. ... Declare a new class which inherits the...
Read more >
pydantic/Lobby - Gitter
from typing import List from pydantic import BaseModel class ... phone: PhoneNumber): return phone.international class Config: orm_mode = True.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found