Skip to content

Commit

Permalink
add ability to create indexes (#5)
Browse files Browse the repository at this point in the history
* update

* add logging

* update readme

* run indexes on connection

* indexes have been run

* consistency in private createIndexes

* add comment

* remove readme change

* remove catch block

* fix duplicate calls

* fix options

* await save correctly in super class

* update error message for duplicate key

* remove useless semicolon

* fix app error

* Removing double imports (#18)

Co-authored-by: Marcus Veloso <[email protected]>
  • Loading branch information
quesurifn and velosomarcus authored Dec 6, 2022
1 parent 6a120d8 commit 8a73424
Show file tree
Hide file tree
Showing 9 changed files with 62 additions and 21 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -121,5 +121,3 @@ yarn demo
[downloads-url]: https://npmjs.org/package/@module-federation/aegis
[gitpod-image]: https://img.shields.io/badge/Gitpod-ready--to--code-908a85?logo=gitpod
[gitpod-url]: https://gitpod.io/github.com/module-federation/aegis


Binary file modified forkrun
Binary file not shown.
4 changes: 2 additions & 2 deletions src/adapters/datasources/datasource-file.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ export class DataSourceFile extends DataSource {
/**
* @param {Set} map
*/
constructor (map, name, options = {}) {
super(map, name, options)
constructor (map, name, namespace, options = {}) {
super(map, name, namespace, options)
this.file = this.getFilePath()
this.className = DataSourceFile.name
}
Expand Down
4 changes: 2 additions & 2 deletions src/adapters/datasources/datasource-meshlink.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ async function fetchSharedObject (name) {
}

export class DataSourceMeshLink extends DataSource {
constructor (dataSource, name, options = {}) {
super(dataSource, name, options)
constructor (map, name, namespace, options = {}) {
super(map, name, namespace, options)
}

/**
Expand Down
43 changes: 32 additions & 11 deletions src/adapters/datasources/datasource-mongodb.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use strict'

import DataSource from '../../domain/datasource'
import { ObjectId, MongoClient } from 'mongodb';
import { Transform, Writable } from 'stream';
import qpm from 'query-params-mongo';
import DataSource from '../../domain/datasource';
import { DataSourceMemory } from './datasource-memory'

const HIGHWATERMARK = 50

const mongodb = require('mongodb')
const { MongoClient } = mongodb
const { Transform, Writable } = require("stream")
const qpm = require("query-params-mongo")
const processQuery = qpm({
autoDetect: [
{ valuePattern: /^null$/i, dataType: 'nullstring' }
Expand All @@ -19,11 +19,13 @@ const processQuery = qpm({

const url = process.env.MONGODB_URL || "mongodb://localhost:27017"
const configRoot = require("../../config").hostConfig

const dsOptions = configRoot.adapters.datasources.DataSourceMongoDb.options || {
runOffline: true,
runOffline: false,
numConns: 2
}
const cacheSize = configRoot.adapters.cacheSize || 3000

const cacheSize = configRoot.adapters.cacheSize || 3000;

/**
* @type {Map<string,MongoClient>}
Expand All @@ -45,6 +47,7 @@ export class DataSourceMongoDb extends DataSource {
super(map, name, namespace, options)
this.cacheSize = cacheSize
this.mongoOpts = mongoOpts
this.options = options
this.runOffline = dsOptions.runOffline
this.url = url
}
Expand All @@ -61,6 +64,13 @@ export class DataSourceMongoDb extends DataSource {
}
const client = connections.shift()
connections.push(client)

if(!this.options?.connOpts?.indexesHaveBeenRun && this.options?.connOpts?.indexes) {
console.info(`running indexes for datasource ${this.name} with index values`, this.options.connOpts.indexes)
await this.createIndexes(client)
this.options.connOpts.indexesHaveBeenRun = true;
}

return client
} catch (error) {
console.error({ fn: this.connection.name, error })
Expand All @@ -71,11 +81,23 @@ export class DataSourceMongoDb extends DataSource {
return (await this.connection()).db(this.namespace).collection(this.name)
}

async createIndexes(client) {
const indexOperations = this.options.connOpts.indexes.map((index) => {
return {
name: index.fields.join("_"),
key: index.fields.reduce((a, v) => ({ ...a, [v]: 1 }), {}),
...index.options,
}
});

return await client.db(this.namespace).collection(this.name).createIndexes(indexOperations);
}

async find (id) {
try {
return (await this.collection()).findOne({ _id: id })
} catch (error) {
console.error({ fn: this.findDb.name, error })
console.error({ fn: this.find.name, error })
}
}

Expand All @@ -91,12 +113,11 @@ export class DataSourceMongoDb extends DataSource {
*/
async save (id, data) {
try {
const col = await this.collection()
col.replaceOne({ _id: id }, { ...data, _id: id }, { upsert: true })
await (await this.collection()).replaceOne({ _id: id }, { ...data, _id: id }, { upsert: true })
} catch (error) {
// default is
if (!this.runOffline) {
throw new Error('db trans failed,', error)
throw new Error(`DB Transaction failed: ${error.message}`)
}
// run while db is down - cache will be ahead
console.error('db trans failed, sync it later', error)
Expand Down
12 changes: 10 additions & 2 deletions src/domain/datasource-factory.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ const DefaultDataSource =
*/
const DsCoreExtensions = superclass =>
class extends superclass {
constructor(map, name, namespace, options) {
super(map, name, namespace, options)
}

set factory (value) {
this[FACTORY] = value
}
Expand Down Expand Up @@ -64,6 +68,7 @@ const DsCoreExtensions = superclass =>
console.error({ fn: this.save.name, error })
throw error
}

}

/**
Expand Down Expand Up @@ -210,7 +215,7 @@ const DataSourceFactory = (() => {

if (adapterName) return adapters[adapterName] || DefaultDataSource

if (spec?.datasource?.adapterFactory) {
if (spec?.datasource?.factory) {
const url = spec.datasource.url
const cacheSize = spec.datasource.cacheSize
const adapterFactory = spec.datasource.factory
Expand Down Expand Up @@ -249,9 +254,12 @@ const DataSourceFactory = (() => {
const DsClass = createDataSourceClass(spec, options)
const DsExtendedClass = extendDataSourceClass(DsClass, options)

if(spec.datasource) {
options = {...options, connOpts: {...spec.datasource}}
}

const newDs = new DsExtendedClass(dsMap, name, namespace, options)
newDs.factory = this // setter to avoid exposing in ctor

if (!options.ephemeral) dataSources.set(name, newDs)

debug && console.debug({ newDs })
Expand Down
10 changes: 10 additions & 0 deletions src/domain/datasource.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,16 @@ export default class DataSource {
return this.dsMap.set(id, data)
}

/**
* Create indexes as defined in the datasource object in the model
* @param {Array<object>}
* @returns {Promise<boolean>} result
*/

async createIndexes(indexes) {
throw new Error('abstract method not implemented')
}

/**
* Find model instance by ID
* @param {*} id record id
Expand Down
4 changes: 2 additions & 2 deletions src/domain/shared-memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ const dataType = {
*/
const SharedMemoryMixin = superclass =>
class extends superclass {
constructor (map, name, options) {
super(map, name, options)
constructor (map, name, namespace, options) {
super(map, name, namespace, options)
}

/**
Expand Down
4 changes: 4 additions & 0 deletions src/domain/util/app-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@
* @returns
*/
export function AppError (error, code = 400, cause = null) {
if(error.code > 500) {
error.code = 400
}

return {
name: error.name,
stack: error.stack,
Expand Down

0 comments on commit 8a73424

Please sign in to comment.