Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 0 additions & 39 deletions .circleci/config.yml

This file was deleted.

51 changes: 51 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
name: Test

on: [push, pull_request]

jobs:
test:
name: Test (Node ${{ matrix.node-version }})
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [18, 20]
services:
mongo:
image: mongo:8
ports:
- 27017:27017
options: >-
--health-cmd="mongosh --eval 'db.runCommand({ ping: 1 })'"
--health-interval=10s
--health-timeout=5s
--health-retries=5

steps:
- name: Checkout code
uses: actions/checkout@v4

- name: Setup Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v4
with:
node-version: ${{ matrix.node-version }}
cache: yarn

- name: Wait for MongoDB
run: |
for i in {1..30}; do
nc -z localhost 27017 && echo "MongoDB is up!" && break
echo "Waiting for MongoDB..."
sleep 2
done

- name: Print versions
run: |
node -v
npm -v
yarn -v

- name: Install dependencies
run: yarn install

- name: Run tests
run: yarn test
1 change: 1 addition & 0 deletions .naverc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
18
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
# @clocklimited/save-mongodb - mongodb persistence engine for **save**

[![CircleCI](https://circleci.com/gh/clocklimited/save-mongodb/tree/master.svg?style=svg)](https://circleci.com/gh/clocklimited/save-mongodb/tree/master)

## Installation

```
npm install @clocklimited/save-mongodb

// There is a peer dependency of mongodb - you have to bring your own!
npm install mongodb@^4
npm install mongodb@^6
```

## Usage

Version >=5 of this package is compatible with MongoDB 8.

If you want to see how this works look at the tests or this simple example:

```js
var MongoClient = require('mongodb').MongoClient // npm install mongodb
var save = require('save') // npm install save
var saveMongodb = require('@clocklimited/save-mongodb')
const MongoClient = require('mongodb').MongoClient // npm install mongodb
const save = require('save') // npm install save
const saveMongodb = require('@clocklimited/save-mongodb')

// connect to your mongodb database.
MongoClient.connect('mongodb://localhost:27017/', function(error, client) {
if (error) return console.error(error.message)
var connection = client.db('test')
const connection = client.db('test')
// Get a collection. This will create the collection if it doesn't exist.
connection.collection('contact', function(error, collection) {
if (error) return console.error(error.message)

// Create a save object and pass in a mongodb engine.
var contactStore = save('Contact', { engine: saveMongodb(collection) })
const contactStore = save('Contact', { engine: saveMongodb(collection) })

// Then we can create a new object.
contactStore.create({ name: 'Paul', email: 'paul@serby.net' }, function(
Expand All @@ -54,8 +54,8 @@ Find now has a streaming interface

```js

var contactStore = save('Contact', { engine: saveMongodb(collection) })
var es = require('event-stream')
const contactStore = save('Contact', { engine: saveMongodb(collection) })
const es = require('event-stream')

contactStore.find({})
.pipe(es.stringify())
Expand Down
10 changes: 5 additions & 5 deletions example/simple.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
var MongoClient = require('mongodb').MongoClient // npm install mongodb
var save = require('save') // npm install save
var saveMongodb = require('..')
const MongoClient = require('mongodb').MongoClient // npm install mongodb
const save = require('save') // npm install save
const saveMongodb = require('..')

// connect to your mongodb database.
MongoClient.connect('mongodb://localhost:27017/', function(error, client) {
if (error) return console.error(error.message)
var connection = client.db('test')
const connection = client.db('test')
// Get a collection. This will create the collection if it doesn't exist.
connection.collection('contact', function(error, collection) {
if (error) return console.error(error.message)

// Create a save object and pass in a mongodb engine.
var contactStore = save('Contact', { engine: saveMongodb(collection) })
const contactStore = save('Contact', { engine: saveMongodb(collection) })

// Then we can create a new object.
contactStore.create({ name: 'Paul', email: 'paul@serby.net' }, function(
Expand Down
12 changes: 6 additions & 6 deletions example/stream.js
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
var MongoClient = require('mongodb').MongoClient // npm install mongodb
var save = require('save') // npm install save
var saveMongodb = require('..')
var es = require('event-stream')
const MongoClient = require('mongodb').MongoClient // npm install mongodb
const save = require('save') // npm install save
const saveMongodb = require('..')
const es = require('event-stream')

// connect to your mongodb database.
MongoClient.connect('mongodb://localhost:27017/', function(error, client) {
if (error) return console.error(error.message)
var connection = client.db('test')
const connection = client.db('test')
// Get a collection. This will create the collection if it doesn't exist.
connection.collection('contact', function(error, collection) {
if (error) return console.error(error.message)

// Create a save object and pass in a mongodb engine.
var contactStore = save('Contact', { engine: saveMongodb(collection) })
const contactStore = save('Contact', { engine: saveMongodb(collection) })

// Then we can create a new object.
contactStore.create({ name: 'Paul', email: 'paul@serby.net' }, function(
Expand Down
66 changes: 31 additions & 35 deletions lib/cast-id-property.js
Original file line number Diff line number Diff line change
@@ -1,43 +1,39 @@
module.exports = init

var ObjectId = require('mongodb').ObjectId
const ObjectId = require('mongodb').ObjectId

function init(property) {
return castIdProperty

function castIdProperty(query) {
var newQuery = Object.assign({}, query)
var idQuery = query[property]
// only convert if id is present
if (!idQuery) {
return newQuery
}

if (Object(idQuery) === idQuery) {
newQuery[property] = castComplexId(idQuery)
} else {
newQuery[property] = ObjectId.isValid(newQuery[property])
? new ObjectId(newQuery[property])
: newQuery[property]
}
const init = property => query => {
let newQuery = Object.assign({}, query)
const idQuery = query[property]

if (!idQuery) {
return newQuery
}

function castComplexId(query) {
var newQuery = Object.assign({}, query)
if (Object(idQuery) === idQuery) {
newQuery[property] = castComplexId(idQuery)
} else {
newQuery[property] = ObjectId.isValid(newQuery[property])
? new ObjectId(newQuery[property])
: newQuery[property]
}

Object.keys(newQuery).map(function(key) {
var value = newQuery[key]
if (Array.isArray(value)) {
newQuery[key] = value.map(function(item) {
return ObjectId.isValid(item) ? new ObjectId(item) : item
})
} else {
newQuery[key] = ObjectId.isValid(value) ? new ObjectId(value) : value
}
})
return newQuery
}

return newQuery
}
const castComplexId = query => {
const newQuery = Object.assign({}, query)

Object.keys(newQuery).map(function(key) {
const value = newQuery[key]
if (Array.isArray(value)) {
newQuery[key] = value.map(function(item) {
return ObjectId.isValid(item) ? new ObjectId(item) : item
})
} else {
newQuery[key] = ObjectId.isValid(value) ? new ObjectId(value) : value
}
})

return newQuery
}

module.exports = init
Loading