Skip to content

Commit

Permalink
feat(schema): allow defining array of aliases in schema as well
Browse files Browse the repository at this point in the history
Fix #12368
  • Loading branch information
vkarpov15 committed Oct 3, 2022
1 parent bfa835f commit 0e8e9c4
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 16 deletions.
28 changes: 28 additions & 0 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,34 @@ function aliasFields(schema, paths) {

const prop = schema.paths[path].path;

if (Array.isArray(alias)) {
for (const a of alias) {
if (typeof a !== 'string') {
throw new Error('Invalid value for alias option on ' + prop + ', got ' + a);
}

schema.aliases[a] = prop;

schema.
virtual(a).
get((function(p) {
return function() {
if (typeof this.get === 'function') {
return this.get(p);
}
return this[p];
};
})(prop)).
set((function(p) {
return function(v) {
return this.$set(p, v);
};
})(prop));
}

continue;
}

if (typeof alias !== 'string') {
throw new Error('Invalid value for alias option on ' + prop + ', got ' + alias);
}
Expand Down
30 changes: 30 additions & 0 deletions test/schema.alias.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,34 @@ describe('schema alias option', function() {
done();
// acquit:ignore:end
});

it('array of aliases (gh-12368)', function() {
const productSchema = new Schema({
n: {
type: String,
alias: ['name', 'product_name']
}
});

const Product = db.model('Test', productSchema);
const doc = new Product({});

doc['product_name'] = 'Turbo Man';
assert.equal(doc.n, 'Turbo Man');
assert.equal(doc.name, 'Turbo Man');
});

it('alias() method (gh-12368)', function() {
const schema = new Schema({ name: String });

schema.alias('name', 'otherName');
assert.equal(schema.aliases['otherName'], 'name');
assert.ok(schema.virtuals['otherName']);

schema.alias('name', ['name1', 'name2']);
assert.equal(schema.aliases['name1'], 'name');
assert.equal(schema.aliases['name2'], 'name');
assert.ok(schema.virtuals['name1']);
assert.ok(schema.virtuals['name2']);
});
});
15 changes: 0 additions & 15 deletions test/schema.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -2868,21 +2868,6 @@ describe('schema', function() {
assert.equal(doc1.domain, doc2.domain);
});


it('alias (gh-12368)', function() {
const schema = new Schema({ name: String });

schema.alias('name', 'otherName');
assert.equal(schema.aliases['otherName'], 'name');
assert.ok(schema.virtuals['otherName']);

schema.alias('name', ['name1', 'name2']);
assert.equal(schema.aliases['name1'], 'name');
assert.equal(schema.aliases['name2'], 'name');
assert.ok(schema.virtuals['name1']);
assert.ok(schema.virtuals['name2']);
});

it('allows defining ObjectIds and Decimal128s using Types.* (gh-12205)', function() {
const schema = new Schema({
testId: mongoose.Types.ObjectId,
Expand Down
6 changes: 6 additions & 0 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,12 @@ declare module 'mongoose' {
/** Adds key path / schema type pairs to this schema. */
add(obj: SchemaDefinition<SchemaDefinitionType<EnforcedDocType>> | Schema, prefix?: string): this;

/**
* Add an alias for `path`. This means getting or setting the `alias`
* is equivalent to getting or setting the `path`.
*/
alias(path: string, alias: string | string[]): this;

/**
* Array of child schemas (from document arrays and single nested subdocs)
* and their corresponding compiled models. Each element of the array is
Expand Down
2 changes: 1 addition & 1 deletion types/schematypes.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ declare module 'mongoose' {
T | typeof SchemaType | Schema<any, any, any> | SchemaDefinition<T> | Function | AnyArray<Function>;

/** Defines a virtual with the given name that gets/sets this path. */
alias?: string;
alias?: string | string[];

/** Function or object describing how to validate this schematype. See [validation docs](https://mongoosejs.com/docs/validation.html). */
validate?: SchemaValidator<T> | AnyArray<SchemaValidator<T>>;
Expand Down

0 comments on commit 0e8e9c4

Please sign in to comment.