1.1.4 • Published 4 years ago

@improvising/mongodb-client v1.1.4

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

Standalone MongoDB client

A Typescript Standalone MongoDB client with data model support.

Install

It's highly recommended to run on Unix-like operating system with Nginx.

Just simply run

yarn add @improvising/mongodb-client

or

npm install @improvising/mongodb-client

to install the module.

Initialization

Simply use a mongodb uri to connect to it. Since River follows the best pattern we considered for our scenes, we don't provide any more options for MongoDB connection to avoid deprecations and performance decays.

await useMongoDB('mongodb://[URI]')

Data Model

Typically, there is a concept called ORM (Object-relational Mapping) or ODM (Object-document Mapping). However, the implementation of ORMs or ODMs usually brings performance decays and makes everything complicated. Here, we don't use any relational mapping. Instead, we use TypeScript's language features to implement a Virtual Model. Let's quickly look at an example:

export interface Place extends BaseModel, Timestamps {
  name: string
  description: string
  location: GeoJsonPoint
  owner: Ref<User>
}

export const PlaceModel = useModel<Place>({
  collection: 'place',
  timestamps: true,
  sync: true,
  indexes: [
    [{ owner: 1 }],
    [{ 'location': '2dsphere' }],
  ],
})

const asyncFunc = async () => {
  await PlaceModel.create({
    name: 'New Place',
    description: 'Something',
    location: {
      type: 'Point',
      coordinates: [0, 0],
    },
    owner: /* An user model's ObjectId */,
  })

  const places = await PlaceModel.find({}).toArray()

  await PlaceModel.populate(places, [
    {
      model: UserModel,
      path: 'owner',
      select: ['nickname', 'avatarUrl'],
    },
  ])

  console.log(places)
}

asyncFunc()

Basically, the query functions are just the same syntax as Node.js MongoDB driver (Because we built River on the top of it). Our useModel function will create a virtual model for you. The virtual model will automatically create the indexes and handle timestamps for your model. In this case, PlaceModel.populate will help you to join references based on BSON ObjectIDs. The usage of this populate function is similar to the one in Mongoose.

How to feedback

If you have any question about it, you can just open an issue, create a pull request, or send an email to opensource@improvising.io for further discussions.

1.1.4

4 years ago

1.1.3

4 years ago

1.1.2

4 years ago

1.1.0

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago