2020-02-03 10:24:25 +01:00
|
|
|
import { getAppApis, getTemplateApi, setupDatastore } from "@budibase/core"
|
|
|
|
import { action } from "./helpers"
|
2019-06-07 15:18:10 +02:00
|
|
|
|
|
|
|
const addField = templateApi => type => (record, name) => {
|
2020-02-03 10:24:25 +01:00
|
|
|
const field = templateApi.getNewField(type)
|
|
|
|
field.name = name
|
|
|
|
field.type = type
|
|
|
|
field.label = name
|
|
|
|
templateApi.addField(record, field)
|
2019-06-07 15:18:10 +02:00
|
|
|
}
|
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
export default async datastore => {
|
|
|
|
datastore = setupDatastore(datastore)
|
|
|
|
const templateApi = await getTemplateApi(datastore)
|
|
|
|
const addStringField = addField(templateApi)("string")
|
|
|
|
const addDateField = addField(templateApi)("datetime")
|
|
|
|
const addBoolField = addField(templateApi)("bool")
|
2019-06-07 15:18:10 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const root = templateApi.getNewRootLevel()
|
2019-06-07 15:18:10 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const clients = templateApi.getNewCollectionTemplate(root)
|
|
|
|
clients.name = "clients"
|
2019-06-07 15:18:10 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const client = templateApi.getNewRecordTemplate(clients)
|
|
|
|
client.name = "client"
|
|
|
|
addStringField(client, "FamilyName")
|
|
|
|
addStringField(client, "Address1")
|
|
|
|
addStringField(client, "Address2")
|
|
|
|
addStringField(client, "Address3")
|
|
|
|
addStringField(client, "Address4")
|
|
|
|
addStringField(client, "Postcode")
|
|
|
|
addDateField(client, "CreatedDate")
|
2019-06-07 15:18:10 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const children = templateApi.getNewCollectionTemplate(client)
|
|
|
|
children.name = "children"
|
2019-06-07 15:18:10 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const child = templateApi.getNewRecordTemplate(children)
|
|
|
|
child.name = "child"
|
|
|
|
addStringField(child, "FirstName")
|
|
|
|
addStringField(child, "Surname")
|
|
|
|
addDateField(child, "DateOfBirth")
|
|
|
|
addBoolField(child, "Current")
|
2019-06-07 15:18:10 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const contacts = templateApi.getNewCollectionTemplate(client)
|
|
|
|
contacts.name = "contacts"
|
2019-06-07 15:18:10 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const contact = templateApi.getNewRecordTemplate(contacts)
|
|
|
|
contact.name = "contact"
|
|
|
|
addStringField(contact, "Name")
|
|
|
|
addStringField(contact, "relationship")
|
|
|
|
addStringField(contact, "phone1")
|
|
|
|
addStringField(contact, "phone2")
|
|
|
|
addBoolField(contact, "active")
|
2019-06-07 15:18:10 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
await templateApi.saveApplicationHeirarchy(root)
|
2019-06-07 15:18:10 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
const apis = await getAppApis(datastore)
|
2019-06-07 15:18:10 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
await apis.collectionApi.initialiseAll()
|
2019-06-07 15:18:10 +02:00
|
|
|
|
2020-02-03 10:24:25 +01:00
|
|
|
return apis
|
|
|
|
}
|