-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix DB issues with object keys (#1125)
Fix DB issues with object keys
- Loading branch information
Showing
28 changed files
with
971 additions
and
504 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
import { Collection } from 'lokijs'; | ||
import { App } from '@flogo-web/core'; | ||
|
||
export const collections: { | ||
apps?: Collection<App>; | ||
} = {}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { initDb, persistedDb } from './db'; | ||
|
||
let collection: Collection<any>; | ||
|
||
beforeEach(async () => { | ||
await initDb({ persist: false }); | ||
collection = persistedDb.addCollection('test'); | ||
}); | ||
|
||
describe('db library integration', () => { | ||
it('should save object with dots in its keys', function() { | ||
const inserted = collection.insert({ | ||
'my.object': { | ||
'is.nested': { | ||
foo: [ | ||
{ | ||
'bar.baz': true, | ||
}, | ||
], | ||
}, | ||
}, | ||
}); | ||
|
||
expect(inserted).toMatchObject({ | ||
'my.object': { | ||
'is.nested': { | ||
foo: [ | ||
{ | ||
'bar.baz': true, | ||
}, | ||
], | ||
}, | ||
}, | ||
}); | ||
|
||
expect(collection.get(inserted.$loki)).toMatchObject({ | ||
'my.object': { | ||
'is.nested': { | ||
foo: [ | ||
{ | ||
'bar.baz': true, | ||
}, | ||
], | ||
}, | ||
}, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import Loki from 'lokijs'; | ||
|
||
import { App } from '@flogo-web/core'; | ||
import { config } from '../../config'; | ||
import { collections } from './collections-registry'; | ||
|
||
const dbPath = config.apps.dbPath; | ||
|
||
export let persistedDb: Loki; | ||
// todo: use by non-persistent collections like contributions | ||
const memoryDb = new Loki('mem.db', { adapter: new Loki.LokiMemoryAdapter() }); | ||
|
||
export function initDb({ persist = true, autosave = true } = {}) { | ||
return new Promise(resolve => { | ||
persistedDb = new Loki(dbPath, { | ||
adapter: persist ? new Loki.LokiFsAdapter() : new Loki.LokiMemoryAdapter(), | ||
autoload: true, | ||
autosave, | ||
autoloadCallback: afterInitDb(resolve), | ||
autosaveInterval: 4000, | ||
}); | ||
}); | ||
} | ||
|
||
export function flushAndCloseDb() { | ||
if (persistedDb) { | ||
return new Promise((resolve, reject) => { | ||
persistedDb.save(err => { | ||
if (err) { | ||
return reject(err); | ||
} | ||
|
||
persistedDb.close(err2 => { | ||
if (err2) { | ||
return reject(err2); | ||
} | ||
resolve(); | ||
}); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
function afterInitDb(signalReadyFn: Function) { | ||
return () => { | ||
let apps = persistedDb.getCollection<App>('apps'); | ||
if (apps == null) { | ||
apps = persistedDb.addCollection('apps', { | ||
unique: ['id'], | ||
indices: ['name'], | ||
clone: true, | ||
}); | ||
} | ||
collections.apps = apps; | ||
signalReadyFn(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
export { apps } from './apps'; | ||
export { indexer } from './indexer'; | ||
export { contributionsDBService } from './contributions'; | ||
|
||
export { initDb, flushAndCloseDb } from './db'; | ||
export { collections } from './collections-registry'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,9 @@ | ||
import { ContainerModule, interfaces } from 'inversify'; | ||
import { apps, indexer } from '../../common/db'; | ||
import { collections } from '../../common/db'; | ||
import { indexer } from '../../common/db/indexer'; | ||
import { TOKENS } from '../../core'; | ||
|
||
export const PersistenceModule = new ContainerModule((bind: interfaces.Bind) => { | ||
bind(TOKENS.AppsDb).toConstantValue(apps); | ||
bind(TOKENS.AppsDb).toDynamicValue(() => collections.apps); | ||
bind(TOKENS.ResourceIndexerDb).toConstantValue(indexer); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.