@@ -22,49 +22,54 @@ export class MediaStore {
2222 private readonly log : Lincoln
2323
2424 constructor ( logger : Lincoln ) {
25- const options = {
25+ const options : PouchDB . Configuration . RemoteDatabaseConfiguration = {
2626 adapter : 'http' ,
2727 auth : {
2828 password : process . env . PLEXIFY_COUCHDB_PASSWORD || 'guest' ,
2929 username : process . env . PLEXIFY_COUCHDB_USERNAME || 'guest' ,
3030 } ,
31- name : COUCHDB_URL ,
31+ name : fs . basename ( COUCHDB_URL ) ,
3232 }
3333
3434 this . database = new PouchDB < MediaInfo > ( COUCHDB_URL , options )
3535 this . log = logger . extend ( 'storage' )
36- this . log . trace ( options )
36+ this . log . trace ( 'options' , options )
3737 }
3838
3939 async all ( options ?: PouchDB . Find . FindRequest < MediaInfo > ) {
4040 try {
4141 const results = await this . database . find ( options || { selector : { } } )
4242 return results . docs
4343 } catch ( error ) {
44- this . log . error ( new BError ( 'all' , error ) )
44+ this . log . error ( new BError ( 'all' , error ) , error )
4545 return [ ]
4646 }
4747 }
4848
49+ dbinfo ( ) {
50+ return this . database . info ( )
51+ }
52+
4953 document ( doc : Partial < MediaInfo > ) : MediaInfo {
50- return { ...doc , _id : this . cleanid ( doc . filename ! ) } as MediaInfo
54+ const document : MediaInfo = { ...doc , _id : this . cleanid ( doc . filename ! ) } as MediaInfo
55+ return document
5156 }
5257
5358 async exists ( id : string ) {
5459 try {
55- const document = await this . database . get < MediaInfo > ( id )
60+ const document = await this . database . get ( id )
5661 return document . _id === this . cleanid ( id )
5762 } catch ( error ) {
58- this . log . error ( new BError ( 'exists' , error ) )
63+ this . log . error ( new BError ( 'exists' , error ) , error )
5964 return false
6065 }
6166 }
6267
6368 async get ( id : string ) {
6469 try {
65- return await this . database . get < MediaInfo > ( this . cleanid ( id ) )
70+ return await this . database . get ( this . cleanid ( id ) )
6671 } catch ( error ) {
67- this . log . error ( new BError ( 'get' , error ) )
72+ this . log . error ( new BError ( 'get' , error ) , error )
6873 return null
6974 }
7075 }
@@ -81,16 +86,16 @@ export class MediaStore {
8186 try {
8287 return await this . upsert ( this . cleanid ( id ) , { source, ...{ locked : true , host : os . hostname ( ) } } )
8388 } catch ( error ) {
84- this . log . error ( new BError ( 'lock' , error ) )
89+ this . log . error ( new BError ( 'lock' , error ) , error )
8590 }
8691 }
8792
8893 async locked ( id : string ) {
8994 try {
90- const document = await this . database . get < MediaInfo > ( this . cleanid ( id ) )
95+ const document = await this . database . get ( this . cleanid ( id ) )
9196 return document . locked === true && document . host !== os . hostname ( )
9297 } catch ( error ) {
93- this . log . error ( new BError ( 'locked' , error ) )
98+ this . log . error ( new BError ( 'locked' , error ) , error )
9499 return false
95100 }
96101 }
@@ -100,15 +105,16 @@ export class MediaStore {
100105 this . log . trace ( 'unlocked' , this . cleanid ( id ) )
101106 return await this . upsert ( this . cleanid ( id ) , { source, ...{ locked : false , host : null } } )
102107 } catch ( error ) {
103- this . log . error ( new BError ( 'unlock' , error ) )
108+ this . log . error ( new BError ( 'unlock' , error ) , error )
104109 }
105110 }
106111
107112 async upsert ( id : string , document : Partial < MediaInfo > ) {
108- const results = await this . database . upsert < MediaInfo > (
113+ const results = await this . database . upsert (
109114 this . cleanid ( id ) ,
110115 ( target : MediaInfo ) =>
111116 ( {
117+ ...{ _id : this . cleanid ( id ) } ,
112118 ...target ,
113119 ...document ,
114120 } as MediaInfo ) ,
@@ -118,7 +124,7 @@ export class MediaStore {
118124 }
119125
120126 private cleanid ( id : string ) : string {
121- return btoa ( this . ident ( id ) )
127+ return Buffer . from ( this . ident ( id ) ) . toString ( 'hex' )
122128 }
123129
124130 private ident ( id : string ) {
0 commit comments