1+ "use strict" ;
12var __awaiter = ( this && this . __awaiter ) || function ( thisArg , _arguments , P , generator ) {
23 function adopt ( value ) { return value instanceof P ? value : new P ( function ( resolve ) { resolve ( value ) ; } ) ; }
34 return new ( P || ( P = Promise ) ) ( function ( resolve , reject ) {
@@ -7,36 +8,38 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
78 step ( ( generator = generator . apply ( thisArg , _arguments || [ ] ) ) . next ( ) ) ;
89 } ) ;
910} ;
11+ var __importDefault = ( this && this . __importDefault ) || function ( mod ) {
12+ return ( mod && mod . __esModule ) ? mod : { "default" : mod } ;
13+ } ;
14+ Object . defineProperty ( exports , "__esModule" , { value : true } ) ;
15+ exports . OfflineDB = void 0 ;
1016/* eslint-disable valid-jsdoc */
1117/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
12- import { join } from 'path' ;
13- import fs from 'fs' ;
14- import { Low , JSONFile } from 'lowdb' ;
15- import lodash from 'lodash' ;
16- class LowDash extends Low {
17- constructor ( adapter ) {
18- super ( adapter ) ;
19- }
20- }
18+ const path_1 = require ( "path" ) ;
19+ const fs_1 = __importDefault ( require ( "fs" ) ) ;
20+ const lowdb_1 = __importDefault ( require ( "lowdb" ) ) ;
21+ const FileAsync_1 = __importDefault ( require ( "lowdb/adapters/FileAsync" ) ) ;
22+ const lodash_1 = require ( "lodash" ) ;
2123/**
2224 * Mocks the Deta Base SDK with a local database
2325 *
2426 * It uses a local JSON file for each Base.
2527 *
2628 * Note: Not all methods are implemented, only the ones need by deta-base-orm
2729 */
28- export class OfflineDB {
30+ class OfflineDB {
2931 constructor ( storagePath = '.deta-base-orm' , fileName = 'base' ) {
30- const folderExists = fs . existsSync ( storagePath ) ;
32+ const folderExists = fs_1 . default . existsSync ( storagePath ) ;
3133 if ( ! folderExists ) {
3234 if ( storagePath !== '.deta-base-orm' ) {
3335 throw new Error ( `The folder "${ storagePath } " does not exist, please create it manually` ) ;
3436 }
35- fs . mkdirSync ( storagePath ) ;
37+ fs_1 . default . mkdirSync ( storagePath ) ;
3638 }
37- const file = join ( storagePath , `${ fileName } .json` ) ;
38- const adapter = new JSONFile ( file ) ;
39- this . db = new LowDash ( adapter ) ;
39+ const file = path_1 . join ( storagePath , `${ fileName } .json` ) ;
40+ this . _baseName = fileName ;
41+ this . _filePath = file ;
42+ this . _adapter = new FileAsync_1 . default ( file ) ;
4043 this . _didLoad = false ;
4144 }
4245 /**
@@ -57,11 +60,11 @@ export class OfflineDB {
5760 */
5861 init ( ) {
5962 return __awaiter ( this , void 0 , void 0 , function * ( ) {
60- yield this . db . read ( ) ;
61- if ( this . db . data === null ) {
62- this . db . data = [ ] ;
63+ this . _db = yield lowdb_1 . default ( this . _adapter ) ;
64+ yield this . _db . read ( ) ;
65+ if ( this . _db . get ( this . _baseName ) . value ( ) === undefined ) {
66+ yield this . _db . set ( this . _baseName , [ ] ) . write ( ) ;
6367 }
64- this . db . chain = lodash . chain ( this . db . data ) ;
6568 this . _didLoad = true ;
6669 } ) ;
6770 }
@@ -81,10 +84,10 @@ export class OfflineDB {
8184 * @returns The inserted data
8285 */
8386 put ( data ) {
87+ var _a ;
8488 return __awaiter ( this , void 0 , void 0 , function * ( ) {
8589 yield this . check ( ) ;
86- this . db . data . push ( data ) ;
87- yield this . db . write ( ) ;
90+ yield ( ( _a = this . _db ) === null || _a === void 0 ? void 0 : _a . get ( this . _baseName ) . push ( data ) . write ( ) ) ;
8891 return data ;
8992 } ) ;
9093 }
@@ -96,7 +99,7 @@ export class OfflineDB {
9699 var _a ;
97100 return __awaiter ( this , void 0 , void 0 , function * ( ) {
98101 yield this . check ( ) ;
99- return ( _a = this . db . chain ) === null || _a === void 0 ? void 0 : _a . value ( ) ;
102+ return ( _a = this . _db ) === null || _a === void 0 ? void 0 : _a . value ( ) ;
100103 } ) ;
101104 }
102105 /**
@@ -108,7 +111,7 @@ export class OfflineDB {
108111 var _a ;
109112 return __awaiter ( this , void 0 , void 0 , function * ( ) {
110113 yield this . check ( ) ;
111- return ( ( _a = this . db . chain ) === null || _a === void 0 ? void 0 : _a . find ( { key } ) . value ( ) ) || null ;
114+ return ( ( _a = this . _db ) === null || _a === void 0 ? void 0 : _a . get ( this . _baseName ) . find ( { key } ) . value ( ) ) || null ;
112115 } ) ;
113116 }
114117 /**
@@ -124,8 +127,8 @@ export class OfflineDB {
124127 let results = [ ] ;
125128 query . forEach ( ( query ) => {
126129 var _a ;
127- const items = ( _a = this . db . chain ) === null || _a === void 0 ? void 0 : _a . filter ( query ) . value ( ) ;
128- results = lodash . unionBy ( results , items , 'key' ) ;
130+ const items = ( _a = this . _db ) === null || _a === void 0 ? void 0 : _a . get ( this . _baseName ) . filter ( query ) . value ( ) ;
131+ results = lodash_1 . unionBy ( results , items , 'key' ) ;
129132 } ) ;
130133 if ( ! results ) {
131134 return { items : [ ] , count : 0 , last : undefined } ;
@@ -143,8 +146,7 @@ export class OfflineDB {
143146 var _a ;
144147 return __awaiter ( this , void 0 , void 0 , function * ( ) {
145148 yield this . check ( ) ;
146- ( _a = this . db . chain ) === null || _a === void 0 ? void 0 : _a . remove ( { key } ) . value ( ) ;
147- yield this . db . write ( ) ;
149+ yield ( ( _a = this . _db ) === null || _a === void 0 ? void 0 : _a . get ( this . _baseName ) . remove ( { key } ) . write ( ) ) ;
148150 return null ;
149151 } ) ;
150152 }
@@ -158,9 +160,9 @@ export class OfflineDB {
158160 var _a ;
159161 return __awaiter ( this , void 0 , void 0 , function * ( ) {
160162 yield this . check ( ) ;
161- ( _a = this . db . chain ) === null || _a === void 0 ? void 0 : _a . update ( key , updates ) . value ( ) ;
162- yield this . db . write ( ) ;
163+ yield ( ( _a = this . _db ) === null || _a === void 0 ? void 0 : _a . get ( this . _baseName ) . update ( key , updates ) . write ( ) ) ;
163164 return null ;
164165 } ) ;
165166 }
166167}
168+ exports . OfflineDB = OfflineDB ;
0 commit comments