Git Product home page Git Product logo

js-data-mongodb's Introduction

js-data logo

js-data-mongodb

Slack NPM Tests Downloads Coverage

A MongoDB adapter for the JSData Node.js ORM.

Installation

npm install --save js-data@3 js-data-mongodb@2 mongodb@3 bson@3

Usage

import { MongoDBAdapter } from 'js-data-mongodb';

// Create an instance of MongoDBAdapter
const adapter = new MongoDBAdapter({
  uri: 'mongodb://localhost:27017'
});

// Other JSData setup hidden

// Register the adapter instance
store.registerAdapter('mongodb', adapter, { default: true });

JSData + MongoDB Tutorial

Start with the JSData + MongoDB tutorial or checkout the API Reference Documentation.

Need help?

Please post a question on Stack Overflow. This is the preferred method.

You can also chat with folks on the Slack Channel. If you end up getting your question answered, please still consider consider posting your question to Stack Overflow (then possibly answering it yourself). Thanks!

Want to contribute?

Awesome! You can get started over at the Contributing guide.

Thank you!

License

The MIT License (MIT)

Copyright (c) 2014-2017 js-data-mongodb project authors

js-data-mongodb's People

Contributors

crobinson42 avatar jmdobry avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

js-data-mongodb's Issues

find() working not correctly

Hello.
I found that I can't use in find() with id of type ObjectID (find() can accept only integer or string). But in other case if I pass string like 553faa667f09fe5822e595c4 - adapter can't find record and throw error.
So we need to wrap id with ObjectID inside find().
It seems this is an issue.

update compat with mongodb@3 +

The library is broken with version mongodb@3 lib. This is a task to upgrade the library's use of mongodb to be compatible with the latest mongodb lib.

This will be a breaking change and major version bump.

increase test coverage

---------------------|----------|----------|----------|----------|----------------|
File                 |  % Stmts | % Branch |  % Funcs |  % Lines |Uncovered Lines |
---------------------|----------|----------|----------|----------|----------------|
All files            |    77.07 |    51.82 |    95.71 |    77.07 |                |
 js-data-mongodb     |      100 |      100 |      100 |      100 |                |
  mocha.start.js     |      100 |      100 |      100 |      100 |                |
 js-data-mongodb/src |    76.24 |    51.82 |    95.59 |    76.24 |                |
  index.js           |    76.24 |    51.82 |    95.59 |    76.24 |... 878,881,941 |
---------------------|----------|----------|----------|----------|----------------|

findAll - where: contains - syntax

What is the right syntax for findAll queries with contains

im trying to query using CONTAINS as filter but it not work's

Sample data structure:

{
    "_id": "1b2c0a744babcc342b27cf9e4c0fb351",
    "id": "1b2c0a744babcc342b27cf9e4c0fb351",
    "type": "class",
    "items": [ 
        "cce40b60d897259857303650c56421adb6508621", 
        "1faeb13afa5ea2feed1b3a5d10b1b1a771238b26", 
        "5a4071b313e67a11f50724449589d0901b9a14bb", 
        "9d083f45fe98a4af4d4aec500d2fe427c6370f91", 
        "5878005cfef2c0f8164a593672e722158405bb4d", 
        "50d2f50e5551651065a3cab188ed68b763c180e3", 
    ],
    "created_at": 1462998180437,
    "update_at": 1477923133426,
},
...

Sample code to run this query:

const JSData = require('js-data');
const DSAdapter = require('js-data-mongodb');

const store = new JSData.DS();
const adapter = new DSAdapter({ uri: 'mongodb://localhost/shop' });
store.registerAdapter('main', adapter, { default: true });

Resource = dataStore.defineResource({ name: 'invoice', idAttribute: '_id', table: 'invoice' });

const params = {
  where: {
    items: {
      contains: '50d2f50e5551651065a3cab188ed68b763c180e3'
    }
  }
}

Resource.findAll(params)
  .then(result => {
    console.log('Result', result.length, result)
  })
  .catch(err => {
    if (err.message.toLowerCase().includes('not found')) return console.log(err);
    throw err;
  });

With the same data structure but with redis adapter and DB work's fine.

I even tried to run some queries with "contains" against strings, with the mongodb sample data - https://docs.mongodb.com/getting-started/shell/import-data/

Thanks, in advance.

Note: using v 0.7.0 (which is weird because in the repo there is only available up to 0.6.0, before passing to the rc 1.0...)

Remove subdocument (atomic update)

Description

versions:

js-data: 2.10.0
js-data-mongodb: 0.7.0

Steps to reproduce

  • Define User resource model
  • Assume that a user data:
    { _id: 'XXXX', groups: { groupOne: true, groupTwo: true } }
  • Do: User.update('XXXX', {groups: {groupOne: null}})
  • Expected result: groupOne gets removed from groups
  • End result: groups was overridden and become groups: {groupOne: null}

If there's no way to do atomic update, the code won't scale.

Thanks!

Feature - Translate all ObjectID fields

There are cases where someone may implement js-data-mongodb towards a pre existing Mongo database collection and some libraries (ORM's) that perform relational mapping store record _id's in the ObjectID bson value, ie: { "_bsontype": "ObjectID", "id": { "type": "Buffer", "data": [87,...156] } }.

When using js-data-mongodb, it would be convenient to pass an option to translate all ObjectID fields to a string.

I would apply the logic in the method _translateIds() on line 218 and change the name and all references to it to "_translateObjectIDs()".

@jmdobry If you'll accept this feature, I'll put in a PR. Please let me know.

Missing 0.7.0

Missing tag for 0.7.0

Please create a tag on commit - af5c2dc0ff1b72a1f2a82ce1d09e9c4f7d773d28 for tag 0.7.0

Assuming that 0.7.0 was the last version that work with js-data v2, and its a version published on npm.

Thanks!

Default conversion _id to id

// Why couldn't Mongo just use "id"?

No shit?!

@jmdobry How do you feel about making default functionality to convert id to _id and vica versa with adapter interactions?

We can still have the config idAttribute property to be specified for override.

Undefined properties are set to null in Mongodb

Description

versions :
js-data: 3.0.0-rc.6
js-data-mongodb : 1.0.0-rc.1

Steps to reproduce

Here is my model schema :

{
  "type": "object",
  "properties": {
    "a": {
      "type": "string"
    },
    "b": {
      "type": "string"
    }
  },
  "required": ["a"],
  "additionalProperties": false
}

When i insert or update not all the properties of my model (example with only property "a" : {a: "a"}), the others properties are set to undefined by js-data. So the input props of methods create or update looks like :

{
  a: "a",
  b: undefined
}

The problem is that the Node.js mongodb driver, insert null in the database for all the undefined properties. So when you just want to update property "a", the previous value of property "b" is erase by a null value.

The workaround solution i found, is to remove all undefined properties of the object in methods beforeCreate and beforeUpdate on the adapter. But i think it could be made directly on insert or update methods.

// Workaround to avoid undefined values to be set to null in MongoDB
const adapter = new MongoDBAdapter({
  uri: config.MONGODB_URI,
  beforeUpdate(mapper, id, props) {
    const removeUndefined = (obj) => {
      Object.keys(obj).forEach(key =>
        (obj[key] && typeof obj[key] === 'object') && removeUndefined(obj[key]) ||
        (obj[key] === undefined) && delete obj[key]
      );
      return obj;
    };
    removeUndefined(props);
  }
});

Solution can be found here : http://stackoverflow.com/questions/286141/remove-blank-attributes-from-an-object-in-javascript. It should be recursive and only remove undefined properties.

Any thoughts ?

Thanks!

hasOne relation inconsistent return

"hasOne" relation inconsistent return

hasOne relationship is a one to one relation, when a related item exists the OBJECT is returned, but if the related item does not exists on the related collection and empty ARRAY is returned.

I think the query should return an empty object or better yet undefined.

js-data: 2.10
js-data-mongo: 0.7.0

Thanks!

Add suport for the '==' operator shorthand

This work fine:

User.findAll({
  where: {
    field: {
      '==': '393fe308fc3d796b6b80c548ceeac69008a68a09'
    }
  }
})
...
User.findAll({
    field: {
      '==': '393fe308fc3d796b6b80c548ceeac69008a68a09'
    }
})
...
User.findAll({
    field: '393fe308fc3d796b6b80c548ceeac69008a68a09'
})

But this not:

User.findAll({
  where: {
    field: '393fe308fc3d796b6b80c548ceeac69008a68a09'
  }
})

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.