UNPKG

3.46 kBPlain TextView Raw
1/*
2 * Copyright © 2019 Atomist, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17import {
18 addressEvent,
19 HandlerContext,
20 QueryNoCacheOptions,
21} from "@atomist/automation-client";
22import { PreferenceStoreFactory } from "@atomist/sdm";
23import { SdmPreferenceByKey } from "../../typings/types";
24import {
25 AbstractPreferenceStore,
26 Preference,
27} from "./AbstractPreferenceStore";
28
29/**
30 * Factory to create a new GraphQLPreferenceStore instance
31 * @deprecated use TeamConfigurationPreferenceStoreFactory
32 */
33// tslint:disable-next-line:deprecation
34export const GraphQLPreferenceStoreFactory: PreferenceStoreFactory = ctx => new GraphQLPreferenceStore(ctx);
35
36/**
37 * PreferenceStore implementation that stores preferences in the backend GraphQL store.
38 * @deprecated use TeamConfigurationPreferenceStore
39 */
40export class GraphQLPreferenceStore extends AbstractPreferenceStore {
41
42 constructor(private readonly context: HandlerContext) {
43 super(context);
44 }
45
46 protected async doGet(name: string, namespace: string): Promise<Preference | undefined> {
47 const key = this.scopeKey(name, namespace);
48 const result = await this.context.graphClient.query<SdmPreferenceByKey.Query, SdmPreferenceByKey.Variables>({
49 name: "SdmPreferenceByKey",
50 variables: {
51 key: [key],
52 },
53 options: QueryNoCacheOptions,
54 });
55 if (!!result.SdmPreference && result.SdmPreference.length === 1) {
56 return {
57 name,
58 namespace,
59 value: result.SdmPreference[0].value,
60 ttl: result.SdmPreference[0].ttl,
61 };
62 }
63 return undefined;
64 }
65
66 protected doPut(pref: Preference): Promise<void> {
67 const key = this.scopeKey(pref.name, pref.namespace);
68 return this.context.messageClient.send({
69 key,
70 value: pref.value,
71 ttl: typeof pref.ttl === "number" ? Date.now() + pref.ttl : undefined,
72 }, addressEvent("SdmPreference"));
73 }
74
75 protected async doList(namespace: string): Promise<Preference[]> {
76 const result = await this.context.graphClient.query<SdmPreferenceByKey.Query, SdmPreferenceByKey.Variables>({
77 name: "SdmPreferenceByKey",
78 options: QueryNoCacheOptions,
79 });
80 if (!!result.SdmPreference) {
81 return result.SdmPreference.filter(p => !namespace || p.key.startsWith(`${namespace}_$_`)).map(p => ({
82 name: p.key.includes("_$_") ? p.key.split("_$_")[1] : p.key,
83 namespace: p.key.includes("_$_") ? p.key.split("_$_")[0] : undefined,
84 value: p.value,
85 ttl: p.ttl,
86 }));
87 }
88 return [];
89 }
90
91 protected async doDelete(name: string, namespace: string): Promise<void> {
92 return this.doPut({ name, namespace, value: undefined, ttl: -100 });
93 }
94
95}