// DataStore is mostly recommended for use in the browserimport{DataStore,Schema,utils,Mapper}from'js-data'importHttpAdapterfrom'js-data-http';import_from'lodash';constschemas=require('./schemas')(Schema);import*asrelationsfrom'./relations';constconvertToDate=function(record){if(typeofrecord.created_at==='string'){record.created_at=newDate(record.created_at)}if(typeofrecord.updated_at==='string'){record.updated_at=newDate(record.updated_at)}};exportconstadapter=newHttpAdapter({// Our API sits behind the /api pathbasePath:'/api',});exportconststore=newDataStore({mapperDefaults:{// Override the original to make sure the date properties are actually Date// objectscreateRecord(props,opts){constresult=this.constructor.prototype.createRecord.call(this,props,opts);if(Array.isArray(result)){result.forEach(convertToDate)}elseif(this.is(result)){convertToDate(result)}returnresult}}});store.registerAdapter('http',adapter,{default:true});// The User Resourcestore.defineMapper('team',{endpoint:'teams'});store.defineMapper('customer',{endpoint:'customers',relations:relations.customer});store.defineMapper('city',{endpoint:'cities'});store.defineMapper('product',{endpoint:'products'});store.defineMapper('productCategory',{endpoint:'products-categories'});store.defineMapper('module',{endpoint:'modules'});store.defineMapper('assign',{endpoint:'assigns'});store.defineMapper('addresses',{endpoint:'customers-addresses'});store.defineMapper('serviceType',{endpoint:'services-types'});
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.