remove importHook and relocate code
This commit is contained in:
121
src/dialects/postgres/utils.test.ts
Normal file
121
src/dialects/postgres/utils.test.ts
Normal file
@@ -0,0 +1,121 @@
|
||||
import assert from "node:assert";
|
||||
import { describe, it } from "node:test";
|
||||
import { sequenceToSerialType } from "./utils.js";
|
||||
|
||||
describe('sequenceToSerialType', () => {
|
||||
it('should remove nextval default value and set has_auto_increment to true', () => {
|
||||
const obj1 = {
|
||||
"fields": [
|
||||
{
|
||||
"collection": "test_collection",
|
||||
"field": "id",
|
||||
"type": "integer",
|
||||
"meta": null,
|
||||
"schema": {
|
||||
"name": "id",
|
||||
"table": "test_collection",
|
||||
"data_type": "integer",
|
||||
"default_value": "nextval('test_collection_id_seq'::regclass)",
|
||||
"max_length": null,
|
||||
"numeric_precision": 32,
|
||||
"numeric_scale": 0,
|
||||
"is_nullable": false,
|
||||
"is_unique": true,
|
||||
"is_primary_key": true,
|
||||
"is_generated": false,
|
||||
"generation_expression": null,
|
||||
"has_auto_increment": false,
|
||||
"foreign_key_table": null,
|
||||
"foreign_key_column": null
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
const obj2 = {
|
||||
"fields": [
|
||||
{
|
||||
"collection": "test_collection",
|
||||
"field": "id",
|
||||
"type": "integer",
|
||||
"meta": null,
|
||||
"schema": {
|
||||
"name": "id",
|
||||
"table": "test_collection",
|
||||
"data_type": "integer",
|
||||
"default_value": null,
|
||||
"max_length": null,
|
||||
"numeric_precision": 32,
|
||||
"numeric_scale": 0,
|
||||
"is_nullable": false,
|
||||
"is_unique": true,
|
||||
"is_primary_key": true,
|
||||
"is_generated": false,
|
||||
"generation_expression": null,
|
||||
"has_auto_increment": true,
|
||||
"foreign_key_table": null,
|
||||
"foreign_key_column": null
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
assert.deepEqual(sequenceToSerialType(obj1), obj2);
|
||||
});
|
||||
|
||||
it('should return same snapshot if serial type is already used everywhere', () => {
|
||||
const obj1 = {
|
||||
"fields": [
|
||||
{
|
||||
"collection": "test_collection",
|
||||
"field": "id",
|
||||
"type": "string",
|
||||
"meta": null,
|
||||
"schema": {
|
||||
"name": "id",
|
||||
"table": "test_collection",
|
||||
"data_type": "integer",
|
||||
"default_value": "test",
|
||||
"max_length": null,
|
||||
"numeric_precision": 32,
|
||||
"numeric_scale": 0,
|
||||
"is_nullable": false,
|
||||
"is_unique": true,
|
||||
"is_primary_key": true,
|
||||
"is_generated": false,
|
||||
"generation_expression": null,
|
||||
"has_auto_increment": false,
|
||||
"foreign_key_table": null,
|
||||
"foreign_key_column": null
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
const obj2 = {
|
||||
"fields": [
|
||||
{
|
||||
"collection": "test_collection",
|
||||
"field": "id",
|
||||
"type": "string",
|
||||
"meta": null,
|
||||
"schema": {
|
||||
"name": "id",
|
||||
"table": "test_collection",
|
||||
"data_type": "integer",
|
||||
"default_value": "test",
|
||||
"max_length": null,
|
||||
"numeric_precision": 32,
|
||||
"numeric_scale": 0,
|
||||
"is_nullable": false,
|
||||
"is_unique": true,
|
||||
"is_primary_key": true,
|
||||
"is_generated": false,
|
||||
"generation_expression": null,
|
||||
"has_auto_increment": false,
|
||||
"foreign_key_table": null,
|
||||
"foreign_key_column": null
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
assert.deepEqual(sequenceToSerialType(obj1), obj2);
|
||||
});
|
||||
});
|
||||
11
src/dialects/postgres/utils.ts
Normal file
11
src/dialects/postgres/utils.ts
Normal file
@@ -0,0 +1,11 @@
|
||||
export function sequenceToSerialType<T extends Record<string, any>>(snapshot: T): T {
|
||||
snapshot.fields
|
||||
.map( (field: any) => {
|
||||
if (field.schema?.default_value=="nextval('"+field.schema?.table+"_"+field.schema?.name+"_seq'::regclass)") {
|
||||
field.schema.default_value = null;
|
||||
field.schema.has_auto_increment = true;
|
||||
}
|
||||
return field;
|
||||
}) as T;
|
||||
return snapshot;
|
||||
}
|
||||
@@ -3,7 +3,7 @@ import { readFile, writeFile } from 'fs/promises';
|
||||
import { condenseAction } from './condenseAction.js';
|
||||
import type { IExporter } from './types';
|
||||
import { ExportHelper } from './utils.js';
|
||||
import { exportHook } from './vendorSpecific.js'
|
||||
import { exportHook } from './schemaExporterHooks.js'
|
||||
|
||||
export class SchemaExporter implements IExporter {
|
||||
private _filePath: string;
|
||||
|
||||
23
src/schemaExporterHooks.ts
Normal file
23
src/schemaExporterHooks.ts
Normal file
@@ -0,0 +1,23 @@
|
||||
import * as pgUtils from './dialects/postgres/utils.js';
|
||||
|
||||
const modifiers: modifiersType = {
|
||||
postgres: [pgUtils.sequenceToSerialType]
|
||||
}
|
||||
|
||||
export function exportHook<T extends Record<string, any>>(snapshot: T) {
|
||||
if (modifiers[snapshot.vendor]?.length)
|
||||
return modifiers[snapshot.vendor]!.reduce((_snapshot, modifier) => {
|
||||
return modifier(_snapshot);
|
||||
}, snapshot)
|
||||
return snapshot;
|
||||
};
|
||||
|
||||
type modifiersType = Record<string, snapshotFunctionType[]>
|
||||
|
||||
type snapshotWithHashType = {
|
||||
snapshot: Record<string, any>,
|
||||
hash: string
|
||||
}
|
||||
|
||||
type snapshotWithHashFunctionType = <T extends snapshotWithHashType>(snapshotWithHash: T) => T
|
||||
type snapshotFunctionType = <T extends Record<string, any>>(snapshotWithHash: T) => T
|
||||
@@ -1,6 +1,6 @@
|
||||
import assert from "node:assert";
|
||||
import { describe, it } from "node:test";
|
||||
import { deepEqual, getDiff, sortObject, postgresSequenceToSerialType } from "./utils.js";
|
||||
import { deepEqual, getDiff, sortObject } from "./utils.js";
|
||||
|
||||
describe('sortObject', () => {
|
||||
it('should sort object keys alphabetically', () => {
|
||||
@@ -101,122 +101,4 @@ describe('deepEqual', () => {
|
||||
assert.strictEqual(deepEqual('hello', 'world'), false);
|
||||
assert.strictEqual(deepEqual(null, undefined), false);
|
||||
});
|
||||
});
|
||||
|
||||
describe('postgresSequenceToSerialType', () => {
|
||||
it('should remove nextval default value and set has_auto_increment to true', () => {
|
||||
const obj1 = {
|
||||
"fields": [
|
||||
{
|
||||
"collection": "test_collection",
|
||||
"field": "id",
|
||||
"type": "integer",
|
||||
"meta": null,
|
||||
"schema": {
|
||||
"name": "id",
|
||||
"table": "test_collection",
|
||||
"data_type": "integer",
|
||||
"default_value": "nextval('test_collection_id_seq'::regclass)",
|
||||
"max_length": null,
|
||||
"numeric_precision": 32,
|
||||
"numeric_scale": 0,
|
||||
"is_nullable": false,
|
||||
"is_unique": true,
|
||||
"is_primary_key": true,
|
||||
"is_generated": false,
|
||||
"generation_expression": null,
|
||||
"has_auto_increment": false,
|
||||
"foreign_key_table": null,
|
||||
"foreign_key_column": null
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
const obj2 = {
|
||||
"fields": [
|
||||
{
|
||||
"collection": "test_collection",
|
||||
"field": "id",
|
||||
"type": "integer",
|
||||
"meta": null,
|
||||
"schema": {
|
||||
"name": "id",
|
||||
"table": "test_collection",
|
||||
"data_type": "integer",
|
||||
"default_value": null,
|
||||
"max_length": null,
|
||||
"numeric_precision": 32,
|
||||
"numeric_scale": 0,
|
||||
"is_nullable": false,
|
||||
"is_unique": true,
|
||||
"is_primary_key": true,
|
||||
"is_generated": false,
|
||||
"generation_expression": null,
|
||||
"has_auto_increment": true,
|
||||
"foreign_key_table": null,
|
||||
"foreign_key_column": null
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
assert.deepEqual(postgresSequenceToSerialType(obj1), obj2);
|
||||
});
|
||||
|
||||
it('should return same snapshot if serial type is already used everywhere', () => {
|
||||
const obj1 = {
|
||||
"fields": [
|
||||
{
|
||||
"collection": "test_collection",
|
||||
"field": "id",
|
||||
"type": "string",
|
||||
"meta": null,
|
||||
"schema": {
|
||||
"name": "id",
|
||||
"table": "test_collection",
|
||||
"data_type": "integer",
|
||||
"default_value": "test",
|
||||
"max_length": null,
|
||||
"numeric_precision": 32,
|
||||
"numeric_scale": 0,
|
||||
"is_nullable": false,
|
||||
"is_unique": true,
|
||||
"is_primary_key": true,
|
||||
"is_generated": false,
|
||||
"generation_expression": null,
|
||||
"has_auto_increment": false,
|
||||
"foreign_key_table": null,
|
||||
"foreign_key_column": null
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
const obj2 = {
|
||||
"fields": [
|
||||
{
|
||||
"collection": "test_collection",
|
||||
"field": "id",
|
||||
"type": "string",
|
||||
"meta": null,
|
||||
"schema": {
|
||||
"name": "id",
|
||||
"table": "test_collection",
|
||||
"data_type": "integer",
|
||||
"default_value": "test",
|
||||
"max_length": null,
|
||||
"numeric_precision": 32,
|
||||
"numeric_scale": 0,
|
||||
"is_nullable": false,
|
||||
"is_unique": true,
|
||||
"is_primary_key": true,
|
||||
"is_generated": false,
|
||||
"generation_expression": null,
|
||||
"has_auto_increment": false,
|
||||
"foreign_key_table": null,
|
||||
"foreign_key_column": null
|
||||
}
|
||||
}
|
||||
]
|
||||
};
|
||||
assert.deepEqual(postgresSequenceToSerialType(obj1), obj2);
|
||||
});
|
||||
});
|
||||
12
src/utils.ts
12
src/utils.ts
@@ -126,16 +126,4 @@ export function sortObject<T extends Record<string, any> | T[]>(obj: T): T {
|
||||
sortedObj[key] = sortObject((obj as Record<string, any>)[key]);
|
||||
});
|
||||
return sortedObj as T;
|
||||
}
|
||||
|
||||
export function postgresSequenceToSerialType<T extends Record<string, any>>(snapshot: T): T {
|
||||
snapshot.fields
|
||||
.map( (field: any) => {
|
||||
if (field.schema?.default_value=="nextval('"+field.schema?.table+"_"+field.schema?.name+"_seq'::regclass)") {
|
||||
field.schema.default_value = null;
|
||||
field.schema.has_auto_increment = true;
|
||||
}
|
||||
return field;
|
||||
}) as T;
|
||||
return snapshot;
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
import { postgresSequenceToSerialType } from './utils';
|
||||
|
||||
const modifiers: modifiersType = {
|
||||
import: {
|
||||
},
|
||||
export: {
|
||||
postgres: [postgresSequenceToSerialType]
|
||||
}
|
||||
}
|
||||
|
||||
export function importHook(snapshot: any) {
|
||||
if (modifiers.import[snapshot.vendor]?.length)
|
||||
return modifiers.import[snapshot.vendor]?.reduce((_snapshot, modifier) => {
|
||||
return modifier(_snapshot);
|
||||
}, snapshot)
|
||||
return snapshot;
|
||||
};
|
||||
|
||||
export function exportHook(snapshot: any) {
|
||||
if (modifiers.export[snapshot.vendor]?.length)
|
||||
return modifiers.export[snapshot.vendor]!.reduce((_snapshot, modifier) => {
|
||||
return modifier(_snapshot);
|
||||
}, snapshot)
|
||||
return snapshot;
|
||||
};
|
||||
|
||||
type modifiersType = {
|
||||
import: Record<string, any[]>
|
||||
export: Record<string, any[]>
|
||||
}
|
||||
@@ -26,5 +26,5 @@
|
||||
"rootDir": "./src",
|
||||
"outDir": "./dist-test",
|
||||
},
|
||||
"include": ["src/utils.ts", "src/utils.test.ts"]
|
||||
"include": ["src/utils.ts", "src/utils.test.ts", "src/dialects/postgres/utils.ts", "src/dialects/postgres/utils.test.ts"]
|
||||
}
|
||||
Reference in New Issue
Block a user