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
180
181
182
183
184
185
186
187 | 9x
9x
9x
9x
9x
441x
441x
13x
81x
5x
8x
620x
620x
968x
620x
37x
37x
443x
37x
443x
443x
443x
443x
443x
3x
3x
3x
1x
2x
2x
2x
2x
11x
11x
11x
83x
4x
4x
4x
4x
3x
1x
7x
7x
7x
7x
9x
| import { forIn, mapValues } from "lodash";
import * as uuid from "uuid";
import neo4js, {
ModelInstance,
Relation,
BaseProps,
RelationType,
} from "./index";
import { CharGenerator, prepareWhere, prepareSet } from "./utils";
export class Model<P, M extends ModelInstance<P>> {
public label: String;
relations: Relation[];
modelInstanceClass: new (props: P) => M;
protected beforeCreate(props: P): P {
return props;
}
protected afterCreate(instance: M): M {
return instance;
}
protected beforeFind(props?: P & BaseProps): (P & BaseProps) | null {
return props;
}
protected afterFind(instance: M): M {
return instance;
}
protected beforeUpdate(
props: P & BaseProps,
newProps: P
): { props: P & BaseProps; newProps: P } {
return { props, newProps };
}
protected afterUpdate(instance: M): M {
return instance;
}
protected _createModelInstance(props: P & BaseProps): M {
let instance: any = this.modelInstanceClass
? new this.modelInstanceClass(props)
: new ModelInstance(props);
this.relations.forEach(
r => (instance = r.addFunctionsToInstance(instance))
);
return instance;
}
constructor(label: string) {
this.label = label;
this.relations = [];
}
public async create(props: P): Promise<M> {
let defaultProps = mapValues(
this.modelInstanceClass
? this.modelInstanceClass.prototype._defaultProps
: {},
prop => (typeof prop === "function" ? prop() : prop)
);
let p: P & BaseProps = this.beforeCreate({
guid: uuid.v4(),
...defaultProps,
...(props as any),
} as P);
p = { ...(p as any) } as P & BaseProps;
const result = await neo4js.run(
`
CREATE (n:${this.label} {p})
RETURN n
`,
{ p }
);
Iif (!result || result.length !== 1) {
throw new Error(
`Create didn't work, cmd: "CREATE (n:${
this.label
} {p}) RETURN n" with params: ${JSON.stringify({ p })}`
);
}
return this.afterCreate(this._createModelInstance(result[0].n));
}
public async findByGuid(guid: string): Promise<M | null> {
const result = await neo4js.run(
`
MATCH (n:${this.label} {guid:{guid}})
RETURN n
`,
{ guid }
);
Iif (!result || result.length > 1) {
throw new Error(
`Match didn't work, cmd: "MATCH (n:${
this.label
} {guid:{guid}}) RETURN n" with params: ${JSON.stringify({
guid,
})}`
);
}
if (result.length === 0) {
return null;
}
return this._createModelInstance(result[0].n);
}
public async delete(
props: P & BaseProps,
detach: boolean = false
): Promise<number> {
const { where, flatProps } = prepareWhere(props, "n");
const result = await neo4js.run(
`
MATCH (n:${this.label})
${where}
${detach ? " DETACH " : ""} DELETE n
`,
flatProps
);
return result._stats.nodesDeleted;
}
public async find(props?: P & BaseProps): Promise<M[]> {
const p = this.beforeFind(props);
const { where, flatProps } = prepareWhere(p, "n");
let result = await neo4js.run(
`
MATCH (n:${this.label})
${where}
RETURN n
`,
flatProps
);
return result.map(p => this.afterFind(this._createModelInstance(p.n)));
}
public async findOne(props?: P & BaseProps): Promise<M | null> {
const p = this.beforeFind(props);
const { where, flatProps } = prepareWhere(p, "n");
let result = await neo4js.run(
`
MATCH (n:${this.label})
${where}
RETURN n
`,
flatProps
);
if (result.length) {
return this.afterFind(this._createModelInstance(result[0].n));
}
return Promise.resolve(null);
}
public async update(props: P & BaseProps, newProps: P): Promise<M[]> {
const params = this.beforeUpdate(props, newProps);
const { where, flatProps } = prepareWhere(params.props, "n");
const { str: setPropsStr, newProps: _newProps } = prepareSet(
params.newProps,
"n"
);
let result = await neo4js.run(
`
MATCH (n:${this.label})
${where}
SET ${setPropsStr}
RETURN n
`,
{ ...flatProps, ..._newProps }
);
return result.map(p => this.afterUpdate(this._createModelInstance(p.n)));
}
}
|