/**
 * this is the test for the typescript-tutorial
 * IMPORTANT: whenever you change something here,
 * ensure it is also changed in /docs-src/tutorials/typescript.md
 */ import { RxDatabase , RxCollection , RxJsonSchema , RxDocument , createRxDatabase , addPouchPlugin , getRxStoragePouch } from 'rxdb' ; import * as MemoryAdapter from 'pouchdb-adapter-memory' ; addPouchPlugin ( MemoryAdapter ) ; /**
 * declare types
 */ type HeroDocType = { passportId : string ; firstName : string ; lastName : string ; age ? : number ; } ; type HeroDocMethods = { scream : ( v : string ) => string ; } ; type HeroDocument = RxDocument < HeroDocType , HeroDocMethods > ; type HeroCollectionMethods = { countAllDocuments : ( ) => Promise < number > ; } ; type HeroCollection = RxCollection < HeroDocType , HeroDocMethods , HeroCollectionMethods > ; type MyDatabaseCollections = { heroes : HeroCollection ; } ; type MyDatabase = RxDatabase < MyDatabaseCollections > ; async function run ( ) { /**
     * create database and collections
     */ const myDatabase = await createRxDatabase < MyDatabaseCollections > ( { name : 'mydb' , storage : getRxStoragePouch ( 'memory' ) } ) ; const heroSchema = { title : 'human schema' , description : 'describes a human being' , version : 0 , keyCompression : true , primaryKey : 'passportId' , type : 'object' , properties : { passportId : { type : 'string' } , firstName : { type : 'string' } , lastName : { type : 'string' } , age : { type : 'integer' } } , required : [ 'firstName' , 'lastName' ] } ; const heroDocMethods = { scream : function ( this , what ) { return this . firstName + ' screams: ' + what . toUpperCase ( ) ; } } ; const heroCollectionMethods = { countAllDocuments : async function ( this ) { const allDocs = await this . find ( ) . exec ( ) ; return allDocs . length ; } } ; await myDatabase . addCollections ( { heroes : { schema : heroSchema , methods : heroDocMethods , statics : heroCollectionMethods } } ) ; myDatabase . heroes . postInsert ( function myPostInsertHook ( this , docData , doc ) { console . log ( 'insert to ' + this . name + '-collection: ' + doc . firstName ) ; } , false ) ; /**
     * use the database
     */ const hero = await myDatabase . heroes . insert ( { passportId : 'myId' , firstName : 'piotr' , lastName : 'potter' , age : 5 } ) ; console . log ( hero . firstName ) ; hero . scream ( 'AAH!' ) ; const amount = await myDatabase . heroes . countAllDocuments ( ) ; console . log ( amount ) ; /**
     * clean up
     */ myDatabase . destroy ( ) ; } run ( ) ;