Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | 9x 82x 9x 65x 65x 65x 1x 64x 64x 9x 8x 8x 8x 8x 8x 9x 2x 2x 2x 9x 1x 1x 1x 1x 9x 4x 4x 4x 2x 2x 2x 9x 2x 2x 2x 1x 1x 1x 1x 1x 1x | import neo4js, { ModelInstance } from "./index"; import { RelationType } from "./Relation"; function getRelationString( label: string, relationType: RelationType, variable: string = "" ) { return `${ !relationType.out && !relationType.any ? "<" : "" }-[${variable}:${label}]-${ !relationType.out && !relationType.any ? "" : ">" }`; } export async function get( instance: ModelInstance<any>, label: string, relationType: RelationType ): Promise<any> { const relationString = getRelationString(label, relationType); const result = await neo4js.run( ` MATCH (a:${this.src.label} {guid:{_srcGuid}})${relationString}(b:${ this.dest.label }) RETURN b `, { _srcGuid: instance.props.guid } ); if (result.length === 0) { return Promise.resolve(null); } Iif (result.length > 1) { return Promise.reject("hasOne has more than one relations"); } return Promise.resolve(this.dest._createModelInstance(result[0].b)); } export async function create( instance: ModelInstance<any>, label: string, relationType: RelationType, props: any ): Promise<any> { const destInstance = await this.dest.create(props); const relationString = getRelationString(label, relationType); const result = await neo4js.run( ` MATCH (a:${this.src.label} {guid:{srcGuid}})${relationString}(b:${ this.dest.label }) DETACH DELETE b `, { srcGuid: instance.props.guid } ); await neo4js.run( ` MATCH (a:${this.src.label} {guid:{srcGuid}}), (b:${this.dest.label} {guid:{destGuid}}) MERGE (a)${relationString}(b) `, { srcGuid: instance.props.guid, destGuid: destInstance.props.guid } ); return Promise.resolve(destInstance); } export async function remove( instance: ModelInstance<any>, label: string, relationType: RelationType ): Promise<any> { const relationString = getRelationString(label, relationType, "c"); const result = await neo4js.run( ` MATCH (a:${this.src.label} {guid:{srcGuid}})${relationString}(b:${ this.dest.label }) DELETE c `, { srcGuid: instance.props.guid } ); return Promise.resolve(result._stats); } export async function add( instance: ModelInstance<any>, label: string, relationType: RelationType, destInstance: ModelInstance<any> ): Promise<boolean> { const relationString = getRelationString(label, relationType); const result = await neo4js.run( ` MATCH (a:${this.src.label} {guid:{srcGuid}}), (b:${this.dest.label} {guid:{destGuid}}) MERGE (a)${relationString}(b) `, { srcGuid: instance.props.guid, destGuid: destInstance.props.guid } ); Eif (result._stats.relationshipsCreated === 1) { return Promise.resolve(true); } return Promise.resolve(false); } export async function hasOne( instance: ModelInstance<any>, label: string, relationType: RelationType ): Promise<boolean> { const relationString = getRelationString(label, relationType); const result = await neo4js.run( ` MATCH (a:${this.src.label} {guid:{srcGuid}})${relationString}(b:${ this.dest.label }) RETURN b `, { srcGuid: instance.props.guid } ); if (result.length === 1) { return Promise.resolve(true); } else Iif (result.length > 1) { return Promise.reject( new Error("HasOne relation has more than one relations") ); } return Promise.resolve(false); } export async function update( instance: ModelInstance<any>, label: string, relationType: RelationType, newProps: any ): Promise<any> { const relationString = getRelationString(label, relationType); let props: any = await neo4js.run( ` MATCH (a:${this.src.label} {guid:{srcGuid}})${relationString}(b:${ this.dest.label }) RETURN b `, { srcGuid: instance.props.guid } ); if (props.length === 0) { return Promise.resolve(true); } else Iif (props.length > 1) { return Promise.reject( new Error("HasOne relation has more than one relations") ); } props = props[0].b; const destInstance = await this.dest.update({ guid: props.guid }, newProps); Eif (destInstance.length === 1) { return Promise.resolve(destInstance[0]); } return Promise.reject(new Error("Problem with the update")); } |