-
Notifications
You must be signed in to change notification settings - Fork 1
/
basic.template.ts
78 lines (67 loc) · 2.76 KB
/
basic.template.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*******************************************************************************************/
/* Basic template showing the use of @yellicode/reverse-sql.
/*
/* Reverse engineers an existing SQL-Server database and generates C# entities, CRUD actions
/* and stored procedure calls.
/*
/* https://github.com/yellicode/reverse-sql
/*
/*******************************************************************************************/
import * as sql from 'mssql';
import * as path from 'path';
import { Generator } from '@yellicode/templating';
import { ReverseDbBuilder, ReverseSqlOptions, DataAccessWriter, SqlServerDatabase } from '@yellicode/reverse-sql'
import { CSharpWriter } from '@yellicode/csharp';
/**
* The connection string to your database. Do not forget to turn on TCP/IP in the SQL Server Network Configuration -> Protocols for ...
* Also, make sure that the user has sufficient permissions.
*/
const connectionString = `Server=myserver,1433;Database=MyDatabase;User Id=MyUserId;Password=mypassword`;
/**
* The directory where the code should be generated. The path must be relative to this template's directory.
*/
const outputDirectory = './output';
/**
* The namepace in which all data access code must be generated.
*/
const namespace = 'MyProject.Data';
/**
* The name of the generated database class (the main class that contains all data-access methods).
*/
const dbClassName = 'MyDatabase';
/**
* Reverse engineering and code generation options. See https://github.com/yellicode/reverse-sql or check out the
* advanced template for examples.
*/
const options: ReverseSqlOptions = {
// using all the defaults
};
/**
* Reverse engineers the target database into an in-memory model, which we will use below.
*/
const buildDbModel = (): Promise<SqlServerDatabase> => {
const pool = new sql.ConnectionPool(connectionString);
const builder = new ReverseDbBuilder(pool, options);
console.log('Building model...');
return builder
.build()
.then((db: SqlServerDatabase)=> {
console.log('Building database model completed.');
return db;
});
}
/**
* Builds the model and then generates all data access code in a single file.
*/
Generator
.buildModel<SqlServerDatabase>(buildDbModel)
.then((db: SqlServerDatabase) => {
const dataAccessFileName = path.join(outputDirectory, `${dbClassName}.cs`);
Generator.generate({ outputFile: dataAccessFileName }, (output) => {
const csharpWriter = new CSharpWriter(output);
const dataAccessWriter = new DataAccessWriter(csharpWriter, namespace, options);
dataAccessWriter.writeAll(db, dbClassName);
});
}).catch(e => {
console.log(`An error has occured: ${e}`);
});