@tsofist/schema-forge 2.2.0 → 2.4.0

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.
@@ -253,6 +253,29 @@ describe('generator for a5', () => {
253
253
  dbIndex: true,
254
254
  type: 'number',
255
255
  },
256
+ indexedField3: {
257
+ dbIndex: {
258
+ name: 'ix_some_indexed_field3WithExtra',
259
+ type: 'gin',
260
+ unique: true,
261
+ },
262
+ type: 'number',
263
+ },
264
+ indexedField4: {
265
+ dbIndex: [
266
+ {
267
+ name: 'ix_some_indexed_field4',
268
+ type: 'gin',
269
+ unique: true,
270
+ },
271
+ {
272
+ type: 'btree',
273
+ },
274
+ 'ix_some_indexed_field4_1',
275
+ true,
276
+ ],
277
+ type: 'number',
278
+ },
256
279
  },
257
280
  required: [
258
281
  'vals',
@@ -267,6 +290,8 @@ describe('generator for a5', () => {
267
290
  'abnormalNames',
268
291
  'indexedField1',
269
292
  'indexedField2',
293
+ 'indexedField3',
294
+ 'indexedField4',
270
295
  ],
271
296
  additionalProperties: false,
272
297
  dbEntity: 'cmn.some',
@@ -0,0 +1,56 @@
1
+ import { ArrayMay } from '@tsofist/stem';
2
+ /**
3
+ * Database index types.
4
+ *
5
+ * @see https://www.postgresql.org/docs/current/indexes-types.html PostgreSQL
6
+ */
7
+ export declare const DBIndexTypeList: readonly ["btree", "hash", "gin", "gist", "spgist", "brin"];
8
+ /**
9
+ * Database index type.
10
+ *
11
+ * @see https://www.postgresql.org/docs/current/indexes-types.html PostgreSQL
12
+ * @default 'btree'
13
+ */
14
+ export type DBIndexType = (typeof DBIndexTypeList)[number];
15
+ /**
16
+ * Database index options.
17
+ */
18
+ export type DBIndexOptions = {
19
+ /**
20
+ * Type of index.
21
+ * @default 'btree'
22
+ */
23
+ type?: DBIndexType;
24
+ /**
25
+ * Name of index.
26
+ * @default 'ix_[schema]_[table]_[column]'
27
+ */
28
+ name?: string;
29
+ /**
30
+ * If true, the index will be unique.
31
+ * @default false
32
+ */
33
+ unique?: boolean;
34
+ };
35
+ export type DBIndexOptionsDef<B extends boolean = boolean> = ArrayMay<DBIndexOptions | string | B>;
36
+ /**
37
+ * Database entity options.
38
+ */
39
+ export type DBEntityOptions = {
40
+ /**
41
+ * Table name.
42
+ *
43
+ * @default '[schema].[table]'
44
+ */
45
+ name?: string;
46
+ /**
47
+ * Indexes of the table.
48
+ * They can be overridden/added at the column level.
49
+ *
50
+ * Use `true` to apply default index options.
51
+ * Use string literal to set index name.
52
+ */
53
+ indexes?: {
54
+ [column: string]: DBIndexOptionsDef<true>;
55
+ };
56
+ };
@@ -0,0 +1,17 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DBIndexTypeList = void 0;
4
+ /**
5
+ * Database index types.
6
+ *
7
+ * @see https://www.postgresql.org/docs/current/indexes-types.html PostgreSQL
8
+ */
9
+ exports.DBIndexTypeList = [
10
+ //
11
+ 'btree',
12
+ 'hash',
13
+ 'gin',
14
+ 'gist',
15
+ 'spgist',
16
+ 'brin',
17
+ ];
package/lib/validator.js CHANGED
@@ -10,6 +10,7 @@ const delay_1 = require("@tsofist/stem/lib/timers/delay");
10
10
  const ajv_1 = require("ajv");
11
11
  const ajv_formats_1 = require("ajv-formats");
12
12
  const types_1 = require("./types");
13
+ const db_types_1 = require("./types/db.types");
13
14
  const index_1 = require("./index");
14
15
  const DEF_OPTIONS = {
15
16
  meta: true,
@@ -329,18 +330,46 @@ function addJSDocKeywords(engine) {
329
330
  ],
330
331
  },
331
332
  });
333
+ const DBIndexOptionsProperties = {
334
+ name: { type: 'string', pattern: IXNamePattern },
335
+ unique: { type: 'boolean' },
336
+ type: {
337
+ type: 'string',
338
+ enum: db_types_1.DBIndexTypeList,
339
+ },
340
+ };
341
+ const DBIndexSchema = {
342
+ type: ['string', 'boolean', 'object', 'array'],
343
+ pattern: IXNamePattern,
344
+ additionalProperties: false,
345
+ properties: DBIndexOptionsProperties,
346
+ items: {},
347
+ minItems: 1,
348
+ };
349
+ DBIndexSchema.items = {
350
+ ...DBIndexSchema,
351
+ type: ['string', 'boolean', 'object'],
352
+ };
332
353
  engine.addKeyword({
333
354
  keyword: 'dbIndex',
334
- metaSchema: {
335
- type: ['string', 'boolean'],
336
- pattern: IXNamePattern,
337
- },
355
+ metaSchema: DBIndexSchema,
338
356
  });
339
357
  engine.addKeyword({
340
358
  keyword: 'dbEntity',
341
359
  metaSchema: {
342
- type: 'string',
360
+ type: ['string', 'object'],
343
361
  pattern: EntityNamePattern,
362
+ additionalProperties: false,
363
+ properties: {
364
+ name: {
365
+ type: 'string',
366
+ pattern: EntityNamePattern,
367
+ },
368
+ indexes: {
369
+ type: 'array',
370
+ items: DBIndexSchema,
371
+ },
372
+ },
344
373
  },
345
374
  });
346
375
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tsofist/schema-forge",
3
- "version": "2.2.0",
3
+ "version": "2.4.0",
4
4
  "description": "Generate JSON schema from TypeScript types",
5
5
  "author": "Andrew Berdnikov <tsofistgudmen@gmail.com>",
6
6
  "license": "LGPL-3.0",