Skip to content

Commit

Permalink
README
Browse files Browse the repository at this point in the history
  • Loading branch information
tykoth committed Sep 2, 2019
1 parent e2e40b1 commit aef7866
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 75 deletions.
45 changes: 45 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# ra-data-dexie



## Instalation
```sh
npm install --save ra-data-dexie
```
or
```sh
yarn add ra-data-dexie
```

## Usage


```js

import React, { Component } from "react";
import ReactDOM from 'react-dom';
import { Admin, Resource, ListGuesser, ShowGuesser, EditGuesser } from "react-admin";
import dexieDataProviter from 'ra-data-dexie';

const dataProvider = dexieDataProvider(
'DATABASE_NAME',
1, // database version
{
"tableName":"++id,name,text,date,bool"
}
);

class App extends Component {
render() {
return (
<Admin dataProvider={dataProvider}>
<Resource name="tableName" list={ListGuesser} />
</Admin>
);
}
}

ReactDOM.render(<App />, document.getElementById('app'));


```
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ra-data-dexie",
"version": "1.0.9",
"version": "1.1.1",
"description": "Experimental React-Admin using Dexie (IndexedDB)",

"main": "src/index.js",
Expand Down
131 changes: 57 additions & 74 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,125 +1,108 @@
import {
GET_LIST,
GET_ONE,
GET_MANY,
GET_MANY_REFERENCE,
CREATE,
UPDATE,
DELETE,
// fetchUtils,
GET_LIST,
GET_ONE,
GET_MANY,
GET_MANY_REFERENCE,
CREATE,
UPDATE,
DELETE,
// fetchUtils,
} from 'react-admin';
import Dexie from 'dexie';




const db = new Dexie('APP');



// db.delete();
db.version(1).stores({
// from
people:"++id,first_name,last_name,email,address,zipcode,city,avatar,birthday,first_seen,last_seen,has_ordered,latest_purchase,has_newsletter,groups,nb_commands,total_spent",
customers:"++id,first_name,last_name,email,address,zipcode,city,avatar,birthday,first_seen,last_seen,has_ordered,latest_purchase,has_newsletter,groups,nb_commands,total_spent",
categories:"++id,name,parent_id",
products:"++id,category_id,reference,width,height,price,thumbnail,image,description,stock",
commands:"++id,reference,date,customer_id,basket,total_ex_taxes,delivery_fees,tax_rate,taxes,total,status,returned",
invoices:"++id,date,command_id,customer_id,total_ex_taxes,delivery_fees,tax_rate,taxes,total",
reviews:"++id,date,status,command_id,product_id,customer_id,rating,comment",

tags:"++id,name,parent_id,published",
servers: '++id,name,description,ip,hostname,status,operating_system,ssh_port,created,updated',
todos: '++id,title',
history:'++id,url,src,alt,href,time',
galleries:'++id,title,host,url,slug,src,hash',

users:"++id,name,username,email,avatar,created,updated",
posts:"++id,title,body,created,updated"
});

/**
* @param {string} type Request type, e.g GET_LIST
* @param {string} resource Resource name, e.g. "posts"
* @param {Object} payload Request parameters. Depends on the request type
* @returns {Promise} the Promise for response
*/
export default (type, resource, params) => new Promise((resolve, reject) => {
console.log([type, resource, params]);

export default (databaseName, databaseVersion, databaseStores) => {

const db = new Dexie(databaseName);
// db.delete();
db.version(databaseVersion).stores(databaseStores);

return (type, resource, params) => new Promise((resolve, reject) => {

db.open();

switch (type) {


case DELETE:

break;
break;


case GET_ONE:
// alert("OK");
// console.log(db.table(resource).schema.instanceTemplate);
// alert("OK");
db.table(resource).get(parseInt(params.id)).then((data) => {
// console.log(data);
// alert("OI");
resolve({data});
});
break;
// alert("OK");
// console.log(db.table(resource).schema.instanceTemplate);
// alert("OK");
db.table(resource).get(parseInt(params.id)).then((data) => {
// console.log(data);
// alert("OI");
resolve({ data });
});


break;

case CREATE:

db.table(resource)
.add(params.data)
.then((id) => {
resolve(params)
});
db.table(resource)
.add(params.data)
.then((id) => {
resolve(params)
});

// db.table(this.tableName)
break;
// db.table(this.tableName)
break;


case UPDATE:
// alert("UPDATE");
// console.log(params);
// return false;
db.table(resource).update(params.data.id, params.data).then((updated) => {
// console.log(updated);
// alert('update made');
resolve(params);
});
// alert("UPDATE");
// console.log(params);
// return false;
db.table(resource).update(params.data.id, params.data).then((updated) => {
// console.log(updated);
// alert('update made');
resolve(params);
});

break;
break;

case GET_LIST:
case GET_MANY:
case GET_MANY_REFERENCE:
db.table(resource).count((count) => {
db.table(resource).count((count) => {

let collection = db.table(resource);
collection
collection
.offset(0)
.limit(100)
.toArray()
.then((data) => {
console.log(data);
resolve({
data: data,
total:count,
total: count,
page: 1,
totalCount: count,
})
})
})
})

break;



default:
default:

break;
}

})
}

0 comments on commit aef7866

Please sign in to comment.