Skip to content

Commit 2938c7d

Browse files
committed
EOD: 7-12-24
1 parent a18d977 commit 2938c7d

File tree

9 files changed

+61
-14
lines changed

9 files changed

+61
-14
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,14 @@
6363
"package-up": "^5.0.0",
6464
"pastel": "^3.0.0",
6565
"react": "^18.3.1",
66-
"uuid": "^10.0.0",
6766
"znv": "^0.4.0",
6867
"zod": "^3.23.8",
6968
"zustand": "^4.5.2"
7069
},
7170
"peerDependencies": {
7271
"neo4j-driver": "^5.21.0",
7372
"openai": "^4.49.0",
74-
"immer": "^10.1.1"
73+
"immer": "^10.1.1",
74+
"uuid": "^10.0.0"
7575
}
7676
}

src/extensions/PropertyVector.ts

Whitespace-only changes.

src/fns/getNodeByKeyFactory.ts

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ export const getNodeByKeyFactory = <
3030
match (node:${nodeKey.nodeType as string} {nodeId: $nodeId})
3131
return node
3232
`, { nodeId: nodeKey.nodeId }).then(res => res.records[0]?.get('node').properties)
33-
console.log(node)
3433
if (!node) return UixErr({
3534
subtype: UixErrSubtype.GET_NODE_BY_KEY_FAILED,
3635
message: `Failed to find node of type ${nodeKey.nodeType as string} with id ${nodeKey.nodeId}`,

src/index.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@ export * from './fns/getVectorNodeByKeyFactory'
1919
export * from './fns/getAllOfNodeTypeFactory'
2020
export * from './fns/getChildNodeSetFactory'
2121
export * from './fns/getUniqueChildNodeFactory'
22-
export * from './fns/getNodeByIndexFactory'
22+
export * from './fns/getNodeByIndexFactory'
23+
24+
// Clients
25+
export { createNeo4jClient } from './clients/neo4j'

src/templates/hooks/useNodeSetTemplate.ts

+34-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import { ConfiguredNodeTypeMap } from './staticObjects'
1414
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
1515
import { NodeSetQueryOptions } from './queryOptions'
1616
import { createNode } from './functionModule'
17-
17+
import { v4 as uuid } from 'uuid'
1818
1919
export const useNodeSet = <
2020
ParentNodeType extends NodeSetParentTypes<ConfiguredNodeTypeMap>,
@@ -33,17 +33,47 @@ export const useNodeSet = <
3333
const queryClient = useQueryClient()
3434
const { data, error } = useQuery(queryOptions)
3535
const createNodeMutation = useMutation({
36-
mutationFn: async (initialState: NodeState<ConfiguredNodeTypeMap[ChildNodeType]>) => {
36+
mutationFn: async ({
37+
nodeId,
38+
createdAt,
39+
updatedAt,
40+
nodeType,
41+
...initialState
42+
}: NodeShape<ConfiguredNodeTypeMap[ChildNodeType]>) => {
3743
return await createNode({
3844
parentNodeKeys: [parentNodeKey],
3945
childNodeType,
40-
initialState
46+
initialState: initialState as NodeState<ConfiguredNodeTypeMap[ChildNodeType]>,
47+
providedNodeId: nodeId,
48+
})
49+
},
50+
onMutate: async (newNode) => {
51+
await queryClient.cancelQueries({queryKey: queryOptions.queryKey})
52+
const previousData = queryClient.getQueryData(queryOptions.queryKey)
53+
queryClient.setQueryData(queryOptions.queryKey, oldData => {
54+
if (!oldData) return [newNode]
55+
return [...oldData, newNode]
4156
})
57+
return { previousData }
58+
},
59+
onError: (err, newData, context) => {
60+
queryClient.setQueryData(queryOptions.queryKey, context?.previousData)
4261
},
4362
onSuccess: () => queryClient.invalidateQueries({
4463
queryKey: [parentNodeKey.nodeType, parentNodeKey.nodeId, childNodeType]
4564
})
4665
})
47-
return { data, error, createNodeMutation }
66+
return {
67+
data, error, createNode: (...[initialState, handlers]: [
68+
NodeState<ConfiguredNodeTypeMap[ChildNodeType]>,
69+
Parameters<typeof createNodeMutation['mutate']>[1]?
70+
]) => createNodeMutation.mutateAsync({
71+
nodeId: uuid(),
72+
createdAt: new Date().getTime(),
73+
updatedAt: new Date().getTime(),
74+
nodeType: childNodeType,
75+
...initialState
76+
}, handlers)
77+
}
4878
}
4979
`

src/templates/staticObjectsTemplate.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export const staticObjectsTemplate = (config: GenericUixConfig) => {
77
return /* ts */`
88
// Start of File
99
import uixConfig from '${path.relative(config.outdir, config.pathToConfig).split(path.sep).join('/').replace(/\.[^/.]+$/, '')}'
10-
import { NodeShape, NodeState, createNeo4jClient, GraphType } from '@thinairthings/uix'
10+
import { NodeShape, NodeState, GraphType } from '@thinairthings/uix'
1111
1212
export const uixGraph = new GraphType(uixConfig.type, uixConfig.nodeTypeSet)
1313
export const nodeTypeMap = uixGraph.nodeTypeMap

src/types/NodeType.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ type TriggerMap<NodeShape extends AnyNodeShape> = Map<'onCreate' | 'onUpdate' |
171171
Map<string, (node: NodeShape) => void>
172172
>;
173173

174-
export type AnyZodDiscriminatedUnion = ZodDiscriminatedUnion<any, [AnyZodObject, AnyZodObject, ...AnyZodObject[]]>;
174+
export type AnyZodDiscriminatedUnion = ZodDiscriminatedUnion<any, any>;
175175
/**
176176
* Represents a node type in a graph database.
177177
*/

tests/uix/generated/staticObjects.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
// Start of File
33
import uixConfig from '../uix.config'
4-
import { NodeShape, NodeState, createNeo4jClient, GraphType } from '@thinairthings/uix'
4+
import { NodeShape, NodeState, GraphType } from '@thinairthings/uix'
55

66
export const uixGraph = new GraphType(uixConfig.type, uixConfig.nodeTypeSet)
77
export const nodeTypeMap = uixGraph.nodeTypeMap

tests/uix/generated/useNodeSet.ts

+18-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { ConfiguredNodeTypeMap } from './staticObjects'
1111
import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
1212
import { NodeSetQueryOptions } from './queryOptions'
1313
import { createNode } from './functionModule'
14-
14+
import { v4 as uuid } from 'uuid'
1515

1616
export const useNodeSet = <
1717
ParentNodeType extends NodeSetParentTypes<ConfiguredNodeTypeMap>,
@@ -30,12 +30,27 @@ export const useNodeSet = <
3030
const queryClient = useQueryClient()
3131
const { data, error } = useQuery(queryOptions)
3232
const createNodeMutation = useMutation({
33-
mutationFn: async (initialState: NodeState<ConfiguredNodeTypeMap[ChildNodeType]>) => {
33+
mutationFn: async ({
34+
nodeId = uuid(),
35+
createdAt = new Date().getTime(),
36+
updatedAt = new Date().getTime(),
37+
...initialState
38+
}: NodeShape<ConfiguredNodeTypeMap[ChildNodeType]>) => {
3439
return await createNode({
3540
parentNodeKeys: [parentNodeKey],
3641
childNodeType,
37-
initialState
42+
initialState,
43+
providedNodeId: nodeId,
44+
})
45+
},
46+
onMutate: async (newNode) => {
47+
await queryClient.cancelQueries({queryKey: queryOptions.queryKey})
48+
const previousData = queryClient.getQueryData(queryOptions.queryKey)
49+
queryClient.setQueryData(queryOptions.queryKey, oldData => {
50+
if (!oldData) return [newNode]
51+
return [...oldData, newNode]
3852
})
53+
return { previousData }
3954
},
4055
onSuccess: () => queryClient.invalidateQueries({
4156
queryKey: [parentNodeKey.nodeType, parentNodeKey.nodeId, childNodeType]

0 commit comments

Comments
 (0)