Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG leveldown module at adapter option throw error. 'leveldb' string solve it #2251

Closed
techo207 opened this issue Jun 5, 2020 · 8 comments
Closed

Comments

@techo207
Copy link

techo207 commented Jun 5, 2020

BUG

RxDBServerPlugin no work correctly if you use the leveldown adapter for nodejs enviroment:

addRxPlugin(require('pouchdb-adapter-leveldb')); // leveldown adapters need the leveldb plugin to work
const leveldown = require('leveldown');

const database = await createRxDatabase({
    name: 'mydatabase',
    adapter: leveldown // the full leveldown-module
});

this throw error :

UnhandledPromiseRejectionWarning: Error: Invalid Adapter: function LevelDOWN (location) {
  if (!(this instanceof LevelDOWN)) {
    return new LevelDOWN(location)
  }

  if (typeof location !== 'string') {
    throw new Error('constructor requires a location string argument')
  }

  AbstractLevelDOWN.call(this, {
    bufferKeys: true,
    snapshots: true,
    permanence: true,
    seek: true,
    clear: true,
    createIfMissing: true,
    errorIfExists: true,
    additionalMethods: {
      approximateSize: true,
      compactRange: true
    }
  })

  this.location = location
  this.context = binding.db_init()
}
    at PouchAlt.PouchDB (/home/techo/MEGA/PROYECTO/TIENDITAS/DB/node_modules/pouchdb-core/lib/index.js:1292:11)
    at new PouchAlt (/home/techo/MEGA/PROYECTO/TIENDITAS/DB/node_modules/pouchdb-core/lib/index.js:1412:13)
    at /home/techo/MEGA/PROYECTO/TIENDITAS/DB/node_modules/express-pouchdb/lib/create-or-delete-dbs.js:30:10
    at processTicksAndRejections (internal/process/task_queues.js:97:5)
(Use `node --trace-warnings ...` to show where the warning was created)
(node:881932) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled promise rejection, use the CLI flag `--unhandled-rejections=strict` (see https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode). (rejection id: 1)
(node:881932) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

if i pass the string 'leveldb' to adapter it init correctly, but when i try to get a collection for example db/users it throw {"error":"not_found","reason":"no_db_file"}. the rxdb.users works correctly

with node websql adapter happenes the same, rxdb works correctly but server throw {"error":"not_found","reason":"no_db_file"}

@pubkey
Copy link
Owner

pubkey commented Jun 10, 2020

It works in this test.
Can you add or modify the tests to reproduce the problem?

@techo207
Copy link
Author

techo207 commented Jun 10, 2020

I'm sorry, i dont give you enough explanation.
The problem happen when using server plugin, only the memory adapter works if you use websql or leveldb you cant get db/items in the browser, it only says {"error":"not_found","reason":"no_db_file"} but the rxdb.items works correctly and list all items

if you use leveldb adaptar it give you the error explained above

import { createRxDatabase, addRxPlugin } from 'rxdb';


// add the server-plugin
import { RxDBServerPlugin } from 'rxdb/plugins/server';
addRxPlugin( RxDBServerPlugin );



addRxPlugin( require( 'pouchdb-adapter-node-websql' ) );


async function run() {
	// create database
	const db = await createRxDatabase( {
		name: 'database/mydb', 
		adapter: 'websql',
	} );

	// create collection
	const mySchema = {
		version: 0,
		type: 'object',
		properties: {
			key: {
				type: 'string',
				primary: true,
			},
			value: {
				type: 'string',
			},
		},
	};
	await db.collection( {
		name: 'items',
		schema: mySchema,
	} );

	// insert one document
	await db.items.insert( {
		key: `${Date.now()}`,
		value: 'bar',
	} );

	const { app, server } = db.server( {
		path: '/db', // (optional)
		port: 3000, // (optional)
		cors: true, // (optional), enable CORS-headers
		startServer: true, // (optional), start express server
		// options of the pouchdb express server
		pouchdbExpressOptions: {
			inMemoryConfig: true, // do not write a config.json
			logPath: '/tmp/rxdb-server-log.txt', // save logs in tmp folder
		},
	} );

	console.log( 'ready' );


}


run();

@techo207
Copy link
Author

techo207 commented Jun 11, 2020

after a days i noticed that the problem only occurs when you set a name with a custom path like 'database/mydb' but if i set 'mydb' it will work. With custom path name the url /mydb-rxdb-0-items is not being created and the function pseudo.allDbs() inside server.js plugin return a database with name database/mydb-rxdb-0-items i think pouchdb server module is creating the route with other name due the characters / are not allowed (url encoding: database%2Fmydb-rxdb-0-items no works) OR the route is not being create at all

@pubkey
Copy link
Owner

pubkey commented Jun 18, 2020

Can you make a PR with a failing test? I can then start fixing this.

@pubkey
Copy link
Owner

pubkey commented Jun 20, 2020

I tried in a project and found the same problem.
It looks like the server does not use the same path as the internal pouchdb instance.

pubkey added a commit that referenced this issue Jun 20, 2020
@pubkey
Copy link
Owner

pubkey commented Jun 20, 2020

Ok so the problem occurs when you have an ending slash in the database name.
This makes sense and I added a check to disallow ending slashes in the path.

When you have a path like /root/foo/bar, RxDB will create folder for the collections that look like /root/foo/bar-rxdb-0-mycollection.

Please check if this helps and tell me if you still have an error.

@pubkey pubkey closed this as completed Jun 20, 2020
@KVAnton-WEB
Copy link

KVAnton-WEB commented Aug 23, 2021

  const leveldown = require("leveldown");
  const db = await createRxDatabase({
    name: "/tmp/bar",
    storage: getRxStoragePouch(leveldown),
    password: "nohvaiP0so7aiWahfe6eimuNg",
  });

  db.server({
    path: "/db",
    port: 10102,
    cors: true,
  });

I get an error after creating the server

index.es.js:1304 Uncaught (in promise) Error: Invalid Adapter: function LevelDOWN (location) {
  if (!(this instanceof LevelDOWN)) {
    return new LevelDOWN(location)
  }

  if (typeof location !== 'string') {
    throw new Error('constructor requires a location string argument')
  }

  AbstractLevelDOWN.call(this, {
    bufferKeys: true,
    snapshots: true,
    permanence: true,
    seek: true,
    clear: true,
    createIfMissing: true,
    errorIfExists: true,
    additionalMethods: {
      approximateSize: true,
      compactRange: true
    }
  })

  this.location = location
  this.context = binding.db_init()
}
    at PouchAlt.PouchDB (index.es.js:1304)
    at new PouchAlt (index.es.js:1424)
    at VM891 create-or-delete-dbs.js:30
    at processTicksAndRejections (VM24 task_queues.js:93)
PouchDB @ index.es.js:1304
PouchAlt @ index.es.js:1424
(anonymous) @ VM891 create-or-delete-dbs.js:30
processTicksAndRejections @ VM24 task_queues.js:93
Promise.then (async)
processTicksAndRejections @ VM24 task_queues.js:93

I have also tried creating a collection:

@pubkey
Copy link
Owner

pubkey commented Aug 23, 2021

@KVAnton-WEB please make a PR with a failing test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants