Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix DbLookup type for db #667

Merged
merged 2 commits into from
Nov 2, 2020
Merged

Conversation

rikutiira
Copy link
Contributor

@rikutiira rikutiira commented Oct 14, 2020

There's a small hiccup in the current DbLookup type which db uses.

Eg. if you have something like schema.db.model, the type of the model is DbCollection which is actually wrong because of what db.createCollection does:

      // Public API has a convenient array interface. It comes at the cost of
      // returning a copy of all records to avoid accidental mutations.
      Object.defineProperty(this, name, {
        get() {
          let recordsCopy = newCollection.all();

          [
            "insert",
            "find",
            "findBy",
            "where",
            "update",
            "remove",
            "firstOrCreate",
          ].forEach(function (method) {
            recordsCopy[method] = function () {
              return newCollection[method](...arguments);
            };
          });

          return recordsCopy;
        },
      });

There's no db.model.all() function as the DbCollection type suggests. This PR changes the DbLookup type to match the actual implementation.

@samselikoff
Copy link
Contributor

I see. Still green on TS but it seems right – the public API is db.users.

(This API was designed a long time ago and I think is definitely worth revisiting after we ship v1.0 and consider changes that would make the library more modern + TS-friendly overall).

Looks like the test is failing, could you take a look? https://travis-ci.com/github/miragejs/miragejs/jobs/399644394#L534

Maybe you can just remove all() altogether. (It's really not meant to be public API at all.)

@rikutiira
Copy link
Contributor Author

I see. Still green on TS but it seems right – the public API is db.users.

(This API was designed a long time ago and I think is definitely worth revisiting after we ship v1.0 and consider changes that would make the library more modern + TS-friendly overall).

Looks like the test is failing, could you take a look? https://travis-ci.com/github/miragejs/miragejs/jobs/399644394#L534

Maybe you can just remove all() altogether. (It's really not meant to be public API at all.)

It seems like the overview-test.ts file had duplicate route definition for the movies route:

  routes() {
    this.namespace = "api";

    this.get("/movies", (schema, request) => {
      return schema.db.movies.all();
    });

    this.get("/movies");
    this.get("/movies/:id");
    this.post("/movies");
    this.patch("/movies/:id");
    this.del("/movies/:id");
  },

The return schema.db.movies.all(); was wrong and shouldn't have even worked but the this.get("/movies"); route shorthand was overwriting the previous route definition so it was never called.

I removed the first movies route definition altogether.

@rikutiira
Copy link
Contributor Author

@samselikoff any update on this?

@samselikoff samselikoff merged commit 4468dff into miragejs:master Nov 2, 2020
@samselikoff
Copy link
Contributor

Sorry about the delay! Merged <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants