slates: adds database retrieval, update, and creation methods

This commit is contained in:
@wwwjim 2020-07-24 17:16:35 -07:00
parent 3d64268058
commit a80ede160c
6 changed files with 125 additions and 0 deletions

View File

@ -4,10 +4,21 @@ import deleteUserByUsername from "~/node_common/data/methods/delete-user-by-user
import getUserByUsername from "~/node_common/data/methods/get-user-by-username";
import getUserById from "~/node_common/data/methods/get-user-by-id";
import createSlate from "~/node_common/data/methods/create-slate";
import getSlateByName from "~/node_common/data/methods/get-slate-by-name";
import getSlateById from "~/node_common/data/methods/get-slate-by-id";
import updateSlateById from "~/node_common/data/methods/update-slate-by-id";
export {
// NOTE(jim): User operations.
createUser,
updateUserById,
deleteUserByUsername,
getUserByUsername,
getUserById,
// NOTE(jim): Slate operations.
createSlate,
getSlateByName,
getSlateById,
updateSlateById,
};

View File

@ -0,0 +1,24 @@
import { runQuery } from "~/node_common/data/utilities";
export default async ({ slatename, data = {} }) => {
return await runQuery({
label: "CREATE_SLATE",
queryFn: async (DB) => {
const query = await DB.insert({
slatename,
data,
})
.into("slates")
.returning("*");
const index = query ? query.pop() : null;
return index;
},
errorFn: async (e) => {
return {
error: "CREATE_SLATE",
source: e,
};
},
});
};

View File

@ -0,0 +1,28 @@
import { runQuery } from "~/node_common/data/utilities";
export default async ({ id }) => {
return await runQuery({
label: "GET_SLATE_BY_ID",
queryFn: async (DB) => {
const query = await DB.select("*")
.from("slates")
.where({ id })
.first();
if (!query || query.error) {
return null;
}
if (query.id) {
return query;
}
return null;
},
errorFn: async (e) => {
return {
error: "GET_SLATE_BY_ID",
source: e,
};
},
});
};

View File

@ -0,0 +1,29 @@
import { runQuery } from "~/node_common/data/utilities";
export default async ({ slatename }) => {
return await runQuery({
label: "GET_SLATE_BY_NAME",
queryFn: async (DB) => {
const query = await DB.select("*")
.from("slates")
.where({ slatename })
.first();
if (!query || query.error) {
return null;
}
if (query.id) {
return query;
}
return null;
},
errorFn: async (e) => {
return {
error: "GET_SLATE_BY_NAME",
source: e,
};
},
});
};

View File

@ -0,0 +1,28 @@
import { runQuery } from "~/node_common/data/utilities";
export default async ({ id, slatename, data }) => {
const updateObject = { data };
if (slatename) {
updateObject.slatename = slatename;
}
return await runQuery({
label: "UPDATE_SLATE_BY_ID",
queryFn: async (DB) => {
const response = await DB.from("slates")
.where("id", id)
.update(updateObject)
.returning("*");
const index = response ? response.pop() : null;
return index;
},
errorFn: async (e) => {
return {
error: "UPDATE_SLATE",
source: e,
};
},
});
};

View File

@ -61,6 +61,11 @@ const createSlatesTable = db.schema.createTable("slates", function(table) {
table.timestamp("published_at").nullable();
table
.string("slatename")
.unique()
.nullable();
table.jsonb("data").nullable();
});