pinstripe 0.17.0 → 0.19.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.
- package/lib/commands/generate_project.js +64 -0
- package/lib/commands/show_config.js +6 -0
- package/lib/database.js +5 -2
- package/lib/extensions/multi-tenant/database/table.js +1 -1
- package/lib/extensions/multi-tenant/database.js +2 -21
- package/lib/extensions/multi-tenant/{services/migrations → migrations}/1627976174_create_tenant_table_and_add_tenant_id_to_existing_tables.js +0 -0
- package/lib/extensions/multi-tenant/{services/migrations → migrations}/_file_importer.js +0 -0
- package/lib/extensions/multi-tenant/services/database.js +18 -5
- package/lib/services/config.js +39 -16
- package/lib/services/send_mail.js +35 -25
- package/lib/services/server.js +0 -2
- package/package.json +2 -2
|
@@ -32,6 +32,70 @@ export default {
|
|
|
32
32
|
},
|
|
33
33
|
}, null, 2));
|
|
34
34
|
});
|
|
35
|
+
|
|
36
|
+
await generateFile(`pinstripe.config.js`, () => {
|
|
37
|
+
line();
|
|
38
|
+
line(`const environment = process.env.NODE_ENV || 'development';`);
|
|
39
|
+
line();
|
|
40
|
+
line(`let database;`);
|
|
41
|
+
line(`if(environment == 'production'){`)
|
|
42
|
+
indent(() => {
|
|
43
|
+
line(`database = {`);
|
|
44
|
+
indent(() => {
|
|
45
|
+
line(`adapter: 'mysql',`)
|
|
46
|
+
line(`host: 'localhost',`);
|
|
47
|
+
line(`user: 'root',`);
|
|
48
|
+
line(`password: '',`);
|
|
49
|
+
line(`database: \`${this.inflector.snakeify(name)}_\${environment}\``);
|
|
50
|
+
});
|
|
51
|
+
line(`};`);
|
|
52
|
+
});
|
|
53
|
+
line(`} else {`);
|
|
54
|
+
indent(() => {
|
|
55
|
+
line(`database = {`);
|
|
56
|
+
indent(() => {
|
|
57
|
+
line(`adapter: 'sqlite',`);
|
|
58
|
+
line(`filename: \`\${environment}.db\``)
|
|
59
|
+
});
|
|
60
|
+
line(`};`);
|
|
61
|
+
});
|
|
62
|
+
line(`}`);
|
|
63
|
+
line();
|
|
64
|
+
line(`let mail;`);
|
|
65
|
+
line(`if(environment == 'production'){`)
|
|
66
|
+
indent(() => {
|
|
67
|
+
line(`mail = {`);
|
|
68
|
+
indent(() => {
|
|
69
|
+
line(`adapter: 'smtp',`)
|
|
70
|
+
line(`host: "smtp.example.com",`)
|
|
71
|
+
line(`port: 465,`);
|
|
72
|
+
line(`secure: true, // use TLS`);
|
|
73
|
+
line(`auth: {`);
|
|
74
|
+
indent(() => {
|
|
75
|
+
line(`user: "username",`);
|
|
76
|
+
line(`pass: "password",`);
|
|
77
|
+
});
|
|
78
|
+
line(`}`);
|
|
79
|
+
});
|
|
80
|
+
line(`};`);
|
|
81
|
+
});
|
|
82
|
+
line(`} else {`);
|
|
83
|
+
indent(() => {
|
|
84
|
+
line(`mail = {`);
|
|
85
|
+
indent(() => {
|
|
86
|
+
line(`adapter: 'dummy'`);
|
|
87
|
+
});
|
|
88
|
+
line(`};`);
|
|
89
|
+
});
|
|
90
|
+
line(`}`);
|
|
91
|
+
line();
|
|
92
|
+
line(`export default {`);
|
|
93
|
+
indent(() => {
|
|
94
|
+
line(`database,`);
|
|
95
|
+
line(`mail`);
|
|
96
|
+
})
|
|
97
|
+
line(`};`);
|
|
98
|
+
});
|
|
35
99
|
|
|
36
100
|
await generateFile(`lib/index.js`, () => {
|
|
37
101
|
line();
|
package/lib/database.js
CHANGED
|
@@ -11,14 +11,17 @@ export const Database = Class.extend().include({
|
|
|
11
11
|
this.assignProps({ name: 'database' });
|
|
12
12
|
},
|
|
13
13
|
|
|
14
|
-
async initialize(client
|
|
14
|
+
async initialize(client){
|
|
15
15
|
this.client = client;
|
|
16
|
-
this.options = options;
|
|
17
16
|
if(!loadSchemaPromise) loadSchemaPromise = Row.loadSchema(client);
|
|
18
17
|
await loadSchemaPromise;
|
|
19
18
|
return trapify(this);
|
|
20
19
|
},
|
|
21
20
|
|
|
21
|
+
get withoutTenantScope(){
|
|
22
|
+
return Database.new(this.client);
|
|
23
|
+
},
|
|
24
|
+
|
|
22
25
|
table(name, fn){
|
|
23
26
|
const out = Table.create(name, this);
|
|
24
27
|
if(fn) return fn.call(out, out);
|
|
@@ -16,7 +16,7 @@ Table.include({
|
|
|
16
16
|
|
|
17
17
|
initialize(...args){
|
|
18
18
|
const out = initialize.call(this, ...args);
|
|
19
|
-
if(this.constructor.columns.tenantId && this.database.
|
|
19
|
+
if(this.constructor.columns.tenantId && this.database.scopedByTenant){
|
|
20
20
|
if(this.database.tenant){
|
|
21
21
|
this.where({ tenantId: this.database.tenant.id });
|
|
22
22
|
} else {
|
|
@@ -2,26 +2,7 @@
|
|
|
2
2
|
import { Database } from '../../database.js';
|
|
3
3
|
|
|
4
4
|
Database.include({
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
this.include({
|
|
9
|
-
async initialize(...args){
|
|
10
|
-
const out = await initialize.call(this, ...args);
|
|
11
|
-
const { tenant } = this.options;
|
|
12
|
-
if(tenant && out.info.tenants){
|
|
13
|
-
const { name, host } = tenant;
|
|
14
|
-
this.tenant = await (await out.tenants.tap(function(){
|
|
15
|
-
this.where('(? = ? or ? = ?)', this.tableReference.createColumnReference('name'), name, this.tableReference.createColumnReference('host'), host);
|
|
16
|
-
})).first();
|
|
17
|
-
}
|
|
18
|
-
return out;
|
|
19
|
-
},
|
|
20
|
-
|
|
21
|
-
get withoutTenantScope(){
|
|
22
|
-
const { tenant, ...options } = this.options;
|
|
23
|
-
return Database.new(this.client, options);
|
|
24
|
-
}
|
|
25
|
-
});
|
|
5
|
+
get scopedByTenant(){
|
|
6
|
+
return this.hasOwnProperty('tenant');
|
|
26
7
|
}
|
|
27
8
|
});
|
|
File without changes
|
|
File without changes
|
|
@@ -8,12 +8,25 @@ export default {
|
|
|
8
8
|
if(!this.context.root.databaseClient){
|
|
9
9
|
this.context.root.databaseClient = Client.new(await this.config.database);
|
|
10
10
|
}
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
11
|
+
|
|
12
|
+
const out = await Database.new(this.context.root.databaseClient);
|
|
13
|
+
|
|
14
|
+
if(out.info.tenants){
|
|
15
|
+
const headers = this.initialParams._headers;
|
|
16
|
+
const hostname = this.initialParams._url.hostname;
|
|
17
|
+
const host = (headers['host'] || hostname).toLowerCase();
|
|
18
|
+
const { primaryDomain = '' } = await this.config;
|
|
19
|
+
const domainSuffix = `\.${primaryDomain}`.toLowerCase();
|
|
20
|
+
|
|
21
|
+
if(host.endsWith(domainSuffix)){
|
|
22
|
+
const name = host.substr(0, host.length - domainSuffix.length);
|
|
23
|
+
out.tenant = await out.tenants.where({ name }).first();
|
|
24
|
+
} else {
|
|
25
|
+
out.tenant = await out.tenants.where({ host }).first();
|
|
15
26
|
}
|
|
16
|
-
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return out;
|
|
17
30
|
});
|
|
18
31
|
}
|
|
19
32
|
};
|
package/lib/services/config.js
CHANGED
|
@@ -1,40 +1,63 @@
|
|
|
1
1
|
|
|
2
|
+
import { existsSync } from 'fs';
|
|
3
|
+
|
|
2
4
|
let createConfigPromise;
|
|
3
5
|
|
|
4
6
|
export default {
|
|
5
7
|
create(){
|
|
6
8
|
return this.defer(() => {
|
|
7
9
|
if(!createConfigPromise){
|
|
8
|
-
createConfigPromise = (
|
|
9
|
-
database: await this.createDatabaseConfig()
|
|
10
|
-
}))();
|
|
10
|
+
createConfigPromise = this.createConfig();
|
|
11
11
|
}
|
|
12
12
|
return createConfigPromise;
|
|
13
13
|
});
|
|
14
14
|
},
|
|
15
15
|
|
|
16
|
-
async
|
|
17
|
-
|
|
16
|
+
async createConfig(){
|
|
17
|
+
let out = {};
|
|
18
|
+
const candidateConfigFilePath = `${await this.project.rootPath}/pinstripe.config.js`;
|
|
19
|
+
if(existsSync(candidateConfigFilePath)){
|
|
20
|
+
out = await (await import(candidateConfigFilePath)).default;
|
|
21
|
+
}
|
|
18
22
|
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
23
|
+
const {
|
|
24
|
+
database = { adapter: 'sqlite' },
|
|
25
|
+
mail = { adapter: 'dummy' }
|
|
26
|
+
} = out;
|
|
27
|
+
|
|
28
|
+
return {
|
|
29
|
+
...out,
|
|
30
|
+
database: await this.normalizeDatabaseConfig(database),
|
|
31
|
+
mail
|
|
32
|
+
};
|
|
33
|
+
},
|
|
34
|
+
|
|
35
|
+
async normalizeDatabaseConfig(config){
|
|
36
|
+
const out = { ...config };
|
|
25
37
|
|
|
26
38
|
const { adapter } = out;
|
|
27
39
|
|
|
28
|
-
|
|
40
|
+
const environment = process.env.NODE_ENV || 'development';
|
|
41
|
+
|
|
42
|
+
if(adapter == 'mysql'){
|
|
29
43
|
return Object.assign({
|
|
30
|
-
|
|
44
|
+
host: 'localhost',
|
|
45
|
+
user: 'root',
|
|
46
|
+
password: '',
|
|
47
|
+
database: `${await this.project.name}_${environment}`
|
|
31
48
|
}, out);
|
|
32
49
|
}
|
|
33
50
|
|
|
34
51
|
return Object.assign({
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
password: '',
|
|
52
|
+
adapter: 'sqlite',
|
|
53
|
+
filename: `${await this.project.rootPath}/${environment}.db`
|
|
38
54
|
}, out);
|
|
55
|
+
},
|
|
56
|
+
|
|
57
|
+
normalizeMailConfig(config){
|
|
58
|
+
return {
|
|
59
|
+
defaults: {},
|
|
60
|
+
...config
|
|
61
|
+
};
|
|
39
62
|
}
|
|
40
63
|
};
|
|
@@ -6,31 +6,41 @@ export default {
|
|
|
6
6
|
create(){
|
|
7
7
|
return this.defer(async () => {
|
|
8
8
|
const { mail: mailConfig = {} } = await this.config;
|
|
9
|
-
const
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
console.log('');
|
|
14
|
-
console.log('----------------------------------------');
|
|
15
|
-
console.log(chalk.green('sendMail'));
|
|
16
|
-
console.log('----------------------------------------');
|
|
17
|
-
Object.keys(otherMailOptions).forEach(name => {
|
|
18
|
-
console.log(`${name}: ${JSON.stringify(otherMailOptions[name])}`)
|
|
19
|
-
});
|
|
20
|
-
if(text){
|
|
21
|
-
console.log('text:')
|
|
22
|
-
console.log(text.replace(/(^|\n)/g, '$1 '));
|
|
23
|
-
}
|
|
24
|
-
if(html){
|
|
25
|
-
console.log('html:')
|
|
26
|
-
console.log(html.replace(/(^|\n)/g, '$1 '));
|
|
27
|
-
}
|
|
28
|
-
if(!Object.keys(mailOptions).length){
|
|
29
|
-
console.log('No mail options have been provided.')
|
|
30
|
-
}
|
|
31
|
-
console.log('----------------------------------------');
|
|
32
|
-
console.log('');
|
|
33
|
-
}
|
|
9
|
+
const { adapter = 'dummy', ...adapterConfig } = mailConfig;
|
|
10
|
+
if(adapter == 'dummy') return this.createDummy();
|
|
11
|
+
if(adapter == 'smtp') return this.createSmtp(adapterConfig);
|
|
12
|
+
throw new Error(`No such mail adapter '${adapter}' exists.`);
|
|
34
13
|
});
|
|
14
|
+
},
|
|
15
|
+
|
|
16
|
+
createDummy({ defaults }){
|
|
17
|
+
return (mailOptions = {}) => {
|
|
18
|
+
const { text, html, ...otherMailOptions } = { ...defaults, ...mailOptions };
|
|
19
|
+
console.log('');
|
|
20
|
+
console.log('----------------------------------------');
|
|
21
|
+
console.log(chalk.green('sendMail'));
|
|
22
|
+
console.log('----------------------------------------');
|
|
23
|
+
Object.keys(otherMailOptions).forEach(name => {
|
|
24
|
+
console.log(`${name}: ${JSON.stringify(otherMailOptions[name])}`)
|
|
25
|
+
});
|
|
26
|
+
if(text){
|
|
27
|
+
console.log('text:')
|
|
28
|
+
console.log(text.replace(/(^|\n)/g, '$1 '));
|
|
29
|
+
}
|
|
30
|
+
if(html){
|
|
31
|
+
console.log('html:')
|
|
32
|
+
console.log(html.replace(/(^|\n)/g, '$1 '));
|
|
33
|
+
}
|
|
34
|
+
if(!Object.keys(mailOptions).length){
|
|
35
|
+
console.log('No mail options have been provided.')
|
|
36
|
+
}
|
|
37
|
+
console.log('----------------------------------------');
|
|
38
|
+
console.log('');
|
|
39
|
+
}
|
|
40
|
+
},
|
|
41
|
+
|
|
42
|
+
createSmtp({ defaults, ...config }){
|
|
43
|
+
const transport = createTransport(config, defaults);
|
|
44
|
+
return (mailOptions = {}) => transport.sendMail(mailOptions);
|
|
35
45
|
}
|
|
36
46
|
};
|
package/lib/services/server.js
CHANGED
|
@@ -40,8 +40,6 @@ export default {
|
|
|
40
40
|
const { method, url, headers } = request;
|
|
41
41
|
const _url = new URL(url, 'http://localhost');
|
|
42
42
|
|
|
43
|
-
if(headers['x-host']) _url.host = headers['x-host'];
|
|
44
|
-
|
|
45
43
|
const urlParams = {};
|
|
46
44
|
_url.searchParams.forEach((value, key) => {
|
|
47
45
|
urlParams[key] = value;
|
package/package.json
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
"type": "module",
|
|
3
3
|
"name": "pinstripe",
|
|
4
4
|
"description": "A slick web framework for Node.js.",
|
|
5
|
-
"version": "0.
|
|
5
|
+
"version": "0.19.0",
|
|
6
6
|
"author": "Jody Salt",
|
|
7
7
|
"license": "MIT",
|
|
8
8
|
"exports": {
|
|
@@ -48,5 +48,5 @@
|
|
|
48
48
|
"tmp": "^0.2.1",
|
|
49
49
|
"unionfs": "^4.4.0"
|
|
50
50
|
},
|
|
51
|
-
"gitHead": "
|
|
51
|
+
"gitHead": "bfe451ab9cdec0d5e4da557562c633d86920fb7a"
|
|
52
52
|
}
|