# new Model(config)
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
config |
object
|
model configuration |
|
mode |
string
|
<optional> |
"memory" | "offline" | "online" |
id |
string
|
<optional> |
|
templateId |
string
|
<optional> |
id of the original template model (used to keep track of the source model) |
name |
string
|
<optional> |
Name of the model: Lead |
namePlural |
string
|
<optional> |
Plural name: Leads |
items |
Array.<object>
|
Array for field definitions |
|
acl |
object
|
model's acl (Access Control List) |
|
methods |
object
|
model's methods |
|
features |
object
|
<optional> |
model's features (workflow, comments, ...) |
icon |
string
|
<optional> |
The Font Awesome icon class. Example: "fas fa-check" |
color |
string
|
<optional> |
Hexa color. Ex: "#00aaee" |
tags |
Array.<string>
|
<optional> |
Ex: ["Leads", "Sales", "CRM", "HRM"] |
domains |
Array.<string>
|
<optional> |
Ex: ["banking", "insurance"] |
Example
// Register a new model
let leadModel = new kiss.data.Model({
name: "lead",
namePlural: "leads",
icon: "fas fa-user",
color: "#00aaee",
// Define model fiels
items: [
{
label: "Name",
id: "name",
type: "text"
},
{
primary: true, // Primary key field
label: "Email",
id: "email",
type: "text",
validationType: "email"
},
{
label: "Category",
id: "category",
type: "select",
options: [
{
label: "National"
value: "NAT",
color: "#00aaee"
},
{
label: "International",
value: "INT",
color: "#aa00ee"
}
]
}
],
// Define model methods
methods: {
// Get all the pending deals for this lead
getPendingDeals: async function() {
return await kiss.app.collections["deal"].find({
filter: {
$and: [
{leadId: this.id},
{status: "pending"}
]
}
})
}
}
})
// Your can create a new instance like this
let myLead = leadModel.create({name: "Bob Wilson", email: "bob@wilson.com", category: "INT"})
// Creating a new instance happens in memory. You have to save it manually with
await myLead.save()
// Updating an instance using default CRUD methods
await myLead.update({name: "Bob Wilson Junior"})
// Calling a custom method
let pendingDeals = await myLead.getPendingDeals()
// Deleting an instance
await myLead.delete()
Methods
# _checkFieldsCyclicDependencies() → {boolean|string}
Check if the model has computed fields with cyclic dependencies
false if no cyclic dependencies, or the field id that is causing the cyclic dependency
boolean
|
string
# _initACLFields()
Init the model's ACL fields
This is only used server-side to define the fields that holds ACL entries (users and/or groups). When a user or group is deleted, the ACL entries in the model's fields are updated accordingly. This is done by kiss.directory.cleanupAllUserReferences
this
# async addField(config, sectionIdopt) → {boolean}
Create a new field configuration. This method also updates the views that are connected to the model.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
config |
object
|
New field config |
|
sectionId |
string
|
<optional> |
Optional section id. If provided, adds the field at the end this section |
true in case of success
boolean
# async checkPermission(action) → {boolean}
Check the permission (client-side) to perform an action on the model.
Parameters:
Name | Type | Description |
---|---|---|
action |
string
|
"update" | "delete" |
true if the permission is granted
boolean
# async connectToModel(foreignModelId, fieldSetup) → {object}
Connect the model to a foreign model using a field.
To connect the 2 models, a symmetric field is created in the foreign model.
Parameters:
Name | Type | Description |
---|---|---|
foreignModelId |
string
|
id of the foreign model to connect |
fieldSetup |
object
|
Setup of the field in the local model |
The generated foreign field
object
# create(recordDataopt, inheritopt) → {object}
Create a new Record from this model
This does not save the record automatically: to save the record into the database, use the save() method of the created record.
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
recordData |
object
|
<optional> |
The new record's data |
inherit |
boolean
|
<optional> |
If true, create a blank record then assign recordData to it |
The new Record object
object
Example
userModel = kiss.app.models["user"]
let Bob = userModel.create({firstName: "Bob", lastName: "Wilson"})
await Bob.save()
# createFromLabels(record)
Create a record using field labels as keys
Parameters:
Name | Type | Description |
---|---|---|
record |
object
|
The record
Example
userModel = kiss.app.models["user"]
let Bob = userModel.createFromLabels({"First name": "Bob", "Last name": "Wilson"})
await Bob.save()
# async deleteElement(elementId)
Delete an element configuration
Parameters:
Name | Type | Description |
---|---|---|
elementId |
string
|
# async deleteField(fieldId) → {boolean}
Delete a field configuration and update all the views that are connected to this model. A primary field can't (and must not) be deleted.
Parameters:
Name | Type | Description |
---|---|---|
fieldId |
string
|
false if the field couldn't be deleted (primary field)
boolean
# async deleteLinksToModel(foreignModelId) → {integer}
Delete all the links that were auto-generated for a given model.
Parameters:
Name | Type | Description |
---|---|---|
foreignModelId |
string
|
The number of deleted links
integer
# exportAsJSON()
Export the model definition as JSON.
This is used to import/export application templates.
# async generateLinksToModel(config) → {Array.<object>}
Generate link records between 2 models when their 2 given fields are equal.
Note: it does not save the links into the database. It's up to the caller function to decide what to do with the links (create them or cancel)
Parameters:
Name | Type | Description |
---|---|---|
config |
object
|
|
foreignModelId |
object
|
|
sourceLinkFieldId |
object
|
|
sourceFieldId |
object
|
|
foreignLinkFieldId |
object
|
|
foreignFieldId |
object
|
link records
Array.<object>
# getActiveFields() → {Array.<object>}
Get visible fields (= non deleted)
Array of field definitions
Array.<object>
# getBatchableFields() → {Array.<object>}
Get the fields which can be used for batch operations.
The list of fields
Array.<object>
# getElement(elementId) → {object}
Get an element by id
Elements are the non-field items of the model, like:
- html
- image
- button
Parameters:
Name | Type | Description |
---|---|---|
elementId |
string
|
The element definition
object
Example
let myHtmlElement = myModel.getElement("xD12z4ml00z")
// Returns...
{
id: "yearlyIncome",
type: "html",
html: "<p>Yearly income is calculated by multiplying the monthly income by 12</p>",
}
# getElements() → {Array.<object>}
Get the model's elements
In KissJS, the model can be directly defined by a complex form with multiple sections and sub items. This method explores the tree and returns only the items which are "elements", like:
- html
- image
- button
Array of element definitions
Array.<object>
# getFeatureFields() → {Array.<object>}
Get the fields brought by the model's active plugins
Array of field definitions
Array.<object>
# getField(fieldId) → {object}
Get a field by id
Note: if the field is not found, the method tries to find the field by its label
Parameters:
Name | Type | Description |
---|---|---|
fieldId |
string
|
The field definition
object
Example
let myField = myModel.getField("xD12z4ml00z")
// Returns...
{
id: "yearlyIncome",
label: "Yearly income",
type: "number",
precision: 2,
formula: "{{Monthly income}} * 12",
}
# getFieldByLabel(fieldLabel) → {object}
Get the first field matching a label.
Note:
- if the field label is not found, it defaults to searching the field id
- deleted field are not taken into consideration
Parameters:
Name | Type | Description |
---|---|---|
fieldLabel |
string
|
The field definition
object
Example
let myField = myModel.getFieldByLabel("Project name")
# getFields() → {Array.<object>}
Get the model's fields
In KissJS, the model can be directly defined by a complex form with multiple sections and sub items. This method explores the tree and returns only the items which are fields.
Important: deleted fields are also returned, with a flag deleted = true
Array of field definitions
Array.<object>
# getFieldsAsOptions(types) → {Array.<object>}
Get fields as a list of options for a Select field
Parameters:
Name | Type | Description |
---|---|---|
types |
Array.<string>
|
Types of fields to return |
Array of options
Array.<object>
Example
let options = myModel.getFieldsAsOptions(["text", "number"])
# getFieldsByType(types) → {Array.<object>}
Get only the fields of specific types
Parameters:
Name | Type | Description |
---|---|---|
types |
string
|
Array.<string>
|
Types of fields to return. When a field is a lookup, its type is the lookup type. |
The fields of the required type, or []
Array.<object>
# getFilterableFields() → {Array.<object>}
Get the fields which can be used for filtering
The list of filterable fields
Array.<object>
# getFormulaFields() → {Array.<object>}
Get the fields which can be used inside a formula
The list of formula fields
Array.<object>
# getGroupableFields() → {Array.<object>}
Get the fields which can be used for grouping
The list of groupable fields
Array.<object>
# getLinkField(foreignModelId) → {object}
Search inside a model which field links to a foreign model
Parameters:
Name | Type | Description |
---|---|---|
foreignModelId |
string
|
Foreign model id |
The field that links to the foreign model
object
# getPrimaryKeyField() → {object}
Get the primary field of this model
The primary field, or the model's 1st field if it wasn't found
object
# getSection(fieldId) → {object}
Get a section by id
Note: if the section is not found, the method tries to find the section by its title
Parameters:
Name | Type | Description |
---|---|---|
fieldId |
string
|
The section definition
object
Example
let mySection = myModel.getSection("General informations")
// Returns...
{
id: "aE7x450",
title: "General informations",
items: [
// ... Section items
]
}
# getSectionByTitle(sectionTitle) → {object}
Get the first section matching a title.
Note: if the section title is not found, it defaults to searching the section id
Parameters:
Name | Type | Description |
---|---|---|
sectionTitle |
string
|
The section definition
object
Example
let mySection = myModel.getSectionByTitle("General informations")
# getSections() → {Array.<object>}
Get the model's sections
In KissJS, the model can be directly defined by a complex form with multiple sections and sub items. This method explores the tree and returns only the items which are sections.
Array of sections definitions
Array.<object>
# getSortableFields() → {Array.<object>}
Get the fields which can be used for sorting
The list of sortable fields
Array.<object>
# getSystemFields() → {Array.<object>}
Initialize the system fields
Array of system fields
Array.<object>
# getViews() → {Array.<Record>}
Get all the views that are connected to this model
Array of records containing the view configurations
Array.<Record>
# getViewsByUser(userId) → {Array.<object>}
Get the views a user is allowed to access.
A user can see a view if:
- he is the account owner
- he is one of the account managers
- he is the view creator
- the view read access is allowed to all authenticated users
- he is mentionned in the field "accessRead"
Parameters:
Name | Type | Description |
---|---|---|
userId |
string
|
The list of authorized views
Array.<object>
# isFirstItemInSection(itemId) → {boolean}
Check if an item is the first in its section. (used to perform checks in the form builder)
Parameters:
Name | Type | Description |
---|---|---|
itemId |
string
|
boolean
# async updateField(fieldId, config, shouldUpdateFormula) → {boolean}
Update a field configuration. This method also updates the views that are connected to the model.
Parameters:
Name | Type | Description |
---|---|---|
fieldId |
string
|
|
config |
object
|
New field config |
shouldUpdateFormula |
boolean
|
If true, re-compute the field value on every record of the collection |
true in case of success
boolean
# async updateFieldFormula()
Recompute the computed field value on every record of the collection.
# async updateSection(sectionId, newSectionConfig)
Update a section configuration
Parameters:
Name | Type | Description |
---|---|---|
sectionId |
string
|
|
newSectionConfig |
object
|