首页 > JS 前端各框架有什么值得推荐的数据持久层框架?

JS 前端各框架有什么值得推荐的数据持久层框架?

现在主流的框架各有什么推荐的数据持久层推荐的?

我理想中的持久层应该是这样的:

// Adapter 是用来和后台(服务器,Local Storage,或者别的持久性服务)联系的
Const PublicAdapter = new Adapter({
    fetchAll: (modelCls)=>{}, // a function to fetch all data via ajax or local storage
    fetch: (modelCls)=>{},
    create: (modelCls, configuration)=>{},
    destroy: (modelInstance)=>{}
});

// 数据模型
Const User = new Model({
    id: DataType('number'),
    email: DataType('string',{'pattern':/\w+@\w+\.\w+)/}),
})

Const ModelName1 = New Model({
    id: DataType('number'),
    date: DataType('date',{'format':'YYYY-MM-DD', 'default':'now'}),
    active: DataType('boolean',{'default':true}),
    users: DataType('oneToMany',{ref:User}),
    parent: DataType('ManyToOne')
    _adapter: PublicAdapter.extend({
        destroy:()=>{} // 覆盖 PublicAdapter 的删除方法,比如先删除所有关联的 User 对象再删除自己
    })
});

// Model 至少应该提供两个方法
Model.save() // 调用 adpater 的对应方法提交数据到后台,自动决定使用 post 还是 put 方法
Model.destroy()


// 一个好用的持久层框架还应该有 store 来提供一些批量化处理的办法

// list 
this.store.fetchAll(ModelName1,{minId:1, maxId:1000})
    .then(data=>console.log('all models between 1 and 100 are fetched'));


// create
this.store
    .create(ModelName1, {id:5, date: Date.now(), active: 1})  // return created model
    .save()
    .then(model=>
        console.log('model saved, might call adapter saved to remote server or save to local storage when network is not available')
     )

// get 
this.store.fetch(ModelName1, {id:1}, {nocache:true}) // 无视 store 中的缓存
    .then(data=>{ console.log('data fetched,added to store') })

// delete
this.store.destroy(modelInstance)
    .then(result=>console.log('model destroyed at remote server and store '))

下面是一些我知道的市面上针对各框架的持久层,肯定还有遗漏,希望各位补充一下。也想问一下, 有没有独立的类似的数据持久层?

【热门文章】
【热门文章】