JavaScript ORM 库 Neurosync

码农软件 · 软件分类 · ORM/持久层框架 · 2019-09-23 11:42:10

软件介绍

Neurosync 是一个 JavaScript ORM 库,支持离线和实时操作。没错,是 ORM,但并没有疯狂到直接连接到后台数据库。对 Neurosync 来说,数据库就是后端的 REST API,而 Neurosync 就是这个 API 的一个门面。

Neurosync 的生命周期非常简单:

  • 保存数据变更到本地存储

  • 发起 REST 请求

  • 如果请求成功则删除本地存储中的变更数据,标识为已保存,并发布到实时 API

  • 如果请求失败,因为应用本身是离线的,将等待应用在线并继续处理数据更改流程

  • 如果有待决的操作但是应用重启了,数据将会被恢复

一个简单的 TODO 应用示例代码:

var Todo = Neuro({
  name: 'todo',
  api: '/api/1.0/todo/',
  fields: ['name', 'finished_at'],
  timestamps: true,
  comparator: ['-finished_at', '-created_at'], // finished go to bottom, most recently created are at the top
  methods: {
    finish: function(finished) {
      this.$save('finished_at', finished ? Date.now() null);
    }
  },
  dynamic: {
    done: function() {
      return !this.finished_at;
    }
  }
});

var t0 = Todo.create({name: 'Download Neurosync'});
t0.$isSaved(); // true
t0.finish( true );
t0.done; // true
t0.$remove();

var t1 = new Todo({name: 'Use Neurosync'});
t1.$isSaved(); // false
t1.id; // UUID
t1.$save();

var t2 = Todo.boot({id: 34, name: 'Profit'}); // Todo data that already exists remotely
t2.$isSaved(); // true
t2.name = '???';
t2.$hasChanges(); // true

var t3 = Todo.fetch(45); // REST call if doesn't exist locally

Todo.all(); // [t1, t2, t3]

Todo.collect(t1, t2); // creates a collection of todos

var allRemote = Todo.fetchAll(function(all) {}); // call REST API

var f0 = Todo.find('name', 'Download Neurosync'); // first match
var f1 = Todo.find({done: true});

var w0 = Todo.where('done', true); // all done todos
var w1 = Todo.where({finished_at: null});

var g0 = Todo.get(34); // get cached version

var g1 = Todo.grab(34); // get cached version, otherwise call REST API
var a0 = Todo.grabAll(function(all) {}); // get all cached, if none cached call REST API

Todo.ready(function() {}); // when it has been initialized locally and/or remotely (depends on options).

Todo.refresh(); // re-fetch from REST API

var search0 = Todo.search({done: true}); // sends a search to the REST API (POST by default)

var searchPaged0 = Todo.searchPaged({done: true, page_offset: 0, page_size: 20});
searchPaged0.$next();

本文地址:https://www.codercto.com/soft/d/15219.html

Learning PHP, MySQL, and JavaScript

Learning PHP, MySQL, and JavaScript

Robin Nixon / O'Reilly Media / 2009-7-21 / USD 39.99

Learn how to create responsive, data-driven websites with PHP, MySQL, and JavaScript - whether or not you know how to program. This simple, streamlined guide explains how the powerful combination of P......一起来看看 《Learning PHP, MySQL, and JavaScript》 这本书的介绍吧!

Markdown 在线编辑器
Markdown 在线编辑器

Markdown 在线编辑器

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具