Commit 5c817915 authored by Aral Balkan's avatar Aral Balkan
Browse files

Add simple example of JSDF version 1.0 to version 2.0 migration

parent b70a740a
# JSDF version 1.0 to 2.0 migration example
There is no automatic built-in migration of version 1.0 (UMD) JSDF files to version 2.0 (ECMAScript Module; ESM) for performance reasons.
If you want to migrate your own databases, you can use the technique demonstrated here.
## Details
This example converts the following JSDF 1.0 file:
```js
globalThis._ = [ { 'name': `Aral`, 'age': 43 }, { 'name': `Laura`, 'age': 34 } ];
(function () { if (typeof define === 'function' && define.amd) { define([], globalThis._); } else if (typeof module === 'object' && module.exports) { module.exports = globalThis._ } else { globalThis.people = globalThis._ } })();
_[1]['age'] = 33;
_[2] = { 'name': `Oskar`, 'age': 8 };
_[2]['name'] = `Osky`;
```
(This is `people.js`, as found in the `db-version-1.0/` folder.)
Into this JSDF 2.0 file:
```js
export const _ = [ { 'name': `Aral`, 'age': 43 }, { 'name': `Laura`, 'age': 34 } ];
_[1]['age'] = 33;
_[2] = { 'name': `Oskar`, 'age': 8 };
_[2]['name'] = `Osky`;
```
(The coverted file is output to the `db/` folder after you run `index.js`.)
## In pseudo-code
To covert from JSDF 1.0 to JSDF 2.0:
1. Replace `globalThis._` with `export const _` on line one.
2. Delete line two.
That’s it.
globalThis._ = [ { 'name': `Aral`, 'age': 43 }, { 'name': `Laura`, 'age': 34 } ];
(function () { if (typeof define === 'function' && define.amd) { define([], globalThis._); } else if (typeof module === 'object' && module.exports) { module.exports = globalThis._ } else { globalThis.people = globalThis._ } })();
_[1]['age'] = 33;
_[2] = { 'name': `Oskar`, 'age': 8 };
_[2]['name'] = `Osky`;
//
// Convert from JSDF 1.0 to JSDF 2.0
//
import fs from 'fs'
// Load in the version 1.0 table.
const tableVersion1 = fs.readFileSync('./db-version-1.0/people.js', 'utf-8')
// Split the string into an array of lines for convenience.
//
// Note: for much larger tables, you will want to use a module like
// n-readlines to read individual lines from your tables.
// See the load() method in lib/JSTable.js for an example of the
// approach you would take.
const tableLines = tableVersion1.split('\n')
// 1. Update the first line.
tableLines[0] = tableLines[0].replace('globalThis._', 'export const _')
// 2. Remove the second line.
tableLines.splice(1,1)
// Rejoin the array into a string.
const tableVersion2 = tableLines.join('\n')
// Write out the version 2.0 table.
if (!fs.existsSync('db')) {
fs.mkdirSync('db')
}
fs.writeFileSync('db/people.js', tableVersion2)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment