Skip to content

Commit

Permalink
EOD: 7-12-24
Browse files Browse the repository at this point in the history
  • Loading branch information
irishcarbomb777 committed Jul 12, 2024
1 parent a18d977 commit 2938c7d
Show file tree
Hide file tree
Showing 9 changed files with 61 additions and 14 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@
"package-up": "^5.0.0",
"pastel": "^3.0.0",
"react": "^18.3.1",
"uuid": "^10.0.0",
"znv": "^0.4.0",
"zod": "^3.23.8",
"zustand": "^4.5.2"
},
"peerDependencies": {
"neo4j-driver": "^5.21.0",
"openai": "^4.49.0",
"immer": "^10.1.1"
"immer": "^10.1.1",
"uuid": "^10.0.0"
}
}
Empty file.
1 change: 0 additions & 1 deletion src/fns/getNodeByKeyFactory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ export const getNodeByKeyFactory = <
match (node:${nodeKey.nodeType as string} {nodeId: $nodeId})
return node
`, { nodeId: nodeKey.nodeId }).then(res => res.records[0]?.get('node').properties)
console.log(node)
if (!node) return UixErr({
subtype: UixErrSubtype.GET_NODE_BY_KEY_FAILED,
message: `Failed to find node of type ${nodeKey.nodeType as string} with id ${nodeKey.nodeId}`,
Expand Down
5 changes: 4 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@ export * from './fns/getVectorNodeByKeyFactory'
export * from './fns/getAllOfNodeTypeFactory'
export * from './fns/getChildNodeSetFactory'
export * from './fns/getUniqueChildNodeFactory'
export * from './fns/getNodeByIndexFactory'
export * from './fns/getNodeByIndexFactory'

// Clients
export { createNeo4jClient } from './clients/neo4j'
38 changes: 34 additions & 4 deletions src/templates/hooks/useNodeSetTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { ConfiguredNodeTypeMap } from './staticObjects'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { NodeSetQueryOptions } from './queryOptions'
import { createNode } from './functionModule'
import { v4 as uuid } from 'uuid'
export const useNodeSet = <
ParentNodeType extends NodeSetParentTypes<ConfiguredNodeTypeMap>,
Expand All @@ -33,17 +33,47 @@ export const useNodeSet = <
const queryClient = useQueryClient()
const { data, error } = useQuery(queryOptions)
const createNodeMutation = useMutation({
mutationFn: async (initialState: NodeState<ConfiguredNodeTypeMap[ChildNodeType]>) => {
mutationFn: async ({
nodeId,
createdAt,
updatedAt,
nodeType,
...initialState
}: NodeShape<ConfiguredNodeTypeMap[ChildNodeType]>) => {
return await createNode({
parentNodeKeys: [parentNodeKey],
childNodeType,
initialState
initialState: initialState as NodeState<ConfiguredNodeTypeMap[ChildNodeType]>,
providedNodeId: nodeId,
})
},
onMutate: async (newNode) => {
await queryClient.cancelQueries({queryKey: queryOptions.queryKey})
const previousData = queryClient.getQueryData(queryOptions.queryKey)
queryClient.setQueryData(queryOptions.queryKey, oldData => {
if (!oldData) return [newNode]
return [...oldData, newNode]
})
return { previousData }
},
onError: (err, newData, context) => {
queryClient.setQueryData(queryOptions.queryKey, context?.previousData)
},
onSuccess: () => queryClient.invalidateQueries({
queryKey: [parentNodeKey.nodeType, parentNodeKey.nodeId, childNodeType]
})
})
return { data, error, createNodeMutation }
return {
data, error, createNode: (...[initialState, handlers]: [
NodeState<ConfiguredNodeTypeMap[ChildNodeType]>,
Parameters<typeof createNodeMutation['mutate']>[1]?
]) => createNodeMutation.mutateAsync({
nodeId: uuid(),
createdAt: new Date().getTime(),
updatedAt: new Date().getTime(),
nodeType: childNodeType,
...initialState
}, handlers)
}
}
`
2 changes: 1 addition & 1 deletion src/templates/staticObjectsTemplate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export const staticObjectsTemplate = (config: GenericUixConfig) => {
return /* ts */`
// Start of File
import uixConfig from '${path.relative(config.outdir, config.pathToConfig).split(path.sep).join('/').replace(/\.[^/.]+$/, '')}'
import { NodeShape, NodeState, createNeo4jClient, GraphType } from '@thinairthings/uix'
import { NodeShape, NodeState, GraphType } from '@thinairthings/uix'
export const uixGraph = new GraphType(uixConfig.type, uixConfig.nodeTypeSet)
export const nodeTypeMap = uixGraph.nodeTypeMap
Expand Down
2 changes: 1 addition & 1 deletion src/types/NodeType.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ type TriggerMap<NodeShape extends AnyNodeShape> = Map<'onCreate' | 'onUpdate' |
Map<string, (node: NodeShape) => void>
>;

export type AnyZodDiscriminatedUnion = ZodDiscriminatedUnion<any, [AnyZodObject, AnyZodObject, ...AnyZodObject[]]>;
export type AnyZodDiscriminatedUnion = ZodDiscriminatedUnion<any, any>;
/**
* Represents a node type in a graph database.
*/
Expand Down
2 changes: 1 addition & 1 deletion tests/uix/generated/staticObjects.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

// Start of File
import uixConfig from '../uix.config'
import { NodeShape, NodeState, createNeo4jClient, GraphType } from '@thinairthings/uix'
import { NodeShape, NodeState, GraphType } from '@thinairthings/uix'

export const uixGraph = new GraphType(uixConfig.type, uixConfig.nodeTypeSet)
export const nodeTypeMap = uixGraph.nodeTypeMap
Expand Down
21 changes: 18 additions & 3 deletions tests/uix/generated/useNodeSet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ConfiguredNodeTypeMap } from './staticObjects'
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
import { NodeSetQueryOptions } from './queryOptions'
import { createNode } from './functionModule'

import { v4 as uuid } from 'uuid'

export const useNodeSet = <
ParentNodeType extends NodeSetParentTypes<ConfiguredNodeTypeMap>,
Expand All @@ -30,12 +30,27 @@ export const useNodeSet = <
const queryClient = useQueryClient()
const { data, error } = useQuery(queryOptions)
const createNodeMutation = useMutation({
mutationFn: async (initialState: NodeState<ConfiguredNodeTypeMap[ChildNodeType]>) => {
mutationFn: async ({
nodeId = uuid(),
createdAt = new Date().getTime(),
updatedAt = new Date().getTime(),
...initialState
}: NodeShape<ConfiguredNodeTypeMap[ChildNodeType]>) => {
return await createNode({
parentNodeKeys: [parentNodeKey],
childNodeType,
initialState
initialState,
providedNodeId: nodeId,
})
},
onMutate: async (newNode) => {
await queryClient.cancelQueries({queryKey: queryOptions.queryKey})
const previousData = queryClient.getQueryData(queryOptions.queryKey)
queryClient.setQueryData(queryOptions.queryKey, oldData => {
if (!oldData) return [newNode]
return [...oldData, newNode]
})
return { previousData }
},
onSuccess: () => queryClient.invalidateQueries({
queryKey: [parentNodeKey.nodeType, parentNodeKey.nodeId, childNodeType]
Expand Down

0 comments on commit 2938c7d

Please sign in to comment.