@node-in-layers/data 1.0.4 → 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +34 -141
- package/libs.d.ts +2 -2
- package/libs.js.map +1 -1
- package/package.json +11 -10
- package/services.js +48 -96
- package/services.js.map +1 -1
- package/types.d.ts +16 -18
- package/types.js.map +1 -1
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# Data - A Node In Layers Package used for handling data and databases.
|
|
2
2
|
|
|
3
|
-
This repository focuses on accessing and manipulating data, especially the ability to easily communicate with different databases. This package provides
|
|
3
|
+
This repository focuses on accessing and manipulating data, especially the ability to easily communicate with different databases. This package provides the "getModelProps" interface, for `@node-in-layers/core` so that Models can be backed with an ORM.
|
|
4
4
|
|
|
5
5
|
# How To Install
|
|
6
6
|
|
|
@@ -8,23 +8,27 @@ This repository focuses on accessing and manipulating data, especially the abili
|
|
|
8
8
|
|
|
9
9
|
## Supported Databases
|
|
10
10
|
|
|
11
|
-
-
|
|
12
|
-
-
|
|
13
|
-
-
|
|
14
|
-
-
|
|
15
|
-
-
|
|
16
|
-
-
|
|
17
|
-
-
|
|
11
|
+
- Mongo - [functional-models-orm-mongo](https://github.com/monolithst/functional-models-orm-mongo)
|
|
12
|
+
- MySql - [functional-models-orm-sql](https://github.com/monolithst/functional-models-orm-sql)
|
|
13
|
+
- ElasticSearch / OpenSearch - [functional-models-orm-elastic](https://github.com/monolithst/functional-models-orm-elastic)
|
|
14
|
+
- Postgresql - [functional-models-orm-sql](https://github.com/monolithst/functional-models-orm-sql)
|
|
15
|
+
- Sqlite - [functional-models-orm-sql](https://github.com/monolithst/functional-models-orm-sql)
|
|
16
|
+
- AWS Dynamo DB - [functional-models-orm-dynamo](https://github.com/monolithst/functional-models-orm-dynamo)
|
|
17
|
+
- In-Memory - [functional-models-orm-memory](https://github.com/monolithst/functional-models-orm-memory)
|
|
18
18
|
|
|
19
19
|
## How To Add To a Node In Layers System
|
|
20
20
|
|
|
21
21
|
To use this package you must do the following:
|
|
22
22
|
|
|
23
|
-
1. Add
|
|
23
|
+
1. Add this package to the `apps` property.
|
|
24
|
+
1. Add `modelFactory: "@node-in-layers/data"` to the core configuration.
|
|
24
25
|
1. Add a `@node-in-layers/data` section to your configuration file.
|
|
26
|
+
1. Optional/Recommended: Add `modelCruds:true` to the core configuration.
|
|
25
27
|
|
|
26
28
|
We recommend that you put the `data` app, as one of the earliest apps, it does not have any requirements, and subsequent packages likely want to use it.
|
|
27
29
|
|
|
30
|
+
We recommend that you put the `models` layer
|
|
31
|
+
|
|
28
32
|
#### Example inside a `config.dev.mjs` file:
|
|
29
33
|
|
|
30
34
|
```javascript
|
|
@@ -40,6 +44,10 @@ const core = {
|
|
|
40
44
|
layerOrder: ['services', 'features'],
|
|
41
45
|
logLevel: 'debug',
|
|
42
46
|
logFormat: 'json',
|
|
47
|
+
// Adds the automatic CRUD wrappers to service and features.
|
|
48
|
+
modelCruds: true,
|
|
49
|
+
// Makes the models backed by an orm.
|
|
50
|
+
modelFactory: '@node-in-layers/data',
|
|
43
51
|
}
|
|
44
52
|
|
|
45
53
|
const data = {
|
|
@@ -60,7 +68,7 @@ export default () => ({
|
|
|
60
68
|
|
|
61
69
|
### Multiple Database Support
|
|
62
70
|
|
|
63
|
-
This package has support for configuring multiple databases through the config file. There must be a "default" database, and any other database can be named and configured.
|
|
71
|
+
This package has support for configuring multiple databases through the config file. There must be a "default" database, and any other database can be named and configured. For models to be backed by the correct database, "customModelFactory" must be configured to say which models are going to be backed by the non-default database.
|
|
64
72
|
|
|
65
73
|
In the following example we configure 3 databases. 1 is the default, the 2nd is a database for "caching" and the 3rd is a database that has "legacy data" in it.
|
|
66
74
|
|
|
@@ -77,6 +85,18 @@ const core = {
|
|
|
77
85
|
layerOrder: ['services', 'features'],
|
|
78
86
|
logLevel: 'debug',
|
|
79
87
|
logFormat: 'json',
|
|
88
|
+
// Adds the automatic CRUD wrappers to service and features.
|
|
89
|
+
modelCruds: true,
|
|
90
|
+
// Makes the models backed by an orm.
|
|
91
|
+
modelFactory: '@node-in-layers/data',
|
|
92
|
+
customModelFactory: {
|
|
93
|
+
myApp: {
|
|
94
|
+
// Model named "MyModelsPluralName" is backed by the myCacheDb database
|
|
95
|
+
MyModelsPluralName: ['@node-in-layers/data', 'myCacheDb'],
|
|
96
|
+
// Model named "AnotherModels" is backed by the database myLegacyData
|
|
97
|
+
AnotherModels: ['@node-in-layers/data', 'myLegacyData'],
|
|
98
|
+
},
|
|
99
|
+
},
|
|
80
100
|
}
|
|
81
101
|
|
|
82
102
|
const data = {
|
|
@@ -107,143 +127,16 @@ export default () => ({
|
|
|
107
127
|
|
|
108
128
|
There are 2 recommended uses of this package.
|
|
109
129
|
|
|
110
|
-
1.
|
|
130
|
+
1. Through the CRUDS interface provided by `@node-in-layers/core`
|
|
111
131
|
1. Direct database access
|
|
112
132
|
|
|
113
|
-
###
|
|
114
|
-
|
|
115
|
-
The Model Cruds Interface (Create, Retrieve, Update, Delete, Search), is a series of wrappers over the top of the `functional-models-orm` framework, so that seamless integration with models can be used. Instead of having to work with the model instances directly, data can pass in and out of the database without having to know anything about how database interactions work.
|
|
116
|
-
|
|
117
|
-
This interface can quickly wrap models at the service level, feature level and above, making it very easy to create model based REST APIS.
|
|
118
|
-
|
|
119
|
-
#### A Quick Note About Designing a System Using Models
|
|
120
|
-
|
|
121
|
-
It has been our experience that data models can and should be used throughout the system, however, the "orm" part of the models, it is recommended that they only be used at the services layer. The best way to do this, is where you need "non-orm" model use (validation, meta data, etc), we recommend using the ["noop" DatastoreProvider](https://github.com/monolithst/functional-models-orm/blob/master/src/datastore/noop.ts) that is made available with the `functional-models-orm` framework.
|
|
122
|
-
|
|
123
|
-
In many other systems (such as Python's Django), when a developer uses the orm part of the modeling code (save/create/delete/etc), anywhere in the application, it can make it very difficult to optimize and understand bottlenecks of a system.
|
|
124
|
-
|
|
125
|
-
<b>Node In Layers was designed from the beginning to fix this problem, by keeping each 'kind of code' in their appropriate layers. Database accessing code should go in services.</b>
|
|
126
|
-
|
|
127
|
-
Here is how to use the Model Cruds Interface:
|
|
128
|
-
|
|
129
|
-
```typescript
|
|
130
|
-
import { loadSystem, Config, ServiceContext } from '@node-in-layers/core'
|
|
131
|
-
import { memoizeValue } from '@node-in-layers/core/libs'
|
|
132
|
-
import { DataNamespace, DataServicesLayer } from '@node-in-layers/data'
|
|
133
|
-
import { createSimpleServiceModelWrappers } from '@node-in-layers/data/libs'
|
|
134
|
-
import { OrmModelFactory, ormQueryBuilder } from 'functional-models-orm'
|
|
135
|
-
import { TextProperty, UniqueIdProperty } from 'functional-models'
|
|
136
|
-
import { FeaturesContext } from '@node-in-layers/core/index.js'
|
|
137
|
-
|
|
138
|
-
type MyCustomModel = Readonly<{
|
|
139
|
-
id: string
|
|
140
|
-
name: string
|
|
141
|
-
}>
|
|
142
|
-
|
|
143
|
-
type AnotherModel = Readonly<{
|
|
144
|
-
id: string
|
|
145
|
-
name: string
|
|
146
|
-
}>
|
|
147
|
-
|
|
148
|
-
const createMyModels = ({ Model }: { Model: OrmModelFactory }) => {
|
|
149
|
-
const MyCustomModels = Model<MyCustomModel>('MyCustomModels', {
|
|
150
|
-
properties: {
|
|
151
|
-
id: UniqueIdProperty({ required: true }),
|
|
152
|
-
name: TextProperty({ required: true }),
|
|
153
|
-
},
|
|
154
|
-
})
|
|
155
|
-
const AnotherModels = Model<AnotherModel>('AnotherModels', {
|
|
156
|
-
properties: {
|
|
157
|
-
id: UniqueIdProperty({ required: true }),
|
|
158
|
-
name: TextProperty({ required: true }),
|
|
159
|
-
},
|
|
160
|
-
})
|
|
161
|
-
return {
|
|
162
|
-
MyCustomModels,
|
|
163
|
-
AnotherModels,
|
|
164
|
-
}
|
|
165
|
-
}
|
|
133
|
+
### Cruds Interface
|
|
166
134
|
|
|
167
|
-
|
|
168
|
-
const myService = {
|
|
169
|
-
// Add DataServices as part of the context.
|
|
170
|
-
create: (context: ServiceContext<Config, DataServicesLayer>) => {
|
|
171
|
-
const dataContext = context.services[DataNamespace.root]
|
|
172
|
-
|
|
173
|
-
// A helper function so we don't have to get this more than once. We memoize it so its only executed once
|
|
174
|
-
const _getModels = memoizeValue(async () => {
|
|
175
|
-
// 1. Create your database instance/s
|
|
176
|
-
const databases = await dataContext.getDatabases()
|
|
177
|
-
|
|
178
|
-
// 2. Create an orm instance with your dbObjects
|
|
179
|
-
const orm = dataContext.getOrm(databases.default)
|
|
180
|
-
|
|
181
|
-
// 3. Create models
|
|
182
|
-
return createMyModels(orm)
|
|
183
|
-
})
|
|
184
|
-
|
|
185
|
-
// Example: CRUDS as a service - Here is a function makes the models available as service functions.
|
|
186
|
-
const getModelCruds = async () => {
|
|
187
|
-
const models = await _getModels()
|
|
188
|
-
// 4. Create Model Cruds Interface Wrapper.
|
|
189
|
-
const wrappedMyCustomModels = dataContext.modelCrudsService(
|
|
190
|
-
models.MyCustomModels
|
|
191
|
-
)
|
|
192
|
-
return {
|
|
193
|
-
[models.MyCustomModels.getName()]: wrappedMyCustomModels,
|
|
194
|
-
}
|
|
195
|
-
|
|
196
|
-
/* An alternative approach is wrapping all of them automagically */
|
|
197
|
-
return dataContext.modelCrudsServiceWrappers(models)
|
|
198
|
-
}
|
|
199
|
-
|
|
200
|
-
// Example: Using models in a service - Here is another function that uses the models to do a specific kind of search
|
|
201
|
-
const mySpecialSearch = async (value: string) => {
|
|
202
|
-
const models = await _getModels()
|
|
203
|
-
return models.AnotherModels.search(
|
|
204
|
-
ormQueryBuilder().property('name', value).take(1).compile()
|
|
205
|
-
).then(i => i.instances[0])
|
|
206
|
-
}
|
|
207
|
-
|
|
208
|
-
return {
|
|
209
|
-
getModelCruds,
|
|
210
|
-
mySpecialSearch,
|
|
211
|
-
}
|
|
212
|
-
},
|
|
213
|
-
}
|
|
214
|
-
|
|
215
|
-
// An Example consumer of the model cruds interface.
|
|
216
|
-
const myFeature = {
|
|
217
|
-
create: async (context: FeaturesContext<MyConfig>) => {
|
|
218
|
-
const myComplexFeature = async () => {
|
|
219
|
-
const myService = context.context['myService']
|
|
220
|
-
const modelServices = await myService.getModelServices()
|
|
221
|
-
// Creates and saves.
|
|
222
|
-
const myModel = await modelServices.MyCustomModels.create({
|
|
223
|
-
id: 'my-id',
|
|
224
|
-
name: 'my-custom-name',
|
|
225
|
-
})
|
|
226
|
-
|
|
227
|
-
// Get It back
|
|
228
|
-
const instance = await modelServices.MyCustomModels.retrieve('my-id')
|
|
229
|
-
|
|
230
|
-
// Use another service function that uses models.
|
|
231
|
-
const value = modelServices.mySpecialSearch(instance.name)
|
|
232
|
-
|
|
233
|
-
// Delete it.
|
|
234
|
-
await modelServices.myCustomModels.delete(instance.id)
|
|
235
|
-
return 'OK'
|
|
236
|
-
}
|
|
237
|
-
return {
|
|
238
|
-
myComplexFeature,
|
|
239
|
-
}
|
|
240
|
-
},
|
|
241
|
-
}
|
|
242
|
-
```
|
|
135
|
+
Look at the `@node-in-layers/core` interface for how to access CRUDS model functions.
|
|
243
136
|
|
|
244
137
|
### Direct Database Access
|
|
245
138
|
|
|
246
|
-
All of the objects that provide database access to `functional-models
|
|
139
|
+
All of the objects that provide database access to orm portion of `functional-models` is made available in the DatabaseObjects object that comes back from `getDatabases()`. These objects (such as `knexClient`, `opensearchClient`, `mongoClient`, etc) provide the ability to do direct queries on the database using the underlying objects.
|
|
247
140
|
|
|
248
141
|
### Important SQL Notice
|
|
249
142
|
|
package/libs.d.ts
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ModelType } from 'functional-models';
|
|
2
2
|
declare const getSystemInfrastructureName: ({ environment, systemName, component, }: {
|
|
3
3
|
component?: string | undefined;
|
|
4
4
|
environment: string;
|
|
5
5
|
systemName: string;
|
|
6
6
|
}) => string;
|
|
7
|
-
declare const defaultGetTableNameForModel: (environment: string, systemName: string, model:
|
|
7
|
+
declare const defaultGetTableNameForModel: (environment: string, systemName: string, model: ModelType<any> | string) => string;
|
|
8
8
|
declare const getMongoCollectionNameForModel: () => (model: any | string) => string;
|
|
9
9
|
export { getMongoCollectionNameForModel, defaultGetTableNameForModel, getSystemInfrastructureName, };
|
package/libs.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"libs.js","sourceRoot":"","sources":["../src/libs.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,MAAM,qBAAqB,CAAA;AAC3C,OAAO,QAAQ,MAAM,oBAAoB,CAAA;AAEzC,MAAM,2BAA2B,GAAG,CAAC,EACnC,WAAW,EACX,UAAU,EACV,SAAS,GAKV,EAAE,EAAE;IACH,OAAO,SAAS;QACd,CAAC,CAAC,SAAS,CAAC,GAAG,UAAU,IAAI,SAAS,IAAI,WAAW,EAAE,CAAC;QACxD,CAAC,CAAC,SAAS,CAAC,GAAG,UAAU,IAAI,WAAW,EAAE,CAAC,CAAA;AAC/C,CAAC,CAAA;AAED,MAAM,2BAA2B,GAAG,CAClC,WAAmB,EACnB,UAAkB,EAClB,
|
|
1
|
+
{"version":3,"file":"libs.js","sourceRoot":"","sources":["../src/libs.ts"],"names":[],"mappings":"AACA,OAAO,SAAS,MAAM,qBAAqB,CAAA;AAC3C,OAAO,QAAQ,MAAM,oBAAoB,CAAA;AAEzC,MAAM,2BAA2B,GAAG,CAAC,EACnC,WAAW,EACX,UAAU,EACV,SAAS,GAKV,EAAE,EAAE;IACH,OAAO,SAAS;QACd,CAAC,CAAC,SAAS,CAAC,GAAG,UAAU,IAAI,SAAS,IAAI,WAAW,EAAE,CAAC;QACxD,CAAC,CAAC,SAAS,CAAC,GAAG,UAAU,IAAI,WAAW,EAAE,CAAC,CAAA;AAC/C,CAAC,CAAA;AAED,MAAM,2BAA2B,GAAG,CAClC,WAAmB,EACnB,UAAkB,EAClB,KAA8B,EAC9B,EAAE;IACF,MAAM,SAAS,GAAG,QAAQ,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,EAAE,CAAA;IAC3D,OAAO,2BAA2B,CAAC,EAAE,UAAU,EAAE,WAAW,EAAE,SAAS,EAAE,CAAC,CAAA;AAC5E,CAAC,CAAA;AAED,MAAM,8BAA8B,GAAG,GAAG,EAAE,CAAC,CAAC,KAAmB,EAAE,EAAE;IACnE,IAAI,QAAQ,CAAC,KAAK,CAAC,EAAE,CAAC;QACpB,OAAO,SAAS,CAAC,KAAK,CAAC,CAAA;IACzB,CAAC;IACD,OAAO,SAAS,CAAC,KAAK,CAAC,OAAO,EAAE,CAAC,CAAA;AACnC,CAAC,CAAA;AAED,OAAO,EACL,8BAA8B,EAC9B,2BAA2B,EAC3B,2BAA2B,GAC5B,CAAA"}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@node-in-layers/data",
|
|
3
3
|
"type": "module",
|
|
4
|
-
"version": "1.0.
|
|
4
|
+
"version": "1.0.6",
|
|
5
5
|
"description": "A Node In Layers package used for handling databases.",
|
|
6
6
|
"main": "index.js",
|
|
7
7
|
"scripts": {
|
|
@@ -10,10 +10,11 @@
|
|
|
10
10
|
"commit": "cz",
|
|
11
11
|
"dist": "npm run build && cd dist && npm publish",
|
|
12
12
|
"eslint": "eslint .",
|
|
13
|
-
"feature-tests": "./node_modules/.bin/cucumber-js -p default",
|
|
14
13
|
"prettier": "prettier --write .",
|
|
14
|
+
"prettier:check": "prettier -c .",
|
|
15
15
|
"test": "export TS_NODE_PROJECT='./tsconfig.test.json' && mocha -r tsx ./test/src/*.test.ts ./test/src/**/*.test.ts ./test/src/**/**/*.test.ts",
|
|
16
|
-
"test:coverage": "nyc --all --reporter cobertura --reporter text --reporter lcov --reporter html npm run test"
|
|
16
|
+
"test:coverage": "nyc --all --reporter cobertura --reporter text --reporter lcov --reporter html npm run test",
|
|
17
|
+
"test:features": "./node_modules/.bin/cucumber-js -p default"
|
|
17
18
|
},
|
|
18
19
|
"config": {
|
|
19
20
|
"commitizen": {
|
|
@@ -64,14 +65,14 @@
|
|
|
64
65
|
"dependencies": {
|
|
65
66
|
"@aws-sdk/client-dynamodb": "^3.721.0",
|
|
66
67
|
"@aws-sdk/lib-dynamodb": "^3.721.0",
|
|
67
|
-
"@node-in-layers/core": "^1.1.
|
|
68
|
+
"@node-in-layers/core": "^1.1.6",
|
|
68
69
|
"@opensearch-project/opensearch": "^2.13.0",
|
|
69
|
-
"functional-models": "^
|
|
70
|
-
"functional-models-orm": "^
|
|
71
|
-
"functional-models-orm-
|
|
72
|
-
"functional-models-orm-
|
|
73
|
-
"functional-models-orm-mongo": "^
|
|
74
|
-
"functional-models-orm-sql": "^
|
|
70
|
+
"functional-models": "^3.0.12",
|
|
71
|
+
"functional-models-orm-dynamo": "^3.0.0",
|
|
72
|
+
"functional-models-orm-elastic": "^3.0.0",
|
|
73
|
+
"functional-models-orm-memory": "^3.0.0",
|
|
74
|
+
"functional-models-orm-mongo": "^3.0.0",
|
|
75
|
+
"functional-models-orm-sql": "^3.0.0",
|
|
75
76
|
"knex": "^3.1.0",
|
|
76
77
|
"lodash": "^4.17.21",
|
|
77
78
|
"modern-async": "^2.0.4",
|
package/services.js
CHANGED
|
@@ -8,8 +8,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
});
|
|
9
9
|
};
|
|
10
10
|
import https from 'node:https';
|
|
11
|
-
import {
|
|
12
|
-
import {
|
|
11
|
+
import { memoizeValueSync } from '@node-in-layers/core/utils.js';
|
|
12
|
+
import { createOrm, } from 'functional-models';
|
|
13
13
|
import { asyncMap } from 'modern-async';
|
|
14
14
|
import merge from 'lodash/merge.js';
|
|
15
15
|
import get from 'lodash/get.js';
|
|
@@ -20,18 +20,18 @@ import { MongoClient } from 'mongodb';
|
|
|
20
20
|
import knex from 'knex';
|
|
21
21
|
import curry from 'lodash/curry.js';
|
|
22
22
|
import omit from 'lodash/omit.js';
|
|
23
|
-
import {
|
|
24
|
-
import {
|
|
25
|
-
import {
|
|
26
|
-
import {
|
|
27
|
-
import
|
|
23
|
+
import { datastoreAdapter as dynamoDatastoreAdapter } from 'functional-models-orm-dynamo';
|
|
24
|
+
import { datastoreAdapter as opensearchDatastoreAdapter } from 'functional-models-orm-elastic';
|
|
25
|
+
import { datastoreAdapter as mongoDatastoreAdapter } from 'functional-models-orm-mongo';
|
|
26
|
+
import { datastoreAdapter as sqlDatastoreAdapter } from 'functional-models-orm-sql';
|
|
27
|
+
import { datastoreAdapter as memoryDatastoreAdapter } from 'functional-models-orm-memory';
|
|
28
28
|
import { SupportedDatabase, DataNamespace, } from './types.js';
|
|
29
29
|
import { getSystemInfrastructureName, defaultGetTableNameForModel, getMongoCollectionNameForModel, } from './libs.js';
|
|
30
30
|
const DEFAULT_MONGO_PORT = 27017;
|
|
31
31
|
const createMongoConnectionString = ({ host, port, username, password, }) => {
|
|
32
32
|
return `mongodb://${username ? `${username}:${password}@` : ''}${host}:${port || DEFAULT_MONGO_PORT}`;
|
|
33
33
|
};
|
|
34
|
-
const createMongoDatabaseObjects = ({ environment, systemName, host, port, username, password, getTableNameForModel, }) =>
|
|
34
|
+
const createMongoDatabaseObjects = ({ environment, systemName, host, port, username, password, getTableNameForModel, }) => {
|
|
35
35
|
const database = getSystemInfrastructureName({
|
|
36
36
|
environment,
|
|
37
37
|
systemName,
|
|
@@ -43,8 +43,8 @@ const createMongoDatabaseObjects = ({ environment, systemName, host, port, usern
|
|
|
43
43
|
password,
|
|
44
44
|
});
|
|
45
45
|
const mongoClient = new MongoClient(connectionString);
|
|
46
|
-
|
|
47
|
-
const
|
|
46
|
+
mongoClient.connect();
|
|
47
|
+
const datastoreAdapter = mongoDatastoreAdapter.create({
|
|
48
48
|
mongoClient,
|
|
49
49
|
databaseName: database,
|
|
50
50
|
getCollectionNameForModel: curry(getTableNameForModel || getMongoCollectionNameForModel)(environment, systemName),
|
|
@@ -54,15 +54,15 @@ const createMongoDatabaseObjects = ({ environment, systemName, host, port, usern
|
|
|
54
54
|
};
|
|
55
55
|
return {
|
|
56
56
|
mongoClient,
|
|
57
|
-
|
|
57
|
+
datastoreAdapter,
|
|
58
58
|
cleanup,
|
|
59
59
|
};
|
|
60
|
-
}
|
|
60
|
+
};
|
|
61
61
|
const createMemoryDatabaseObjects = () => {
|
|
62
|
-
const
|
|
62
|
+
const datastoreAdapter = memoryDatastoreAdapter.create();
|
|
63
63
|
return {
|
|
64
64
|
cleanup: () => Promise.resolve(),
|
|
65
|
-
|
|
65
|
+
datastoreAdapter,
|
|
66
66
|
};
|
|
67
67
|
};
|
|
68
68
|
const createOpensearchDatabaseObjects = ({ environment, systemName, username, password, host, getTableNameForModel, }) => {
|
|
@@ -73,7 +73,7 @@ const createOpensearchDatabaseObjects = ({ environment, systemName, username, pa
|
|
|
73
73
|
return {
|
|
74
74
|
cleanup: () => Promise.resolve(),
|
|
75
75
|
opensearchClient: client,
|
|
76
|
-
|
|
76
|
+
datastoreAdapter: opensearchDatastoreAdapter.create({
|
|
77
77
|
client,
|
|
78
78
|
getIndexForModel: curry(getTableNameForModel || defaultGetTableNameForModel)(environment, systemName),
|
|
79
79
|
}),
|
|
@@ -96,7 +96,7 @@ const createSqlDatabaseObjects = (props) => {
|
|
|
96
96
|
])) });
|
|
97
97
|
// @ts-ignore
|
|
98
98
|
const knexClient = knex(knexConfig);
|
|
99
|
-
const
|
|
99
|
+
const datastoreAdapter = sqlDatastoreAdapter.create({
|
|
100
100
|
knex: knexClient,
|
|
101
101
|
getTableNameForModel: curry(props.getTableNameForModel || defaultGetTableNameForModel)(props.environment, props.systemName),
|
|
102
102
|
//propertyTypeToParser: sqlParsers.BasicPropertyTypeToParser
|
|
@@ -104,7 +104,7 @@ const createSqlDatabaseObjects = (props) => {
|
|
|
104
104
|
return {
|
|
105
105
|
knexClient,
|
|
106
106
|
cleanup: () => Promise.resolve(),
|
|
107
|
-
|
|
107
|
+
datastoreAdapter,
|
|
108
108
|
};
|
|
109
109
|
};
|
|
110
110
|
const createDynamoDatabaseObjects = ({ awsRegion, environment, systemName, httpsAgentConfig, getTableNameForModel, }) => {
|
|
@@ -118,18 +118,18 @@ const createDynamoDatabaseObjects = ({ awsRegion, environment, systemName, https
|
|
|
118
118
|
};
|
|
119
119
|
const dynamoDbClient = new dynamo.DynamoDBClient(awsConfig);
|
|
120
120
|
const aws3 = Object.assign(Object.assign({}, dynamo), libDynamo);
|
|
121
|
-
const
|
|
121
|
+
const datastoreAdapter = dynamoDatastoreAdapter.create({
|
|
122
122
|
aws3: Object.assign(Object.assign({}, aws3), { dynamoDbClient }),
|
|
123
123
|
getTableNameForModel: curry(getTableNameForModel || defaultGetTableNameForModel)(environment, systemName),
|
|
124
124
|
});
|
|
125
125
|
return {
|
|
126
126
|
dynamoLibs: aws3,
|
|
127
127
|
dynamoDbClient,
|
|
128
|
-
|
|
128
|
+
datastoreAdapter,
|
|
129
129
|
cleanup: () => Promise.resolve(),
|
|
130
130
|
};
|
|
131
131
|
};
|
|
132
|
-
const
|
|
132
|
+
const _supportedToDatastoreAdapterFunc = {
|
|
133
133
|
[SupportedDatabase.memory]: createMemoryDatabaseObjects,
|
|
134
134
|
[SupportedDatabase.dynamo]: createDynamoDatabaseObjects,
|
|
135
135
|
[SupportedDatabase.mongo]: createMongoDatabaseObjects,
|
|
@@ -138,88 +138,30 @@ const _supportedToDatastoreProviderFunc = {
|
|
|
138
138
|
[SupportedDatabase.mysql]: createSqlDatabaseObjects,
|
|
139
139
|
[SupportedDatabase.postgres]: createSqlDatabaseObjects,
|
|
140
140
|
};
|
|
141
|
-
const createModelCrudsService = (model) => {
|
|
142
|
-
const update = (data) => {
|
|
143
|
-
return model
|
|
144
|
-
.create(data)
|
|
145
|
-
.save()
|
|
146
|
-
.then(instance => {
|
|
147
|
-
if (!instance) {
|
|
148
|
-
throw new Error(`Impossible situation`);
|
|
149
|
-
}
|
|
150
|
-
return instance.toObj();
|
|
151
|
-
});
|
|
152
|
-
};
|
|
153
|
-
const create = update;
|
|
154
|
-
const del = (id) => __awaiter(void 0, void 0, void 0, function* () {
|
|
155
|
-
const instance = yield model.retrieve(id);
|
|
156
|
-
if (!instance) {
|
|
157
|
-
return undefined;
|
|
158
|
-
}
|
|
159
|
-
yield instance.delete();
|
|
160
|
-
return undefined;
|
|
161
|
-
});
|
|
162
|
-
const retrieve = (id) => {
|
|
163
|
-
return model.retrieve(id).then(instance => {
|
|
164
|
-
if (!instance) {
|
|
165
|
-
return undefined;
|
|
166
|
-
}
|
|
167
|
-
return instance.toObj();
|
|
168
|
-
});
|
|
169
|
-
};
|
|
170
|
-
const search = (ormQuery) => {
|
|
171
|
-
return model.search(ormQuery).then((result) => __awaiter(void 0, void 0, void 0, function* () {
|
|
172
|
-
const instances = (yield asyncMap(result.instances, i => i.toObj()));
|
|
173
|
-
return {
|
|
174
|
-
instances,
|
|
175
|
-
page: result.page,
|
|
176
|
-
};
|
|
177
|
-
}));
|
|
178
|
-
};
|
|
179
|
-
return {
|
|
180
|
-
getModel: () => model,
|
|
181
|
-
create,
|
|
182
|
-
update,
|
|
183
|
-
delete: del,
|
|
184
|
-
retrieve,
|
|
185
|
-
search,
|
|
186
|
-
};
|
|
187
|
-
};
|
|
188
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
189
141
|
const create = (context) => {
|
|
190
142
|
const databases = get(context, 'config.@node-in-layers/data.databases');
|
|
191
143
|
if (!databases) {
|
|
192
144
|
throw new Error(`Must include "${DataNamespace.root}.databases" inside of a config that uses the "${DataNamespace.root}" namespace`);
|
|
193
145
|
}
|
|
194
|
-
const modelCrudsServices = createModelCrudsService;
|
|
195
|
-
const modelCrudsServiceWrappers = (models) => {
|
|
196
|
-
return merge(models.map(m => {
|
|
197
|
-
return {
|
|
198
|
-
[m.getName()]: createModelCrudsService(m),
|
|
199
|
-
};
|
|
200
|
-
}));
|
|
201
|
-
};
|
|
202
146
|
const getDatabaseObjects = (props) => {
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
return func(props);
|
|
209
|
-
});
|
|
147
|
+
const func = _supportedToDatastoreAdapterFunc[props.datastoreType];
|
|
148
|
+
if (!func) {
|
|
149
|
+
throw new Error(`Unhandled type ${props.datastoreType}`);
|
|
150
|
+
}
|
|
151
|
+
return func(props);
|
|
210
152
|
};
|
|
211
153
|
const getOrm = (props) => {
|
|
212
|
-
const { Model, fetcher } =
|
|
154
|
+
const { Model, fetcher } = createOrm(props);
|
|
213
155
|
return {
|
|
214
156
|
Model,
|
|
215
157
|
fetcher,
|
|
216
158
|
};
|
|
217
159
|
};
|
|
218
160
|
const cleanup = () => __awaiter(void 0, void 0, void 0, function* () {
|
|
219
|
-
const databases =
|
|
220
|
-
yield asyncMap(Object.values(databases), d => d.cleanup(), 1);
|
|
161
|
+
const databases = getDatabases();
|
|
162
|
+
yield asyncMap(Object.values(databases), (d) => d.cleanup(), 1);
|
|
221
163
|
});
|
|
222
|
-
const _getDatabases = () =>
|
|
164
|
+
const _getDatabases = () => {
|
|
223
165
|
const neededProps = {
|
|
224
166
|
environment: context.config.environment,
|
|
225
167
|
systemName: context.config.systemName,
|
|
@@ -229,24 +171,34 @@ const create = (context) => {
|
|
|
229
171
|
const otherProps2 = Object.entries(otherProps).reduce((acc, [x, y]) => {
|
|
230
172
|
return merge(acc, { [x]: merge(y, neededProps) });
|
|
231
173
|
}, {});
|
|
232
|
-
const defaultDb =
|
|
233
|
-
const otherDatabases =
|
|
234
|
-
const
|
|
235
|
-
const dbObjects = yield getDatabaseObjects(props[1]);
|
|
174
|
+
const defaultDb = getDatabaseObjects(defaultDbProps);
|
|
175
|
+
const otherDatabases = Object.entries(otherProps2).reduce((acc, props) => {
|
|
176
|
+
const dbObjects = getDatabaseObjects(props[1]);
|
|
236
177
|
return merge(acc, {
|
|
237
178
|
[props[0]]: dbObjects,
|
|
238
179
|
});
|
|
239
|
-
}
|
|
180
|
+
}, {});
|
|
240
181
|
return Object.assign({ default: defaultDb }, otherDatabases);
|
|
241
|
-
}
|
|
242
|
-
const getDatabases = (
|
|
182
|
+
};
|
|
183
|
+
const getDatabases = memoizeValueSync(_getDatabases);
|
|
184
|
+
const getModelProps = (context, datastoreName) => {
|
|
185
|
+
datastoreName = datastoreName || 'default';
|
|
186
|
+
const database = getDatabases()[datastoreName];
|
|
187
|
+
if (!database) {
|
|
188
|
+
throw new Error(`No database named ${datastoreName}`);
|
|
189
|
+
}
|
|
190
|
+
const orm = getOrm(database);
|
|
191
|
+
return {
|
|
192
|
+
Model: orm.Model,
|
|
193
|
+
fetcher: orm.fetcher,
|
|
194
|
+
};
|
|
195
|
+
};
|
|
243
196
|
return {
|
|
244
197
|
getDatabaseObjects,
|
|
245
198
|
getOrm,
|
|
246
199
|
getDatabases,
|
|
247
200
|
cleanup,
|
|
248
|
-
|
|
249
|
-
modelCrudsServiceWrappers,
|
|
201
|
+
getModelProps,
|
|
250
202
|
};
|
|
251
203
|
};
|
|
252
204
|
export { create };
|
package/services.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"services.js","sourceRoot":"","sources":["../src/services.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,KAAK,MAAM,YAAY,CAAA;AAC9B,OAAO,EAAE,
|
|
1
|
+
{"version":3,"file":"services.js","sourceRoot":"","sources":["../src/services.ts"],"names":[],"mappings":";;;;;;;;;AAAA,OAAO,KAAK,MAAM,YAAY,CAAA;AAC9B,OAAO,EAAE,gBAAgB,EAAE,MAAM,+BAA+B,CAAA;AAEhE,OAAO,EAEL,SAAS,GAEV,MAAM,mBAAmB,CAAA;AAC1B,OAAO,EAAE,QAAQ,EAAE,MAAM,cAAc,CAAA;AACvC,OAAO,KAAK,MAAM,iBAAiB,CAAA;AACnC,OAAO,GAAG,MAAM,eAAe,CAAA;AAC/B,OAAO,KAAK,MAAM,MAAM,0BAA0B,CAAA;AAClD,OAAO,KAAK,SAAS,MAAM,uBAAuB,CAAA;AAClD,OAAO,EAAE,MAAM,IAAI,gBAAgB,EAAE,MAAM,gCAAgC,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|
3
|
-
import { Config } from '@node-in-layers/core/index.js';
|
|
1
|
+
import { DataDescription, ModelType, DatastoreAdapter, OrmModel, OrmSearch, Orm } from 'functional-models';
|
|
2
|
+
import { Config, GetModelPropsFunc } from '@node-in-layers/core/index.js';
|
|
4
3
|
declare enum DataNamespace {
|
|
5
4
|
root = "@node-in-layers/data"
|
|
6
5
|
}
|
|
@@ -16,7 +15,7 @@ declare enum SupportedDatabase {
|
|
|
16
15
|
type BasicDatabaseProps = Readonly<{
|
|
17
16
|
environment: string;
|
|
18
17
|
systemName: string;
|
|
19
|
-
getTableNameForModel?: (systemName: string, environment: string, model:
|
|
18
|
+
getTableNameForModel?: (systemName: string, environment: string, model: ModelType<any>) => string;
|
|
20
19
|
}>;
|
|
21
20
|
type KnexConfigProps = Readonly<{
|
|
22
21
|
username?: string;
|
|
@@ -51,7 +50,7 @@ type DynamoDatabaseObjectsProps = Readonly<{
|
|
|
51
50
|
type DatabaseObjectsProps = Readonly<{
|
|
52
51
|
datastoreType: SupportedDatabase;
|
|
53
52
|
}> & BasicDatabaseProps & (DynamoDatabaseObjectsProps | OpensearchDatabaseObjectsProps | MongoDatabaseObjectsProps | SqlDatabaseObjectsProps);
|
|
54
|
-
type SearchResult<T extends
|
|
53
|
+
type SearchResult<T extends DataDescription> = Readonly<{
|
|
55
54
|
instances: readonly T[];
|
|
56
55
|
page?: any;
|
|
57
56
|
}>;
|
|
@@ -74,9 +73,9 @@ type DataConfig = Config & {
|
|
|
74
73
|
*/
|
|
75
74
|
type DatabaseObjects<T extends object = object> = {
|
|
76
75
|
/**
|
|
77
|
-
* This
|
|
76
|
+
* This datastoreAdapter is used for backing the ORM system.
|
|
78
77
|
*/
|
|
79
|
-
|
|
78
|
+
datastoreAdapter: DatastoreAdapter;
|
|
80
79
|
/**
|
|
81
80
|
* A cleanup function that should run at the end of the application, that cleans up database connections.
|
|
82
81
|
*/
|
|
@@ -85,7 +84,7 @@ type DatabaseObjects<T extends object = object> = {
|
|
|
85
84
|
/**
|
|
86
85
|
* An interface for making CRUDS (create/retrieve/update/delete/search) commands into a database.
|
|
87
86
|
*/
|
|
88
|
-
type ModelCrudsInterface<T extends
|
|
87
|
+
type ModelCrudsInterface<T extends DataDescription> = Readonly<{
|
|
89
88
|
/**
|
|
90
89
|
* Gets the underlying model.
|
|
91
90
|
*/
|
|
@@ -114,19 +113,16 @@ type ModelCrudsInterface<T extends FunctionalModel> = Readonly<{
|
|
|
114
113
|
* Searches the corresponding table for this item.
|
|
115
114
|
* @param ormQuery
|
|
116
115
|
*/
|
|
117
|
-
search: (ormQuery:
|
|
116
|
+
search: (ormQuery: OrmSearch) => Promise<SearchResult<T>>;
|
|
118
117
|
}>;
|
|
119
118
|
/**
|
|
120
119
|
* Data services.
|
|
121
120
|
*/
|
|
122
121
|
type DataServices = Readonly<{
|
|
123
|
-
getDatabaseObjects: (props: DatabaseObjectsProps) =>
|
|
122
|
+
getDatabaseObjects: (props: DatabaseObjectsProps) => DatabaseObjects;
|
|
124
123
|
getOrm: (props: {
|
|
125
|
-
|
|
126
|
-
}) =>
|
|
127
|
-
Model: OrmModelFactory;
|
|
128
|
-
fetcher: ModelFetcher;
|
|
129
|
-
};
|
|
124
|
+
datastoreAdapter: DatastoreAdapter;
|
|
125
|
+
}) => Orm;
|
|
130
126
|
/**
|
|
131
127
|
* Gets all databases. This is memoized, so on the first attempt, it will create connections to 1 or more databases
|
|
132
128
|
* and then give you access to those database objects for further use. Very useful in a services layer.
|
|
@@ -136,8 +132,10 @@ type DataServices = Readonly<{
|
|
|
136
132
|
* Runs cleanup on every database connection. Only run when the application is ending.
|
|
137
133
|
*/
|
|
138
134
|
cleanup: () => Promise<void>;
|
|
139
|
-
|
|
140
|
-
|
|
135
|
+
/**
|
|
136
|
+
* A function that gives ModelProps. This is useful for getting enabling ORM based models.
|
|
137
|
+
*/
|
|
138
|
+
getModelProps: GetModelPropsFunc;
|
|
141
139
|
}>;
|
|
142
140
|
/**
|
|
143
141
|
* The services for the Data package.
|
|
@@ -149,7 +147,7 @@ type DataServicesLayer = Readonly<{
|
|
|
149
147
|
* The Features for the Data package.
|
|
150
148
|
*/
|
|
151
149
|
type DataFeatures = Readonly<{
|
|
152
|
-
wrapModelCrudsService: <T extends
|
|
150
|
+
wrapModelCrudsService: <T extends DataDescription>(modelCruds: ModelCrudsInterface<T>, overrides: Partial<ModelCrudsInterface<T>>) => ModelCrudsInterface<T>;
|
|
153
151
|
wrapAllModelCrudsServices: (objs: Record<string, ModelCrudsInterface<any>>, overrides?: Record<string, ModelCrudsInterface<any>>) => Record<string, ModelCrudsInterface<any>>;
|
|
154
152
|
}>;
|
|
155
153
|
/**
|
package/types.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAUA,IAAK,aAEJ;AAFD,WAAK,aAAa;IAChB,8CAA6B,CAAA;AAC/B,CAAC,EAFI,aAAa,KAAb,aAAa,QAEjB;AAED,IAAK,iBAQJ;AARD,WAAK,iBAAiB;IACpB,sCAAiB,CAAA;IACjB,sCAAiB,CAAA;IACjB,oCAAe,CAAA;IACf,8CAAyB,CAAA;IACzB,oCAAe,CAAA;IACf,0CAAqB,CAAA;IACrB,sCAAiB,CAAA;AACnB,CAAC,EARI,iBAAiB,KAAjB,iBAAiB,QAQrB;AA2MD,OAAO,EAIL,aAAa,EACb,iBAAiB,GAiBlB,CAAA"}
|