Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: set createdAt timestamp field value on collection insert if not exists #303

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/__tests__/tigris.rpc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,25 @@ describe("rpc tests", () => {
});
insertionPromise.then((insertedBook) => {
expect(insertedBook.id).toBe(1);
expect(insertedBook.createdAt).toBeDefined();
});
return insertionPromise;
});

it("insert_with_createdAt_value", async () => {
const tigris = new Tigris({ ...testConfig, projectName: "db3" });
const db1 = tigris.getDatabase();
const book: IBook = {
author: "author name",
id: 0,
tags: ["science"],
title: "science book",
createdAt: new Date(),
}
const insertionPromise = db1.getCollection<IBook>("books").insertOne(book);
insertionPromise.then((insertedBook) => {
expect(insertedBook.id).toBe(1);
expect(insertedBook.createdAt).toEqual(book.createdAt);
});
return insertionPromise;
});
Expand Down Expand Up @@ -915,6 +934,8 @@ export class IBook implements TigrisCollectionType {
author: string;
@Field({ elements: TigrisDataTypes.STRING })
tags?: string[];
@Field(TigrisDataTypes.DATE_TIME, { timestamp: "createdAt" })
createdAt?: Date;
}

export interface IBook1 extends TigrisCollectionType {
Expand Down
37 changes: 36 additions & 1 deletion src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ import { Cursor, ReadCursorInitializer } from "./consumables/cursor";
import { SearchIterator, SearchIteratorInitializer } from "./consumables/search-iterator";
import { SearchQuery } from "./search";
import { SearchResult } from "./search";
import { DecoratorMetaStorage } from "./decorators/metadata/decorator-meta-storage";
import { getDecoratorMetaStorage } from "./globals";

interface ICollection {
readonly collectionName: string;
Expand All @@ -49,6 +51,8 @@ export class Collection<T extends TigrisCollectionType> implements ICollection {
readonly branch: string;
readonly grpcClient: TigrisClient;
readonly config: TigrisClientConfig;
private readonly _metadataStorage: DecoratorMetaStorage;


constructor(
collectionName: string,
Expand All @@ -61,6 +65,7 @@ export class Collection<T extends TigrisCollectionType> implements ICollection {
this.db = db;
this.branch = branch;
this.grpcClient = grpcClient;
this._metadataStorage = getDecoratorMetaStorage();
this.config = config;
}

Expand Down Expand Up @@ -90,7 +95,12 @@ export class Collection<T extends TigrisCollectionType> implements ICollection {
if (error) {
reject(error);
} else {
const clonedDocs = this.setDocsMetadata(docs, response.getKeysList_asU8());
let clonedDocs: Array<T>;
clonedDocs = this.setDocsMetadata(docs, response.getKeysList_asU8());
if (response.getMetadata().hasCreatedAt()) {
clonedDocs = this.setCreatedAtForDocsIfNotExists(clonedDocs,
new Date(response.getMetadata()?.getCreatedAt()?.getSeconds() * 1000));
}
resolve(clonedDocs);
}
}
Expand Down Expand Up @@ -725,4 +735,29 @@ export class Collection<T extends TigrisCollectionType> implements ICollection {

return clonedDocs;
}

private setCreatedAtForDocsIfNotExists(docs: Array<T>, createdAt: Date): Array<T> {
const collectionTarget = this._metadataStorage.collections.get(this.collectionName)?.target;
const collectionFields = this._metadataStorage.getCollectionFieldsByTarget(collectionTarget);

const collectionCreatedAtFieldNames = collectionFields.filter((field) => {
return field?.schemaFieldOptions?.timestamp === "createdAt";
}).map(f => f.name);

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for addressing the issue @akhill10 . We can cache the collectionCreatedAtFieldNames at instantiation in constructor. That will significantly improve the performance. We will be good to merge this then.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good suggestion. We can tackle that in a separate PR.


const clonedDocs: T[] = Object.assign([], docs);
let docIndex = 0;

for (const doc of docs) {

collectionCreatedAtFieldNames.map((fieldName) => {
if (!Reflect.has(doc, fieldName)) {
Reflect.set(clonedDocs[docIndex], fieldName, createdAt);
}
});
docIndex++;
}

return clonedDocs;
}
}