Skip to content

Commit

Permalink
refa(mongo): rename optimizeIndex to virtualKey
Browse files Browse the repository at this point in the history
  • Loading branch information
shigma committed Apr 26, 2022
1 parent adaccae commit cdd8a4a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/mongo/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@cosmotype/driver-mongo",
"version": "1.1.0",
"version": "1.1.1",
"description": "MongoDB Driver for Cosmotype",
"main": "lib/index.js",
"typings": "lib/index.d.ts",
Expand Down
14 changes: 7 additions & 7 deletions packages/mongo/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ namespace MongoDriver {
/** connection string (will overwrite all configs except 'name') */
uri?: string
/**
* use internal `_id` for single primary fields
* store single primary key in `_id` field to enhance index performance
* @default false
*/
optimizeIndex?: boolean
virtualKey?: boolean
}
}

Expand Down Expand Up @@ -91,7 +91,7 @@ class MongoDriver extends Driver {

;[primary, ...unique].forEach((keys, index) => {
// use internal `_id` for single primary fields
if (this.config.optimizeIndex && !index && typeof keys === 'string') return
if (this.config.virtualKey && !index && typeof keys === 'string') return

// if the index is already created, skip it
keys = makeArray(keys)
Expand Down Expand Up @@ -158,14 +158,14 @@ class MongoDriver extends Driver {

private getVirtualKey(table: string) {
const { primary } = this.model(table)
if (typeof primary === 'string' && this.config.optimizeIndex) {
if (typeof primary === 'string' && this.config.virtualKey) {
return primary
}
}

private patchVirtual(table: string, row: any) {
const { primary } = this.model(table)
if (typeof primary === 'string' && this.config.optimizeIndex) {
if (typeof primary === 'string' && this.config.virtualKey) {
row[primary] = row['_id']
delete row['_id']
}
Expand All @@ -174,7 +174,7 @@ class MongoDriver extends Driver {

private unpatchVirtual(table: string, row: any) {
const { primary } = this.model(table)
if (typeof primary === 'string' && this.config.optimizeIndex) {
if (typeof primary === 'string' && this.config.virtualKey) {
row['_id'] = row[primary]
delete row[primary]
}
Expand Down Expand Up @@ -281,7 +281,7 @@ class MongoDriver extends Driver {
const { primary, fields, autoInc } = model

if (typeof primary === 'string' && !(primary in data)) {
const key = this.config.optimizeIndex ? '_id' : primary
const key = this.config.virtualKey ? '_id' : primary
if (autoInc) {
const [latest] = await coll.find().sort(key, -1).limit(1).toArray()
data[primary] = latest ? +latest[key] + 1 : 1
Expand Down

0 comments on commit cdd8a4a

Please sign in to comment.