diff --git a/packages/mongo/index.ts b/packages/mongo/index.ts index a4cdc2205..33bebd92d 100644 --- a/packages/mongo/index.ts +++ b/packages/mongo/index.ts @@ -7,7 +7,7 @@ * * You should have received a copy of the MIT License along with this program. */ - + export * from './src/mapping.js'; export * from './src/adapter.js'; export * from './src/persistence.js'; @@ -15,3 +15,5 @@ export * from './src/query.model.js'; export * from './src/query.resolver.js'; export * from './src/query.js'; export * from './src/client/client.js'; + +export { UpdateCommand } from './src/client/command/update.js'; \ No newline at end of file diff --git a/packages/mongo/tests/mongo-convert.spec.ts b/packages/mongo/tests/mongo-convert.spec.ts new file mode 100644 index 000000000..c548a0465 --- /dev/null +++ b/packages/mongo/tests/mongo-convert.spec.ts @@ -0,0 +1,66 @@ +import { expect, test } from '@jest/globals'; +import { + entity, + MongoId, + PrimaryKey, + ReflectionClass, +} from '@deepkit/type'; +import { createDatabase } from './utils'; +import { UpdateCommand } from '../src/client/command/update'; + +Error.stackTraceLimit = 100; + +@entity.name('Model').collection('test_convert') +export class Model { + _id: MongoId & PrimaryKey = ''; + + a: number = 0; + + constructor(public name: string) { + } +} + +@entity.name('ModelString').collection('test_convert') +export class ModelString { + _id: MongoId & PrimaryKey = ''; + + a: string = ''; + + constructor(public name: string) { + } +} + +// This is a use case for UpdateCommand to make a $convert query possible, +// which is not directly supported by the orm. +// Therefore UpdateCommand is exported by the mongo package. +test('raw $convert UpdateCommand', async () => { + const db = await createDatabase('testing'); + await db.query(Model).deleteMany(); + + const item = new Model('foo'); + item.a = 123; + + const item2 = new Model('foo2'); + item2.a = 456; + + await db.persist(item); + await db.persist(item2); + + expect(await db.query(Model).filter({a: 456}).has()).toBe(true); + + + const schema = ReflectionClass.from(Model); + const command = new UpdateCommand( + schema, + [{ + // for types see: https://www.mongodb.com/docs/manual/reference/operator/aggregation/type/#available-types + // int to string + q: {a: {$type: 16}}, + u: [{ $set: { a: { $convert: { input: "$a", to: 2 } } } }], + multi: true + }], + ); + await db.adapter.client.execute(command); + + expect(await db.query(ModelString).filter({a: '456'}).has()).toBe(true); +});