/**
 * this is the test for the server-tutorial
 * IMPORTANT: whenever you change something here,
 * ensure it is also changed in /docs-src/tutorials/server.md
 */ import { addRxPlugin , createRxDatabase , addPouchPlugin , RxJsonSchema , getRxStoragePouch } from 'rxdb' ; import * as MemoryAdapter from 'pouchdb-adapter-memory' ; addPouchPlugin ( MemoryAdapter ) ; import { RxDBServerPlugin } from 'rxdb/plugins/server' ; addRxPlugin ( RxDBServerPlugin ) ; import * as PouchHttpPlugin from 'pouchdb-adapter-http' ; addPouchPlugin ( PouchHttpPlugin ) ; import AsyncTestUtil from 'async-test-util' ; import * as request from 'request-promise-native' ; import * as assert from 'assert' ; import * as os from 'os' ; import * as path from 'path' ; declare type ItemDocumentData = { key : string ; value : string ; } ; async function run ( ) { const db = await createRxDatabase ( { name : 'mydb' , storage : getRxStoragePouch ( 'memory' ) } ) ; const mySchema = { version : 0 , type : 'object' , primaryKey : 'key' , properties : { key : { type : 'string' } , value : { type : 'string' } } , required : [ 'key' ] } ; await db . addCollections ( { items : { schema : mySchema } } ) ; await db . items . insert ( { key : 'foo' , value : 'bar' } ) ; const serverState = await db . server ( { path : '/db' , port : 3000 , cors : true , pouchdbExpressOptions : { inMemoryConfig : true , logPath : path . join ( os . tmpdir ( ) , 'rxdb-server-log.txt' ) } } ) ; console . log ( 'You can now open http://localhost:3000/db' ) ; const colUrl = 'http://localhost:3000/db/items' ; console . log ( 'You can now open ' + colUrl ) ; const gotJson = await request ( colUrl ) ; const got = JSON . parse ( gotJson ) ; assert . strictEqual ( got . doc_count , 1 ) ; /**
     * on the client
     */ const clientDB = await createRxDatabase ( { name : 'clientdb' , storage : getRxStoragePouch ( 'memory' ) } ) ; await clientDB . addCollections ( { items : { schema : mySchema } } ) ; clientDB . items . syncCouchDB ( { remote : 'http://localhost:3000/db/items' } ) ; await AsyncTestUtil . waitUntil ( async ( ) => { const docs = await clientDB . items . find ( ) . exec ( ) ; return docs . length === 1 ; } ) ; serverState . server . close ( ) ; db . destroy ( ) ; clientDB . destroy ( ) ; process . exit ( ) ; } run ( ) ;