init cypher queries

This commit is contained in:
2025-02-24 22:45:51 +01:00
parent 0161931cb4
commit a8c4aa9351
12 changed files with 288 additions and 193 deletions

View File

@@ -1,7 +1,8 @@
import { Node, Relationship, Date } from 'neo4j-driver';
import { Integer } from 'neo4j-driver';
interface PersonProperties {
export interface PersonProperties {
allow_admin_access: boolean;
id: string;
google_id: string;
first_name: string;
@@ -65,7 +66,7 @@ export type FamilyRelationship = Relationship<Integer, {
}>;
interface RecipeProperties {
export interface RecipeProperties {
id: string;
name: string;
origin: string;
@@ -79,11 +80,19 @@ interface RecipeProperties {
}
export type Recipe = Node<Integer, RecipeProperties>;
export type RecipeRelationship = Relationship<Integer, {
export type Likes = Relationship<Integer, {
favourite: boolean;
like_it: boolean;
could_make_it: boolean;
}>;
export interface FamilyTree {
ancestors: Person;
prel1: FamilyRelationship;
children: Person;
prel2: FamilyRelationship;
spouses: Person;
srel: FamilyRelationship;
user: Person;
}

View File

@@ -0,0 +1 @@
CREATE (p:Person $Person) RETURN p

View File

@@ -0,0 +1,7 @@
MATCH (n:Person { id: $person_id })-[p:Parent*1..]->(family:Person)
OPTIONAL MATCH (family)-[c:Child]->(children:Person)
WITH family, p, children, c, n
OPTIONAL MATCH (children)<-[p2:Parent]-(OtherParents:Person)
WITH family, p, children, c, OtherParents, p2, n
OPTIONAL MATCH (family)-[s:Spouse]-(spouse:Person)
RETURN family, p, children, c, OtherParents, p2, spouse, s, n;

View File

@@ -0,0 +1 @@
MATCH (n:Person) WHERE n.id = $id RETURN n AS people

View File

@@ -0,0 +1 @@
MATCH (n:Person) WHERE n.google_id = $google_id RETURN n as person

View File

@@ -0,0 +1 @@
MATCH (n:Person) WHERE n.id = $id RETURN n as person

View File

@@ -0,0 +1,17 @@
CREATE CONSTRAINT ON (n:Person) ASSERT EXISTS (n.id);
CREATE CONSTRAINT ON (n:Person) ASSERT EXISTS (n.last_name);
CREATE CONSTRAINT ON (n:Person) ASSERT EXISTS (n.first_name);
CREATE CONSTRAINT ON (n:Person) ASSERT EXISTS (n.born);
CREATE CONSTRAINT ON (n:Person) ASSERT EXISTS (n.mothers_first_name);
CREATE CONSTRAINT ON (n:Person) ASSERT EXISTS (n.mothers_last_name);
CREATE CONSTRAINT ON (n:Person) ASSERT n.id IS UNIQUE;
CREATE CONSTRAINT ON (n:Person) ASSERT n.google_id IS UNIQUE;
CREATE CONSTRAINT ON (n:Person) ASSERT n.last_name, n.first_name, n.born, n.mothers_first_name, n.mothers_last_name IS UNIQUE;
CREATE INDEX ON :Person(id);
CREATE INDEX ON :Person(google_id);
CREATE INDEX ON :Person(last_name);
CREATE INDEX ON :Person(first_name);
CREATE INDEX ON :Person(born);
CREATE INDEX ON :Person(mothers_first_name);
CREATE INDEX ON :Person(mothers_last_name);

View File

@@ -0,0 +1,4 @@
MATCH (n:Person {id: $id})
SET n:DeletedPerson
REMOVE n:Person
RETURN labels(n) AS labels, n AS person

View File

@@ -0,0 +1,3 @@
MATCH (n:Person $props)
SET n += $props
RETURN n AS person

View File

@@ -1,31 +1,22 @@
import type { Session } from 'neo4j-driver';
import type { Session, QueryResult, Transaction} from 'neo4j-driver';
import type{ Person,PersonProperties, FamilyTree } from '$lib/model';
import CreatePersonQuery from '$lib/server/queries/createPerson.cypher?raw';
import UpdatePersonQuery from '$lib/server/queries/updatePerson.cypher?raw';
export function createUser(db: Session, googleId: string, email: string, first_name: string, family_name: string, middle_name: string): User {
const row = db.run
if (row === null) {
throw new Error("Unexpected error");
}
const user: User = {
id: row.number(0),
googleId,
email,
};
return user;
export function createUser(db: Session, Person: PersonProperties): Promise<QueryResult<Person>> {
return db.executeWrite(tx => tx.run<Person>(
CreatePersonQuery, {Person})
);
}
export function getUserFromGoogleId(googleId: string): User | null {
const row = db.queryOne("SELECT id, google_id, email, name, picture FROM user WHERE google_id = ?", [googleId]);
if (row === null) {
return null;
}
const user: User = {
id: row.number(0),
googleId: row.string(1),
email: row.string(2),
name: row.string(3),
picture: row.string(4)
};
return user;
export function updateUser(db: Session, Person: PersonProperties): Promise<QueryResult<Person>> {
return db.executeWrite(tx => tx.run<Person>(
CreatePersonQuery, {Person})
);
}
export function getUserFromGoogleId(db: Session,googleId: string): Promise<QueryResult<Person>> {
return db.executeRead(tx => tx.run<Person>(""))
}
export interface User {