1
0
Fork 0
mirror of synced 2024-07-13 10:15:49 +12:00
budibase/packages/datastores/tests/setup.js

62 lines
1.9 KiB
JavaScript
Raw Normal View History

import { getAppApis, getTemplateApi, setupDatastore } from "@budibase/core"
2019-06-08 01:18:10 +12:00
const addField = templateApi => type => (record, name) => {
const field = templateApi.getNewField(type)
field.name = name
field.type = type
field.label = name
templateApi.addField(record, field)
2019-06-08 01:18:10 +12: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-08 01:18:10 +12:00
const root = templateApi.getNewRootLevel()
2019-06-08 01:18:10 +12:00
const clients = templateApi.getNewCollectionTemplate(root)
clients.name = "clients"
2019-06-08 01:18:10 +12: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-08 01:18:10 +12:00
const children = templateApi.getNewCollectionTemplate(client)
children.name = "children"
2019-06-08 01:18:10 +12:00
const child = templateApi.getNewRecordTemplate(children)
child.name = "child"
addStringField(child, "FirstName")
addStringField(child, "Surname")
addDateField(child, "DateOfBirth")
addBoolField(child, "Current")
2019-06-08 01:18:10 +12:00
const contacts = templateApi.getNewCollectionTemplate(client)
contacts.name = "contacts"
2019-06-08 01:18:10 +12: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-08 01:18:10 +12:00
await templateApi.saveApplicationHeirarchy(root)
2019-06-08 01:18:10 +12:00
const apis = await getAppApis(datastore)
2019-06-08 01:18:10 +12:00
await apis.collectionApi.initialiseAll()
2019-06-08 01:18:10 +12:00
return apis
}