Skip to content

Commit df930a5

Browse files
committed
Updates and tidies for Database resource
1 parent cd33e76 commit df930a5

14 files changed

+291
-227
lines changed

Snowflake-Common/package-lock.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Snowflake-Common/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
},
2222
"devDependencies": {
2323
"@types/jest": "^28.1.4",
24-
"@types/node": "^12.0.0",
24+
"@types/node": "^14.0.0",
2525
"jest": "^28.1.1",
2626
"ts-jest": "^28.0.5",
2727
"typescript": "^4.1.2"

Snowflake-Common/src/snowflake-client.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,22 @@ export class SnowflakeClient {
44
private readonly account: string;
55
private readonly username: string;
66
private readonly password: string;
7+
private readonly application: string;
78

8-
constructor(account: string, username: string, password: string) {
9+
constructor(account: string, username: string, password: string, application: string) {
910
this.account = account;
1011
this.username = username;
1112
this.password = password;
13+
this.application = application;
1214
}
1315

1416
public async doRequest(sqlCommand: string, binds: any[]): Promise<any[]> {
1517
return new Promise(async (resolve, reject) => {
1618
let connection = snowflake.createConnection({
1719
account: this.account,
1820
username: this.username,
19-
password: this.password
21+
password: this.password,
22+
application: this.application
2023
});
2124
connection.connect(
2225
function (err, conn) {

Snowflake-Common/src/util.ts

+181
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
export enum CaseTransformer {
2+
PASCAL_TO_CAMEL,
3+
PASCAL_TO_SNAKE,
4+
SNAKE_TO_CAMEL,
5+
}
6+
7+
export class Transformer {
8+
private readonly _object: { [key: string]: any; };
9+
private caseTransformer: CaseTransformer;
10+
private useSafeKeys: boolean;
11+
private LANGUAGE_KEYWORDS = [
12+
"abstract",
13+
"any",
14+
"as",
15+
"async",
16+
"await",
17+
"bigint",
18+
"boolean",
19+
"break",
20+
"case",
21+
"catch",
22+
"class",
23+
"configurable",
24+
"const",
25+
"constructor",
26+
"continue",
27+
"debugger",
28+
"declare",
29+
"default",
30+
"delete",
31+
"do",
32+
"else",
33+
"enum",
34+
"enumerable",
35+
"export",
36+
"extends",
37+
"false",
38+
"finally",
39+
"for",
40+
"from",
41+
"function",
42+
"get",
43+
"if",
44+
"in",
45+
"implements",
46+
"import",
47+
"instanceof",
48+
"interface",
49+
"is",
50+
"let",
51+
"module",
52+
"namespace",
53+
"never",
54+
"new",
55+
"null",
56+
"number",
57+
"of",
58+
"package",
59+
"private",
60+
"protected",
61+
"public",
62+
"readonly",
63+
"require",
64+
"return",
65+
"set",
66+
"static",
67+
"string",
68+
"super",
69+
"switch",
70+
"symbol",
71+
"this",
72+
"throw",
73+
"true",
74+
"try",
75+
"type",
76+
"typeof",
77+
"undefined",
78+
"value",
79+
"var",
80+
"void",
81+
"while",
82+
"with",
83+
"writable",
84+
"yield",
85+
];
86+
87+
constructor(object: { [key: string]: any }) {
88+
this._object = object;
89+
}
90+
91+
/**
92+
* Returns an instance of `Transformer` for the given object, that let you chain other options/methods.
93+
* This is intended to always call at then end {@link Transformer#transform} to return the transformed object.
94+
*
95+
* @param object The object for which you wish the transform its keys.
96+
*/
97+
public static for(object: { [key: string]: any }) {
98+
return new Transformer(object);
99+
}
100+
101+
/**
102+
* Used to specify how to transform the keys, like PascalCase to camelCase.
103+
* See enum {@link CaseTransformer} for all available options.
104+
*
105+
* @param caseTransformer The transformer to use on the object keys.
106+
*/
107+
public transformKeys(caseTransformer: CaseTransformer) {
108+
this.caseTransformer = caseTransformer;
109+
return this;
110+
}
111+
112+
/**
113+
* Used to generate transform keys with format that the model auto-generation for TypeScript expects.
114+
*
115+
* Detail: The model auto-generation will suffix keys with `_` if they are keywords from the language
116+
* (see https://github.com/cloudsoft/cloudformation-cli-typescript-plugin/blob/master/python/rpdk/typescript/utils.py#L83-L86)
117+
*/
118+
public forModelIngestion() {
119+
this.useSafeKeys = true;
120+
return this;
121+
}
122+
123+
/**
124+
* Transform the given object keys, according to the given {@link CaseTransformer}.
125+
*
126+
* This will return `undefined` if given `object` is not set.
127+
* This will throw an exception is the given {@link CaseTransformer} is invalid.
128+
*/
129+
public transform() {
130+
switch (this.caseTransformer) {
131+
case CaseTransformer.PASCAL_TO_CAMEL:
132+
return this.transformObjectKeys(this._object, key => key.substring(0, 1).toLocaleLowerCase() + key.substring(1));
133+
case CaseTransformer.PASCAL_TO_SNAKE:
134+
return this.transformObjectKeys(this._object, key => key.substring(0, 1).toLocaleLowerCase() + key.substring(1).replace(/([A-Z])/g, (input) => `_${input.toLocaleLowerCase()}`));
135+
case CaseTransformer.SNAKE_TO_CAMEL:
136+
return this.transformObjectKeys(this._object, key => key.substring(0, 1).toLocaleLowerCase() + key.substring(1).replace(/_([a-z])/g, (input, p1) => `${p1.toLocaleUpperCase()}`));
137+
default:
138+
throw new Error(`Case transformer "${this.caseTransformer}" not supported`);
139+
}
140+
}
141+
142+
private transformObjectKeys(object: { [key: string]: any }, transformer: (key: string) => string) {
143+
if (!object) {
144+
return object;
145+
}
146+
147+
return Object.keys(object).reduce((map, key) => {
148+
let value = object[key];
149+
if (value && value instanceof Object && !(value instanceof Array) && !(value instanceof Set)) {
150+
value = this.transformObjectKeys(value, transformer);
151+
}
152+
if (value && value instanceof Set) {
153+
value = Array.of(...value);
154+
}
155+
if (value && Array.isArray(value)) {
156+
value = value.map(item => item && item instanceof Object && !(item instanceof Array) && !(item instanceof Set)
157+
? this.transformObjectKeys(item, transformer)
158+
: item);
159+
}
160+
let newKey = transformer(key);
161+
if (this.useSafeKeys) {
162+
newKey = this.safeKey(newKey);
163+
}
164+
map[newKey] = value;
165+
return map;
166+
}, {} as { [key: string]: any })
167+
}
168+
169+
private safeKey(key: string) {
170+
return this.LANGUAGE_KEYWORDS.includes(key)
171+
? `${key}_`
172+
: key;
173+
}
174+
}
175+
176+
// For backwards compatibility
177+
export function transformObjectCase(model: { [key: string]: any }, caseTransformer: CaseTransformer){
178+
return Transformer.for(model)
179+
.transformKeys(caseTransformer)
180+
.transform();
181+
}

Snowflake-Database-Database/.rpdk-config

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"artifact_type": "RESOURCE",
33
"typeName": "Snowflake::Database::Database",
44
"language": "typescript",
5-
"runtime": "nodejs12.x",
5+
"runtime": "nodejs14.x",
66
"entrypoint": "dist/Snowflake-Database-Database/src/handlers.entrypoint",
77
"testEntrypoint": "dist/Snowflake-Database-Database/src/handlers.testEntrypoint",
88
"settings": {

Snowflake-Database-Database/docs/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Snowflake::Database::Database
22

3-
An example resource schema demonstrating some basic constructs and validation rules.
3+
Allows for the creation and modification of a Snowflake Database. https://docs.snowflake.com/en/user-guide/databases.html
44

55
## Syntax
66

@@ -16,7 +16,7 @@ To declare this entity in your AWS CloudFormation template, use the following sy
1616
"<a href="#dataretentiontimeindays" title="DataRetentionTimeInDays">DataRetentionTimeInDays</a>" : <i>Integer</i>,
1717
"<a href="#maxdataextensiontimeindays" title="MaxDataExtensionTimeInDays">MaxDataExtensionTimeInDays</a>" : <i>Integer</i>,
1818
"<a href="#defaultddlcollation" title="DefaultDdlCollation">DefaultDdlCollation</a>" : <i>String</i>,
19-
"<a href="#comment" title="Comment">Comment</a>" : <i>String</i>,
19+
"<a href="#comment" title="Comment">Comment</a>" : <i>String</i>
2020
}
2121
}
2222
</pre>

Snowflake-Database-Database/docs/database.md

-80
This file was deleted.

Snowflake-Database-Database/package-lock.json

+7-7
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)