bd-orm 0.0.1
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/.github/workflows/build-and-test.yml +26 -0
- package/.github/workflows/publish.yml +36 -0
- package/.prettierrc.json +14 -0
- package/CHANGELOG.md +3 -0
- package/README.md +5 -0
- package/check NBR en TODO.txt +32 -0
- package/dist/_/BdORMBase.d.ts +111 -0
- package/dist/_/BdORMBase.js +262 -0
- package/dist/_/BdORMCrud.d.ts +51 -0
- package/dist/_/BdORMCrud.js +223 -0
- package/dist/_/BdORMError.d.ts +4 -0
- package/dist/_/BdORMError.js +14 -0
- package/dist/_/BdOrmConnection.d.ts +32 -0
- package/dist/_/BdOrmConnection.js +5 -0
- package/dist/_/cdsBaseOrm.d.ts +12 -0
- package/dist/_/cdsBaseOrm.js +21 -0
- package/dist/dbConnections/BdOrmDbConnectionCapHana/index.d.ts +26 -0
- package/dist/dbConnections/BdOrmDbConnectionCapHana/index.js +78 -0
- package/dist/dbConnections/BdOrmDbConnectionCapHana/utils.d.ts +14 -0
- package/dist/dbConnections/BdOrmDbConnectionCapHana/utils.js +49 -0
- package/dist/dbConnections/BdOrmDbConnectionCapSqlite/index.d.ts +22 -0
- package/dist/dbConnections/BdOrmDbConnectionCapSqlite/index.js +85 -0
- package/dist/dbConnections/BdOrmDbConnectionCapSqlite/utils.d.ts +3 -0
- package/dist/dbConnections/BdOrmDbConnectionCapSqlite/utils.js +74 -0
- package/dist/dbConnections/BdOrmDbConnectionSQLBase.d.ts +32 -0
- package/dist/dbConnections/BdOrmDbConnectionSQLBase.js +99 -0
- package/dist/index.d.ts +8 -0
- package/dist/index.js +18 -0
- package/dist/test-assets/Post.d.ts +12 -0
- package/dist/test-assets/Post.js +13 -0
- package/dist/test-assets/User.d.ts +16 -0
- package/dist/test-assets/User.js +13 -0
- package/dist/test-assets/jestUtils.d.ts +5 -0
- package/dist/test-assets/jestUtils.js +30 -0
- package/jest.config.js +7 -0
- package/package.json +53 -0
- package/src/_/BdORMBase.ts +288 -0
- package/src/_/BdORMCrud.test.ts +198 -0
- package/src/_/BdORMCrud.ts +240 -0
- package/src/_/BdORMError.ts +10 -0
- package/src/_/BdORMbase.test.ts +166 -0
- package/src/_/BdOrmConnection.ts +39 -0
- package/src/_/cdsBaseOrm.ts +18 -0
- package/src/dbConnections/BdOrmDbConnectionCapHana/index.ts +95 -0
- package/src/dbConnections/BdOrmDbConnectionCapHana/utils.ts +47 -0
- package/src/dbConnections/BdOrmDbConnectionCapHana.test.ts +18 -0
- package/src/dbConnections/BdOrmDbConnectionCapSqlite/index.ts +89 -0
- package/src/dbConnections/BdOrmDbConnectionCapSqlite/utils.ts +75 -0
- package/src/dbConnections/BdOrmDbConnectionCapSqlite.test.ts +41 -0
- package/src/dbConnections/BdOrmDbConnectionSQLBase.ts +124 -0
- package/src/index.ts +8 -0
- package/src/test-assets/Post.ts +16 -0
- package/src/test-assets/User.ts +18 -0
- package/src/test-assets/db/BdOrm.cds +20 -0
- package/src/test-assets/db/csv/bdorm-user.csv +3 -0
- package/src/test-assets/db/views/user.hdbview +3 -0
- package/src/test-assets/jestUtils.ts +24 -0
- package/tsconfig.json +16 -0
- package/validate-package.js +113 -0
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
name: build and test
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
pull_request:
|
|
5
|
+
branches: [main]
|
|
6
|
+
paths:
|
|
7
|
+
- 'src/**'
|
|
8
|
+
- 'package.json'
|
|
9
|
+
- 'package-lock.json'
|
|
10
|
+
|
|
11
|
+
jobs:
|
|
12
|
+
build-and-test:
|
|
13
|
+
runs-on: ubuntu-latest
|
|
14
|
+
steps:
|
|
15
|
+
- uses: actions/checkout@v3
|
|
16
|
+
- uses: actions/setup-node@v3
|
|
17
|
+
with:
|
|
18
|
+
node-version: 20
|
|
19
|
+
- name: install dependencies
|
|
20
|
+
run: npm ci
|
|
21
|
+
- run: npm run build
|
|
22
|
+
env:
|
|
23
|
+
CI: true
|
|
24
|
+
- run: npm test
|
|
25
|
+
env:
|
|
26
|
+
CI: true
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
name: Publish to npm
|
|
2
|
+
|
|
3
|
+
on:
|
|
4
|
+
release:
|
|
5
|
+
types: [published]
|
|
6
|
+
|
|
7
|
+
jobs:
|
|
8
|
+
publish:
|
|
9
|
+
runs-on: ubuntu-latest
|
|
10
|
+
|
|
11
|
+
steps:
|
|
12
|
+
- uses: actions/checkout@v4
|
|
13
|
+
|
|
14
|
+
- name: Set up Node.js
|
|
15
|
+
uses: actions/setup-node@v4
|
|
16
|
+
with:
|
|
17
|
+
node-version: 20
|
|
18
|
+
registry-url: 'https://registry.npmjs.org/'
|
|
19
|
+
|
|
20
|
+
- name: Install dependencies
|
|
21
|
+
run: npm ci
|
|
22
|
+
|
|
23
|
+
- name: run tests
|
|
24
|
+
run: npm test
|
|
25
|
+
env:
|
|
26
|
+
CI: true
|
|
27
|
+
|
|
28
|
+
- name: Build
|
|
29
|
+
run: npm run build
|
|
30
|
+
env:
|
|
31
|
+
CI: true
|
|
32
|
+
|
|
33
|
+
- name: Publish
|
|
34
|
+
run: npm publish --access public
|
|
35
|
+
env:
|
|
36
|
+
NODE_AUTH_TOKEN: ${{ secrets.NPM_PUBLISH }}
|
package/.prettierrc.json
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
{
|
|
2
|
+
"semi": true,
|
|
3
|
+
"trailingComma": "all",
|
|
4
|
+
"singleQuote": true,
|
|
5
|
+
"printWidth": 120,
|
|
6
|
+
"tabWidth": 4,
|
|
7
|
+
"useTabs": false,
|
|
8
|
+
"arrowParens": "avoid",
|
|
9
|
+
"bracketSpacing": true,
|
|
10
|
+
"endOfLine": "auto",
|
|
11
|
+
"importOrderSeparation": true,
|
|
12
|
+
"importOrderSortSpecifiers": true,
|
|
13
|
+
"importOrderParserPlugins": ["typescript", "decorators-legacy"]
|
|
14
|
+
}
|
package/CHANGELOG.md
ADDED
package/README.md
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
gebruik van:
|
|
2
|
+
- joi er uit
|
|
3
|
+
|
|
4
|
+
renames:
|
|
5
|
+
- getChangeFields -> getChangedProperties
|
|
6
|
+
- isChanged -> hasPropertyValueChanged
|
|
7
|
+
- countRows -> count
|
|
8
|
+
|
|
9
|
+
Kijken of dit een specifiek nbr model moet worden:
|
|
10
|
+
- check UPDATED_DATE en setChangeDateToCurrentDate
|
|
11
|
+
- getTableName
|
|
12
|
+
- FOREIGN_KEY en getForeignKey
|
|
13
|
+
|
|
14
|
+
Controleren:
|
|
15
|
+
- contructor origineel > kan niet met mixens overweg ?
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
r uit:
|
|
19
|
+
- get CDS_MODEL
|
|
20
|
+
- cdsSelect()
|
|
21
|
+
- deleteModels
|
|
22
|
+
- registerModel
|
|
23
|
+
- getSubModel
|
|
24
|
+
- callbackPromise
|
|
25
|
+
- createError
|
|
26
|
+
- isModelError
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
functie params gewijzigd:
|
|
30
|
+
- duplicate was duplicate(deepcopy, { newData, excludeColumns }) is nu duplicate({ asDraft, newData, excludeColumns})
|
|
31
|
+
|
|
32
|
+
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
declare const definePropertyDescriptors: (instance: BdORMBase, data: Record<string, any>) => void;
|
|
2
|
+
/**
|
|
3
|
+
* Class which only contains the getters and setters for the base ORM
|
|
4
|
+
*/
|
|
5
|
+
declare abstract class BdORMBase {
|
|
6
|
+
/**
|
|
7
|
+
* Properties will be defined in the constructor (see definePropertyDescriptors)
|
|
8
|
+
*/
|
|
9
|
+
[key: string]: any;
|
|
10
|
+
/**
|
|
11
|
+
* @returns the model name
|
|
12
|
+
*/
|
|
13
|
+
static getModelName(): string;
|
|
14
|
+
constructor(data?: Record<string, any>);
|
|
15
|
+
/**
|
|
16
|
+
* This method is ONLY used in the FETCH method
|
|
17
|
+
* @returns the columns to fetch from the database
|
|
18
|
+
*/
|
|
19
|
+
static readonly COLUMNS_TO_FETCH: string | string[];
|
|
20
|
+
/**
|
|
21
|
+
* which default methods are not allowed to be used
|
|
22
|
+
* for example: ['delete'] will prevent you from using model.delete();
|
|
23
|
+
*/
|
|
24
|
+
static readonly METHODS_NOT_ALLOWED: string[];
|
|
25
|
+
/**
|
|
26
|
+
* Get the primary key for the current table
|
|
27
|
+
* @returns the primary key for the current table
|
|
28
|
+
*/
|
|
29
|
+
static readonly PRIMARY_KEY: string;
|
|
30
|
+
/**
|
|
31
|
+
* Specify which properties should be set as read-only.
|
|
32
|
+
* These properties can only be set once, when the object is created.
|
|
33
|
+
* @return array
|
|
34
|
+
*/
|
|
35
|
+
static readonly PROPERTIES_NOT_ALLOWED_TO_CHANGE: string[];
|
|
36
|
+
/**
|
|
37
|
+
* If there are values that should not be duplicated,
|
|
38
|
+
* specify here which properties those should be.
|
|
39
|
+
* @return array
|
|
40
|
+
*/
|
|
41
|
+
static readonly PROPERTIES_NOT_ALLOWED_TO_DUPLICATE: string[];
|
|
42
|
+
/**
|
|
43
|
+
* Specify which properties are required when creating a new object.
|
|
44
|
+
* @return array
|
|
45
|
+
*/
|
|
46
|
+
static readonly REQUIRED_PROPERTIES: string[];
|
|
47
|
+
static isMethodAllowed(method: string, { throwErrorIfNotAllowed }?: {
|
|
48
|
+
throwErrorIfNotAllowed?: boolean | undefined;
|
|
49
|
+
}): boolean;
|
|
50
|
+
/**
|
|
51
|
+
* Get the table name as used in the database
|
|
52
|
+
* @returns the table name as used in the database
|
|
53
|
+
*/
|
|
54
|
+
static readonly DB_TABLE: string;
|
|
55
|
+
static get TABLE(): string;
|
|
56
|
+
/**
|
|
57
|
+
* Sometimes you want to use a custom view to read your model data
|
|
58
|
+
* @returns the view name as used in the database
|
|
59
|
+
*/
|
|
60
|
+
static readonly DB_VIEW: string;
|
|
61
|
+
static get VIEW(): string;
|
|
62
|
+
protected _setChangedProperties(changedProperties: string[]): void;
|
|
63
|
+
/**
|
|
64
|
+
* @returns the changed properties
|
|
65
|
+
*/
|
|
66
|
+
getChangedProperties(): string[];
|
|
67
|
+
/**
|
|
68
|
+
* @returns the data in json format
|
|
69
|
+
* @paaam {boolean} [preventFormatting=false] - prevent formatting of the data
|
|
70
|
+
*/
|
|
71
|
+
getData(preventFormatting?: boolean): Record<string, any>;
|
|
72
|
+
/**
|
|
73
|
+
* @returns the primary key for the current table
|
|
74
|
+
*/
|
|
75
|
+
getPrimaryKey(): string;
|
|
76
|
+
getPrimaryKeyValue(): number;
|
|
77
|
+
/**
|
|
78
|
+
* get the model property
|
|
79
|
+
*/
|
|
80
|
+
getProperty(key: string, { preventFormatting }?: {
|
|
81
|
+
preventFormatting?: boolean | undefined;
|
|
82
|
+
}): string | number | null | undefined;
|
|
83
|
+
/**
|
|
84
|
+
* @returns the columns of the required properties needed to create a new object
|
|
85
|
+
*/
|
|
86
|
+
getRequiredProperties(): string[];
|
|
87
|
+
/**
|
|
88
|
+
* Get the table name as used in the database
|
|
89
|
+
* @returns the table name as used in the database
|
|
90
|
+
*/
|
|
91
|
+
getTable(): string;
|
|
92
|
+
/**
|
|
93
|
+
*
|
|
94
|
+
* @returns
|
|
95
|
+
*/
|
|
96
|
+
hasChanges(): boolean;
|
|
97
|
+
/**
|
|
98
|
+
* Check if the property has been changed
|
|
99
|
+
*/
|
|
100
|
+
hasPropertyValueChanged(property: string): boolean;
|
|
101
|
+
/**
|
|
102
|
+
* Check if the current object is a new object
|
|
103
|
+
* @returns true if the object is new, false if it is not
|
|
104
|
+
*/
|
|
105
|
+
isNew(): boolean;
|
|
106
|
+
setProperty(property: string, value: any, { forceChange }?: {
|
|
107
|
+
forceChange?: boolean | undefined;
|
|
108
|
+
}): void;
|
|
109
|
+
}
|
|
110
|
+
export default BdORMBase;
|
|
111
|
+
export { definePropertyDescriptors };
|
|
@@ -0,0 +1,262 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.definePropertyDescriptors = void 0;
|
|
4
|
+
const BdORMError_1 = require("./BdORMError");
|
|
5
|
+
// Local variables
|
|
6
|
+
const PROPERTIES_WEAKMAP = new WeakMap(), // memory leak prevention
|
|
7
|
+
LOCAL_WEAKMAP = new WeakMap();
|
|
8
|
+
// Helper methods
|
|
9
|
+
const getLocalWeakMapValue = (instance, key) => {
|
|
10
|
+
const localWeakMap = LOCAL_WEAKMAP.get(instance) ?? {};
|
|
11
|
+
return key in localWeakMap ? localWeakMap[key] : null;
|
|
12
|
+
}, getPropertyDescriptor = (instance, property) => {
|
|
13
|
+
let descriptor;
|
|
14
|
+
while (instance && !descriptor) {
|
|
15
|
+
descriptor = Object.getOwnPropertyDescriptor(instance, property);
|
|
16
|
+
instance = Object.getPrototypeOf(instance);
|
|
17
|
+
}
|
|
18
|
+
return descriptor;
|
|
19
|
+
}, definePropertyDescriptors = (instance, data) => {
|
|
20
|
+
Object.keys(data).forEach(property => {
|
|
21
|
+
const propertyDefinion = getPropertyDescriptor(instance, property) ?? {};
|
|
22
|
+
if (!('get' in propertyDefinion) || propertyDefinion.get === undefined) {
|
|
23
|
+
propertyDefinion.get = () => {
|
|
24
|
+
const value = instance.getProperty(property);
|
|
25
|
+
if (typeof value === 'string' && (value.trim().startsWith('{') || value.trim().startsWith('['))) {
|
|
26
|
+
try {
|
|
27
|
+
return JSON.parse(value);
|
|
28
|
+
}
|
|
29
|
+
catch (e) {
|
|
30
|
+
return value;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return value;
|
|
34
|
+
};
|
|
35
|
+
}
|
|
36
|
+
if (!('set' in propertyDefinion) || propertyDefinion.set === undefined) {
|
|
37
|
+
propertyDefinion.set = (value) => {
|
|
38
|
+
if (value && typeof value === 'object') {
|
|
39
|
+
try {
|
|
40
|
+
instance.setProperty(property, JSON.stringify(value));
|
|
41
|
+
}
|
|
42
|
+
catch (e) {
|
|
43
|
+
instance.setProperty(property, value);
|
|
44
|
+
}
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
instance.setProperty(property, value);
|
|
48
|
+
};
|
|
49
|
+
}
|
|
50
|
+
if ('value' in propertyDefinion && propertyDefinion.value === undefined) {
|
|
51
|
+
delete propertyDefinion.value;
|
|
52
|
+
if ('writable' in propertyDefinion)
|
|
53
|
+
delete propertyDefinion.writable;
|
|
54
|
+
}
|
|
55
|
+
try {
|
|
56
|
+
Object.defineProperty(instance, property, propertyDefinion);
|
|
57
|
+
}
|
|
58
|
+
catch (e) {
|
|
59
|
+
throw new BdORMError_1.BdORMError(`Invalid property descriptor "${property}" ${propertyDefinion.value} in ${instance.constructor.name}`);
|
|
60
|
+
}
|
|
61
|
+
// data toevoegen
|
|
62
|
+
if (data[property] !== undefined)
|
|
63
|
+
instance[property] = data[property];
|
|
64
|
+
});
|
|
65
|
+
};
|
|
66
|
+
exports.definePropertyDescriptors = definePropertyDescriptors;
|
|
67
|
+
/**
|
|
68
|
+
* Class which only contains the getters and setters for the base ORM
|
|
69
|
+
*/
|
|
70
|
+
class BdORMBase {
|
|
71
|
+
/**
|
|
72
|
+
* @returns the model name
|
|
73
|
+
*/
|
|
74
|
+
static getModelName() {
|
|
75
|
+
return this.name;
|
|
76
|
+
}
|
|
77
|
+
constructor(data = {}) {
|
|
78
|
+
const ormData = { ...data };
|
|
79
|
+
// primary key to 0 if not present
|
|
80
|
+
const primaryKey = this.getPrimaryKey();
|
|
81
|
+
if (!(primaryKey in ormData)) {
|
|
82
|
+
ormData[primaryKey] = 0; // 0 means new entry
|
|
83
|
+
}
|
|
84
|
+
// required property check
|
|
85
|
+
if (this.getRequiredProperties().length > 0) {
|
|
86
|
+
this.getRequiredProperties().forEach(property => {
|
|
87
|
+
if (!(property in ormData))
|
|
88
|
+
throw new BdORMError_1.BdORMError(`Property "${property}" is required`);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
// set properties
|
|
92
|
+
definePropertyDescriptors(this, ormData);
|
|
93
|
+
}
|
|
94
|
+
/**
|
|
95
|
+
* This method is ONLY used in the FETCH method
|
|
96
|
+
* @returns the columns to fetch from the database
|
|
97
|
+
*/
|
|
98
|
+
static COLUMNS_TO_FETCH = '*';
|
|
99
|
+
/**
|
|
100
|
+
* which default methods are not allowed to be used
|
|
101
|
+
* for example: ['delete'] will prevent you from using model.delete();
|
|
102
|
+
*/
|
|
103
|
+
static METHODS_NOT_ALLOWED = [];
|
|
104
|
+
/**
|
|
105
|
+
* Get the primary key for the current table
|
|
106
|
+
* @returns the primary key for the current table
|
|
107
|
+
*/
|
|
108
|
+
static PRIMARY_KEY = 'id';
|
|
109
|
+
/**
|
|
110
|
+
* Specify which properties should be set as read-only.
|
|
111
|
+
* These properties can only be set once, when the object is created.
|
|
112
|
+
* @return array
|
|
113
|
+
*/
|
|
114
|
+
static PROPERTIES_NOT_ALLOWED_TO_CHANGE = [];
|
|
115
|
+
/**
|
|
116
|
+
* If there are values that should not be duplicated,
|
|
117
|
+
* specify here which properties those should be.
|
|
118
|
+
* @return array
|
|
119
|
+
*/
|
|
120
|
+
static PROPERTIES_NOT_ALLOWED_TO_DUPLICATE = [];
|
|
121
|
+
/**
|
|
122
|
+
* Specify which properties are required when creating a new object.
|
|
123
|
+
* @return array
|
|
124
|
+
*/
|
|
125
|
+
static REQUIRED_PROPERTIES = [];
|
|
126
|
+
static isMethodAllowed(method, { throwErrorIfNotAllowed = false } = {}) {
|
|
127
|
+
const allowed = !this.METHODS_NOT_ALLOWED.includes(method);
|
|
128
|
+
if (!allowed && throwErrorIfNotAllowed) {
|
|
129
|
+
throw new BdORMError_1.BdORMError(`Method "${method}" not allowed`);
|
|
130
|
+
}
|
|
131
|
+
return allowed;
|
|
132
|
+
}
|
|
133
|
+
/**
|
|
134
|
+
* Get the table name as used in the database
|
|
135
|
+
* @returns the table name as used in the database
|
|
136
|
+
*/
|
|
137
|
+
static DB_TABLE;
|
|
138
|
+
static get TABLE() {
|
|
139
|
+
if (!this.DB_TABLE)
|
|
140
|
+
throw BdORMError_1.BdORMError.staticNotImplemented('get TABLE()');
|
|
141
|
+
return this.DB_TABLE;
|
|
142
|
+
}
|
|
143
|
+
/**
|
|
144
|
+
* Sometimes you want to use a custom view to read your model data
|
|
145
|
+
* @returns the view name as used in the database
|
|
146
|
+
*/
|
|
147
|
+
static DB_VIEW;
|
|
148
|
+
static get VIEW() {
|
|
149
|
+
return this.DB_VIEW ?? this.TABLE;
|
|
150
|
+
}
|
|
151
|
+
_setChangedProperties(changedProperties) {
|
|
152
|
+
const local = LOCAL_WEAKMAP.get(this) || {};
|
|
153
|
+
LOCAL_WEAKMAP.set(this, {
|
|
154
|
+
...local,
|
|
155
|
+
changedProperties,
|
|
156
|
+
});
|
|
157
|
+
}
|
|
158
|
+
/**
|
|
159
|
+
* @returns the changed properties
|
|
160
|
+
*/
|
|
161
|
+
getChangedProperties() {
|
|
162
|
+
return !this.isNew() ? (getLocalWeakMapValue(this, 'changedProperties') ?? []) : [];
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* @returns the data in json format
|
|
166
|
+
* @paaam {boolean} [preventFormatting=false] - prevent formatting of the data
|
|
167
|
+
*/
|
|
168
|
+
getData(preventFormatting = false) {
|
|
169
|
+
const data = {}, returnData = {};
|
|
170
|
+
const properties = PROPERTIES_WEAKMAP.get(this) ?? {};
|
|
171
|
+
Object.keys(properties).forEach(property => {
|
|
172
|
+
data[property] = this.getProperty(property, { preventFormatting });
|
|
173
|
+
returnData[property] = preventFormatting ? data[property] : (this[property] ?? data[property]);
|
|
174
|
+
});
|
|
175
|
+
return returnData;
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* @returns the primary key for the current table
|
|
179
|
+
*/
|
|
180
|
+
getPrimaryKey() {
|
|
181
|
+
return this.constructor.PRIMARY_KEY;
|
|
182
|
+
}
|
|
183
|
+
getPrimaryKeyValue() {
|
|
184
|
+
return this.getProperty(this.getPrimaryKey());
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* get the model property
|
|
188
|
+
*/
|
|
189
|
+
getProperty(key, { preventFormatting = false } = {}) {
|
|
190
|
+
const properties = PROPERTIES_WEAKMAP.get(this) ?? {};
|
|
191
|
+
let value = key in properties ? properties[key] : null; // never properties[key] as it can be set to null, undefined or 0
|
|
192
|
+
if (!preventFormatting && value instanceof Buffer)
|
|
193
|
+
value = value.toString();
|
|
194
|
+
return value;
|
|
195
|
+
}
|
|
196
|
+
/**
|
|
197
|
+
* @returns the columns of the required properties needed to create a new object
|
|
198
|
+
*/
|
|
199
|
+
getRequiredProperties() {
|
|
200
|
+
return this.constructor.REQUIRED_PROPERTIES;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* Get the table name as used in the database
|
|
204
|
+
* @returns the table name as used in the database
|
|
205
|
+
*/
|
|
206
|
+
getTable() {
|
|
207
|
+
return this.constructor.TABLE;
|
|
208
|
+
}
|
|
209
|
+
/**
|
|
210
|
+
*
|
|
211
|
+
* @returns
|
|
212
|
+
*/
|
|
213
|
+
hasChanges() {
|
|
214
|
+
return this.getChangedProperties().length > 0;
|
|
215
|
+
}
|
|
216
|
+
/**
|
|
217
|
+
* Check if the property has been changed
|
|
218
|
+
*/
|
|
219
|
+
hasPropertyValueChanged(property) {
|
|
220
|
+
return this.getChangedProperties().includes(property);
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* Check if the current object is a new object
|
|
224
|
+
* @returns true if the object is new, false if it is not
|
|
225
|
+
*/
|
|
226
|
+
isNew() {
|
|
227
|
+
return !this.getPrimaryKeyValue();
|
|
228
|
+
}
|
|
229
|
+
setProperty(property, value, { forceChange = false } = {}) {
|
|
230
|
+
// prevent overwrite of primary key
|
|
231
|
+
if (property === this.getPrimaryKey() && this.getPrimaryKeyValue() && this.getPrimaryKeyValue() !== value) {
|
|
232
|
+
throw new BdORMError_1.BdORMError(`Primary key cannot be changed from ${this.getPrimaryKeyValue()} to ${value}`);
|
|
233
|
+
}
|
|
234
|
+
// prevent overwrite of read-only properties
|
|
235
|
+
if (!forceChange &&
|
|
236
|
+
!this.isNew() &&
|
|
237
|
+
this.constructor.PROPERTIES_NOT_ALLOWED_TO_CHANGE.includes(property) &&
|
|
238
|
+
![undefined, null].includes(this.getProperty(property)) &&
|
|
239
|
+
this.getProperty(property) != value // != is used to check for null and undefined
|
|
240
|
+
) {
|
|
241
|
+
throw new BdORMError_1.BdORMError(`Property "${property}" is not allowed to be changed`);
|
|
242
|
+
}
|
|
243
|
+
if (!this.isNew() && this.getPrimaryKey() === property && this.getPrimaryKeyValue() !== value) {
|
|
244
|
+
throw new BdORMError_1.BdORMError(`Primary key "${property}" is not allowed to be changed`);
|
|
245
|
+
}
|
|
246
|
+
// set changed flag
|
|
247
|
+
const origValue = this.getProperty(property);
|
|
248
|
+
if (![undefined, null].includes(this.getProperty(property)) && origValue !== value) {
|
|
249
|
+
// todo set orig
|
|
250
|
+
const changedProperties = this.getChangedProperties();
|
|
251
|
+
if (!changedProperties.includes(property)) {
|
|
252
|
+
changedProperties.push(property);
|
|
253
|
+
this._setChangedProperties(changedProperties);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
// waarde instellen
|
|
257
|
+
const properties = PROPERTIES_WEAKMAP.get(this) ?? {};
|
|
258
|
+
properties[property] = value;
|
|
259
|
+
PROPERTIES_WEAKMAP.set(this, properties);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
exports.default = BdORMBase;
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import BdORMBase from './BdORMBase';
|
|
2
|
+
import type BdOrmDbConnection from './BdOrmConnection';
|
|
3
|
+
type MethodParams<K extends string> = {
|
|
4
|
+
[key in K]?: any;
|
|
5
|
+
};
|
|
6
|
+
export type BdORMDuplicateNewDataMethod = (v: any) => string | boolean | null | number;
|
|
7
|
+
export interface BdORMDuplicateParams {
|
|
8
|
+
asDraft?: boolean;
|
|
9
|
+
newData?: {
|
|
10
|
+
[key: string]: string | boolean | null | number | BdORMDuplicateNewDataMethod;
|
|
11
|
+
};
|
|
12
|
+
excludeColumns?: string[];
|
|
13
|
+
}
|
|
14
|
+
export default class BdORMCrud extends BdORMBase {
|
|
15
|
+
private static _DbConnection;
|
|
16
|
+
protected beforeCreate<K extends string>(beforeCreateParams?: MethodParams<K>): Promise<void>;
|
|
17
|
+
protected beforeUpdate<K extends string>(beforeUpdateParams?: MethodParams<K>): Promise<void>;
|
|
18
|
+
protected beforeSave<K extends string>(beforeSaveParams?: MethodParams<K>): Promise<void>;
|
|
19
|
+
protected afterCreate<K extends string>(afterCreateParams?: MethodParams<K>): Promise<void>;
|
|
20
|
+
protected afterUpdate<K extends string>(afterUpdateParams?: MethodParams<K>): Promise<void>;
|
|
21
|
+
protected afterSave<K extends string>(afterSaveParams?: MethodParams<K>): Promise<void>;
|
|
22
|
+
protected beforeDelete(): Promise<void>;
|
|
23
|
+
protected afterDelete(): Promise<void>;
|
|
24
|
+
private _execDBConnectionMethod;
|
|
25
|
+
private _create;
|
|
26
|
+
private _update;
|
|
27
|
+
/**
|
|
28
|
+
* Delete an instance from the database
|
|
29
|
+
*/
|
|
30
|
+
delete(): Promise<void>;
|
|
31
|
+
/**
|
|
32
|
+
* Duplicates the current instance and if the options asDraft is not defined or false it will be saved it to the database
|
|
33
|
+
*/
|
|
34
|
+
duplicate({ asDraft, newData, excludeColumns }?: BdORMDuplicateParams): Promise<InstanceType<typeof BdORMCrud>>;
|
|
35
|
+
save({ beforeSaveParams, afterSaveParams, preventAfterSave, preventBeforeSave, }?: {
|
|
36
|
+
beforeSaveParams?: {} | undefined;
|
|
37
|
+
afterSaveParams?: {} | undefined;
|
|
38
|
+
preventAfterSave?: boolean | undefined;
|
|
39
|
+
preventBeforeSave?: boolean | undefined;
|
|
40
|
+
}): Promise<InstanceType<typeof BdORMCrud>>;
|
|
41
|
+
static count(where?: Record<string, any> | string, values?: any[]): Promise<number>;
|
|
42
|
+
static create<T extends typeof BdORMCrud>(this: T, data: any): Promise<InstanceType<T>>;
|
|
43
|
+
static fetch<T extends typeof BdORMCrud>(this: T, where?: Record<string, any> | string, values?: any[]): Promise<InstanceType<T>[]>;
|
|
44
|
+
static fetchById<T extends typeof BdORMCrud>(this: T, id: string | number): Promise<InstanceType<T> | undefined>;
|
|
45
|
+
static fetchOne<T extends typeof BdORMCrud>(this: T, where: Record<string, any> | string, values?: any[]): Promise<InstanceType<T> | undefined>;
|
|
46
|
+
static fetchByPrimaryKey<T extends typeof BdORMCrud>(this: T, primaryKeyValue: string | number): Promise<InstanceType<T> | undefined>;
|
|
47
|
+
static setDbConnection(dbConnection: InstanceType<typeof BdOrmDbConnection>): void;
|
|
48
|
+
static clearDbConnection(): void;
|
|
49
|
+
static getDbConnection(): BdOrmDbConnection;
|
|
50
|
+
}
|
|
51
|
+
export {};
|