Class: Foundation

Foundation


new Foundation(config)

Foundation boostrap class.

Foundation boostrap class
Parameters:
Name Type Description
config object Foundation configuration
Properties
Name Type Description
name string Foundation name
dataStrategy string Data strategy. Recognized values: offlineFirst, onlineFirst, offline, online
useWorker boolean Use a ServiceWorker in Background
schemas object map of data schemas
Author:
Source:
Example
// =========> main.js
// import React
import React from 'react'
import ReactDOM from 'react-dom'

// import Bootstrap
import 'bootstrap/dist/css/bootstrap.css'

// import React app
import App from './App'

// import agnostic foundation foundation class
import Foundation from './foundation/Foundation'

const CustomerSchema = new Foundation.Schema({
    name: {
        type: String,
        required: true,
        index: true
    },
    address: {
        type: String,
        required: true,
        index: true
    },
    email: {
        type: String,
        required: true,
        index: true
    },
    cards: {
        type: [],
        required: true
    }
})

const OrderSchema = new Foundation.Schema({
    name: {
        type: String,
        required: true,
        index: true
    },
    shipTo: {
        type: String,
        required: true,
        index: true
    },
    paymentMethod: {
        type: String,
        required: true,
        index: true
    },
    amount: {
        type: Number,
        required: true,
        default: 0,
        index: true
    },
    date: {
        type: Date,
        default: Date.now,
        index: true
    }
})

const ProductSchema = new Foundation.Schema({
    name: {
        type: String,
        required: true,
        index: true
    },
    vendor: {
        type: String,
        required: true,
        index: true
    },
    price_cost: {
        type: Number,
        required: true,
        default: 0,
        index: true
    }
})

const UserSchema = new Foundation.Schema({
    name: {
        type: String,
        required: true
    },
    username: {
        type: String,
        required: true
    }
})

const foundation = new Foundation({
    name: 'My App',
    useWorker: true,
    dataStrategy: 'offlineFirst',
    schemas: {
        User: UserSchema,
        Product: ProductSchema,
        Order: OrderSchema,
        Customer: CustomerSchema
    }
})

foundation.on('foundation:start', async function(eventObj) {
    const {
        foundation,
        error
    } = eventObj
    if (error) {
        throw new Error(`Error starting foundation stack: ${error}`)
    }
    const {
        User,
        Product
    } = foundation.data
    const Eduardo = await User.add({
        name: 'Eduardo Almeida',
        username: 'web2'
    })
    console.debug('Eduardo', Eduardo)

    const Volvo = await Product.add({
        name: 'Volvo XC90',
        vendor: 'Volvo',
        price_cost: 150000
    })
    console.debug('Volvo', Volvo)
})

// start foundation and get it ready to be used
await foundation.start()

const start = await foundation.start()
if (start.error) {
    throw new Error(`Error starting foundation stack: ${start.error}`)
}
// console.debug('start', start)
ReactDOM.render(
  <App foundation={foundation} />,
  document.getElementById('root')
)

Extends

  • EventSystem

Members


<static> applicationWorker :getter

Get the Foundation worker.

Get the Foundation worker
Type:
  • getter
Source:
Example
Foundation.applicationWorker.postMessage()

<static> data :getter

Get the Foundation data API(DataEntity).

Get the Foundation data API(DataEntity)
Type:
  • getter
Source:
Example
const { User, Product } = foundation.data
      const Eduardo = await User.add({
        name: 'Eduardo Almeida',
        username: 'web2'
      })
      console.debug(Eduardo)
      // {  
      //    data: {__id: 1, _id: "600e0ae8d9d7f50000e1444b", name: "Eduardo Almeida", username: "web2", id: "600e0ae8d9d7f50000e1444b"}
      //    error: null
      // }

<static> dataStrategy :getter

Get the data strategy being used.

Get the data strategy being used.
Possible values are: offlineFirst, onlineFirst, offline, online.
Default: offlineFirst
Type:
  • getter
Source:
Example
console.log(Foundation.dataStrategy)

<static> guid :getter

Get the Foundation Session guid currently being used.

Get the Foundation Session guid currently being used.
Type:
  • getter
Source:
Example
console.log(Foundation.guid)

<static> name :getter

Get the Foundation name.

Get the Foundation name
Type:
  • getter
Source:
Example
console.log(Foundation.name)

<static> name :setter

Set the Foundation name.

Set the Foundation name
Type:
  • setter
Source:
Example
Foundation.name = 'Provide the name here'

<static> Schema :getter

Creates new data schema.

Creates new data schema
Type:
  • getter
Source:
Example
new Foundation.Schema({})

<static> started :getter

Get the start state.

Get the start state
Type:
  • getter
Source:
Example
console.log(Foundation.started)

<static> tabId :getter

Get the Browser tab ID.

Get the Browser tab ID
Type:
  • getter
Source:
Example
console.log(foundation.tabId)

<static> useWorker :getter

flag if is there ServiceWorker being used.

flag if is there ServiceWorker being used
Type:
  • getter
Source:

Methods


<async, static> registerApplicationWorker()

Setup and Register the main Service worker used by foundation core.

Setup and Register the main Service worker used by foundation core
Source:
Returns:
  • signature - Default methods signature format { error, data }
    Type
    object
  • signature.error - Execution error
    Type
    string | object
  • signature.data - Worker Registration Object
    Type
    object

<async, static> registerWorker(name, workerFile)

Setup and Register a Service worker and get it ready for usage into your application scope.

Setup and Register a Service worker and get it ready for usage into your application scope
Parameters:
Name Type Description
name string Worker name. Used to access it from the namespace
workerFile string Worker file name
Source:
Returns:
  • signature - Default methods signature format { error, data }
    Type
    object
  • signature.error - Execution error
    Type
    string | object
  • signature.data - Worker Registration Object
    Type
    object

<static> setGuidStorage(guid)

save Foundation GUID to localStorage.

save Foundation GUID to localStorage
Parameters:
Name Type Description
guid string
Source:
Returns:
Foundation GUID saved on localStorage

<static> setupAppGuid()

check if Foundation has a GUID saved o.

check if Foundation has a GUID saved o
Source:
Returns:
Foundation GUID saved on localStorage

<async, static> start()

Starts foundation stack and get it ready to use.

Starts foundation stack and get it ready to use.
it calls this.#startVitals() internally
Source:
Returns:
  • signature - Default methods signature format { error, data }
    Type
    object
  • signature.error - Execution error
    Type
    string | object
  • signature.data - Foundation data
    Type
    object