-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathschema.ts
182 lines (157 loc) · 4.58 KB
/
schema.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import fs from "fs";
import path from "path";
import { copy, fetch } from "./helper";
import { ConfigInterface, SchemaInterface } from "./types";
const cwd = process.cwd();
export function pickSchema(schema: SchemaInterface): SchemaInterface {
if (!schema) {
throw new Error("Schema not found.");
}
if (!schema.className) {
throw new Error("Schema is missing 'className' key.");
}
schema = copy(schema);
if (schema.fields) {
delete schema.fields.objectId;
delete schema.fields.createdAt;
delete schema.fields.updatedAt;
delete schema.fields.ACL;
}
return {
className: schema.className,
fields: schema.fields || {},
classLevelPermissions: schema.classLevelPermissions || {},
};
}
export async function getLocalSchema(
schemaPath: string,
prefix: string = "",
filter?: (className: string) => boolean
): Promise<SchemaInterface[]> {
if (!fs.existsSync(schemaPath)) {
throw new Error(`No local schema at '${schemaPath}'`);
}
let schema: SchemaInterface[] = [];
if (schemaPath.endsWith(".json")) {
schema = JSON.parse(fs.readFileSync(schemaPath, "utf-8")).map(pickSchema);
} else {
schema = fs
.readdirSync(schemaPath)
.filter((p) => p.endsWith(".json"))
.map((p) => ({
className: p.replace(".json", ""),
...JSON.parse(fs.readFileSync(path.resolve(schemaPath, p), "utf-8")),
}))
.map(pickSchema);
}
if (filter) {
schema = schema.filter((s) => filter(prefix + s.className));
}
return schema;
}
export async function getRemoteSchema(
{ publicServerURL, appId, masterKey }: ConfigInterface,
filter?: (className: string) => boolean
): Promise<SchemaInterface[]> {
let schema: SchemaInterface[] = await fetch({
url: publicServerURL + "/schemas",
headers: {
"X-Parse-Application-Id": appId,
"X-Parse-Master-Key": masterKey,
},
})
.then((res) => res.results || [])
.then((res) => res.map(pickSchema));
if (filter) {
schema = schema.filter((s) => filter(s.className));
}
schema.sort((a, b) => {
if (a.className < b.className) {
return -1;
}
if (a.className > b.className) {
return 1;
}
return 0;
});
for (const s of schema) {
const keys = Object.keys(s.fields);
keys.sort((a, b) => a.localeCompare(b));
s.fields = Object.fromEntries(keys.map((key) => [key, s.fields[key]]));
}
return schema;
}
export async function createSchema(
{ publicServerURL, appId, masterKey }: ConfigInterface,
schema: SchemaInterface
) {
return await fetch({
url: publicServerURL + "/schemas",
method: "POST",
headers: {
"X-Parse-Application-Id": appId,
"X-Parse-Master-Key": masterKey,
"Content-Type": "application/json",
},
body: JSON.stringify(schema),
});
}
export async function updateSchema(
{ publicServerURL, appId, masterKey }: ConfigInterface,
schema: SchemaInterface
) {
return await fetch({
url: publicServerURL + "/schemas/" + schema.className,
method: "PUT",
headers: {
"X-Parse-Application-Id": appId,
"X-Parse-Master-Key": masterKey,
"Content-Type": "application/json",
},
body: JSON.stringify(schema),
});
}
export async function deleteSchema(
{ publicServerURL, appId, masterKey }: ConfigInterface,
{ className }: { className: string },
{ options }: { options: { deleteNonEmptyClass: boolean | undefined } }
) {
if (options.deleteNonEmptyClass) {
await deleteNonEmptySchemaObjects(
{ publicServerURL, appId, masterKey },
{ className }
);
}
return await fetch({
url: publicServerURL + "/schemas/" + className,
method: "DELETE",
headers: {
"X-Parse-Application-Id": appId,
"X-Parse-Master-Key": masterKey,
},
});
}
async function deleteNonEmptySchemaObjects(
{ publicServerURL, appId, masterKey }: ConfigInterface,
{ className }: { className: string }
) {
//Purge all objects in the class
const objects: { results: any[] } = await fetch({
url: publicServerURL + "/classes/" + className,
method: "GET",
headers: {
"X-Parse-Application-Id": appId,
"X-Parse-Master-Key": masterKey,
},
});
for await (const entry of objects.results) {
await fetch({
url: publicServerURL + "/classes/" + className + "/" + entry.objectId,
method: "DELETE",
headers: {
"X-Parse-Application-Id": appId,
"X-Parse-Master-Key": masterKey,
},
});
}
}