crkn-rcdr / sapindale

Sapper/Svelte access platform administration interface
2 stars 2 forks source link

Convert remaining Sapindale couch code to server routes #104

Closed SaschaAdler closed 3 years ago

SaschaAdler commented 3 years ago

Sapindale client code shouldn't be talking to Couch directly. Rewrite components to use server routes to talk to Couch.

Currently in use:

Not currently in use (and can therefore be put off until later):

As a result of this change, we can switch to using nano instead of rolling our own Couch request code.

SaschaAdler commented 3 years ago

@RussellMcOrmond did I miss anything in the list above?

RussellMcOrmond commented 3 years ago

ArchivalManifest is what launches smelter, so I'm guessing that porting it over will be soonish?

I've been the only one actively using it, to try to keep the two sets of databases updated with the things they are adding (and I do that whenever I run loadseries as it complains about any missing manifest noid that it tries to add to collections). I have tickets coming up where I'll be doing loadseries stuff.

SaschaAdler commented 3 years ago

How to convert code from client-side Couch requests to server-side Couch requests, using repository stats as an example:

  1. Create a server route for each time data needs to be preloaded into a client route, each time data needs to be updated from the server, or each time the client needs to update the server with data.
    • In the case of repository stats, there needs to be one route, which will perform the work of the refreshValues method. Since repository is a static route, i.e. it doesn't accept any parameters, this server route should be set up in routes/repository.json.js. Server routes, like client routes, can accept parameters by using square brackets in the file/directory name.
    • repository.json.js should export a get method, which performs Couch calls and For an example of a server route that exports a get method, see routes/collection/slug/[slug].json.js.
    • The way I've set server routes up to work is that the last line in them should be a call to res.finalizeJSON(obj), where obj is an Object with three fields:
      • status: HTTP status code. The repository get method should return 200 on success. Don't worry about errors yet.
      • content: the JSON content output when the server route completes successfully, or {} if the route has an error
      • message: the error message text, if there has been an error. Not required on successful route completions
  2. Fill in the server route methods with the required Couch calls. Use resources/couch.js instead of couch.js. Couch calls will not need an authentication token because they are only performed on the server. If the server routes are long and complicated, or if there are multiple routes relating to the same logical concept, their code can be refactored into the models directory.
    • The first step with the repository stats will be to copy over all of refreshValues into the server route. There is also a utility function calculateHuman and an import of the filesize package that need to be carried over. The filesize import should actually use import.
    • The repository stats code uses methods in couch/repository.js that call _request in couch.js. I suspect these methods are not actually necessary, as the generic Couch calling functions in resources/couch.js should handle the things they're trying to do. If we need to add new generic functions in resources/couch.js, we can.
    • I don't know what the value of the content object that should be sent to res.finalizeJSON() is, but it should contain all of the data that refreshValues puts together.
    • All of the repository stats code is handled in one server route. Even if it's long, there's no reason to refactor it yet. I suspect it doesn't have to be as complex as it is, also.
    • You can test this step by visiting https://admin-dev.canadiana.ca/repository.json
  3. Use the preload method in the client route's module context to fetch the data from the server route.
    • See routes/collection/[prefix]/[noid]/index.svelte for an example. The one for repository stats won't be as complex.
  4. If any client-side code needs to request new data or update data, hook it up to the relevant server routes.
    • The "Refresh" button in the repository client route needs to fetch and overwrite the same data that the preload function does.
  5. Update the interface of the client route in the event that the structure of the data it uses has changed.
RussellMcOrmond commented 3 years ago

Am I correct in thinking that https://github.com/crkn-rcdr/sapindale/blob/main/src/couch/repository.js is no longer used, and can be removed?

Of the remaining things in https://github.com/crkn-rcdr/sapindale/tree/main/src/couch , should I take on any?

I know that @DivyaKrishnan26 is currently working on CouchView. As part of that the array of known databases in https://github.com/crkn-rcdr/sapindale/blob/main/src/couch.js should be moved elsewhere, as I believe part of the goal of this ticket is to remove that file.

RussellMcOrmond commented 3 years ago

During @DivyaKrishnan26 's vacation I will be taking these on for a bit, and seeing how I progress.