stagync
Persiste dados modelados e sincroniza, com o foco principal em frontend, web e apps baseados em javascript
Razão
O Stagync foi criado para suprir a necessidade de controle de dados no cliente. Seu proposito é salvar e recuperar dados em SPA (single page application) e react-native, de forma global e automatica. Sendo assim, é possivél sincronizar dados em toda a aplicação baseado em uma ou diversas origem.
Curiosidade
O nome Stagync vem na junção das palavras Storage e Sync
Exemplo de uso
./src/app.js
const handler = async { User User const fullName fistName = await User console}
stagync-storage
Stagync does not work alone. To use it you need a * stagync-storage *. Stagync-storage are plugins that connect your data model to a storage location.
Example
stagync-storage-memory
You persist the data in memory. That is, a global variable is created by persisting in it the saved data.
stagync-storage-native-async-storage
You persist data using AsyncStorage
stagync-storage-localforage
You persist data using LocalForage
Instalação
npm install --save stagync stagync-storage-memory
Storage
Storage defines persistence type, table name, database name, schema, and additional methods. Through a data storage you can manage specific data in your application.
Example
./src/Storages/user.js
Storage database: 'myDataBase' table: 'user' storage: Memory schema: age: type: 'number' active: type: 'boolean' default: true tags: type: 'array' firstName: type: 'string' lastName: type: 'string' fullName: // Virtual prop async const firstName lastName = await this return firstName + ' ' + lastName listener: 'fisrtName' 'lastName' nickname: // Virtual prop return 'assis' methods: { const nickname firstName lastName fullName = await this return nickname firstName lastName fullName }
database
Name of your database
database: 'myDataBase'
table
Data table name
table: 'user'
storage
Type of storage. Here you will need a stagync-storage to connect your data model to a storage location.
schema
Data schema. You need to define a data schema for your model, data not defined in the schema will return with error when you try to add them.
Each data property can receive the following attributes:
- type: Definição de typo de dado, possibilidades:
-
- string
-
- number
-
- boolean
-
- array
-
- object
-
- function
-
- xml
- default: Default value of the property.
- get: Define that the declared property is a virtual prop
- listener Quando a propriedade é uma virtual prop, a propriedade escuta os valores de listener e emitir sincronismo quando esses valores são modificados, veja lisneter
- listener When the property is a * virtual * property, the property listens for the listener values and emits synchronization when those values are modified, see lisneter
## methods Additional methods that can be called with your model
methods: { const username password = await this try const token = await rest this catch err iferrstatusCode === 401 }
### Example
schema: age: type: 'number' default: 28 name: type: 'string' nameAge // Virtual Prop { const name age = this return ` com ` } listener: 'name' 'age'
Virtual props
Define properties that execute a function. This function can return treated properties or any other value, and can be asynchronous or synchronous.
//... models/user.jsfullName: // Virtual prop async const firstName lastName = await this return firstName + ' ' + lastName listener: 'fisrtName' 'lastName' indexindexjsxt fullName = Userconsole // Philippe Assis
listener
Array with the name of the dependent property. When listener is set to a virtual property this property emits an event for synchronization when the properties defined in listener are updated.
//...User User//..
Metodos
Setter and Getter
set(props)
Adds data by an object to storage
User
get(prop)
Recover data from storage.
const nickname tags = await User
You can specify the property to retrieve.
const data = await User
merge
Merge new data with data already saved.
User /*Result:{ "nickiname": "Assis", "tags": ['Brazil', 'Dev'], "active": false}*/
Synchronizers
sync(props)
Synchronizes properties defined on an object by executing a callback every time the property is updated.
User
syncAll(callback)
Synchronizes all properties by performing a callback with each update and passing only the updated property.
User
syncMany(props, callback)
Synchronizes all the properties passed in an array by executing a callback every time the property is updated.
User
clear()
Clears the entire table
Userclear
restoreDefaultValues()
Restores the data to the default values registered in the schema
User
Stagync with Webpack
When using Stagync with Webpack, you should not set the type of storage with * type *. Instead, you must directly import the storage library and set in the ** storage ** property.
Otherwise the webpack will not be able to perform the * require * in the plugin. Making it common to display the error: Can not find module '."
Storage database: 'myDataBase' table: 'user' storage: Memory // Include lib