Skip to content

Documents are saved into @empty collection #414

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

Closed
zaksnet opened this issue Feb 29, 2024 · 3 comments
Closed

Documents are saved into @empty collection #414

zaksnet opened this issue Feb 29, 2024 · 3 comments

Comments

@zaksnet
Copy link

zaksnet commented Feb 29, 2024

I am trying to build an adapter for Lucia but the DatabaseSession object is saved in the @empty collection.

public async setSession(databaseSession: DatabaseSession): Promise<void> {
      const session = documentStore.openSession();
      await session.store(databaseSession);
      await session.saveChanges();
}

I also saw this issue but i am already passing the object directly without explicitly defining an id. It is worth noting that the DatabaseSession interface is part of an external library and i have no control over it.

export interface DatabaseSession {
    userId: string;
    expiresAt: Date;
    id: string;
    attributes: RegisteredDatabaseSessionAttributes;
}

At first i though this might be due to the id being generated by Lucia instead of being null, but when i tried this:

public async setSession(databaseSession: DatabaseSession): Promise<void> {
    (databaseSession as any).id = null;
    const session = documentStore.openSession();
    await session.store<DatabaseSession>(databaseSession);
    await session.saveChanges();
}

I still received the same result (I also checked in the debugger to see if the id is null, and it was.).

EDIT: This Worked. The session is saved in RavenDbDatabaseSession collection :

public async setSession(databaseSession: DatabaseSession): Promise<void> {
      var newSession = new RavenDbDatabaseSession(null, databaseSession.userId, databaseSession.expiresAt, databaseSession.attributes);
      const session = documentStore.openSession();
      await session.store(newSession);
      await session.saveChanges();
}
	
class RavenDbDatabaseSession {
    constructor(
          id: string | null,
          userId: string,
          expiresAt: Date,
          attributes: RegisteredDatabaseSessionAttributes
) {
      Object.assign(this, {
	      id,
	      userId,
	      expiresAt,
	      attributes
      });
    }
}

EDIT 2: That seemed to work:

public async setSession(databaseSession: DatabaseSession): Promise<void> {
      const session = documentStore.openSession();
      (databaseSession as any)['@metadata'] = { "@collection": 'Sessions' };
      await session.store<DatabaseSession>(databaseSession);
      await session.saveChanges();
}
@ayende
Copy link
Member

ayende commented Mar 2, 2024

In JS, this is how we detect your entity collection: https://github.com/ravendb/ravendb-nodejs-client/blob/v5.4/src/Documents/Conventions/DocumentConventions.ts#L658

Basically, if you don't have a constructor field (which JS provides for classes), you need to use the findCollectionNameForObjectLiteral on the conventions to figure out what the type is.

@ml054
Copy link
Contributor

ml054 commented Mar 2, 2024

@zaksnet
Copy link
Author

zaksnet commented Mar 6, 2024

Thanks!

@zaksnet zaksnet closed this as completed Mar 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants