@stonyx/orm 0.2.1-alpha.4 → 0.2.1-alpha.5

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 CHANGED
@@ -4,7 +4,7 @@
4
4
  "stonyx-async",
5
5
  "stonyx-module"
6
6
  ],
7
- "version": "0.2.1-alpha.4",
7
+ "version": "0.2.1-alpha.5",
8
8
  "description": "",
9
9
  "main": "src/main.js",
10
10
  "type": "module",
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') Orm.store.set(name, new Map());
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
@@ -10,6 +10,7 @@ export default class Model {
10
10
  * Override in subclass: static memory = false;
11
11
  */
12
12
  static memory = true;
13
+ static pluralName = undefined;
13
14
 
14
15
  id = attr('number');
15
16
 
@@ -6,6 +6,7 @@ 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 { getPluralName } from '../plural-registry.js';
9
10
  import { pluralize } from '../utils.js';
10
11
  import config from 'stonyx/config';
11
12
  import log from 'stonyx/log';
@@ -17,7 +18,7 @@ const defaultDeps = {
17
18
  introspectModels, getTopologicalOrder, schemasToSnapshot,
18
19
  loadLatestSnapshot, detectSchemaDrift,
19
20
  buildInsert, buildUpdate, buildDelete, buildSelect,
20
- createRecord, store, confirm, readFile, pluralize, config, log, path
21
+ createRecord, store, confirm, readFile, getPluralName, pluralize, config, log, path
21
22
  };
22
23
 
23
24
  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 { pluralize } from '../utils.js';
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: pluralize(relName),
56
+ references: getPluralName(relName),
57
57
  column: 'id',
58
58
  };
59
59
  }
60
60
 
61
61
  schemas[name] = {
62
- table: pluralize(name),
62
+ table: getPluralName(name),
63
63
  idType,
64
64
  columns,
65
65
  foreignKeys,
@@ -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 { pluralize } from './utils.js';
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 = pluralize(model);
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 { pluralize } from './utils.js';
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 = pluralize(modelName);
60
+ const pluralizedModelName = getPluralName(modelName);
61
61
  const recordId = data.id;
62
62
  const relationships = {};
63
63
  const attributes = {};
@@ -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 { pluralize } from './utils.js';
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 = pluralize(model);
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
  }