|
| 1 | +/** |
| 2 | + * Copyright 2018, GeoSolutions Sas. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | +const ConfigUtils = require("../../utils/ConfigUtils"); |
| 9 | +const ApiProviders = { |
| 10 | + geostore: require("../../observables/geostore") |
| 11 | +}; |
| 12 | +/** |
| 13 | + * MapStore Persistence layer. |
| 14 | + * By default MapStore persists resources on geostrore. You can add a persistence provider creating an object that |
| 15 | + * implements the CRUD interface (createResource, getResource, updateResource and deleteResource) |
| 16 | + * and adding it to the API providers calling `addApi`. |
| 17 | + * Then you can select your provider by settings the `persistenceApi` property in `localConfig.json` |
| 18 | + * or by programmatically calling `setApi` method. LocalConfig takes precedence. |
| 19 | + */ |
| 20 | +const Persistence = { |
| 21 | + api: "geostore", |
| 22 | + /** |
| 23 | + * Add a new API implementation |
| 24 | + * @param {string} name the key of the added api implementation |
| 25 | + * @param {object} api the api implementation |
| 26 | + */ |
| 27 | + addApi: (name, api) => { |
| 28 | + ApiProviders[name] = api; |
| 29 | + }, |
| 30 | + /** |
| 31 | + * Set the current API |
| 32 | + * @param {string} name the key of the api implementation to be used |
| 33 | + */ |
| 34 | + setApi: (name = "geostore") => { |
| 35 | + Persistence.api = name; |
| 36 | + }, |
| 37 | + /** |
| 38 | + * Add a new api implementation |
| 39 | + * @return {object} Current api |
| 40 | + */ |
| 41 | + getApi: () => { |
| 42 | + return ApiProviders[ConfigUtils.getConfigProp("persistenceApi") || Persistence.api]; |
| 43 | + }, |
| 44 | + /** |
| 45 | + * Retrieves a resource with data with all information about user's permission on that resource, attributes and data. |
| 46 | + * @param {number} id the id of the resource to get |
| 47 | + * @param {options} param1 `includeAttributes` and `withData` flags, both true by default |
| 48 | + * @return an observable that emits the requested resource |
| 49 | + */ |
| 50 | + getResource: (...args) => Persistence.getApi().getResource(...args), |
| 51 | + /** |
| 52 | + * Returns an observable for saving a "Resource" and it's linked resources. |
| 53 | + * Linked resources are geostore resources like thumbnail or details. The main resource contains a link |
| 54 | + * to that resources as attributes. (the URL is double encoded to avoid issues with conversions in other pieces of the API) |
| 55 | + * Required format of the resource object is: |
| 56 | + * ``` |
| 57 | + * { |
| 58 | + * id: "id", // if present. Otherwise a new item will be created |
| 59 | + * category: "string", |
| 60 | + * metadata: { |
| 61 | + * name: "name", |
| 62 | + * description: "description", |
| 63 | + * attribute1: "value1", |
| 64 | + * attribute2: "value2" |
| 65 | + * } |
| 66 | + * permission: [{}] // permissions to save |
| 67 | + * data: {} |
| 68 | + * linkedResources: { |
| 69 | + * thumbnail: { |
| 70 | + * tail: '/raw?decode=datauri' // for thumbnails, this will be appended to the resource URL in the main resource |
| 71 | + * data: {} |
| 72 | + * |
| 73 | + * } |
| 74 | + * } |
| 75 | + * ``` |
| 76 | + * } |
| 77 | + * @param {resource} param0 resource content |
| 78 | + * @return an observable that emits the id of the resource |
| 79 | + */ |
| 80 | + createResource: (...args) => Persistence.getApi().createResource(...args), |
| 81 | + /** |
| 82 | + * Updates a resource setting up permission and linked resources |
| 83 | + * @param {resource} param0 the resource to update (must contain the id) |
| 84 | + * @return an observable that emits the id of the updated resource |
| 85 | + */ |
| 86 | + updateResource: (...args) => Persistence.getApi().updateResource(...args), |
| 87 | + /** |
| 88 | + * Deletes a resource and its linked attributes |
| 89 | + * @param {object} resource the resource with the id |
| 90 | + * @param {object} options properties: deleteLinkedResources default true |
| 91 | + * @param {object} API the API to use |
| 92 | + * @return an observable that emits axios response for the deleted resource and for each of its deleted attributes |
| 93 | + */ |
| 94 | + deleteResource: (...args) => Persistence.getApi().deleteResource(...args) |
| 95 | +}; |
| 96 | + |
| 97 | +module.exports = Persistence; |
0 commit comments