Skip to content

Commit

Permalink
fix: redo IdAssigner initialisation logic
Browse files Browse the repository at this point in the history
  • Loading branch information
mernxl committed Jul 24, 2018
1 parent 08e5b22 commit 0de5e4e
Show file tree
Hide file tree
Showing 6 changed files with 190 additions and 72 deletions.
25 changes: 2 additions & 23 deletions src/MongooseIdAssigner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,14 @@ import { Collection } from 'mongodb';
import { Document, Model, Schema } from 'mongoose';
import { AssignerOptions, FieldConfig } from './assigner.interfaces';
import { localStateStore, SchemaState } from './LocalStateStore';
import {
initialiseOptions,
normaliseOptions,
throwPluginError,
waitPromise,
} from './utils';
import { initialiseOptions, normaliseOptions, throwPluginError } from './utils';
import { refreshOptions } from './utils/assign-fields-ids';
import { configureSchema } from './utils/configure-schema';

export interface NormalisedOptions {
modelName: string;
network: boolean;
timestamp?: number | null;
fields?: Map<string, FieldConfig>;
}

Expand Down Expand Up @@ -53,7 +49,6 @@ export class MongooseIdAssigner extends EventEmitter {
this.options = normaliseOptions(options);
this.modelName = this.options.modelName;
this._saveState();
this._modelNameIndex();
configureSchema(this);
}

Expand Down Expand Up @@ -110,20 +105,4 @@ export class MongooseIdAssigner extends EventEmitter {
idAssigner: this,
});
}

private _modelNameIndex() {
this.on('ready', async () => {
if (!this.options.fields) {
return;
}

try {
await waitPromise(1); // nextTick

await this.collection.createIndex('modelName');
} catch (e) {
throw e;
}
});
}
}
14 changes: 10 additions & 4 deletions src/__mocks__/mongoose.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,18 @@ export function getMongoose() {
return mongoose;
} else {
jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
mongoose.connect(
'mongodb://localhost:27017/test_ia',
{
/*const originalConnect = mongoose.connect;
(mongoose as any).connect = () => {
originalConnect.bind(mongoose)('mongodb://localhost:27017/test_ia', {
useNewUrlParser: true,
},
});
};*/
mongoose.connect(
'mongodb://localhost:27017/demoDB',
{ useNewUrlParser: true },
);

return mongoose;
}
}
30 changes: 30 additions & 0 deletions src/utils/__tests__/initialise-options.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { AssignerOptions, FieldConfigTypes } from '../../assigner.interfaces';
import { checkAndUpdateOptions } from '../initialise-options';
import { normaliseOptions } from '../normalise-options';

describe('initialise-options ->', () => {
const options: AssignerOptions = {
modelName: 'Person',
fields: {
_id: true,
clientId: true,
withUUID: 'UUID',
String: '5555',
Number: 5555,
last: {
type: FieldConfigTypes.Number,
nextId: 5641,
},
},
};

const normalised = normaliseOptions(options);

describe('checkAndUpdateOptions()', () => {
it('should return current option if freshUnAvailable', async () => {
expect(checkAndUpdateOptions(normalised, '' as any).options).toEqual(
normalised,
);
});
});
});
13 changes: 11 additions & 2 deletions src/utils/get-next-ids/get-next-id-number.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { NumberFieldConfig } from '../../assigner.interfaces';
import { MongooseIdAssigner } from '../../MongooseIdAssigner';
import { waitPromise } from '../index';
import { throwPluginError, waitPromise } from '../index';

export async function getNextIdNumber(
field: string,
Expand Down Expand Up @@ -31,7 +31,10 @@ export async function getNextIdNumber(
modelName: idAssigner.modelName,
[updateField]: nextId,
},
{ $set: { [updateField]: afterNextId } },
{
$set: { [updateField]: afterNextId },
$currentDate: { timestamp: true },
},
{ projection: { value: 1 } },
);

Expand All @@ -40,6 +43,12 @@ export async function getNextIdNumber(
await waitPromise(idAssigner.retryMillis * multiplier);
await idAssigner.refreshOptions();
return getNextIdNumber(field, idAssigner, fieldConfig, ++retries);
} else if (!update.value && retries > idAssigner.retryTime) {
throwPluginError(
`Maximum retryTime to set value attained!`,
idAssigner.modelName,
field,
);
}
} catch (e) {
return Promise.reject(e);
Expand Down
13 changes: 11 additions & 2 deletions src/utils/get-next-ids/get-next-id-string.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { StringFieldConfig } from '../../assigner.interfaces';
import { MongooseIdAssigner } from '../../MongooseIdAssigner';
import { waitPromise } from '../index';
import { throwPluginError, waitPromise } from '../index';
import { stringIncrementer } from './utils/string-incrementer';

export async function getNextIdString(
Expand Down Expand Up @@ -31,7 +31,10 @@ export async function getNextIdString(
modelName: idAssigner.modelName,
[updateField]: nextId,
},
{ $set: { [updateField]: afterNextId } },
{
$set: { [updateField]: afterNextId },
$currentDate: { timestamp: true },
},
{ projection: { value: 1 } },
);

Expand All @@ -40,6 +43,12 @@ export async function getNextIdString(
await waitPromise(idAssigner.retryMillis * multiplier);
await idAssigner.refreshOptions();
return getNextIdString(field, idAssigner, fieldConfig, ++retries);
} else if (!update.value && retries > idAssigner.retryTime) {
throwPluginError(
`Maximum retryTime to set value attained!`,
idAssigner.modelName,
field,
);
}
} catch (e) {
return Promise.reject(e);
Expand Down
Loading

0 comments on commit 0de5e4e

Please sign in to comment.