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

Multiple connections #1

Open
andershagebakken opened this issue Dec 10, 2018 · 1 comment
Open

Multiple connections #1

andershagebakken opened this issue Dec 10, 2018 · 1 comment

Comments

@andershagebakken
Copy link

Hello. So far, your work looks great. I have one question. Can this be used with multiple connection, if yes, how? Today I do: const Model = db.model('Model', ModelSchema);. Can I do this using your project as well? Thanks.

@lstkz
Copy link
Owner

lstkz commented Dec 11, 2018

Hi!

You can always create your own wrapper.

function typedModel<T extends Schema>(
  name: string,
  schema?: T,
  collection?: string,
  skipInit?: boolean
): Model<Document & Extract<T>> {
  return db.model(name, schema, collection, skipInit) as any;
}
//-------^ add `db.` prefix here

// then

const Model = typedModel('Model', ModelSchema)

or

function typedModel<T extends Schema>(
  model: typeof Model, // <- extra param
  name: string,
  schema?: T,
  collection?: string,
  skipInit?: boolean
): Model<Document & Extract<T>> {
  return model(name, schema, collection, skipInit) as any;
}


// then

const Model = typedModel(db.model, 'Model', ModelSchema)

If you don't need 3rd and 4th args, you can remove them.

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

2 participants