loopbackio / loopbackio/loopback-next
New syntax for INDEX and FOREIGN KEY definition
Nadie ha tomado este issue todavía.
- Lenguaje dominante
- TypeScript
- Estrellas
- 5.1k
- Forks
- 1.1k
- Merge medio
- 2 d 21 h
- PR fusionados (30 d)
- 27
Descripción
This is a follow-up task for the proposal outlined in https://github.com/strongloop/loopback-next/issues/2712.
Indexes at property level
Support the following two short-hand forms only. Ask users to use model-level
form to define indexes that are more complex.
-
a "plain" index with no special configuration
@property({ type: 'string', index: true, }) email: string; -
UNIQUE index with no special configuration
@property({ type: 'string', unique: true, }) email: string;
Indexes at model level
At high-level, keep the current syntax where indexes are defined via a key-value
map stored in settings.indexes property, the key is the index name and the
value is an index definition object.
@model({
strict: false,
forceID: true,
indexes: {
uniqueEmail: {
// index definition
},
nameQueries: {
// index definition
},
},
})
class MyModel extends Entity {}
Individual indexes can be defined as follows:
-
Add a new field
propertiesas a key-value map from property names to
indexing order:// definition of an individual index emailIndex: { properties: { email: 1, // ASC createdAt: 'DESC', // alias for -1 bio: 'text', // database-specific value (MongoDB's "text") } }Important: property names are mapped to database column names when building
the index definition. -
Keep supporting
keysfield as a key-value map from database column name to
indexing order, see the description of the actual status below. Entries from
keysshould be merged with entries fromproperties,keystaking
precedence (replacingpropertiesentries). -
Keep supporting
uniquefield (set it totrueto let the index enforce
uniqueness). -
Database-specific options will be stored under a key with the connector name:
emailIndex: { properties: { email: 'ASC', }, mongodb: { sparse: true, }, mysql: { kind: 'fulltext', type: 'hash', }, postgresql: { type: 'hash', } }
Foreign keys at property level
Introduce a new property metadata "references" (inspired by ANSI SQL):
@property({
type: 'number',
required: true,
references: {
// a TypeResolver
model: () => Category,
// name of the target property
property: 'id',
// referential actions (optional)
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
}
})
categoryId: number;
Foreign keys at model level
Modify the current connector-dependant syntax to make it easier to read and
support composite foreign keys too.
@model({
foreignKeys: {
[keyName]: {
// optional, overrides keyName
name: 'constraint_name_for_db',
// Property name(s) (will be mapped to column name)
// formerly: foreignKey
sourceProperties: ['source property name'],
// formerly: entity
targetModel: 'TargetModel',
// Property name(s) (will be mapped to column name)
// formerly: entityKey
targetProperties: ['target property name'],
// referential actions (optional)
onUpdate: 'CASCADE',
onDelete: 'CASCADE',
},
},
})
class MyModel extends Entity {}
Acceptance criteria
-
Describe the new syntax in definition interfaces in model.ts (see the spike proposal for inspiration), include comprehensive API documentation. Make it clear that this new syntax is a work in progress and may not be supported by all connectors yet. Add links to relevant GitHub issues where people can track progress.
-
Modify
DefaultCrudRepositoryconstructor to process model-level indexes and foreign keys; it needs to fill the corresponding fields in juggler model and property settings. The actual index/fk definitions should be passed to juggler mostly as-is. -
Update examples/todo-list to define FK and UNIQUE constraints to support existing relation definitions.
-
Update loopback4-example-shopping to define FK and UNIQUE constraints to support existing relation definitions
-
Update CLI templates for relations to define the constraints too. If the pull request #2426 is not landed yet then create a follow-up story instead.
Guía de contribución
Primeros pasos
- Lee el issue completo y luego la guía de contribución del proyecto.
- Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
- Haz un fork del repositorio y trabaja en una rama.
- Abre un pull request que haga referencia al número del issue.
Línea de trabajo
Comienza con las interfaces de definición en model.ts y la propuesta de spike vinculada; después, sigue el constructor de DefaultCrudRepository. Revisa las actualizaciones de todo-list y loopback4-example-shopping, así como las plantillas de relaciones de la CLI. Se considera terminado cuando la sintaxis documentada, el procesamiento del repositorio, los ejemplos y los cambios aplicables en las plantillas cumplen todos los criterios de aceptación indicados.
Escrito por el modelo de indexación a partir del texto del issue.
Evaluación
- Stack tecnológico
- typescript
- Área
- backend-api-design, databases
- Tipo de issue
- Nueva funcionalidad
- Dificultad
- 5/5
- Tiempo estimado
- Más de una semana
- Estado de actividad
- Estancado
- Claridad
- Bastante claro
- Aptitud para principiantes
- 18/100