@stonyx/orm 0.2.1-alpha.4 → 0.2.1-alpha.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/package.json +1 -1
- package/src/main.js +5 -1
- package/src/model.js +1 -0
- package/src/mysql/mysql-db.js +1 -2
- package/src/mysql/schema-introspector.js +3 -3
- package/src/orm-request.js +2 -2
- package/src/plural-registry.js +12 -0
- package/src/record.js +2 -2
- package/src/setup-rest-server.js +2 -2
package/package.json
CHANGED
package/src/main.js
CHANGED
|
@@ -19,6 +19,7 @@ import config from 'stonyx/config';
|
|
|
19
19
|
import log from 'stonyx/log';
|
|
20
20
|
import { forEachFileImport } from '@stonyx/utils/file';
|
|
21
21
|
import { kebabCaseToPascalCase, pluralize } from '@stonyx/utils/string';
|
|
22
|
+
import { registerPluralName } from './plural-registry.js';
|
|
22
23
|
import setupRestServer from './setup-rest-server.js';
|
|
23
24
|
import baseTransforms from './transforms.js';
|
|
24
25
|
import Store from './store.js';
|
|
@@ -66,7 +67,10 @@ export default class Orm {
|
|
|
66
67
|
// Transforms keep their original name, everything else gets converted to PascalCase with the type suffix
|
|
67
68
|
const alias = type === 'Transform' ? name : `${kebabCaseToPascalCase(name)}${type}`;
|
|
68
69
|
|
|
69
|
-
if (type === 'Model')
|
|
70
|
+
if (type === 'Model') {
|
|
71
|
+
Orm.store.set(name, new Map());
|
|
72
|
+
registerPluralName(name, exported);
|
|
73
|
+
}
|
|
70
74
|
|
|
71
75
|
return this[pluralize(lowerCaseType)][alias] = exported;
|
|
72
76
|
}, { ignoreAccessFailure: true, rawName: true, recursive: true, recursiveNaming: true });
|
package/src/model.js
CHANGED
package/src/mysql/mysql-db.js
CHANGED
|
@@ -6,7 +6,6 @@ import { buildInsert, buildUpdate, buildDelete, buildSelect } from './query-buil
|
|
|
6
6
|
import { createRecord, store } from '@stonyx/orm';
|
|
7
7
|
import { confirm } from '@stonyx/utils/prompt';
|
|
8
8
|
import { readFile } from '@stonyx/utils/file';
|
|
9
|
-
import { pluralize } from '../utils.js';
|
|
10
9
|
import config from 'stonyx/config';
|
|
11
10
|
import log from 'stonyx/log';
|
|
12
11
|
import path from 'path';
|
|
@@ -17,7 +16,7 @@ const defaultDeps = {
|
|
|
17
16
|
introspectModels, getTopologicalOrder, schemasToSnapshot,
|
|
18
17
|
loadLatestSnapshot, detectSchemaDrift,
|
|
19
18
|
buildInsert, buildUpdate, buildDelete, buildSelect,
|
|
20
|
-
createRecord, store, confirm, readFile,
|
|
19
|
+
createRecord, store, confirm, readFile, config, log, path
|
|
21
20
|
};
|
|
22
21
|
|
|
23
22
|
export default class MysqlDB {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import Orm from '@stonyx/orm';
|
|
2
2
|
import { getMysqlType } from './type-map.js';
|
|
3
3
|
import { camelCaseToKebabCase } from '@stonyx/utils/string';
|
|
4
|
-
import {
|
|
4
|
+
import { getPluralName } from '../plural-registry.js';
|
|
5
5
|
import { dbKey } from '../db.js';
|
|
6
6
|
|
|
7
7
|
function getRelationshipInfo(property) {
|
|
@@ -53,13 +53,13 @@ export function introspectModels() {
|
|
|
53
53
|
for (const relName of Object.keys(relationships.belongsTo)) {
|
|
54
54
|
const fkColumn = `${relName}_id`;
|
|
55
55
|
foreignKeys[fkColumn] = {
|
|
56
|
-
references:
|
|
56
|
+
references: getPluralName(relName),
|
|
57
57
|
column: 'id',
|
|
58
58
|
};
|
|
59
59
|
}
|
|
60
60
|
|
|
61
61
|
schemas[name] = {
|
|
62
|
-
table:
|
|
62
|
+
table: getPluralName(name),
|
|
63
63
|
idType,
|
|
64
64
|
columns,
|
|
65
65
|
foreignKeys,
|
package/src/orm-request.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { Request } from '@stonyx/rest-server';
|
|
2
2
|
import Orm, { createRecord, updateRecord, store } from '@stonyx/orm';
|
|
3
3
|
import { camelCaseToKebabCase } from '@stonyx/utils/string';
|
|
4
|
-
import {
|
|
4
|
+
import { getPluralName } from './plural-registry.js';
|
|
5
5
|
import { getBeforeHooks, getAfterHooks } from './hooks.js';
|
|
6
6
|
import config from 'stonyx/config';
|
|
7
7
|
|
|
@@ -215,7 +215,7 @@ export default class OrmRequest extends Request {
|
|
|
215
215
|
|
|
216
216
|
this.model = model;
|
|
217
217
|
this.access = access;
|
|
218
|
-
const pluralizedModel =
|
|
218
|
+
const pluralizedModel = getPluralName(model);
|
|
219
219
|
|
|
220
220
|
const modelRelationships = getModelRelationships(model);
|
|
221
221
|
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { pluralize } from './utils.js';
|
|
2
|
+
|
|
3
|
+
const registry = new Map();
|
|
4
|
+
|
|
5
|
+
export function registerPluralName(modelName, modelClass) {
|
|
6
|
+
const plural = modelClass.pluralName || pluralize(modelName);
|
|
7
|
+
registry.set(modelName, plural);
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function getPluralName(modelName) {
|
|
11
|
+
return registry.get(modelName) || pluralize(modelName);
|
|
12
|
+
}
|
package/src/record.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { store } from './index.js';
|
|
2
2
|
import { getComputedProperties } from "./serializer.js";
|
|
3
3
|
import { camelCaseToKebabCase } from '@stonyx/utils/string';
|
|
4
|
-
import {
|
|
4
|
+
import { getPluralName } from './plural-registry.js';
|
|
5
5
|
export default class Record {
|
|
6
6
|
__data = {};
|
|
7
7
|
__relationships = {};
|
|
@@ -57,7 +57,7 @@ export default class Record {
|
|
|
57
57
|
const { fields, baseUrl } = options;
|
|
58
58
|
const { __data:data } = this;
|
|
59
59
|
const modelName = this.__model.__name;
|
|
60
|
-
const pluralizedModelName =
|
|
60
|
+
const pluralizedModelName = getPluralName(modelName);
|
|
61
61
|
const recordId = data.id;
|
|
62
62
|
const relationships = {};
|
|
63
63
|
const attributes = {};
|
package/src/setup-rest-server.js
CHANGED
|
@@ -5,7 +5,7 @@ import MetaRequest from './meta-request.js';
|
|
|
5
5
|
import RestServer from '@stonyx/rest-server';
|
|
6
6
|
import { forEachFileImport } from '@stonyx/utils/file';
|
|
7
7
|
import { dbKey } from './db.js';
|
|
8
|
-
import {
|
|
8
|
+
import { getPluralName } from './plural-registry.js';
|
|
9
9
|
import log from 'stonyx/log';
|
|
10
10
|
|
|
11
11
|
export default async function(route, accessPath, metaRoute) {
|
|
@@ -43,7 +43,7 @@ export default async function(route, accessPath, metaRoute) {
|
|
|
43
43
|
|
|
44
44
|
// Configure endpoints for models with access configuration
|
|
45
45
|
for (const [model, access] of Object.entries(accessFiles)) {
|
|
46
|
-
const pluralizedModel =
|
|
46
|
+
const pluralizedModel = getPluralName(model);
|
|
47
47
|
const modelName = name === 'index' ? pluralizedModel : `${name}/${pluralizedModel}`;
|
|
48
48
|
RestServer.instance.mountRoute(OrmRequest, { name: modelName, options: { model, access } });
|
|
49
49
|
}
|