-
-
Save lbeschastny/ac0908c4b61facad848ded7eb6f48102 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
npm-debug.log* | |
node_modules/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { MongoClient } = require('mongodb') | |
const { Collection } = require('mongodbext') | |
const relationsPlugin = require('mongodbext-relations') | |
exports.connect = async () => { | |
const client = await MongoClient.connect('mongodb://localhost:27017') | |
const db = client.db('mongodbext-relations-test') | |
const coll1 = new Collection(db, 'coll1') | |
const coll2 = new Collection(db, 'coll2') | |
coll2.addPlugin(relationsPlugin, { | |
relations: { | |
foreignKey: { | |
collection: coll1, | |
projection: { _id: 1, foo: 1 } | |
} | |
} | |
}) | |
return { client, db, coll1, coll2 } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const { EOL } = require('os') | |
const { connect } = require('./collections') | |
const test = async (coll1, coll2) => { | |
console.log('creating document in coll1') | |
const doc1 = await coll1.insertOne({ | |
foo: 'bar' | |
}) | |
console.log('document created:') | |
console.log(doc1) | |
console.log(EOL) | |
console.log('creating document in coll2') | |
const doc2 = await coll2.insertOne({ | |
foreignKey: doc1 | |
}) | |
console.log('document created:') | |
console.log(doc2) | |
console.log(EOL) | |
console.log('updating document in coll1') | |
const update1 = await coll1.findOneAndUpdate({ | |
_id: doc1._id | |
}, { | |
$set: { foo: 'baz' } | |
}, { | |
returnOriginal: false | |
}) | |
console.log('document updated:') | |
console.log(update1) | |
console.log(EOL) | |
console.log('reading document in coll2') | |
const read1 = await coll2.findOne({ | |
_id: doc2._id | |
}) | |
console.log('document fetched:') | |
console.log(read1) | |
console.log(EOL) | |
console.log('replacing document in coll1') | |
const update2 = await coll1.findOneAndReplace({ | |
_id: doc1._id | |
}, { | |
foo: 'foo bar' | |
}, { | |
returnOriginal: false | |
}) | |
console.log('document updated:') | |
console.log(update2) | |
console.log(EOL) | |
console.log('reading document in coll2') | |
const read2 = await coll2.findOne({ | |
_id: doc2._id | |
}) | |
console.log('document fetched:') | |
console.log(read2) | |
console.log(EOL) | |
} | |
const main = async () => { | |
const { client, coll1, coll2 } = await connect() | |
try { | |
await test(coll1, coll2) | |
} catch (err) { | |
console.error(err) | |
} finally { | |
client.close() | |
} | |
} | |
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"requires": true, | |
"lockfileVersion": 1, | |
"dependencies": { | |
"bson": { | |
"version": "1.0.6", | |
"resolved": "https://registry.npmjs.org/bson/-/bson-1.0.6.tgz", | |
"integrity": "sha512-D8zmlb46xfuK2gGvKmUjIklQEouN2nQ0LEHHeZ/NoHM2LDiMk2EYzZ5Ntw/Urk+bgMDosOZxaRzXxvhI5TcAVQ==" | |
}, | |
"is-plain-object": { | |
"version": "2.0.3", | |
"resolved": "https://registry.npmjs.org/is-plain-object/-/is-plain-object-2.0.3.tgz", | |
"integrity": "sha1-wVvz5LZrYtcu+vKSWEhmPsvGGbY=", | |
"requires": { | |
"isobject": "3.0.1" | |
} | |
}, | |
"isobject": { | |
"version": "3.0.1", | |
"resolved": "https://registry.npmjs.org/isobject/-/isobject-3.0.1.tgz", | |
"integrity": "sha1-TkMekrEalzFjaqH5yNHMvP2reN8=" | |
}, | |
"mhook": { | |
"version": "1.0.1", | |
"resolved": "https://registry.npmjs.org/mhook/-/mhook-1.0.1.tgz", | |
"integrity": "sha1-C6igyiARIlgzA9tKsdDi/5jytUw=" | |
}, | |
"mongodb": { | |
"version": "3.1.0-beta4", | |
"resolved": "https://registry.npmjs.org/mongodb/-/mongodb-3.1.0-beta4.tgz", | |
"integrity": "sha512-nJAK59fFlMWTdEJaTyGp3HeerkIahnupoMNjbszPJVnO63/36aFnlWHqpYrrJwj9GKlmb47YWBIvNo6fdrUL4Q==", | |
"requires": { | |
"mongodb-core": "3.1.0-beta4" | |
} | |
}, | |
"mongodb-core": { | |
"version": "3.1.0-beta4", | |
"resolved": "https://registry.npmjs.org/mongodb-core/-/mongodb-core-3.1.0-beta4.tgz", | |
"integrity": "sha512-aiwUKBGmFZwBx4CdC1iK5kjZyk0zhDxQ0edRtpTZUkk1jyWkdzYd6GEA6wMl7O6ZX/dOiFM2ujkrUR7+vkqJPw==", | |
"requires": { | |
"bson": "1.0.6", | |
"require_optional": "1.0.1", | |
"saslprep": "1.0.0" | |
} | |
}, | |
"mongodbext": { | |
"version": "2.2.4", | |
"resolved": "https://registry.npmjs.org/mongodbext/-/mongodbext-2.2.4.tgz", | |
"integrity": "sha1-txpu9zRRfJJl9JlTatKu9Gtu5VE=", | |
"requires": { | |
"is-plain-object": "2.0.3", | |
"mhook": "1.0.1" | |
} | |
}, | |
"mongodbext-relations": { | |
"version": "1.0.9", | |
"resolved": "https://registry.npmjs.org/mongodbext-relations/-/mongodbext-relations-1.0.9.tgz", | |
"integrity": "sha1-jbiKpyFO8X/P7d/12qmQyCiSsKQ=" | |
}, | |
"require_optional": { | |
"version": "1.0.1", | |
"resolved": "https://registry.npmjs.org/require_optional/-/require_optional-1.0.1.tgz", | |
"integrity": "sha512-qhM/y57enGWHAe3v/NcwML6a3/vfESLe/sGM2dII+gEO0BpKRUkWZow/tyloNqJyN6kXSl3RyyM8Ll5D/sJP8g==", | |
"requires": { | |
"resolve-from": "2.0.0", | |
"semver": "5.5.0" | |
} | |
}, | |
"resolve-from": { | |
"version": "2.0.0", | |
"resolved": "https://registry.npmjs.org/resolve-from/-/resolve-from-2.0.0.tgz", | |
"integrity": "sha1-lICrIOlP+h2egKgEx+oUdhGWa1c=" | |
}, | |
"saslprep": { | |
"version": "1.0.0", | |
"resolved": "https://registry.npmjs.org/saslprep/-/saslprep-1.0.0.tgz", | |
"integrity": "sha512-5lvKUEQ7lAN5/vPl5d3k8FQeDbEamu9kizfATfLLWV5h6Mkh1xcieR1FSsJkcSRUk49lF2tAW8gzXWVwtwZVhw==", | |
"optional": true | |
}, | |
"semver": { | |
"version": "5.5.0", | |
"resolved": "https://registry.npmjs.org/semver/-/semver-5.5.0.tgz", | |
"integrity": "sha512-4SJ3dm0WAwWy/NVeioZh5AntkdJoWKxHxcmyP622fOkgHa4z3R0TdBJICINyaSDE6uNwVc8gZr+ZinwZAH4xIA==" | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"author": { | |
"name": "Leonid Beschastny", | |
"email": "lbeschastny@gmail.com", | |
"url": "https://github.com/lbeschastny" | |
}, | |
"dependencies": { | |
"mongodb": "^3.1.0-beta4", | |
"mongodbext": "^2.2.4", | |
"mongodbext-relations": "^1.0.9" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment