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: 'offline',
    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> importDataEntity(spec)

Alias to Foundation.mapToDataEntityAPI(entity = '', dataEntity = {})

An Data Entity abstraction is an instance of the DataEntity. Once it is mapped to foundation Data API, you can reach every Data Entity in the system from a single source point. This method dont works as expected if you call it after Foundation.start method
Parameters:
Name Type Description
spec object Data Entity abstraction specification
Properties
Name Type Description
entity string Data Entity name
dataEntity dataEntity An DataEntity instance for the entity defined on `spec.entity`
Source:
Example
const productSchema = new Foundation.Schema({
  name: {
    type: String,
    required: true,
    index: true
  },
  vendor: {
    type: String,
    required: true,
    index: true
  },
  price: {
    type: Number,
    required: true,
    index: true
  }
})

// start the foundation
const foundation = new Foundation({
  name: 'My Test app',
  schemas: {
    // Customer: schema
  }
})

// Build a customized Data Entity abstraction
const MyCustomizedDataEntity = class extends DataEntity {
  constructor (config) {
    super(config)
  }

  sell (primaryKey, orderId) {
    // primaryKey is Product primary key value
    // orderId is the primaryKey of an Order
    // const foundOrder = await Order.findById(orderId)
    // if (foundOrder.error) {
    //  CAN NOT TO SELL
    // }
    // const items = foundOrder.data.lineItems.filter(i => (i.productId === primaryKey))
    // If  Order has the product listed item
    // if(items[0])
    // {
    //    await this.delete(primaryKey) // deletes a Product from Products
    // }
  }
}

// instance of the custimized Data Entity
const productDataEntity = new MyCustomizedDataEntity({
  foundation,
  entity: 'Product',
  schema: productSchema
})

// import data entity
foundation.importDataEntity({
  entity: 'Product',
  dataEntity: productDataEntity
})

// start the foundation
await foundation.start()

// you can now do things like:

const { Product } = foundation.data

await Product.add({
  name: 'Big Mac',
  vendor: 'McDonalds',
  price: 3
})

<static> mapToDataEntityAPI(entity, dataEntity)

Maps an Data Entity abstraction to foundation Data API

An Data Entity abstraction is an instance of the DataEntity. Once it is mapped to foundation Data API, you can reach every Data Entity in the system from a single source point. This method dont works as expected if you call it after Foundation.start method. See Foundation.importDataEntity for usage further information.
Parameters:
Name Type Description
entity string Data Entity name
dataEntity dataEntity An DataEntity instance
Source:

<static> setGuidStorage(guid)

save Foundation uuid to localStorage.

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

<static> setupAppGuid()

check if Foundation has a uuid saved o.

check if Foundation has a uuid saved o
Source:
Returns:
Foundation uuid 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