1.1.3 • Published 1 year ago

@ederzadravec/fastify-mongoose v1.1.3

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

Fastify MongoDB Plugin using Mongoose and typescript

Installation

yarn add @ederzadravec/fastify-mongoose

Usage

// ...Other Plugins
fastify.register(
  require("fastify-mongoose-driver").plugin,
  {
    uri: "mongodb://admin:pass@localhost:27017/database_name",
    settings: {
      useNewUrlParser: true,
      config: {
        autoIndex: true,
      },
    },
    models: [
      {
        name: "posts",
        alias: "Post",
        schema: {
          title: {
            type: String,
            required: true,
          },
          content: {
            type: String,
            required: true,
          },
          author: {
            type: "ObjectId",
            ref: "Account",
          },
        },
      },
      {
        name: "accounts",
        alias: "Account",
        schema: {
          username: {
            type: String,
          },
          password: {
            type: String,
            select: false,
            required: true,
          },
          email: {
            type: String,
            unique: true,
            required: true,
          },
          createdAtUTC: {
            type: Date,
            required: true,
          },
        },
        virtualize: (schema) => {
          schema.virtual("posts", {
            ref: "Post",
            localKey: "_id",
            foreignKey: "author",
          });
        },
      },
    ],
    useNameAndAlias: true,
  },
  (err) => {
    if (err) throw err;
  }
);

fastify.get("/", (request, reply) => {
  console.log(fastify.mongoose.instance); // Mongoose ODM instance
  console.log(fastify.mongoose.Account); // Any models declared are available here
});

Options

OptionDescription
uriRequired, the Unique Resource Identifier to use when connecting to the Database.
settingsOptional, the settings to be passed on to the MongoDB Driver as well as the Mongoose-specific options. Refer here for further info.
modelsOptional, any models to be declared and injected under fastify.mongoose
useNameAndAliasOptional, declares models using mongoose.model(alias, schema, name) instead of mongoose.model(name, schema)

Any models declared should follow the following format:

{
  name: "profiles", // Required, should match name of model in database
  alias: "Profile", // Optional, an alias to inject the model as
  schema: schemaDefinition // Required, should match schema of model in database,
  class: classDefinition // Optional, should be an ES6 class wrapper for the model
}

The schemaDefinition variable should be created according to the Mongoose Model Specification. The classDefinition variable should be created according to the Mongoose Class Specification.

Refer

this library was based on fastify-mongoose of Alexander Papageorgiou

I rewrote in typescript using your repository as a base

1.1.3

1 year ago

1.1.2

3 years ago

1.1.1

4 years ago

1.1.0

4 years ago

1.0.0

4 years ago