score:1
Try this:
sqlite3 example "DROP TABLE IF EXISTS some_table;";
sqlite3 example "CREATE TABLE IF NOT EXISTS some_table (id INTEGER PRIMARY KEY AUTOINCREMENT, anattr VARCHAR, anotherattr VARCHAR);";
sqlite3 example "INSERT INTO some_table VALUES (NULL, '1stAttr', 'AttrA');";
sqlite3 example "INSERT INTO some_table VALUES (NULL, '2ndAttr', 'AttrB');";
## Create three JSON fragment files
sqlite3 example ".output result_prefix.json" "SELECT '{ \"docs\": ['";
sqlite3 example ".output rslt.json" "SELECT '{ \"_id\": \"someTable_' || SUBSTR(\"000000000\" || id, LENGTH(\"000000000\" || id) - 8, 9) || '\", \"anattr\": \"' || anattr || '\", \"anotherattr\": \"' || anotherattr || '\" },' FROM some_table;";
sqlite3 example ".output result_suffix.json" "SELECT '] }'";
## strip trailing comma of last record
sed -i '$ s/.$//' rslt.json;
## concatenate to a single file
cat result_prefix.json rslt.json result_suffix.json > result.json;
cat result.json;
You should be able simply to paste the above lines onto the (unix) command line, seeing output:
{ "docs": [
{ "_id": "someTable_000000001", "anattr": "1stAttr", "anotherattr": "AttrA" },
{ "_id": "someTable_000000002", "anattr": "2ndAttr", "anotherattr": "AttrB" }
] }
If you have jq
installed you can do instead ...
cat result.json | jq .
... obtaining:
{
"docs": [
{
"_id": "someTable_000000001",
"anattr": "1stAttr",
"anotherattr": "AttrA"
},
{
"_id": "someTable_000000002",
"anattr": "2ndAttr",
"anotherattr": "AttrB"
}
]
}
You'll find an example of how quickly to initialize PouchDB from JSON files in part 2 of the blog post Prebuilt databases with PouchDB.
So, if you have a CouchDB server available you can do the following;
export COUCH_DB=example;
export COUCH_URL= *** specify yours here ***;
export FILE=result.json;
## Drop database
curl -X DELETE ${COUCH_URL}/${COUCH_DB};
## Create database
curl -X PUT ${COUCH_URL}/${COUCH_DB};
## Load database from JSON file
curl -H "Content-type: application/json" -X POST "${COUCH_URL}/${COUCH_DB}/_bulk_docs" -d @${FILE};
## Extract database with meta data to PouchDB initialization file
pouchdb-dump ${COUCH_URL}/${COUCH_DB} > example.json
## Inspect PouchDB initialization file
cat example.json | jq .
Obviously you'll need some adaptations, but the above should give you no problems.
score:0
Since Couch/Pouch-DB are document-oriented DBs all records aka docs there are just JSON aka JS-objects. In my RN app when I met similar task I just put all docs I wanted to be "prepopulated" in PouchDB in an array of JS-objects, import it as module in my app and then write them during app init to PDB as necessarry docs. That's all prepopulation. How to export your SQL DB records to JSON - you decide, surely it depends on source DB structure and data logic you want to be in PDB.
Source: stackoverflow.com
Related Query
- What is the correct way to load prebuilt SQlite databases with or without PouchDB in a React App
- What is the correct way of dispatching an action not tied to a component with Redux?
- What is the correct way to call a function with param on click only?
- React - What is the correct way to make an API call with user input?
- What is the correct way to initiate a side-effect on page load when using React Strict Mode?
- What is the correct way to React Hook Fetch Api and store to Redux and retrieve it with Selector immediately?
- What is the correct way to work with GraphQL Custom Input types?
- What is the correct way to use getInitialProps with getDefaultProps (React)
- ReactJS + ServerSide with ExpressJS - need a way to dictate what to load on the server vs client
- What is the correct way to push jsx with embedded js variables on to an array
- What is the correct way to add to an array with splice in react
- What is the correct way to use a menu with an AppBar in Material UI
- What is the correct way to collect data in react without Re-rendering and Perfomance Issues
- What is the correct way to handle an accesstoken with react to secure routes?
- Using React, what is an easy way to load files based off clicking buttons with the same name?
- What is the best way to deal with a fetch error in react redux?
- What is the correct way of adding a dependency to react in your package.json for a react component
- What is the proper way to use React Memo with Flow?
- What is the correct way to access props in the constructor?
- What is the right way to use Ionic Native / Cordova Plugins with Ionic (React) & Capacitor?
- TS2322 Error when extending Button, component property not found. What is the correct way to type?
- What is the correct way to animate/transition between routes in react router
- What is the correct way to use inline style in react?
- what is the proper way to publish a React component module in NPM without external dependencies inside?
- What is the best way to test Window Scroll Event Handlers with Enzyme?
- What is the correct way to extend Material UI ListItem?
- What is the correct way to pass parameters to a React-query useQuery method that uses Axios
- What is the correct way to change Navbar values in React based on if user is logged in?
- What is the correct way to define a React component's contextTypes in TypeScript?
- What is the way to add horizontal scroll on material-ui table with many columns?
More Query from same tag
- how to add element as title in tree for reactjs
- How to pass React component as a child and add props inside this conponent
- Get data from Redux Store after data has been added
- Property 'previous' is missing in type - TypeScript + ReactJS
- Why Am I getting this Router Error , Check Description for error (React)
- React: How to combine multiple external makestyle using material ui?
- How to use axios in Effect Hook?
- Why is my new string value undefined in React.js?
- How to read redux state inside a component the right way?
- TypeError: undefined is not an object (evaluating 'this.__reactAutoBindMap')
- React HOC to handle clicks outside a given Component
- React router how to load css based on global var
- react-leaflet WMSTileLayer 'params' option causes map layer to flicker when any unrelated state variable is updated
- TypeError: projects.map is not a function (React)
- ReactJS - Destroy old Component-Instance and create new
- Updating a table with react.js results in the wrong row order
- Loop or map in React JS
- ASP.Core Uploading image with Axios
- Is it more efficient to check function props existence before calling them in React or to provide empty function as default prop for them?
- Is there a way to let users view blurred content only in React?
- How to display content properly in React
- <img src='img.png'> not showing local file (react)
- How can I manage multiple setState calls in the same React component?
- Using sweet alert 2 getting this.props is undefined after using Warning alert
- Do react native uses same code for ios and android or both platform code should be written separately?
- how to call child component's method from parent component's event in Reactjs
- How to render a list of components held as a key value
- How to use react-i18-next on .json or .js file?
- Reactjs - Changing an item in randomized array via button
- Apex charts disable scrolling / zooming