UNPKG

gpii-universal

Version:

Cross platform, core components of the GPII personalization infrastructure.

266 lines (255 loc) 8.87 kB
/*! GPII CouchDB Data Store Copyright 2016-2019 OCAD university Licensed under the New BSD license. You may not use this file except in compliance with this License. You may obtain a copy of the License at https://github.com/GPII/universal/blob/master/LICENSE.txt */ /* * gpii.dbOperation.dbDataStore provides APIs for the authorization server to communicate with the backend * data storage using CouchDB. * * This DB data store is a re-writing of the initial synchronized in memory data store. It now uses * async promise API to satisfy the async database operations. However, the in memory data store continues * to serve as a good reference with an much simpler logic. It can be found at: * https://github.com/GPII/universal/blob/820e4919907e56f6412b2e3bab18675d5388b00b/gpii/node_modules/gpii-oauth2/gpii-oauth2-datastore/src/InMemoryDataStore.js */ "use strict"; var fluid = fluid || require("infusion"); var gpii = fluid.registerNamespace("gpii"); if (typeof require !== "undefined") { fluid.require("kettle"); } fluid.defaults("gpii.dbOperation.dbDataSource", { gradeNames: ["kettle.dataSource.URL", "kettle.dataSource.CouchDB"], baseUrl: null, // Supplied by integrators port: null, // Supplied by integrators dbName: null, // Supplied by integrators requestUrl: null, // Supplied by integrators termMap: { }, directModel: { }, notFoundIsEmpty: true, rules: { writePayload: { "": "" }, readPayload: { "": "" } }, // requestUrl needs to be resolved upfront because it contains more string templates that need to be replaced at the // next round when kettle.dataSource.URL kicks in to compose the actual URL. // An example of requestUrl is "/%id", in which case the expected url should be "%baseUrl:%port/%dbName/%id" instead // of having "%requestUrl" embedded. The expander below is to prepare the url that's sensible to kettle.dataSource. url: { expander: { funcName: "fluid.stringTemplate", args: ["%baseUrl:%port/%dbName%requestUrl", { requestUrl: "{that}.options.requestUrl", baseUrl: "{that}.options.baseUrl", port: "{that}.options.port", dbName: "{that}.options.dbName" }] } } }); fluid.defaults("gpii.dbOperation.dbDataSource.writable", { gradeNames: ["gpii.dbOperation.dbDataSource", "kettle.dataSource.CouchDB.writable"], writable: true, writeMethod: "PUT" }); fluid.defaults("gpii.dbOperation.dbDataStore", { gradeNames: ["gpii.dbOperation.dataStore"], // Supplied by GPII configuration to config all gpii.dbOperation.dbDataSource instances. // It contains these elements: // 1. gradeNames: The mixin grade // 2. baseUrl: The base URL to where the database is located. For example, a default locally installed CouchDB uses http://127.0.1.1 // 3. port: The port where the database is located. For example, a default locally installed CouchDB uses port 5984 // 4. dbName: The database name dataSourceConfig: { }, distributeOptions: { "dbDataStore.dataSourceConfig": { source: "{that}.options.dataSourceConfig", target: "{that > gpii.dbOperation.dbDataSource}.options" } }, components: { findByIdDataSource: { type: "gpii.dbOperation.dbDataSource", options: { requestUrl: "/%id", termMap: { id: "%id" } } }, findPrefsSafeByGpiiKeyDataSource: { type: "gpii.dbOperation.dbDataSource", options: { requestUrl: "/_design/views/_view/findPrefsSafeByGpiiKey?key=%22%gpiiKey%22&include_docs=true", termMap: { gpiiKey: "%gpiiKey" }, rules: { readPayload: { "": "rows.0" } } } }, findClientByOauth2ClientIdDataSource: { type: "gpii.dbOperation.dbDataSource", options: { requestUrl: "/_design/views/_view/findClientByOauth2ClientId?key=%22%oauth2ClientId%22&include_docs=true", termMap: { oauth2ClientId: "%oauth2ClientId" }, rules: { readPayload: { "": "rows.0" } } } }, findAllViewsDataSource: { type: "gpii.dbOperation.dbDataSource", options: { requestUrl: "/_design/views" } }, findInfoByAccessTokenDataSource: { type: "gpii.dbOperation.dbDataSource", options: { requestUrl: "/_design/views/_view/findInfoByAccessToken?key=%22%accessToken%22&include_docs=true", termMap: { accessToken: "%accessToken" }, rules: { readPayload: { "": "rows.0" } } } }, saveDataSource: { type: "gpii.dbOperation.dbDataSource.writable", options: { requestUrl: "/%id", termMap: { id: "%id" } } } }, invokers: { findById: { funcName: "gpii.dbOperation.dbDataStore.findRecord", args: [ "{that}.findByIdDataSource", { id: "{arguments}.0" }, "id" ] // id }, findGpiiKey: { func: "{that}.findById" // gpiiKey }, findClientById: { func: "{that}.findById" // clientId }, findClientCredentialById: { func: "{that}.findById" // clientCredentialId }, findPrefsSafeByGpiiKey: { funcName: "gpii.dbOperation.dbDataStore.findRecord", args: [ "{that}.findPrefsSafeByGpiiKeyDataSource", { gpiiKey: "{arguments}.0" }, "gpiiKey", gpii.dbOperation.dbDataStore.findPrefsSafeByGpiiKeyPostProcess ] // gpiiKey }, findClientByOauth2ClientId: { funcName: "gpii.dbOperation.dbDataStore.findRecord", args: [ "{that}.findClientByOauth2ClientIdDataSource", { oauth2ClientId: "{arguments}.0" }, "oauth2ClientId", gpii.dbOperation.dbDataStore.findClientByOauth2ClientIdPostProcess ] // oauth2ClientId }, findInfoByAccessToken: { funcName: "gpii.dbOperation.dbDataStore.findRecord", args: [ "{that}.findInfoByAccessTokenDataSource", { accessToken: "{arguments}.0" }, "accessToken", gpii.dbOperation.dbDataStore.findInfoByAccessTokenPostProcess ] // accessToken }, findAllViews: { funcName: "gpii.dbOperation.dbDataStore.findRecord", args: ["{that}.findAllViewsDataSource"] }, addGpiiKey: { funcName: "gpii.dbOperation.dbDataStore.addGpiiKey", args: [ "{that}.saveDataSource", "{arguments}.0" ] // gpiiKeyData }, updateGpiiKey: { funcName: "gpii.dbOperation.dbDataStore.updateGpiiKey", args: [ "{that}.saveDataSource", "{arguments}.0", "{arguments}.1" ] // gpiiKey, gpiiKeyData }, addPrefsSafe: { funcName: "gpii.dbOperation.dbDataStore.addPrefsSafe", args: [ "{that}.saveDataSource", "{arguments}.0" ] // prefsSafeData }, updatePrefsSafe: { funcName: "gpii.dbOperation.dbDataStore.updatePrefsSafe", args: [ "{that}.saveDataSource", "{arguments}.0", "{arguments}.1" ] // prefsSafeId, prefsSafeData }, addAuthorization: { funcName: "gpii.dbOperation.dbDataStore.addAuthorization", args: [ "{that}.saveDataSource", "{arguments}.0" ] // authorizationData } } });