# Model

The model is at the hearth of this package. It boasts a lot of features, so they have been broken down into the following sections:

# Creating Models

To create a model, you should first define your model class:

// User.js
import { Model } from '@upfrontjs/framework';

export default class User extends Model {}
1
2
3
4

Then you can call your model in various way, for example

// myScript.js
import User from '@Models/User';

User.find(1);
// or 
new User({ my: attributes });
// etc...
1
2
3
4
5
6
7

# Getters

# primaryKey

The primaryKey is a getter of the column name which is used to identify your model. The default value is 'id'.

// User.js
import { Model } from '@upfrontjs/framework';

export default class User extends Model {
    get primaryKey() {
        return 'id';
    }
}
1
2
3
4
5
6
7
8

# exists

The exists property is a getter on the model that returns a boolean which can be used to assert that the model has been persisted. It takes the primary key, timestamps and soft deletes into account.

# primaryKey

The primaryKey is property is a getter that defines what is the primary identifier's key from the model attributes. The default value is 'id'.

# Additional methods

# is

The is method compares the given model with the current model based on the getKey and getName

import User from '@Models/User';
import Shift from '@Models/Shift';

const user = new User({ id: 1 });
const user2 = new User({ id: 2 });
const shift = new Shift({ id: 1 });

user.is(user); // true
user.is(user2); // false
user.is(shift); // false
1
2
3
4
5
6
7
8
9
10

# isNot

The isNot method is the inverse of the is method.

# getKey

The getKey method returns the value of the primary key from the model.

# getKeyName

The getKeyName method returns the primaryKey of the model.

# getName

The getName method returns the current class' name. For example a class called User will return 'User'.

# replicate

The replicate method copies the instance into a non-existent instance. Meaning primary key and the timestamps won't be copied.

import User from '@Models/User';

const user = User.factory().create();
user.getKey(); // 1
user.name; // 'the name'
user.getAttribute(user.getCreatedAtColumn()); // Date instance

const userCopy = user.replicate();
userCopy.getKey(); // undefined
userCopy.name; // 'the name'
userCopy.getAttribute(userCopy.getCreatedAtColumn()); // undefined
1
2
3
4
5
6
7
8
9
10
11

# factory

static

The factory is a method that returns a Factory instance. Optionally it takes a number argument which is a shorthand for the times method.

import User from '@Models/User';

const user = User.factory().create(); // User
const users = User.factory(2).create(); // ModelCollection
1
2
3
4

# all

staticasync

The all method will initiate a request that returns a ModelCollection from the underlying get method.

import User from '@Models/User';

const users = await User.all();
users; // ModelCollection[User, ...]
1
2
3
4

# save

async

The save method will update or save your model based on whether the model exists or not. If the model exists it will send a PATCH request containing the changes, and the optionally passed in attributes. If the model does not exists it will send a POST request. The method returns the same current user updated with the response data if any.

# update

async

The update method sets the correct endpoint then initiates a patch request. If the model does not exists it will throw an error.

import User from '@Models/User';

const user = User.factory.make();
user.save({ optionalExtra: 'data' });
1
2
3
4

# find

async

The find method sends a GET request to the model endpoint supplemented with the given id. Available both static and non-statically.

import User from '@Models/User';

const user = User.find('8934d792-4e4d-42a1-bb4b-45b34b1140b4');
1
2
3

# findMany

async

The findMany method similar to the find method sends a GET request to the model endpoint but adds a whereKey constraint to the request, returning a ModelCollection. Available both static and non-statically.

import User from '@Models/User';

const users = User.findMany([1, 2]);
users; // ModelCollection[User, User]
1
2
3
4

# refresh

async

The refresh method updates all the attributes on the model by selecting the present attribute keys and setting the attributes from the response. This will reset any attribute changes.

import User from '@Models/User';

const user = await User.find(1);
user.name = 'new name';
user.getChanges(); // { name: 'new name' }
await user.refresh();
user.getChanges(); // {}
1
2
3
4
5
6
7