Westore Cloud – Hidden Cloud, NoBackEnd, NoSql, HiddenDB

Good design is no sense of design

Develop small programs, but: no back end! No operations! There is no DBA! No domain name! No certificate! No money! No energy!

It doesn’t matter, just know javascript, Westore Cloud will take you off ~~

Github

Github.com/dntzhang/we…

Applets cloud development introduction

Developers can use cloud development to develop wechat mini programs and games, without building a server, you can use cloud capabilities.

Cloud development provides complete cloud support for developers, weakening the concepts of back-end and operation and maintenance. Without setting up servers, core business development can be carried out using API provided by the platform, enabling rapid launch and iteration. At the same time, this capability is mutually compatible with the cloud services already used by developers, rather than mutually exclusive.

Currently, three basic capabilities are provided:

  • Cloud function: the code running in the cloud, wechat private protocol natural authentication, developers only need to write their own business logic code
  • Database: A JSON database that can be operated on the front end of an applet or read and write in cloud functions
  • Storage: upload/download cloud files directly in front of the small program, and manage them visually in the cloud development console

The official documentation for more information on the Applets cloud can be found here

Westore Cloud profile

Westore Cloud’s database capability based on the small program Cloud makes developers unaware of the existence of the database (invisible Cloud). They only need to focus on the flow of local data, local data logic and local data, and synchronize local data and Cloud database through simple pull, push, Add and remove. The official documentation related to the database can be found here. The architecture diagram is as follows:

Typical Data First architecture design, small and medium-sized projects can do away with the Models and Adapter modules. This can be compared to the Model First architecture:

In the Model First architecture, if persistent storage is not required, the Database can be removed and only Models left. Models has nothing to do with rendering, focusing on the abstraction of the model and the logic between the Models. Whether rendering to the Web, Android, IOS, Flash or WPF is not a model’s concern.

Westore Cloud features

  • Applets directly connect to the database
  • Database data item function extension
  • Minimal API design (Pull Push add Remove)
  • You only need one programming language (javascript) to write the front end, the back end, and mongodb
  • Developers only need to focus on the data and the logic of the data (i.e. the store), the stealth database synchronization functionality
  • Delay-free design (update the local refresh view first, sync DB, and finally diff local update view)

Quick start

The new collection

Defining a mapping Store

Install the name of the set created above to correspond to the data of the established store:

export default {
  data: {
    // User specifies the collectionName of the DB
    'user': [].// Other collections can be added
    'product': []},env:'test-06eb2e'
}
Copy the code

The env above corresponds to the cloud console environment ID:

Add test data

Add data to the user collection using the add method:

this.store.add('user', {
  name: 'dntzhang'.city: 'shenzhen'.age: 22.gender: 1
}).then((res) = >{})Copy the code

Add data to collection Product using the add method:

this.store.add('product', {
  address: {
    province:'Guangdong'.city:'Shenzhen',},agent: [ 'wechat Pay'.'Search it on wechat'.'Wechat Reading']})Copy the code

Expand the database for each method

export default {
  data: {
    'user': [].'product': []},methods: {// Here you can extend the method for each item of collection
    'product': {'agentString':function(){
        //this.agent corresponds to the agent field of the product set
        return this.agent.join(The '-')}}},env:'test-06eb2e'
}
Copy the code

Using the extension method above, we can directly bind the agentString property to the view while iterating through each item of the Product table, such as an agentString that displays the first local data:

<view>{{product[0].agentString}}</view>
Copy the code

Pull the data

this.store.pull('user').then(res= > {
  this.store.data.user = res.data
  this.update()
})
Copy the code

Bind data to the view

<view class="container"> <view class="title" > User information </view> < view > age: {{user [0]. Age}} < / view > < view > city: {{user [0]. The city}} < / view > < view > gender: {{user [0]. Gender = = = 1? 'male' : 'female'}} < / view > < the view {{product[0].address.type}}</view> City: < view > {{product [0]. Address. The city}} < / view > < view > agents: {{product [0]. AgentString}} < / view > < the view class = "split" > < / view > <user-list></user-list> <view> <button ontap="addUser">Copy the code

Modify the data

this.store.data.user[0].name = 'dntzhang' + Math.floor(Math.random() * 100)
this.store.push().then((res) = > {
  console.log('Cloud database updated successfully')})Copy the code

The push method equals update local + Update Cloud. So not only does the local view refresh, but the cloud database synchronizes updates, and the update callback is executed in the THEN.

Support precise update of deep nested attributes, such as:

this.store.data.product[0].address.city = 'Guangzhou'
this.store.data.product[0].agent[0] = 'WeChat'
this.store.data.product[0].agent[1] = 'QQ'
this.store.data.product[0].agent[2] = 'Tencent Cloud'
this.store.push()
Copy the code

After the update:

Delete the data

const item = this.store.data.user.splice(index, 1) [0]
this.update() // Update local data and views
this.store.remove('user', item._id)  // Synchronize to the cloud database
Copy the code

The new data

const user = {
  name: 'new user' + this.store.data.user.length,
  age: 1.city: 'jiangxi'.gender: 2
}
this.store.data.user.push(user)
// Update the local view first
this.update() 
// Add to the cloud database
this.store.add('user', user)
Copy the code

If the newly added data needs to be modified later and synchronized to the cloud database, the _id needs to be set, that is, the last line of code is changed to:

this.store.add('user', user).then((res) = > {
  // Set the _id for push
  user._id = res._id
  this.update()
})
Copy the code

The full DEMO can be added by clicking here.

API

this.store.pull(collectionName, [where])

Pull JSON data from a collection of cloud databases

parameter

The name of the Whether or not an optional type describe
collectionName Must be string Collection name
where Don’t have to JSON Object Age = 18 {age: 18}

More apis for building query criteria for WHERE can be found here.

The return value

Returns an instance of the Promise object.

The instance

Select * from user where age 18:

this.store.pull('user', {age: 18}).then(res= > {
  this.store.data.user = res.data
  this.update()
})
Copy the code

this.store.push()

Synchronize local JSON to the cloud database

The return value

Returns an instance of the Promise object.

The sample

this.store.data.user[0].name = 'dntzhang'
this.store.data.product[0].address.city = 'Guangzhou'
this.store.data.product[0].agent[1] = 'QQ'
this.store.data.product[0].agent[2] = 'Tencent Cloud'
this.store.push().then((res) = > {
  console.log('Sync data complete! ')})Copy the code

this.store.add(collectionName, data)

Add JSON data to the database

parameter

The name of the Whether or not an optional type describe
collectionName Must be string Collection name
data Must be JSON Object Data items added to the database

The return value

Returns an instance of the Promise object.

The sample

const user = {
  name: 'new user' + this.store.data.user.length,
  age: 1.city: 'jiangxi'.gender: 2
}
this.store.data.user.push(user)
this.update()
this.store.add('user', user).then((res) = > {
  / / set the _id
  user._id = res._id
  this.update()
})
Copy the code

this.store.remove(collectionName, id)

Delete data from the database by ID

parameter

The name of the Whether or not an optional type describe
collectionName Must be string Collection name
id Must be string The id field is automatically generated in the database

The return value

Returns an instance of the Promise object.

The sample

const item = this.store.data.user.splice(index, 1) [0]
this.update()
this.store.remove('user', item._id)
Copy the code

The principle of

JSON Diff Result turns into a database update request

diffToPushObj({ 'user[2].name': { cc: 1 }, 'user[2].age': 13.'user[1].a.b': { xxx: 1}})Copy the code

Returns:

{ 'user-2': { 'name': { 'cc': 1 }, 'age': 13 }, 'user-1': { 'a': { 'b': { 'xxx': 1}}}}Copy the code

Where, ‘user-2’.split(‘-‘) can get the DB set name user, the number 2 represents the third local data.

Star & Fork

Github.com/dntzhang/we…

License

MIT @dntzhang