grunt-generate-database

Π Π΅ΠΏΠΎΠ·ΠΈΡΠΎΡΠΈΠΉ, ΠΊΠΎΡΠΎΡΡΠΉ Ρ
ΡΠ°Π½ΠΈΡ Π² ΡΠ΅Π±Π΅ ΠΏΠ»Π°Π³ΠΈΠ½ Π΄Π»Ρ ΡΠΎΠ·Π΄Π°Π½ΠΈΡ DBWrapper, ΡΡΠΈΠ³Π³Π΅ΡΠ° ΠΈ ΡΡΠ½ΠΊΡΠΈΠΈ ΡΠΏΡΠ°Π²Π»Π΅Π½ΠΈΡ ΡΡΠΈΠ³Π³Π΅ΡΠΎΠΌ Ρ ΠΏΠΎΠΌΠΎΡΡΡ typeorm ΠΈ postgres
Π£ΡΡΠ°Π½ΠΎΠ²ΠΊΠ°
npm install grunt-generate-database
ΠΠ°ΠΊ Π½Π°ΡΠ°ΡΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ
- Π‘ΠΎΠ·Π΄Π°ΠΉΡΠ΅ declaration.json Π² ΠΊΠΎΡΠ½Π΅Π²ΠΎΠΌ ΠΊΠ°ΡΠ°Π»ΠΎΠ³Π΅
[
{
"db": "postgres",
"name" : "base1",
"dbtype" : "dbtype1",
"dbhost" : "dbhost1",
"dbport" : "dbport1",
"dbusername" : "dbusername1",
"dbpassword" : "dbpassword1",
"dbdatabase" : "dbdatabase1",
"pathToDBWrappers": "./dbscript",
"schemas" :
[
{
"namespace": "testnamespace",
"recreate":true,
"tables":
[
{
"name": "Class",
"pathToModel": "./models/class"
}
]
}
]
}
]
- Π£ΡΡΠ°Π½ΠΎΠ²ΠΈΡΠ΅ Π΄Π΅ΠΊΠΎΡΠ°ΡΡ Π½Π° Π½ΡΠΆΠ½ΡΠ΅ ΠΌΠΎΠ΄Π΅Π»ΠΈ.
import {Column, Entity, PrimaryGeneratedColumn} from "typeorm";
import { GenerateHistory } from "grunt-generate-history-model";
@Entity()
@GenerateHistory({"historyPath": "./test/src/model/hero"})
export class Hero {
@PrimaryGeneratedColumn()
public id?: number;
@Column()
public name: string;
public data: string;
@Column()
public detailId?: number;
@Column({"type": "integer", "array": true, "nullable": true})
public simpleArray: number[];
}
- Π package.json Π΄ΠΎΠ±Π°Π²ΡΡΠ΅ ΠΈΠ½ΠΈΡΠΈΠ°Π»ΠΈΠ·ΠΈΡΡΡΡΡΡ ΠΊΠΎΠΌΠ°Π½Π΄Ρ Π² ΡΠ²ΠΎΠΉΡΡΠ²ΠΎ "scripts":
"scripts": {
"generation": "generateDatabase"
}
Π³Π΄Π΅ "generateDatabase" - ΡΡΡΠΎΠΊΠ° Π΄Π»Ρ Π·Π°ΠΏΡΡΠΊΠ° ΠΏΠ»Π°Π³ΠΈΠ½Π°
-
npm run generation
-
ΠΏΠΎΡΠ»Π΅ Π·Π°Π²Π΅ΡΡΠ΅Π½ΠΈΡ ΡΠ°Π±ΠΎΡΡ ΠΏΠ»Π°Π³ΠΈΠ½Π° ΠΏΠΎ ΠΏΡΡΠΈ, ΡΠΊΠ°Π·Π°Π½Π½ΠΎΠΌΡ Π² declaration.json Π² ΡΠ²ΠΎΠΉΡΡΠ²Π΅ "pathToDBWrappers", ΠΏΠΎΡΠ²ΡΡΡΡ ΡΠ°ΠΉΠ»Ρ Ρ ΡΠ°ΡΡΠΈΡΠ΅Π½ΠΈΠ΅ΠΌ ".ts" :
import { Class } from '../../../models/class';
import { createbase1TriggerFuncstestnamespace } from './function';
import { createbase1Triggerstestnamespace } from './trigger';
import * as dotenv from 'dotenv';
import {createConnection, Connection, getManager, EntityManager} from 'typeorm';
export class testnamespaceDBWrapper {
private static connection: Connection;
public static async initialize(dropSchema?: boolean, sync?: boolean): Promise<void> {
await this.close();
if (! dropSchema) {
dropSchema = false;
}
if (! sync) {
sync = false;
}
this.connection = await this.createTables(dropSchema, sync);
if (dropSchema) {
await createbase1TriggerFuncstestnamespace();
await createbase1Triggerstestnamespace();
}
}
private static async createTables(dropSchema?: boolean, sync?: boolean) {
return await createConnection({
name: 'testnamespace',
type: 'postgres',
replication: {
master: {
host: process.env.dbhost1,
port: parseInt(process.env.dbport1, 10),
username: process.env.dbusername1,
password: process.env.dbpassword1,
database: process.env.dbdatabase1
},
slaves: []
},
entities: [
Class
],
schema: 'testnamespace',
synchronize: sync,
dropSchema: dropSchema
});
}
public static getEntityManager (): EntityManager {
return getManager('testnamespace');
}
public static async close(): Promise<void> {
if (this.connection) {
await this.connection.close();
this.connection = null;
}
}
}
- Π’ΡΠΈΠ³Π³Π΅ΡΠ½Π°Ρ ΡΡΠ½ΠΊΡΠΈΡ(ΠΏΡΡΡΠ°Ρ Π² ΠΏΡΠΈΠΌΠ΅ΡΠ΅ Π² ΡΠ²ΡΠ·ΠΈ Ρ ΠΎΡΡΡΡΡΡΠ²ΠΈΠ΅ΠΌ ΠΌΠΎΠ΄Π΅Π»ΠΈ Π»ΠΎΠ³ΠΈΡΠΎΠ²Π°Π½ΠΈΡ)
import {createConnection, ConnectionOptions} from 'typeorm';
export async function createbase1TriggerFuncstestnamespace() {
const pgp = require('pg-promise')({});
await pgp.end();
const connectionString = 'postgres://' + process.env.dbusername1 + ':' +
process.env.dbpassword1 + '@' + process.env.dbhost1 + ':' + process.env.dbport1 + '/' + process.env.dbdatabase1;
const db = pgp(connectionString);
let queryproc = '';
pgp.end();
}
import {createConnection, ConnectionOptions} from 'typeorm';
export async function createbase1Triggerstestnamespace() {
const pgp = require('pg-promise')({});
await pgp.end();
const connectionString = 'postgres://' + process.env.dbusername1 + ':' +
process.env.dbpassword1 + '@' + process.env.dbhost1 + ':' + process.env.dbport1 + '/' + process.env.dbdatabase1;
const db = pgp(connectionString);
let queryproc;
let lowerStringName;
let lowewrStringSchema;
pgp.end();
}
ΠΡΠΈΠΌΠ΅ΡΠ°Π½ΠΈΡ ΠΊ ΡΠ°ΠΉΠ»Ρ ΠΊΠΎΠ½ΡΠΈΠ³ΡΡΠ°ΡΠΈΠΈ
- ΠΠΎΠ»Π΅ "db" ΠΈΠΌΠ΅Π΅Ρ Π΄Π²Π° Π·Π½Π°ΡΠ΅Π½ΠΈΡ : "mongo" ΠΈ "postgres"
- ΠΠ½Π°ΡΠ΅Π½ΠΈΡΠΌΠΈ ΠΏΠΎΠ»Π΅ΠΉ ,Π½Π°ΡΠΈΠ½Π°ΡΡΠΈΡ
ΡΡ Ρ db(ΠΊΡΠΎΠΌΠ΅ ΠΏΠΎΠ»Ρ "db") ΡΠ²Π»ΡΡΡΡΡ Π½Π°Π·Π²Π°Π½ΠΈΡ ΠΏΠ΅ΡΠ΅ΠΌΠ΅Π½Π½ΡΡ
Π² ΠΎΠ±ΡΠ΅ΠΊΡΠ΅ process.env
- ΠΠ»Π΅ΠΌΠ΅Π½ΡΠΎΠΌ ΠΌΠ°ΡΡΠΈΠ²Π° ΡΠ²Π»ΡΠ΅ΡΡΡ ΠΎΠΏΠΈΡΠ°Π½ΠΈΠ΅ ΠΎΡΠ΄Π΅Π»ΡΠ½ΠΎΠΉ Π±Π°Π·Ρ Π΄Π°Π½Π½ΡΡ
.
- Π‘Π²ΠΎΠΉΡΡΠ²Π° Ρ ΠΏΡΠ΅ΡΠΈΠΊΡΠΎΠΌ db ΡΠ²Π»ΡΡΡΡΡ ΠΏΠ°ΡΠ°ΠΌΠ΅ΡΡΠ°ΠΌΠΈ ΠΏΠΎΠ΄ΠΊΠ»ΡΡΠ΅Π½ΠΈΡ ΠΊ Π±Π°Π·Π΅.
- namespace ΠΎΡΠΎΠ±ΡΠ°ΠΆΠ°Π΅Ρ ΠΈΠΌΡ ΡΡ
Π΅ΠΌΡ Π² Π±Π°Π·Π΅ Π΄Π°Π½Π½ΡΡ
.
- ΠΠ°ΡΡΠΈΠ² tables ΠΏΠΎΠΊΠ°Π·ΡΠ²Π°Π΅Ρ ΠΊΠ°ΠΊΠΈΠ΅ ΡΠ°Π±Π»ΠΈΡΡ Π±ΡΠ΄ΡΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡΡΡ ΠΏΡΠΈ ΡΠ°Π±ΠΎΡΠ΅ ΡΠΎ ΡΡ
Π΅ΠΌΠΎΠΉ.
- Π£ ΠΊΠ°ΠΆΠ΄ΠΎΠ³ΠΎ ΡΠ»Π΅ΠΌΠ΅Π½ΡΠ° table ΡΡΡΠ΅ΡΡΠ²ΡΠ΅Ρ ΠΎΠΏΡΠΈΠΎΠ½Π°Π»ΡΠ½ΠΎΠ΅ ΠΏΠΎΠ»Π΅ historyPath, ΠΊΠΎΡΠΎΡΠΎΠ΅ ΠΏΠΎΠΊΠ°Π·ΡΠ²Π°Π΅Ρ Π΅ΡΡΡ Π»ΠΈ Ρ ΠΌΠΎΠ΄Π΅Π»ΠΈ ΠΌΠΎΠ΄Π΅Π»Ρ Π»ΠΎΠ³ΠΈΡΠΎΠ²Π°Π½ΠΈΡ.
- ΠΠ΅Π»Π°ΡΠ΅Π»ΡΠ½ΠΎ Π΄Π»Ρ ΡΠΎΠ·Π΄Π°Π½ΠΈΡ ΠΌΠΎΠ΄Π΅Π»Π΅ΠΉ Π»ΠΎΠ³ΠΈΡΠΎΠ²Π°Π½ΠΈΡ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΡ npm ΠΏΠ°ΠΊΠ΅Ρ grunt-generate-history-model ΠΈ Π΅Π³ΠΎ Π΄Π΅ΠΊΠΎΡΠ°ΡΠΎΡΡ, Π° Π½Π΅ ΡΠΎΠ·Π΄Π°Π½Π½ΡΠ΅ Π²ΡΡΡΠ½ΡΡ ΠΌΠΎΠ΄Π΅Π»ΠΈ Π»ΠΎΠ³ΠΈΡΠΎΠ²Π°Π½ΠΈΡ.
- ΠΡΠΈ ΠΈΡΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°Π½ΠΈΠΈ npm ΠΏΠ°ΠΊΠ΅ΡΠ° Π΄Π»Ρ ΡΠΎΠ·Π΄Π°Π½ΠΈΡ ΠΌΠΎΠ΄Π΅Π»Π΅ΠΉ Π»ΠΎΠ³ΠΈΡΠΎΠ²Π°Π½ΠΈΡ Π½Π΅ ΠΎΠ±ΡΠ·Π°ΡΠ΅Π»ΡΠ½ΠΎ ΡΠΊΠ°Π·ΡΠ²Π°ΡΡ ΠΏΡΡΡ ΠΊ ΠΌΠΎΠ΄Π΅Π»ΡΠΌ Π»ΠΎΠ³ΠΈΡΠΎΠ²Π°Π½ΠΈΡ Π² ΠΊΠΎΠ½ΡΠΈΠ³ΡΡΠ°ΡΠΈΠΈ.