Experiments in how neode could become more object oriented.
-
Create your model as a class:
// Create
@Node({ labels: ['Person', 'Actor'] })
export default class Person {
@Uuid()
@Primary()
@Unique()
id: string;
@String()
name: string;
getId() {
return this.id
}
getName() {
return this.name
}
}
-
Write some code:
// Connect from .env
const neode = neode.fromEnv()
// TOOD: Open a transaction
neode.openTransaction()
// Create an object
const adam = new Person("92702e6b-4d39-4ad2-a4ad-cc3923496090", 'Adam')
// Save it
neode.save(adam)
// Retrieve it
const found = neode.find(Person, "92702e6b-4d39-4ad2-a4ad-cc3923496090")
-
Create a repository based on the model:
// Use decorator to register the PersonRepository as using the Person node @RepositoryDecorator(Person) export default class PersonRepository extends Repository<Person> {}
-
Create an instance of the repository and use it
[source,typescript]w
const repo = new PersonRepository() const adam = repo.find("92702e6b-4d39-4ad2-a4ad-cc3923496090")
-
Call the main repo to open a transaction - this will return a transaction based instance of neode. The
save
,create
, (etc) methods will run everything in the context of a single transaction. -
Otherwise the base class will create a new transaction every time.
neode.inWriteTransaction()
.then(async inTx => {
const adam = new Person("92702e6b-4d39-4ad2-a4ad-cc3923496090", 'Adam')
await inTx.save(adam)
const arthur = new Person("92702e6b-4d39-4ad2-a4ad-cc3923496090", 'Arthur')
await inTx.save(arthur)
await inTx.commit()
})