Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Why selector-related code is located in handlers file? #308

Open
FreePhoenix888 opened this issue Feb 15, 2024 · 0 comments
Open

Why selector-related code is located in handlers file? #308

FreePhoenix888 opened this issue Feb 15, 2024 · 0 comments
Labels
question Further information is requested

Comments

@FreePhoenix888
Copy link
Member

FreePhoenix888 commented Feb 15, 2024

export async function insertSelector() {
const typeTypeId = await deep.id('@deep-foundation/core', 'Type');
const { data: [{ id: ty0 }] } = await deep.insert({
type_id: typeTypeId,
});
const { data: [{ id: ty1 }] } = await deep.insert({
type_id: typeTypeId,
from_id: ty0,
to_id: ty0,
});
const treeIncludeDownTypeId = await deep.id('@deep-foundation/core', 'TreeIncludeDown');
const { data: [{ id: tr1, out: treeIncludes }] } = await deep.insert({
type_id: await deep.id('@deep-foundation/core', 'Tree'),
out: { data: [
{
type_id: treeIncludeDownTypeId,
to_id: ty1,
},
{
type_id: treeIncludeDownTypeId,
to_id: ty0,
},
] }
}, {
returning: `
id
out(where: { to_id: { _in: [${ty0}, ${ty1}] }, type_id: { _eq: ${treeIncludeDownTypeId} } }) {
id
}
`,
}) as any;
const { data: [{ id: id0 }] } = await deep.insert({
type_id: ty0,
});
const selectorIncludeTypeId = await deep.id('@deep-foundation/core', 'SelectorInclude');
const selectorTreeTypeId = await deep.id('@deep-foundation/core', 'SelectorTree');
const { data: [{ id: s1, out: selectorData }] } = await deep.insert({
type_id: await deep.id('@deep-foundation/core', 'Selector'),
out: { data: [
{
type_id: selectorIncludeTypeId,
to_id: id0,
out: { data: {
type_id: selectorTreeTypeId,
to_id: tr1
} },
},
] }
}, {
returning: `
id
out(where: { to_id: { _eq: ${id0} }, type_id: { _eq: ${selectorIncludeTypeId} } }) {
id
out(where: { to_id: { _eq: ${tr1} }, type_id: { _eq: ${selectorTreeTypeId} } }) {
id
}
}
`,
}) as any;
return {
nodeTypeId: ty0,
linkTypeId: ty1,
treeId: tr1,
treeIncludesIds: treeIncludes.map(({ id }) => id),
selectorId: s1,
selectorIncludeId: selectorData[0].id,
selectorTreeId: selectorData[0].out[0].id,
rootId: id0,
};
};
export async function insertSelectorItems({ selectorId, nodeTypeId, linkTypeId, treeId, rootId }) {
// const { data: [{ id: id1 }] } = await deep.insert({
// type_id: nodeTypeId,
// in: { data: {
// type_id: linkTypeId,
// from_id: rootId,
// } }
// });
const { data: [{ id: linkId1, to: { id: nodeId1 } }] } = await deep.insert({
type_id: linkTypeId,
from_id: rootId,
to: { data: {
type_id: nodeTypeId,
} }
}, { returning: 'id to { id }' }) as any;
// return linkId1; // doesn't work
// const { data: [{ id: id2 }] } = await deep.insert({
// type_id: nodeTypeId,
// in: { data: {
// type_id: linkTypeId,
// from_id: id1,
// } }
// });
const { data: [{ id: linkId2, to: { id: nodeId2 } }] } = await deep.insert({
from_id: nodeId1,
type_id: linkTypeId,
to: { data: {
type_id: nodeTypeId,
} }
}, { returning: 'id to { id }' }) as any;
// const n1 = await deep.select({
// item_id: { _eq: id2 }, selector_id: { _eq: selectorId }
// }, { table: 'selectors', returning: 'item_id selector_id' });
// assert.lengthOf(n1?.data, 1, `item_id ${id2} must be in selector_id ${selectorId}`);
// return linkId2;
return [
{ linkId: linkId1, nodeId: nodeId1 },
{ linkId: linkId2, nodeId: nodeId2 },
]
};
export async function insertSelectorItem({ selectorId, nodeTypeId, linkTypeId, treeId, rootId }) {
const { data: [{ id: linkId }] } = await deep.insert({
type_id: linkTypeId,
from_id: rootId,
to: { data: {
type_id: nodeTypeId,
} }
}, { returning: 'id to { id }' }) as any;
return { linkId };
};

export const deleteSelector = async (selector: any) => {
const { treeIncludesIds, ...withoutTreeIncluds } = selector;
const ids = (_.concat(treeIncludesIds, Object.values(withoutTreeIncluds)));
const _ids = []
const compact = {id: {_in: _ids.concat(_.compact(ids))}};
await deep.delete(compact);
};

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

1 participant