Skip to content

Commit

Permalink
Merge pull request #135 from AthennaIO/develop
Browse files Browse the repository at this point in the history
Automatically parse id to _id when using mongo
  • Loading branch information
jlenon7 authored Jan 11, 2024
2 parents 7daf44a + cc049a3 commit b4d585b
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 53 deletions.
10 changes: 7 additions & 3 deletions configurer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -166,11 +166,15 @@ export default class DatabaseConfigurer extends BaseConfigurer {
mongo: 'mongoose'
}

this.logger
const instruction = this.logger
.instruction()
.head('Run following commands to get started:')
.add(`npm install ${libraries[connection]}`)
.add(`docker-compose up -d`)
.render()

if (connection !== 'sqlite') {
instruction.add(`docker-compose up -d`)
}

instruction.render()
}
}
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@athenna/database",
"version": "4.27.0",
"version": "4.28.0",
"description": "The Athenna database handler for SQL/NoSQL.",
"license": "MIT",
"author": "João Lenon <[email protected]>",
Expand Down
19 changes: 6 additions & 13 deletions src/models/annotations/Column.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@ import type { ColumnOptions } from '#src/types/columns/ColumnOptions'
* Create column for model class.
*/
export function Column(
options: Omit<ColumnOptions, 'property'> = {}
options: Omit<ColumnOptions, 'property' | 'hasSetName'> = {}
): PropertyDecorator {
return (target: any, key: any) => {
let hasSetName = false

if (options.name) {
hasSetName = true
}
const hasSetName = !!options.name

options = Options.create(options, {
name: AthennaString.toCamelCase(key),
Expand All @@ -48,19 +44,16 @@ export function Column(
// @ts-ignore
options.property = key

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
options.hasSetName = hasSetName

if (options.isMainPrimary) {
options.isPrimary = true
}

const Target = target.constructor

const connection = Target.connection()
const driver = Config.get(`database.connections.${connection}.driver`)

if (!hasSetName && options.name === 'id' && driver === 'mongo') {
options.name = '_id'
}

debug('registering column metadata for model %s: %o', Target.name, options)

Annotation.defineColumnMeta(Target, options)
Expand Down
6 changes: 6 additions & 0 deletions src/models/schemas/ModelSchema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,12 @@ export class ModelSchema<M extends BaseModel = any> {

if (!options) {
options = this.columns.find(c => c.name === 'id')

if (options) {
if (!options.hasSetName && this.getModelDriverName() === 'mongo') {
options.name = '_id'
}
}
}

if (!options) {
Expand Down
6 changes: 6 additions & 0 deletions src/types/columns/ColumnOptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ export type ColumnOptions = {
*/
name?: string

/**
* Holds if the user has set the name property
* in @Column or not.
*/
hasSetName?: boolean

/**
* The column type in database. This value
* only matters when you are using MongoDB
Expand Down
40 changes: 6 additions & 34 deletions tests/unit/models/annotations/ColumnAnnotationTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,40 +24,7 @@ export default class ColumnAnnotationTest {
assert.deepEqual(Annotation.getColumnsMeta(User), [
{
name: 'id',
defaultTo: null,
type: String,
isIndex: false,
isSparse: false,
isPrimary: false,
isHidden: false,
isUnique: false,
isNullable: true,
isMainPrimary: false,
isCreateDate: false,
isUpdateDate: false,
isDeleteDate: false,
property: 'id',
persist: true
}
])
}

@Test()
public async shouldAutomaticallyDefineNameAsUnderlineIdWhenUsingMongoDriver({ assert }: Context) {
Config.set('database.connections.mongo.driver', 'mongo')

class User extends BaseModel {
public static connection() {
return 'mongo'
}

@Column()
public id: string
}

assert.deepEqual(Annotation.getColumnsMeta(User), [
{
name: '_id',
hasSetName: false,
defaultTo: null,
type: String,
isIndex: false,
Expand Down Expand Up @@ -90,6 +57,7 @@ export default class ColumnAnnotationTest {
assert.deepEqual(Annotation.getColumnsMeta(User), [
{
name: 'id',
hasSetName: true,
defaultTo: null,
type: String,
isIndex: false,
Expand Down Expand Up @@ -133,6 +101,7 @@ export default class ColumnAnnotationTest {
assert.deepEqual(Annotation.getColumnsMeta(User), [
{
name: '_id',
hasSetName: true,
type: Schema.ObjectId,
defaultTo: '1',
isPrimary: true,
Expand Down Expand Up @@ -163,6 +132,7 @@ export default class ColumnAnnotationTest {
assert.deepEqual(Annotation.getColumnsMeta(User), [
{
name: 'id',
hasSetName: false,
defaultTo: null,
type: String,
isPrimary: false,
Expand Down Expand Up @@ -191,6 +161,7 @@ export default class ColumnAnnotationTest {
assert.deepEqual(Annotation.getColumnsMeta(User), [
{
name: 'id',
hasSetName: false,
defaultTo: null,
type: String,
isPrimary: true,
Expand Down Expand Up @@ -219,6 +190,7 @@ export default class ColumnAnnotationTest {
assert.deepEqual(Annotation.getColumnsMeta(User), [
{
name: 'id',
hasSetName: false,
type: String,
defaultTo: null,
isPrimary: true,
Expand Down
6 changes: 6 additions & 0 deletions tests/unit/models/schemas/ModelSchemaTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ export default class ModelSchemaTest {

assert.deepEqual(column, {
defaultTo: null,
hasSetName: false,
isCreateDate: false,
isDeleteDate: false,
isHidden: false,
Expand Down Expand Up @@ -123,6 +124,7 @@ export default class ModelSchemaTest {

assert.deepEqual(column, {
defaultTo: null,
hasSetName: true,
isCreateDate: false,
isDeleteDate: false,
isHidden: false,
Expand Down Expand Up @@ -163,6 +165,7 @@ export default class ModelSchemaTest {

assert.deepEqual(column, {
defaultTo: null,
hasSetName: false,
isCreateDate: true,
isDeleteDate: false,
isHidden: false,
Expand Down Expand Up @@ -191,6 +194,7 @@ export default class ModelSchemaTest {

assert.deepEqual(column, {
defaultTo: null,
hasSetName: false,
isCreateDate: false,
isDeleteDate: false,
isHidden: false,
Expand Down Expand Up @@ -219,6 +223,7 @@ export default class ModelSchemaTest {

assert.deepEqual(column, {
defaultTo: null,
hasSetName: false,
isCreateDate: false,
isDeleteDate: true,
isHidden: false,
Expand Down Expand Up @@ -247,6 +252,7 @@ export default class ModelSchemaTest {

assert.deepEqual(column, {
defaultTo: null,
hasSetName: false,
isCreateDate: false,
isDeleteDate: false,
isHidden: false,
Expand Down

0 comments on commit b4d585b

Please sign in to comment.