From 7b7eb9c668c35222a0a9bb36193d458d79c59e3c Mon Sep 17 00:00:00 2001 From: mernxl Date: Sat, 11 May 2019 23:37:59 +0100 Subject: [PATCH] docs(README): add new point to note, update interfaces, update strain test --- README.md | 83 +++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 53 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index 9229398..2a96fae 100644 --- a/README.md +++ b/README.md @@ -55,11 +55,14 @@ TypeName: **AssignerOptions** and value a Configuration Map for fields on that discriminator that need unique values. Any discriminator without a fieldConfig will use that of the baseModel. -### Point to Note -At every Network Assigner init(i.e Assigner with Number, String FieldConfigTypes), the Assigner(for a Model) refreshes and syncs with the db stored options. Take example micro-service cluster, +### Points to Note +- At every Network Assigner init(i.e Assigner with Number, String FieldConfigTypes), the Assigner(for a Model) refreshes and syncs with the db stored options. Take example micro-service cluster, the last app to init always gives most recent field configs, if the db have a field that is not in the most recent field config, it is auto dropped. Therefore always make sure all your micro-service clusters start up with same fieldsConfigs as the last to start rewrites the db and only keeps nextIds. +- Always set FieldConfig Type to reflect schema's path Type, that is if schema Type is `Number`, then use `Number` FieldConfigType. +See `ExampleSchema` below with its associated IdAssigners. + ## Examples Lets create our Mongoose Schema. ```js @@ -231,41 +234,60 @@ get an instance of the Assigner, then use the `getNextId` method. It is async me * If Options does not contain fields(AssignerFieldsConfigMap), * Then setup assigner for _id field, does not use network */ -export interface AssignerOptions { +interface AssignerOptions { fields?: AssignerFieldsConfigMap; discriminators?: DiscriminatorConfigMap; } -export interface AssignerPluginOptions { +interface AssignerPluginOptions { modelName: string; fields?: AssignerFieldsConfigMap; discriminators?: DiscriminatorConfigMap; } /** - * fieldOption = string, then nextId = string, default incrementer, - * fieldOption = number, then nextId = number, incrementBy = 1 - * fieldOption = boolean(true), then fieldType = ObjectId - * fieldOption = GUID | UUID, then use UUID v4 + * fieldConfig = string, then nextId = string, default incrementer, + * fieldConfig = number, then nextId = number, incrementBy = 1 + * fieldConfig = boolean(true), then fieldType = ObjectId + * fieldConfig = GUID | UUID, then use UUID v1 */ interface AssignerFieldsConfigMap { - [fieldName: string]: FieldConfig | string | number | boolean | 'GUID' | 'UUID'; + [fieldName: string]: + | FieldConfig + | string + | number + | boolean + | FieldConfigTypes.GUID + | FieldConfigTypes.UUID; } /** * A map of discriminatorName(modelName) and its own AssignerFieldsConfigMap */ -export interface DiscriminatorConfigMap { +interface DiscriminatorConfigMap { [discriminatorName: string]: AssignerFieldsConfigMap; } -// noSpace, insure we consume all possible values, i.e. we must have 1, 2, 3, 4 -// order doesn't matter but all those keys must be present, no 1, 3, 4, 6 +/** + * + * @property {Boolean} noSpace - noSpace, insure we consume all possible values, i.e. we must have 1, 2, 3, 4 + * order doesn't matter but all those keys must be present, no 1, 3, 4, 6. + * If noSpace is true, then on holdTimeout, that nextId will be use on any newly saving doc, else nextId discarded + * + * @property {Number} maxHold[50] - As there may be performance issues when holding ids, maxHold will be set, + * @property {String} holdTimeout - default timeout string, must be parse-able to number by `ms` plugin + * @property {Number} holdTimeout - default timeout millis, gotten id stays onHold for this length of time + * @property {Boolean} holdTimeout - if true, will always getNextId with default timeout of `1 week` else use getOnly on getNextIds + */ type FieldConfig = { index?: boolean; unique?: boolean; noSpace?: boolean; -} & ( DefaultFieldConfig | StringFieldConfig | NumberFieldConfig | UUIDFieldConfig ); +} & ( + | DefaultFieldConfig + | StringFieldConfig + | NumberFieldConfig + | UUIDFieldConfig); enum FieldConfigTypes { UUID = 'UUID', @@ -275,27 +297,27 @@ enum FieldConfigTypes { ObjectId = 'ObjectId', } -interface DefaultFieldConfig { - type: 'ObjectId'; +export interface DefaultFieldConfig { + type: FieldConfigTypes.ObjectId; } -interface StringFieldConfig { - type: 'String'; - nextId: string; // the id that will be assigned next - separator?: string; // default `-` e.g. 434-344 - nextIdFunction?: (nextId: string) => string; // custom function to generate nextIds +export interface StringFieldConfig { + type: FieldConfigTypes.String; + nextId: string; + separator?: string; + nextIdFunction?: (nextId: string) => string; } -interface NumberFieldConfig { - type: 'Number'; +export interface NumberFieldConfig { + type: FieldConfigTypes.Number; nextId: number; incrementBy?: number; - nextIdFunction?: (nextId: number, incrementBy?: number) => number; // custom function to generate nextIds + nextIdFunction?: (nextId: number, incrementBy?: number) => number; } -interface UUIDFieldConfig { - type: 'UUID' | 'GUID'; - asBinary?: boolean; // default string, if true, saves as Binary +export interface UUIDFieldConfig { + type: FieldConfigTypes.UUID | FieldConfigTypes.GUID; + asBinary?: boolean; // default string version?: 1 | 4; // supports 1 and 4, default 1 versionOptions?: any; } @@ -320,7 +342,9 @@ describe('MongooseIdAssigner', () => { nextId: 'SPEC-7382-4344-3232', separator: '-', }, - uuidFieldString: FieldConfigTypes.UUID, + uuidFieldString: { + type: FieldConfigTypes.UUID, + }, uuidFieldBuffer: { type: FieldConfigTypes.UUID, version: 1, @@ -331,8 +355,7 @@ describe('MongooseIdAssigner', () => { }; try { - const exampleModel = mongoose.model('TestModel', exampleSchema); - + exampleModel = mongoose.model('example8', exampleSchema); const ExampleIA = new MongooseIdAssigner(exampleModel, options); expect(ExampleIA.readyState).toBe(2); // initialising @@ -361,7 +384,7 @@ describe('MongooseIdAssigner', () => { expect(typeof photoId).toBe('number'); expect(typeof emailId).toBe('string'); expect(personId).toMatch(/(SPEC-7382-4344-3)\d+/); - expect(objectIdField).toBeInstanceOf(mongoose.Types.ObjectId); + expect(objectIdField).toBeInstanceOf(Types.ObjectId); expect(typeof uuidFieldString).toBe('string'); expect(uuidFieldBuffer).toBeInstanceOf(Binary);