Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
452 additions
and
32 deletions.
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
packages/cli/generators/discover/import-discovered-model.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
// Copyright IBM Corp. 2019. All Rights Reserved. | ||
// Node module: @loopback/cli | ||
// This file is licensed under the MIT License. | ||
// License text available at https://opensource.org/licenses/MIT | ||
|
||
'use strict'; | ||
|
||
const {pascalCase, stringifyModelSettings} = require('../../lib/utils'); | ||
const {sanitizeProperty} = require('../../lib/model-discoverer'); | ||
const { | ||
createPropertyTemplateData, | ||
findBuiltinType, | ||
} = require('../model/property-definition'); | ||
|
||
module.exports = { | ||
importDiscoveredModel, | ||
}; | ||
|
||
/** | ||
* Convert model definition created by loopback-datasource-juggler discovery | ||
* into template data used by LB4 model generator. | ||
* | ||
* @param {object} discoveredDefinition Model definition as discovered from DB | ||
* @returns {object} Template data for model source file template | ||
*/ | ||
function importDiscoveredModel(discoveredDefinition) { | ||
const modelName = discoveredDefinition.name; | ||
const templateData = { | ||
name: modelName, | ||
className: pascalCase(modelName), | ||
modelBaseClass: 'Entity', | ||
isModelBaseBuiltin: true, | ||
settings: importModelSettings(discoveredDefinition.settings), | ||
properties: importModelProperties(discoveredDefinition.properties), | ||
allowAdditionalProperties: true, | ||
}; | ||
|
||
templateData.modelSettings = stringifyModelSettings(templateData.settings); | ||
|
||
return templateData; | ||
} | ||
|
||
function importModelSettings(discoveredSettings = {}) { | ||
// Currently a no-op, we may want to apply transformation in the future | ||
// See migrateModelSettings in ../import-lb3-models/migrate-model.js | ||
return { | ||
// Shallow-clone to avoid accidental modification of input data | ||
...discoveredSettings, | ||
}; | ||
} | ||
|
||
function importModelProperties(discoveredProps) { | ||
const templateData = {}; | ||
for (const prop in discoveredProps) { | ||
templateData[prop] = importPropertyDefinition(discoveredProps[prop]); | ||
} | ||
return templateData; | ||
} | ||
|
||
function importPropertyDefinition(discoveredDefinition) { | ||
const propDef = { | ||
...discoveredDefinition, | ||
}; | ||
|
||
const builtinType = findBuiltinType(propDef.type); | ||
if (builtinType) propDef.type = builtinType; | ||
|
||
sanitizeProperty(propDef); | ||
return createPropertyTemplateData(propDef); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
packages/cli/snapshots/integration/generators/discover.integration.snapshots.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
// IMPORTANT | ||
// This snapshot file is auto-generated, but designed for humans. | ||
// It should be checked into source control and tracked carefully. | ||
// Re-generate by setting UPDATE_SNAPSHOTS=1 and running tests. | ||
// Make sure to inspect the changes in the snapshots below. | ||
// Do not ignore changes! | ||
|
||
'use strict'; | ||
|
||
exports[`lb4 discover integration model discovery generates all models without prompts using --all --dataSource 1`] = ` | ||
import {Entity, model, property} from '@loopback/repository'; | ||
@model() | ||
export class Schema extends Entity { | ||
// Define well-known properties here | ||
// Indexer property to allow additional data | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
[prop: string]: any; | ||
constructor(data?: Partial<Schema>) { | ||
super(data); | ||
} | ||
} | ||
export interface SchemaRelations { | ||
// describe navigational properties here | ||
} | ||
export type SchemaWithRelations = Schema & SchemaRelations; | ||
`; | ||
|
||
|
||
exports[`lb4 discover integration model discovery generates all models without prompts using --all --dataSource 2`] = ` | ||
import {Entity, model, property} from '@loopback/repository'; | ||
@model() | ||
export class View extends Entity { | ||
// Define well-known properties here | ||
// Indexer property to allow additional data | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
[prop: string]: any; | ||
constructor(data?: Partial<View>) { | ||
super(data); | ||
} | ||
} | ||
export interface ViewRelations { | ||
// describe navigational properties here | ||
} | ||
export type ViewWithRelations = View & ViewRelations; | ||
`; | ||
|
||
|
||
exports[`lb4 discover integration model discovery uses a different --outDir if provided 1`] = ` | ||
import {Entity, model, property} from '@loopback/repository'; | ||
@model() | ||
export class Test extends Entity { | ||
@property({ | ||
type: 'date', | ||
}) | ||
dateTest?: string; | ||
@property({ | ||
type: 'number', | ||
}) | ||
numberTest?: number; | ||
@property({ | ||
type: 'string', | ||
}) | ||
stringTest?: string; | ||
@property({ | ||
type: 'boolean', | ||
}) | ||
booleanText?: boolean; | ||
@property({ | ||
type: 'number', | ||
required: true, | ||
scale: 0, | ||
id: 1, | ||
}) | ||
id: number; | ||
// Define well-known properties here | ||
// Indexer property to allow additional data | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
[prop: string]: any; | ||
constructor(data?: Partial<Test>) { | ||
super(data); | ||
} | ||
} | ||
export interface TestRelations { | ||
// describe navigational properties here | ||
} | ||
export type TestWithRelations = Test & TestRelations; | ||
`; |
55 changes: 55 additions & 0 deletions
55
packages/cli/snapshots/integration/generators/model.integration.snapshots.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
// IMPORTANT | ||
// This snapshot file is auto-generated, but designed for humans. | ||
// It should be checked into source control and tracked carefully. | ||
// Re-generate by setting UPDATE_SNAPSHOTS=1 and running tests. | ||
// Make sure to inspect the changes in the snapshots below. | ||
// Do not ignore changes! | ||
|
||
'use strict'; | ||
|
||
exports[`lb4 model integration discovers a model from a datasource 1`] = ` | ||
import {Entity, model, property} from '@loopback/repository'; | ||
@model() | ||
export class Test extends Entity { | ||
@property({ | ||
type: 'date', | ||
}) | ||
dateTest?: string; | ||
@property({ | ||
type: 'number', | ||
}) | ||
numberTest?: number; | ||
@property({ | ||
type: 'string', | ||
}) | ||
stringTest?: string; | ||
@property({ | ||
type: 'boolean', | ||
}) | ||
booleanText?: boolean; | ||
@property({ | ||
type: 'number', | ||
required: true, | ||
scale: 0, | ||
id: 1, | ||
}) | ||
id: number; | ||
constructor(data?: Partial<Test>) { | ||
super(data); | ||
} | ||
} | ||
export interface TestRelations { | ||
// describe navigational properties here | ||
} | ||
export type TestWithRelations = Test & TestRelations; | ||
`; |
Oops, something went wrong.