diff --git a/src/index.ts b/src/index.ts index 3840c7e..1987d85 100644 --- a/src/index.ts +++ b/src/index.ts @@ -23,6 +23,7 @@ export { ValidateNested, Contains, IsString, + Link, } from './lib'; export type { Collection, ObjectId, JSONDocument } from './lib'; export type { @@ -44,4 +45,5 @@ export type { Subscription, Subscriber, MapFunction, + RecordLink, } from './types'; diff --git a/src/lib/Db/Link.ts b/src/lib/Db/Link.ts new file mode 100644 index 0000000..a5159fa --- /dev/null +++ b/src/lib/Db/Link.ts @@ -0,0 +1,24 @@ +/** @format */ + +import { RecordLink } from '../../types'; +import { Collection } from './Collection'; +import { ObjectId } from './ObjectId'; + +/** + * @description + * Method to create a **Record Link** token from a Collection and an Id. + * + * @param { string | Collection } collection - the namespace of a Collection or a Collection instance + * @param { ObjectId } recordId + * @returns + */ + +export const Link = (collection: string | Collection>, recordId: ObjectId): RecordLink => { + let namespace = collection; + + if (collection instanceof Collection && 'namespace' in collection) { + namespace = collection.namespace; + } + + return `${namespace}:${recordId.valueOf()}`; +}; diff --git a/src/lib/Db/index.ts b/src/lib/Db/index.ts index 55cb655..e09baa1 100644 --- a/src/lib/Db/index.ts +++ b/src/lib/Db/index.ts @@ -5,3 +5,4 @@ export { Collection } from './Collection'; export { ObjectId } from './ObjectId'; export { JSONDocument } from './JSONDocument'; export { Observable } from './Observable'; +export { Link } from './Link'; diff --git a/src/lib/index.ts b/src/lib/index.ts index 23f4321..517a662 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -1,6 +1,6 @@ /** @format */ -export { Flotsam, Collection, ObjectId, JSONDocument } from './Db'; +export { Flotsam, Collection, ObjectId, JSONDocument, Link } from './Db'; export { Exactly, GreaterThan, diff --git a/src/types/RecordLink.d.ts b/src/types/RecordLink.d.ts new file mode 100644 index 0000000..07a640e --- /dev/null +++ b/src/types/RecordLink.d.ts @@ -0,0 +1,3 @@ +/** @format */ + +export type RecordLink = `${string}:${string}`; diff --git a/src/types/index.d.ts b/src/types/index.d.ts index 7edfcf6..2a2094a 100644 --- a/src/types/index.d.ts +++ b/src/types/index.d.ts @@ -27,3 +27,4 @@ export type { Subscriber } from './Subscriber'; export type { MapFunction } from './MapFunction'; export type { Handler } from './Handler'; export type { ObservedQuery } from './ObservedQuery'; +export type { RecordLink } from './RecordLink';