|
1 | 1 | import { DocumentNotFoundError, DropCollectionOptions } from 'couchbase'; |
2 | | -import { Query, SearchConsistency, setValueByPath } from '..'; |
| 2 | +import { getValueByPath, Query, SearchConsistency, setValueByPath } from '..'; |
3 | 3 | import { BuildIndexQueryError, OttomanError } from '../exceptions/ottoman-errors'; |
4 | 4 | import { createMany, find, FindOptions, ManyQueryResponse, removeMany, updateMany } from '../handler'; |
5 | 5 | import { FindByIdOptions, IFindOptions } from '../handler/'; |
@@ -104,7 +104,7 @@ export const _buildModel = (metadata: ModelMetadata) => { |
104 | 104 | super(data); |
105 | 105 | const strategy = options.strategy || CAST_STRATEGY.DEFAULT_OR_DROP; |
106 | 106 | const strict = options.strict !== undefined ? options.strict : schema.options.strict; |
107 | | - const skip = options.skip || [modelKey, ID_KEY]; |
| 107 | + const skip = options.skip || [modelKey.split('.')[0], ID_KEY]; |
108 | 108 | const schemaData = cast(data, schema, { strategy, strict, skip }); |
109 | 109 |
|
110 | 110 | this._applyData(schemaData, strategy === CAST_STRATEGY.THROW ? CAST_STRATEGY.THROW : true); |
@@ -186,16 +186,27 @@ export const _buildModel = (metadata: ModelMetadata) => { |
186 | 186 |
|
187 | 187 | static findById = async (id: string, options: FindByIdOptions = {}): Promise<Model | Record<string, unknown>> => { |
188 | 188 | const { populate, populateMaxDeep: deep, select, lean, enforceRefCheck = false, ...findOptions } = options; |
| 189 | + const modelKeyClean = modelKey.split('.')[0]; |
| 190 | + let isModelKeyAddedToSelect = false; |
189 | 191 | if (select) { |
190 | | - findOptions['project'] = extractSelect(select, { noCollection: true }, false, modelKey); |
| 192 | + const selectArray = typeof select === 'string' ? select.split(',').map((x) => x.trim()) : select; |
| 193 | + if (selectArray.findIndex((x) => x === modelKeyClean) === -1) { |
| 194 | + selectArray.push(modelKeyClean); |
| 195 | + isModelKeyAddedToSelect = true; |
| 196 | + } |
| 197 | + findOptions['project'] = extractSelect(selectArray, { noCollection: true }, false, modelKey.split('.')[0]); |
191 | 198 | } |
192 | 199 | const key = _keyGenerator!(keyGenerator, { metadata, id }, keyGeneratorDelimiter); |
193 | 200 | const { value: pojo } = await collection().get(key, findOptions); |
194 | 201 |
|
195 | | - if (pojo[metadata.modelKey] !== metadata.modelName) { |
| 202 | + if (getValueByPath(pojo, metadata.modelKey) !== metadata.modelName) { |
196 | 203 | throw new DocumentNotFoundError(); |
197 | 204 | } |
198 | 205 |
|
| 206 | + if (isModelKeyAddedToSelect) { |
| 207 | + delete pojo[modelKeyClean]; |
| 208 | + } |
| 209 | + |
199 | 210 | if (populate) { |
200 | 211 | return getPopulated({ fieldsName: populate, deep, lean, pojo, schema, modelName, ottoman, enforceRefCheck }); |
201 | 212 | } |
|
0 commit comments