Skip to content

Commit

Permalink
Finished links tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Bero committed Feb 20, 2024
1 parent 78e6353 commit 0795d44
Show file tree
Hide file tree
Showing 10 changed files with 604 additions and 340 deletions.
48 changes: 43 additions & 5 deletions @types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ declare module 'meteor/cultofcoders:grapher' {

// Check: lib/links/config.schema.js:LinkConfigSchema
export type LinkConfig = {
type: LinkConfigType;
// not needed for inversed links
type?: LinkConfigType;

// Looks like intention is to support other collections, not just mongodb
collection:
Expand Down Expand Up @@ -39,22 +40,35 @@ declare module 'meteor/cultofcoders:grapher' {
linkConfig: LinkConfig,
);

createLink(): Grapher.LinkBaseClass;
linkStorageField: string | undefined;
linkConfig: LinkConfig;
mainCollection: Mongo.Collection<T, U>;

createLink(
object: unknown,
collection?: Mongo.Collection<unknown> | null,
): LinkBaseClass;

isVirtual(): boolean;
isSingle(): boolean;
}

export type DefaultFiltersWithMeta<T> = Mongo.Selector<T> & {
$meta?: unknown;
};

export type LinkObject = Record<string, unknown>;

export class LinkBaseClass {
constructor<T, U = T>(
linker: LinkerClass,
object: unknown,
object: LinkObject,
collection: Mongo.Collection<T, U>,
);

get config(): LinkConfig; // processed
get isVirtual(): boolean;
object: LinkObject;

// Stored link information value
value(): unknown;
Expand All @@ -80,9 +94,28 @@ declare module 'meteor/cultofcoders:grapher' {
add(what: unknown): Promise<this>;
remove(what: unknown): Promise<this>;
set(what: unknown, metadata?): Promise<this>;
unset(): Promise<this>;
metadata(): Promise<this>;

clean(): void;
}

type BaseDocumentShape = {
_id?: string;
};

type IdSingleOption = string | BaseDocumentShape;

type IdOption = IdSingleOption | IdSingleOption[];

type SmartArgumentsOptions =
| {
saveToDatabase: true;
collection: Mongo.Collection<unknown>;
}
| {
saveToDatabase?: false;
};
}

namespace Grapher {
Expand All @@ -91,8 +124,13 @@ namespace Grapher {

namespace Mongo {
interface Collection<T, U = T> {
addLinks: (links: Record<string, LinkConfig>) => void;
__links: Record<string, Grapher.LinkerClass>;

addLinks(links: Record<string, Grapher.LinkConfig>): void;
getLinker(name: string): Grapher.LinkerClass | undefined;
getLink(doc: unknown, name: string): Grapher.LinkBaseClass | undefined;
getLink(
doc: unknown,
name: string,
): Promise<Grapher.LinkBaseClass | undefined>;
}
}
161 changes: 91 additions & 70 deletions lib/links/extension.js
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);
},
});
Loading

0 comments on commit 0795d44

Please sign in to comment.