Skip to content

Commit

Permalink
feat: implement method to create a record link token
Browse files Browse the repository at this point in the history
  • Loading branch information
IamSebastianDev committed Feb 26, 2023
1 parent e55ac17 commit c455787
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export {
ValidateNested,
Contains,
IsString,
Link,
} from './lib';
export type { Collection, ObjectId, JSONDocument } from './lib';
export type {
Expand All @@ -44,4 +45,5 @@ export type {
Subscription,
Subscriber,
MapFunction,
RecordLink,
} from './types';
24 changes: 24 additions & 0 deletions src/lib/Db/Link.ts
Original file line number Diff line number Diff line change
@@ -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<Record<PropertyKey, unknown>>, recordId: ObjectId): RecordLink => {
let namespace = collection;

if (collection instanceof Collection && 'namespace' in collection) {
namespace = collection.namespace;
}

return `${namespace}:${recordId.valueOf()}`;
};
1 change: 1 addition & 0 deletions src/lib/Db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export { Collection } from './Collection';
export { ObjectId } from './ObjectId';
export { JSONDocument } from './JSONDocument';
export { Observable } from './Observable';
export { Link } from './Link';
2 changes: 1 addition & 1 deletion src/lib/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/** @format */

export { Flotsam, Collection, ObjectId, JSONDocument } from './Db';
export { Flotsam, Collection, ObjectId, JSONDocument, Link } from './Db';
export {
Exactly,
GreaterThan,
Expand Down
3 changes: 3 additions & 0 deletions src/types/RecordLink.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/** @format */

export type RecordLink = `${string}:${string}`;
1 change: 1 addition & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

0 comments on commit c455787

Please sign in to comment.