feat(node_common/data): add views methods

This commit is contained in:
Aminejv 2022-09-12 12:27:46 +01:00
parent a66ddceeb4
commit 5c50b7652a
6 changed files with 101 additions and 0 deletions

View File

@ -0,0 +1,32 @@
import { runQuery } from "~/node_common/data/utilities";
export default async ({ ownerId, name, filterBySource, filterBySlateId, metadata = {} }) => {
return await runQuery({
label: "CREATE_VIEW",
queryFn: async (DB) => {
let query = await DB.insert({
ownerId,
name,
filterBySource,
filterBySlateId,
metadata: JSON.stringify(metadata),
})
.into("views")
.returning("*");
if (!query) {
return null;
}
query = query.pop();
return JSON.parse(JSON.stringify(query));
},
errorFn: async () => {
return {
error: true,
decorator: "CREATE_VIEW",
};
},
});
};

View File

@ -16,6 +16,8 @@ export default async ({ id }) => {
const activity = await DB("activity").where({ slateId: id }).del();
const views = await DB("views").where({ filterBySlateId: id }).del();
const data = await DB("slates").where({ id }).del().returning("*");
let slate = data ? data.pop() : data;

View File

@ -39,6 +39,8 @@ export default async ({ id }) => {
const usage = await DB.from("usage").where({ userId: id }).del();
const views = await DB.from("views").where({ ownerId: id }).del();
const data = await DB.from("users").where({ id }).del().returning("*");
let user = data ? data.pop() : data;

View File

@ -0,0 +1,18 @@
import { runQuery } from "~/node_common/data/utilities";
export default async ({ id }) => {
return await runQuery({
label: "DELETE_VIEW",
queryFn: async (DB) => {
const data = await DB.from("views").where({ id }).del().returning("*");
return data;
},
errorFn: async (e) => {
return {
error: true,
decorator: "DELETE_VIEW",
};
},
});
};

View File

@ -0,0 +1,22 @@
import { runQuery } from "~/node_common/data/utilities";
export default async ({ id }) => {
return await runQuery({
label: "GET_VIEW_BY_ID",
queryFn: async (DB) => {
const query = await DB.select("*").from("views").where({ id }).first();
if (!query || query.error) {
return null;
}
return JSON.parse(JSON.stringify(query));
},
errorFn: async (e) => {
return {
error: true,
decorator: "GET_VIEW_BY_ID",
};
},
});
};

View File

@ -0,0 +1,25 @@
import { runQuery } from "~/node_common/data/utilities";
export default async ({ ownerId }) => {
return await runQuery({
label: "GET_VIEWS_BY_USER_ID",
queryFn: async (DB) => {
const query = await DB.select("*")
.from("views")
.where({ ownerId })
.orderBy("createdAt", "asc");
if (!query || query.error) {
return null;
}
return JSON.parse(JSON.stringify(query));
},
errorFn: async (e) => {
return {
error: true,
decorator: "GET_VIEWS_BY_USER_ID",
};
},
});
};