Skip to content

Commit

Permalink
Update Readme, release first version 0.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nhat-phan authored and Nhat Phan committed Dec 23, 2018
1 parent 5cb9825 commit e8b6d39
Show file tree
Hide file tree
Showing 5 changed files with 97 additions and 7 deletions.
89 changes: 89 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,95 @@ npm install najs-binding najs-eloquent najs-eloquent-mongodb

That's it.

## Define Model runs with MongodbDriver

You can register MongodbDriver as default driver by this way

```typescript
import { DriverProvider } from 'najs-eloquent'
import { MongodbDriver } from 'najs-eloquent-mongodb'

DriverProvider.register(MongodbDriver, 'mongodb', true)
```

Or if you want to use MongodbDriver for some specific models only, you can extends from `MongodbModel` instead of `Model`

```typescript
import { MongodbModel } from 'najs-eloquent-mongodb'

export class User extends MongodbModel {
// define a property belongs to User model
email: string

// define a class name which used for Dependency injection
// (this feature provided by "najs-binding" package)
getClassName() {
return 'YourNamespace.User'
}
}

// Register the User class
MongodbModel.register(User)
```

## Connect to database and provide custom MongoClient instance

You may want to connect and provide your own `MongoClient` instance, you can do it by using `.bind()` function of the `najs-binding` package. _Note: Please ensure that this file is loaded before using najs-eloquent._

```typescript
import { bind } from 'najs-binding'
import { MongodbProviderFacade } from 'najs-eloquent-mongodb'

// Load your mongodb instance instead of mongodb dependency in "najs-eloquent-mongodb" package
import { Db, MongoClient } from 'mongodb'

export class YourMongodbProvider {
static className: string = 'YourNamespace.YourMongodbProvider'

getClassName() {
return YourMongodbProvider.className
}

connect(url: string): Promise<this> {
return new Promise((resolve, reject) => {
MongoClient.connect(
url,
{ useNewUrlParser: true },
(error: any, client: MongoClient) => {
if (error) {
return reject(error)
}

this.mongoClient = client
resolve(this)
}
)
})
}

close(): this {
if (this.mongoClient) {
this.mongoClient.close()
}

return this
}

getMongoClient(): MongoClient {
return this.mongoClient
}

getDatabase(dbName?: string): Db {
return this.mongoClient && this.mongoClient.db(dbName)
}
}

bind('NajsEloquent.Provider.MongodbProvider', YourMongodbProvider.className)

// Reload the facade, then najs-eloquent will use your mongodb instance
MongodbProviderFacade.reloadFacadeRoot()
```

## Contribute

PRs are welcomed to this project, and help is needed in order to keep up with the changes of Laravel Eloquent. If you want to improve the library, add functionality or improve the docs please feel free to submit a PR.
Expand Down
2 changes: 1 addition & 1 deletion dist/lib/providers/MongodbProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class MongodbProvider extends najs_facade_1.Facade {
}
connect(url) {
return new Promise((resolve, reject) => {
mongodb_1.MongoClient.connect(url, (error, client) => {
mongodb_1.MongoClient.connect(url, { useNewUrlParser: true }, (error, client) => {
if (error) {
return reject(error);
}
Expand Down
6 changes: 3 additions & 3 deletions dist/test/drivers/mongodb/MongodbQueryExecutor.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,10 @@ describe('MongodbQueryExecutor', function () {
collectionUsers = db.collection('users');
collectionRoles = db.collection('roles');
for (const data of dataset) {
await collectionUsers.save(data);
await collectionUsers.insertOne(data);
}
for (let i = 0; i < 10; i++) {
await collectionRoles.save({
await collectionRoles.insertOne({
name: 'role-' + i,
deleted_at: new Date()
});
Expand Down Expand Up @@ -671,7 +671,7 @@ describe('MongodbQueryExecutor', function () {
const handler = makeQueryBuilderHandler('users');
const result = await makeQueryBuilder(handler)
.native(function (collection) {
return collection.remove({});
return collection.deleteOne({});
})
.execute();
expect(result).toEqual({ n: 1, ok: 1 });
Expand Down
1 change: 1 addition & 0 deletions lib/providers/MongodbProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export class MongodbProvider extends Facade implements Najs.Contracts.Eloquent.M
return new Promise((resolve, reject) => {
MongoClient.connect(
url,
{ useNewUrlParser: true },
(error: any, client: MongoClient) => {
if (error) {
return reject(error)
Expand Down
6 changes: 3 additions & 3 deletions test/drivers/mongodb/MongodbQueryExecutor.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,10 @@ describe('MongodbQueryExecutor', function() {
collectionUsers = db.collection('users')
collectionRoles = db.collection('roles')
for (const data of dataset) {
await collectionUsers.save(data)
await collectionUsers.insertOne(data)
}
for (let i = 0; i < 10; i++) {
await collectionRoles.save({
await collectionRoles.insertOne({
name: 'role-' + i,
deleted_at: new Date()
})
Expand Down Expand Up @@ -868,7 +868,7 @@ describe('MongodbQueryExecutor', function() {
const handler = makeQueryBuilderHandler('users')
const result = await makeQueryBuilder(handler)
.native(function(collection) {
return collection.remove({})
return collection.deleteOne({})
})
.execute()

Expand Down

0 comments on commit e8b6d39

Please sign in to comment.