initial commit

This commit is contained in:
Elias Schneider
2022-04-25 15:15:17 +02:00
commit 61be87b72a
72 changed files with 11502 additions and 0 deletions

View File

@@ -0,0 +1,11 @@
import axios from "axios";
const api = () =>
axios.create({
baseURL: process.env["APPWRITE_HOST"],
headers: {
cookie: `a_session_console=${process.env["APPWRITE_USER_TOKEN"]}`,
},
});
export default api;

View File

@@ -0,0 +1,44 @@
import api from "./api.service";
import rl from "readline-sync";
import cookie from "cookie";
const getToken = async () => {
var email = rl.question("Email: ");
var password = rl.question("Password: ", {
hideEchoBack: true,
});
const credentials = await api().post("/account/sessions", {
email,
password,
});
return cookie.parse(credentials.headers["set-cookie"].toString())
.a_session_console_legacy;
};
const generateApiKey = async () => {
const res = await api().post("/projects/pingvin-share/keys", {
name: "Setup key",
scopes: [
"collections.read",
"collections.write",
"attributes.read",
"attributes.write",
"indexes.read",
"indexes.write",
"documents.read",
"documents.write",
"functions.read",
"functions.write",
"execution.read",
"execution.write",
],
});
return res.data.secret;
};
export default {
getToken,
generateApiKey,
};

View File

@@ -0,0 +1,18 @@
import sdk from "node-appwrite";
const aw = () => {
let client = new sdk.Client();
client
.setEndpoint(process.env["APPWRITE_HOST"])
.setProject("pingvin-share")
.setKey(process.env["APPWRITE_API_KEY"])
.setSelfSigned();
const database = new sdk.Database(client);
const storage = new sdk.Database(client);
const functions = new sdk.Functions(client);
return { database, storage, functions };
};
export default aw;

View File

@@ -0,0 +1,137 @@
import api from "./api.service";
import aw from "./aw.service";
import collections from "../data/collections";
import functions from "../data/functions";
import zipDirectory from "../utils/compress.util";
import fs from "fs";
const createProject = async () => {
const teamId = (
await api().post("/teams", {
teamId: "unique()",
name: "Pingvin Share",
})
).data.$id;
return await api().post("/projects", {
projectId: "pingvin-share",
name: "Pingvin Share",
teamId,
});
};
const addPlatform = async (hostname : string) => {
await api().post("/projects/pingvin-share/platforms", {
type: "web",
name: "Pingvin Share Web Frontend",
hostname: hostname,
});
};
const createCollections = async () => {
for (const collection of collections) {
const { attributes } = collection;
const { indexes } = collection;
await aw().database.createCollection(
collection.$id,
collection.name,
collection.permission,
collection.$read,
collection.$write
);
for (const attribute of attributes) {
if (attribute.type == "string") {
await aw().database.createStringAttribute(
collection.$id,
attribute.key,
attribute.size,
attribute.required,
attribute.default,
attribute.array
);
} else if (attribute.type == "integer") {
await aw().database.createIntegerAttribute(
collection.$id,
attribute.key,
attribute.required,
attribute.min,
attribute.max,
attribute.default,
attribute.array
);
} else if (attribute.type == "boolean") {
await aw().database.createBooleanAttribute(
collection.$id,
attribute.key,
attribute.required,
attribute.default,
attribute.array
);
}
}
for (const index of indexes) {
aw().database.createIndex(
collection.$id,
index.key,
index.type,
index.attributes
);
}
}
};
const generateFunctionsApiKey = async () => {
const res = await api().post("/projects/pingvin-share/keys", {
name: "Functions API Key",
scopes: [
"documents.read",
"documents.write",
"buckets.read",
"buckets.write",
"files.read",
],
});
return res.data.secret;
};
const createFunctions = async () => {
for (const fcn of functions()) {
await aw().functions.create(
fcn.$id,
fcn.name,
fcn.execute,
fcn.runtime,
fcn.vars,
fcn.events,
fcn.schedule,
fcn.timeout
);
}
};
const createFunctionDeployments = async () => {
let path: string;
for (const fcn of functions()) {
(path = (await zipDirectory(fcn.$id)) as string),
await aw().functions.createDeployment(
fcn.$id,
"src/index.js",
path,
true
);
}
// Delete zip
fs.unlinkSync(path);
};
export default {
createProject,
createCollections,
createFunctions,
createFunctionDeployments,
generateFunctionsApiKey,
addPlatform,
};
function token(token: any) {
throw new Error("Function not implemented.");
}