GetDKAN / data-catalog-components

https://getdkan.github.io/data-catalog-components
9 stars 6 forks source link

Prototypal inheritance to replace Datastore class #94

Closed dgading closed 3 years ago

dgading commented 4 years ago

The current build of datastore.js uses Class notation. This is great because it allows for easy access to methods and using this. when working within the class. We can get all this using objects in JavaScript as well along with the other features of objects.

Current when building a new datastore class in code

const store = new datastore.dkan(id, rootURL)
store.query(params);

To extend this would require creating a new class locally, extending it and adding our new methods to search for the particular features of the app we are building.

All of the above is currently nested in our resource_functions when calling getDKANDatastore. The code I am proposing should do the same when calling the resource_functions, but the functions now call

const store = datastore.create(identifier, rootURL);
store.query(params);

The biggest change here is the loss of the new keyword and removing of some legacy code when we had datastore types of csv or dkan.

Instead of needing to create a new class and extending it, we can now do something like this in our locals:

import { datastore } from '@civicactions/data-catalog-components';
datastore.myNewFunction = function() { return 'string'};

const myString = datastore.myNewFunction('dkan');

Or if you want to change up the create function only but keep many of the other features:

import { datastore } from '@civicactions/data-catalog-components';
const localDatastore = Object.create(datastore);
  localDatastore.create = function(rootUrl, id, newThing) {
    const entity = Object.create(this);
    entity.rootUrl = rootUrl;
    entity.id = id;
    entity.myNewthing = newThing;
    entity.datastoreUrl = `${rootUrl}/datastore/sql/?query=`;
    return entity;
  };

const isNewThing = localDatastore.create(root, id, newthing);
console.log(isNewThing.myNewThing);

This PR currently has all the dist/library files included. To test out the branch just update your app's package.json to use "@civicactions/data-catalog-components": "github:GetDKAN/data-catalog-components#prototypal-inheritance", instead of the npm version and you shouldn't see any difference in your build. But you can now import datastore as a named import into your app and customize as needed.

dgading commented 3 years ago

Closing as this feature will be moving to the services repo and change to the json query endpoint.