Skip to content

Commit

Permalink
Build system (#121)
Browse files Browse the repository at this point in the history
Big rewrite to prepare for future changes. This now uses Typescript and Webpack to build everything.
Some breaking changes to eliminate extend usage.
Supersedes #116 and thus should also solve #86
  • Loading branch information
jlami committed Mar 4, 2020
1 parent 19e871a commit fecdbbd
Show file tree
Hide file tree
Showing 22 changed files with 11,195 additions and 2,533 deletions.
36 changes: 36 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@

module.exports = {
root: true,
parserOptions: {
// ecmaVersion: 2017,
sourceType: 'module',
// project: "./tsconfig.json",
tsconfigRootDir: __dirname,
project: ['./test.tsconfig.json'],
},
parser: '@typescript-eslint/parser',
plugins: [
'@typescript-eslint',
],
extends: [
// 'eslint:recommended',
// 'plugin:@typescript-eslint/eslint-recommended',
// 'plugin:@typescript-eslint/recommended',
// "plugin:@typescript-eslint/recommended-requiring-type-checking",
],
env: {
browser: true,
node: true,
es6: true,
},
// plugins: [
// "promise"
// ],
rules: {
"@typescript-eslint/no-floating-promises": "error",
// "await-promise": 2,
// "no-debugger": 0,
// "no-console": 0,
// "no-mixed-spaces-and-tabs": 0,
}
};
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ coverage
test/test-bundle.js
npm-debug.log
dist
/.nyc_output
/lib
/dist
33 changes: 0 additions & 33 deletions .jshintrc

This file was deleted.

100 changes: 56 additions & 44 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Then include it after `pouchdb.js` in your HTML page:
```html
<script src="pouchdb.js"></script>
<script src="pouchdb.find.js"></script>
<script src="pouchdb.relational-pouch.js"></script>
<script src="pouchdb.relational-pouch.browser.js"></script>
```

### In Node.js
Expand All @@ -55,6 +55,31 @@ PouchDB.plugin(require('relational-pouch'));
PouchDB.plugin(require('pouchdb-find'));
```

#### Typescript

This package contains its own type definitions. Due to the nature of setSchema, which alters the database on which it is called, typescript needs to know about these changes. This is done by returning a new type. So working with this plugin should look something like this:

```js
import Pouch from 'pouchdb-core';
//import some adapter
import find from 'pouchdb-find';
import rel from 'relational-pouch';

Pouch
//.plugin(someadapter)
.plugin(find)
.plugin(rel);

const baseDB = new Pouch(...);//adapter options
const relDB = baseDB.setSchema(...);//schema options

let relDoc = await relDB.rel.find('sometype', 'someid');

//non relation pouch API is still available
let doc = await relDB.get('someid');
```


API
----------

Expand Down Expand Up @@ -167,14 +192,8 @@ Result:

```js
{
"posts": [
{
"title": "Rails is Omakase",
"text": "There are a lot of a-la-carte software...",
"id": "14760983-285C-6D1F-9813-D82E08F1AC29",
"rev": "1-84df2c73028e5b8d0ae1cbb401959370"
}
]
"id": "14760983-285C-6D1F-9813-D82E08F1AC29",
"rev": "1-84df2c73028e5b8d0ae1cbb401959370"
}
```

Expand All @@ -192,14 +211,8 @@ Result:

```js
{
"posts": [
{
"title": "Rails is Unagi",
"text": "Delicious unagi. Mmmmmm.",
"id": 1,
"rev": "1-0ae315ee597b22cc4b1acf9e0edc35ba"
}
]
"id": 1,
"rev": "1-0ae315ee597b22cc4b1acf9e0edc35ba"
}
```

Expand Down Expand Up @@ -359,25 +372,10 @@ var attachment = new Blob(['Is there life on Mars?']);
db.rel.putAttachment('post', {id:1, rev:"1-..."}, 'file', attachment, 'text/plain');
```

Result:
This returns the new rev:

```js
{
"posts": [
{
"attachments": {
"file": {
"content_type": "text/plain",
"digest": "md5-1cz9JKh0i+1OLJonmitgiQ==",
"length": 22,
// ... http://pouchdb.com/guides/attachments.html
}
},
"id": 1,
"rev": "2-...."
}
]
}
"2-...."
```

### db.rel.getAttachment(type, id, attachmentId)
Expand Down Expand Up @@ -409,17 +407,10 @@ Or continuing from the `putAttachment` example:
db.rel.removeAttachment('post', {id: 1, rev:"2-09d5c5bd86fc170c064b296773044ea9"} , 'file');
```

Result:
This returns the new rev:

```js
{
"posts": [
{
"id": 1,
"rev": "3-...."
}
]
}
"3-...."
```

### db.rel.parseDocID(docID)
Expand Down Expand Up @@ -1118,10 +1109,14 @@ Testing
### In Node
This will run the tests in Node using LevelDB:
This will run the tests in Node using memory and http adapter:
npm test
if you don't have a admin party setup you can specify admin credentials in the RELATIONAL_POUCH_DB_AUTH environment variable like this:
RELATIONAL_POUCH_DB_AUTH=user:password@
You can also check for 100% code coverage using:
npm run coverage
Expand Down Expand Up @@ -1150,3 +1145,20 @@ You can run e.g.
CLIENT=selenium:phantomjs npm test
This will run the tests automatically and the process will exit with a 0 or a 1 when it's done. Firefox uses IndexedDB, and PhantomJS uses WebSQL.
## Changelog
### 4.0.0
- Breaking change: To prevent us from having to do cloning of input documents, we have changed the `save`, `putAttachment` and `removeAttachment` API. These functions no longer return the complete document. The attachment functions only return the new `rev` value, while the save will also return the `id`. So after these promises resolve you have to manually update your in app data to reflect this new revision (and possibly id) if you want to update the document later. You can use something like the following:
```js
let updatedData = await db.rel.save('post', post);
Object.assign(post, updatedData);
```
or
```js
post.rev = await db.rel.putAttachment('post', post, 'file', fileData);
```
- This library now uses Typescript, Webpack and Babel in its build setup. The build creates files in 2 output directories: lib and dist.
- The lib directory will contain the output of `tsc` in esnext mode. So this can be used by Webpack and other module aware systems. These will require Babel transformations if you want to use them, but this way you can specify your own target.
- The dist directory contains 2 files, pouchdb.relational-pouch.browser.js and pouchdb.relational-pouch.node.js. These are compiled by webpack with targets ">2%, not ie 11" and "node 10". This should be sufficient for now, but otherwise you can build your own with Webpack.
104 changes: 77 additions & 27 deletions bin/dev-server.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,46 +5,96 @@
var COUCH_HOST = process.env.COUCH_HOST || 'http://127.0.0.1:5984';
var HTTP_PORT = 8001;

const ForkTsCheckerWebpackPlugin = require('fork-ts-checker-webpack-plugin');

var Promise = require('bluebird');
var request = require('request');
var http_server = require("http-server");
var fs = require('fs');
var indexfile = "./test/test.js";
var dotfile = "./test/.test-bundle.js";
var outfile = "./test/test-bundle.js";
var watchify = require("watchify");
var browserify = require('browserify');
var b = browserify(indexfile, {
cache: {},
packageCache: {},
plugin: [watchify]
})
var webpack = require('webpack');
var path = require('path');

b.on('update', bundle);
bundle();
var b = webpack({
target: "web",
entry: "./test/test.ts",
mode: 'development',
devtool: 'source-map',
output: {
path: path.resolve(__dirname, '../test'),
filename: 'test-bundle.js',
libraryTarget: 'umd',
},
plugins: [
new ForkTsCheckerWebpackPlugin({eslint: true, tsconfig: "test.tsconfig.json"}),
new webpack.EnvironmentPlugin(['RELATIONAL_POUCH_DB_AUTH']),
],
module: {
rules: [
// {
// enforce: 'pre',
// test: /\.[tj]s$/,
// exclude: /node_modules/,
// loader: 'eslint-loader',
// },
{
test: /\.[tj]s$/,
exclude: /(node_modules|bower_components)/,
use: [
{
loader: 'babel-loader',
options: {
presets: [
['@babel/preset-env',
{
"targets": "last 1 Chrome versions",
"modules": false,
useBuiltIns: "usage",
corejs: 3,
}],
],
},
},
{
loader: 'ts-loader',
options: {
transpileOnly: true,
experimentalWatchApi: true,
context: path.resolve(__dirname, '../'),
configFile: "test.tsconfig.json",
},
},
],
}
]
},
resolve: {
extensions: ['tsx', '.ts', '.js', '.json'],
},
}).watch({}, (error, stats) => {
if (error) {
console.error(error);
return;
}

let logOptions = {all: false, colors: true, assets: true, errors: true, errorDetails: true, warnings: true, errorStack: true};
if (!stats.hasErrors()) {
console.log('Updated');
console.log(stats.toString(logOptions));
filesWritten = true;
checkReady();
} else {
const info = stats.toJson();
console.error(stats.toString(logOptions));//children: false, entrypoints: false, hash: false, modules: false, , chunks: false
}
});

var filesWritten = false;
var serverStarted = false;
var readyCallback;

function bundle() {
var wb = b.bundle();
wb.on('error', function (err) {
console.error(String(err));
});
wb.on("end", end);
wb.pipe(fs.createWriteStream(dotfile));

function end() {
fs.rename(dotfile, outfile, function (err) {
if (err) { return console.error(err); }
console.log('Updated:', outfile);
filesWritten = true;
checkReady();
});
}
}

function startServers(callback) {
readyCallback = callback;
// enable CORS globally, because it's easier this way
Expand Down
7 changes: 4 additions & 3 deletions bower.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"name": "relational-pouch",
"version": "3.2.0",
"version": "4.0.0",
"description": "PouchDB, relational-style",
"homepage": "https://github.com/pouchdb-community/relational-pouch",
"authors": [
"Nolan Lawson <[email protected]>"
],
"main": "dist/pouchdb.relational-pouch.js",
"main": "dist/pouchdb.relational-pouch.browser.js",
"moduleType": [
"node"
],
Expand All @@ -23,6 +23,7 @@
"bower_components",
"test",
"tests",
"vendor"
"vendor",
"lib"
]
}
Loading

0 comments on commit fecdbbd

Please sign in to comment.