DbModel - do-/node-doix-db GitHub Wiki
DbModel is an object representing the logical structure of a database.
Constructor
const myDB = // initialize connection
const myModel = new DbModel ({
db: myDB,
// src: '/my/project/db/model', // {root}
// src: ['/my/project/db/model'], // [{root}]
src: [
{
// name: null,
// schemaName: 'public',
root: // [
'/my/project/db/model',
// ...
// ],
// filter: (str, arr) => arr.at (-2) === 'Model', // **/Model/*
// ext: '.js',
// merger: new myDbObjectMerger (someMergingOptions),
},
...
]
})
If db is set, the model injects there a reference to itself: myModel.db.model === myModel.
More on the src option
In general, src is a list of elements corresponding to individual schemata entries.
If the model contains only one schema, src may be a single object instead of a 1-element Array.
A string value may be used instead of the Object element: in this case, the it is considered the root value.
Any model contains the default schema which name is null. The completely missing src acts as src: [{name: null}].
| Name | Type | Description |
|---|---|---|
name |
String | The logical schema name. null for the default schema. Must be unique across the model. |
schemaName |
String | The physical schema name. If omitted, defaults to name. Must be unique across the model. |
root |
String or [String] |
see DbSchemaSource |
filter |
(String, Array) => Boolean |
see DbSchemaSource |
ext |
String |
see DbSchemaSource |
merger |
ObjectMerger | see DbSchemaSource |
Properties
| Name | Type | Description |
|---|---|---|
schemata |
Map | Collection of DbSchemas of the database described |
defaultSchema |
DbSchema | The default schema behind this model, alias for this.getSchema (null) |
db |
DbPool | Source of physical connections to the database |
lang |
DbLang | Normally, a copy of db.lang made by loadModules () |
Events
| Name | AKA | Description |
|---|---|---|
'objects-created' |
DbModel.EV_OBJECTS_CREATED |
Emitted at end of createObjects (). By default, the resolveReferences () invoker is subscribed here. |
Methods
User API
Initialization
loadModules
This synchronous method scans schemata and calls schema.src.load () wherever available (thus, loading schema content from module files). At the end, the 'objects-created' event is emitted.
Run time
assignData
For a given record Object and an Array of DbTable names, copies the .data property from each of them to record under the corresponding name. Returns the modified record.
createQuery
This synchronous method takes no parameters and returns a new instance of DbQuery for the underlying database.
find
For a given name in the defaultSchema or ${schemaName}.${objectName}, returns the corresponding DbObject or undefined unless it exists.
objects
This synchronous generator provides the sequence conaining all DbObjects in all schemas of the model.
Internal
addSchema
For a given src element (see above), creates a new DbSchema and stores it into this.schemata.
getSchema
For a given name, returns the corresponding DbSchema.
resolveReferences
This synchronous method scans this.map for DbRelation instances and completes initialization of DbReferences of their columns by setting targetRelation / targetColumnName / targetColumn and copying targetColumn.type to the cost column type.