Skip to content

Commit 9e26062

Browse files
gsi-alejandroejscribner
authored andcommitted
fix: some fixes after rebasing with master
1 parent d1b92a3 commit 9e26062

File tree

5 files changed

+36
-16
lines changed

5 files changed

+36
-16
lines changed

__test__/query-array.spec.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ describe('Query Builder Array', () => {
1313
});
1414

1515
test('Basics', async () => {
16-
set('DEBUG', true);
1716
const House = model('House', houseSchema);
1817
await startInTest(getDefaultInstance());
1918

@@ -27,13 +26,12 @@ describe('Query Builder Array', () => {
2726
const filter = {
2827
title: 'Beach House',
2928
$any: {
30-
$expr: [{ search: 'numbers' }],
31-
$satisfies: { search: { $in: [10] } },
29+
$expr: [{ search: { $in: 'numbers' } }],
30+
$satisfies: { search: 10 },
3231
},
3332
};
3433

3534
const results = await House.find(filter, { consistency: SearchConsistency.LOCAL });
36-
console.log(results);
3735
expect(results.rows.length).toBeGreaterThanOrEqual(1);
3836
});
3937
});

src/model/create-model.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DocumentNotFoundError, DropCollectionOptions } from 'couchbase';
2-
import { Query, SearchConsistency, setValueByPath } from '..';
2+
import { getValueByPath, Query, SearchConsistency, setValueByPath } from '..';
33
import { BuildIndexQueryError, OttomanError } from '../exceptions/ottoman-errors';
44
import { createMany, find, FindOptions, ManyQueryResponse, removeMany, updateMany } from '../handler';
55
import { FindByIdOptions, IFindOptions } from '../handler/';
@@ -104,7 +104,7 @@ export const _buildModel = (metadata: ModelMetadata) => {
104104
super(data);
105105
const strategy = options.strategy || CAST_STRATEGY.DEFAULT_OR_DROP;
106106
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];
108108
const schemaData = cast(data, schema, { strategy, strict, skip });
109109

110110
this._applyData(schemaData, strategy === CAST_STRATEGY.THROW ? CAST_STRATEGY.THROW : true);
@@ -186,16 +186,27 @@ export const _buildModel = (metadata: ModelMetadata) => {
186186

187187
static findById = async (id: string, options: FindByIdOptions = {}): Promise<Model | Record<string, unknown>> => {
188188
const { populate, populateMaxDeep: deep, select, lean, enforceRefCheck = false, ...findOptions } = options;
189+
const modelKeyClean = modelKey.split('.')[0];
190+
let isModelKeyAddedToSelect = false;
189191
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]);
191198
}
192199
const key = _keyGenerator!(keyGenerator, { metadata, id }, keyGeneratorDelimiter);
193200
const { value: pojo } = await collection().get(key, findOptions);
194201

195-
if (pojo[metadata.modelKey] !== metadata.modelName) {
202+
if (getValueByPath(pojo, metadata.modelKey) !== metadata.modelName) {
196203
throw new DocumentNotFoundError();
197204
}
198205

206+
if (isModelKeyAddedToSelect) {
207+
delete pojo[modelKeyClean];
208+
}
209+
199210
if (populate) {
200211
return getPopulated({ fieldsName: populate, deep, lean, pojo, schema, modelName, ottoman, enforceRefCheck });
201212
}

src/model/utils/model.utils.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,13 @@ export const getPopulated = async (options: PopulateAuxOptionsType): Promise<Mod
6262
const current = fieldsName?.[field];
6363
let select: string | string[] = '';
6464
if (isObject && current !== '*' && current?.select !== '*') {
65-
select =
66-
typeof current === 'string' || Array.isArray(current)
67-
? current
68-
: extractPopulateFieldsFromObject(current) ?? '';
65+
if (typeof current === 'string') {
66+
select = current;
67+
} else if (Array.isArray(current)) {
68+
select = current.join(',');
69+
} else {
70+
select = extractPopulateFieldsFromObject(current) ?? '';
71+
}
6972
}
7073

7174
const populate = populateMaxDeep === 0 ? undefined : current?.populate ?? current ?? '*';

src/model/utils/store-life-cycle.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const storeLifeCycle = async ({ key, id, data, options, metadata, refKeys
2020
document = validate(document, schema, {
2121
strict: schema.options.strict,
2222
strategy: CAST_STRATEGY.THROW,
23-
skip: [modelKey, ID_KEY],
23+
skip: [modelKey.split('.')[0], ID_KEY],
2424
});
2525

2626
// enforceRefCheck logic

src/schema/schema.ts

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -200,9 +200,17 @@ export class Schema {
200200
for (const key in this.fields) {
201201
const field = this.fields[key];
202202
if (typeof obj[field.name] === 'undefined' && field instanceof CoreType) {
203-
const _val = field.buildDefault();
204-
if (typeof _val !== 'undefined') {
205-
obj[field.name] = _val;
203+
if (field instanceof EmbedType) {
204+
const _val = {};
205+
(field as EmbedType).schema.applyDefaultsToObject(_val);
206+
if (Object.keys(_val).length > 0) {
207+
obj[field.name] = _val;
208+
}
209+
} else {
210+
const _val = field.buildDefault();
211+
if (typeof _val !== 'undefined') {
212+
obj[field.name] = _val;
213+
}
206214
}
207215
}
208216
}

0 commit comments

Comments
 (0)