-
Notifications
You must be signed in to change notification settings - Fork 53
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Bero
committed
Feb 20, 2024
1 parent
78e6353
commit 0795d44
Showing
10 changed files
with
604 additions
and
340 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,88 +1,109 @@ | ||
import { Mongo } from 'meteor/mongo'; | ||
import { LINK_STORAGE } from './constants.js'; | ||
import Linker from './linker.js'; | ||
import { _ } from 'meteor/underscore'; | ||
|
||
Object.assign(Mongo.Collection.prototype, { | ||
/** | ||
* The data we add should be valid for config.schema.js | ||
*/ | ||
addLinks(data) { | ||
if (!this[LINK_STORAGE]) { | ||
this[LINK_STORAGE] = {}; | ||
} | ||
/** | ||
* The data we add should be valid for config.schema.js | ||
* | ||
* @this {Mongo.Collection<unknown>} | ||
* @param {Object.<string, Grapher.LinkConfig>} data | ||
* | ||
*/ | ||
addLinks(data) { | ||
if (!this[LINK_STORAGE]) { | ||
this[LINK_STORAGE] = {}; | ||
} | ||
|
||
_.each(data, (linkConfig, linkName) => { | ||
if (this[LINK_STORAGE][linkName]) { | ||
throw new Meteor.Error( | ||
`You cannot add the link with name: ${linkName} because it was already added to ${ | ||
this._name | ||
} collection` | ||
); | ||
} | ||
_.each(data, (linkConfig, linkName) => { | ||
if (this[LINK_STORAGE][linkName]) { | ||
throw new Meteor.Error( | ||
`You cannot add the link with name: ${linkName} because it was already added to ${this._name} collection`, | ||
); | ||
} | ||
|
||
const linker = new Linker(this, linkName, linkConfig); | ||
const linker = new Linker(this, linkName, linkConfig); | ||
|
||
_.extend(this[LINK_STORAGE], { | ||
[linkName]: linker, | ||
}); | ||
}); | ||
}, | ||
_.extend(this[LINK_STORAGE], { | ||
[linkName]: linker, | ||
}); | ||
}); | ||
}, | ||
|
||
getLinks() { | ||
return this[LINK_STORAGE]; | ||
}, | ||
/** | ||
* @this {Mongo.Collection<unknown>} | ||
* @returns | ||
*/ | ||
getLinks() { | ||
return this[LINK_STORAGE]; | ||
}, | ||
|
||
getLinker(name) { | ||
if (this[LINK_STORAGE]) { | ||
return this[LINK_STORAGE][name]; | ||
} | ||
}, | ||
/** | ||
* @this {Mongo.Collection<unknown>} | ||
* @param {string} name | ||
* @returns | ||
*/ | ||
getLinker(name) { | ||
if (this[LINK_STORAGE]) { | ||
return this[LINK_STORAGE][name]; | ||
} | ||
}, | ||
|
||
hasLink(name) { | ||
if (!this[LINK_STORAGE]) { | ||
return false; | ||
} | ||
/** | ||
* @this {Mongo.Collection<unknown>} | ||
* @param {string} name | ||
* @returns {boolean} | ||
*/ | ||
hasLink(name) { | ||
if (!this[LINK_STORAGE]) { | ||
return false; | ||
} | ||
|
||
return !!this[LINK_STORAGE][name]; | ||
}, | ||
return !!this[LINK_STORAGE][name]; | ||
}, | ||
|
||
getLink(objectOrId, name) { | ||
let linkData = this[LINK_STORAGE]; | ||
/** | ||
* | ||
* @this {Mongo.Collection<unknown>} | ||
* @param {unknown} objectOrId | ||
* @param {string} name | ||
* @returns | ||
*/ | ||
async getLink(objectOrId, name) { | ||
let linkData = this[LINK_STORAGE]; | ||
|
||
if (!linkData) { | ||
throw new Meteor.Error( | ||
`There are no links defined for collection: ${this._name}` | ||
); | ||
} | ||
if (!linkData) { | ||
throw new Meteor.Error( | ||
`There are no links defined for collection: ${this._name}`, | ||
); | ||
} | ||
|
||
if (!linkData[name]) { | ||
throw new Meteor.Error( | ||
`There is no link ${name} for collection: ${this._name}` | ||
); | ||
} | ||
if (!linkData[name]) { | ||
throw new Meteor.Error( | ||
`There is no link ${name} for collection: ${this._name}`, | ||
); | ||
} | ||
|
||
const linker = linkData[name]; | ||
let object = objectOrId; | ||
if (typeof objectOrId == 'string') { | ||
if (!linker.isVirtual()) { | ||
object = this.findOne(objectOrId, { | ||
fields: { | ||
[linker.linkStorageField]: 1, | ||
}, | ||
}); | ||
} else { | ||
object = { _id: objectOrId }; | ||
} | ||
const linker = linkData[name]; | ||
let object = objectOrId; | ||
if (typeof objectOrId == 'string') { | ||
if (!linker.isVirtual()) { | ||
object = await this.findOneAsync(objectOrId, { | ||
fields: { | ||
[linker.linkStorageField]: 1, | ||
}, | ||
}); | ||
} else { | ||
object = { _id: objectOrId }; | ||
} | ||
|
||
if (!object) { | ||
throw new Meteor.Error( | ||
`We could not find any object with _id: "${objectOrId}" within the collection: ${ | ||
this._name | ||
}` | ||
); | ||
} | ||
} | ||
if (!object) { | ||
throw new Meteor.Error( | ||
`We could not find any object with _id: "${objectOrId}" within the collection: ${this._name}`, | ||
); | ||
} | ||
} | ||
|
||
return linkData[name].createLink(object); | ||
}, | ||
return linkData[name].createLink(object); | ||
}, | ||
}); |
Oops, something went wrong.