mirror of
https://github.com/filecoin-project/slate.git
synced 2024-11-27 01:03:08 +03:00
slates: adds database retrieval, update, and creation methods
This commit is contained in:
parent
3d64268058
commit
a80ede160c
@ -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,
|
||||
};
|
||||
|
24
node_common/data/methods/create-slate.js
Normal file
24
node_common/data/methods/create-slate.js
Normal 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,
|
||||
};
|
||||
},
|
||||
});
|
||||
};
|
28
node_common/data/methods/get-slate-by-id.js
Normal file
28
node_common/data/methods/get-slate-by-id.js
Normal 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,
|
||||
};
|
||||
},
|
||||
});
|
||||
};
|
29
node_common/data/methods/get-slate-by-name.js
Normal file
29
node_common/data/methods/get-slate-by-name.js
Normal 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,
|
||||
};
|
||||
},
|
||||
});
|
||||
};
|
28
node_common/data/methods/update-slate-by-id.js
Normal file
28
node_common/data/methods/update-slate-by-id.js
Normal 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,
|
||||
};
|
||||
},
|
||||
});
|
||||
};
|
@ -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();
|
||||
});
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user