Skip to content

Commit

Permalink
docs(README): add new point to note, update interfaces, update strain…
Browse files Browse the repository at this point in the history
… test
  • Loading branch information
mernxl committed May 11, 2019
1 parent af56d13 commit 7b7eb9c
Showing 1 changed file with 53 additions and 30 deletions.
83 changes: 53 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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',
Expand All @@ -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;
}
Expand All @@ -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,
Expand All @@ -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

Expand Down Expand Up @@ -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);

Expand Down

0 comments on commit 7b7eb9c

Please sign in to comment.