Skip to main content

Database Model

This is the base class for your models.

Model attributes#

objects#

This is the ObjectManager connected to the model, it provides interface to query the database.

const { DatabaseModel } = require('djorm/models/DatabaseModel')
const { DateField } = require('djorm/fields/DateField')
const { CharField } = require('djorm/fields/CharField')
class Person extends DatabaseModel {
static firstName = new CharField()
static lastName = new CharField()
static birth = new DateField()
}
async function getAllJohns() {
// Accessing the object manager
return await Person.objects.filter({ firstName: 'John' }).all()
}

Model instance methods#

This is the public model API intended for use. Feel free to extend these methods

save()#

Store the model instance in the database updating the current database record given it was pulled from the database or creating new one otherwise.

Save calls create and update internally.

const person = await Person.objects.get({ id: 1 })
person.firstName = 'George'
await person.save()

create()#

Create new database record.

const person = new Person({
firstName: 'Matthew',
lastName: 'Barnes',
birth: '1964-12-24'
})
await person.create()

update()#

Update existing database record.

const person = await Person.objects.get({ id: 1 })
person.firstName = 'George'
await person.update()

delete()#

Delete existing database record.

const person = await Person.objects.get({ id: 1 })
await person.delete()

reload()#

Query the database for current entity state and store it in the instance.

const person = await Person.objects.get({ id: 1 })
await person.refresh()

Model static methods#

create(values)#

Creates an instance of model and immediately storese it in the database.

await Person.create({
firstName: 'Matthew',
lastName: 'Barnes',
birth: '1964-12-24'
})