diff --git a/package.json b/package.json index dcf787f..df78385 100644 --- a/package.json +++ b/package.json @@ -63,7 +63,6 @@ "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" @@ -71,6 +70,7 @@ "peerDependencies": { "neo4j-driver": "^5.21.0", "openai": "^4.49.0", - "immer": "^10.1.1" + "immer": "^10.1.1", + "uuid": "^10.0.0" } } \ No newline at end of file diff --git a/src/extensions/PropertyVector.ts b/src/extensions/PropertyVector.ts new file mode 100644 index 0000000..e69de29 diff --git a/src/fns/getNodeByKeyFactory.ts b/src/fns/getNodeByKeyFactory.ts index 5145f74..b5f34f8 100644 --- a/src/fns/getNodeByKeyFactory.ts +++ b/src/fns/getNodeByKeyFactory.ts @@ -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}`, diff --git a/src/index.ts b/src/index.ts index 3c5331d..ed42a28 100644 --- a/src/index.ts +++ b/src/index.ts @@ -19,4 +19,7 @@ export * from './fns/getVectorNodeByKeyFactory' export * from './fns/getAllOfNodeTypeFactory' export * from './fns/getChildNodeSetFactory' export * from './fns/getUniqueChildNodeFactory' -export * from './fns/getNodeByIndexFactory' \ No newline at end of file +export * from './fns/getNodeByIndexFactory' + +// Clients +export { createNeo4jClient } from './clients/neo4j' \ No newline at end of file diff --git a/src/templates/hooks/useNodeSetTemplate.ts b/src/templates/hooks/useNodeSetTemplate.ts index dae1a54..05a890e 100644 --- a/src/templates/hooks/useNodeSetTemplate.ts +++ b/src/templates/hooks/useNodeSetTemplate.ts @@ -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, @@ -33,17 +33,47 @@ export const useNodeSet = < const queryClient = useQueryClient() const { data, error } = useQuery(queryOptions) const createNodeMutation = useMutation({ - mutationFn: async (initialState: NodeState) => { + mutationFn: async ({ + nodeId, + createdAt, + updatedAt, + nodeType, + ...initialState + }: NodeShape) => { return await createNode({ parentNodeKeys: [parentNodeKey], childNodeType, - initialState + initialState: initialState as NodeState, + 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, + Parameters[1]? + ]) => createNodeMutation.mutateAsync({ + nodeId: uuid(), + createdAt: new Date().getTime(), + updatedAt: new Date().getTime(), + nodeType: childNodeType, + ...initialState + }, handlers) + } } ` \ No newline at end of file diff --git a/src/templates/staticObjectsTemplate.ts b/src/templates/staticObjectsTemplate.ts index 08e1502..5b7684f 100644 --- a/src/templates/staticObjectsTemplate.ts +++ b/src/templates/staticObjectsTemplate.ts @@ -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 diff --git a/src/types/NodeType.ts b/src/types/NodeType.ts index ed339f9..d28c57b 100644 --- a/src/types/NodeType.ts +++ b/src/types/NodeType.ts @@ -171,7 +171,7 @@ type TriggerMap = Map<'onCreate' | 'onUpdate' | Map void> >; -export type AnyZodDiscriminatedUnion = ZodDiscriminatedUnion; +export type AnyZodDiscriminatedUnion = ZodDiscriminatedUnion; /** * Represents a node type in a graph database. */ diff --git a/tests/uix/generated/staticObjects.ts b/tests/uix/generated/staticObjects.ts index 47c4d28..d00eef2 100644 --- a/tests/uix/generated/staticObjects.ts +++ b/tests/uix/generated/staticObjects.ts @@ -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 diff --git a/tests/uix/generated/useNodeSet.ts b/tests/uix/generated/useNodeSet.ts index 8b88473..cdc7eca 100644 --- a/tests/uix/generated/useNodeSet.ts +++ b/tests/uix/generated/useNodeSet.ts @@ -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, @@ -30,12 +30,27 @@ export const useNodeSet = < const queryClient = useQueryClient() const { data, error } = useQuery(queryOptions) const createNodeMutation = useMutation({ - mutationFn: async (initialState: NodeState) => { + mutationFn: async ({ + nodeId = uuid(), + createdAt = new Date().getTime(), + updatedAt = new Date().getTime(), + ...initialState + }: NodeShape) => { 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]