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.
@@ -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();
@@ -0,0 +1,6 @@
1
+
2
+ export default {
3
+ async run(){
4
+ console.log(JSON.stringify(await this.config, null, 2));
5
+ }
6
+ };
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, options = {}){
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.options.tenant){
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
- meta(){
6
- const { initialize } = this.prototype;
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
  });
@@ -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
- return Database.new(this.context.root.databaseClient, {
12
- tenant: {
13
- name: this.initialParams._headers['x-tenant'] || this.initialParams._url.hostname.replace(/\..*$/, ''),
14
- host: this.initialParams._url.hostname
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
  };
@@ -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 = (async () => ({
9
- database: await this.createDatabaseConfig()
10
- }))();
10
+ createConfigPromise = this.createConfig();
11
11
  }
12
12
  return createConfigPromise;
13
13
  });
14
14
  },
15
15
 
16
- async createDatabaseConfig(){
17
- const out = { adapter: 'sqlite' };
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
- Object.keys(process.env).forEach(name => {
20
- const matches = name.match(/^DATABASE_(.+)$/);
21
- if(!matches) return;
22
- const normalizedName = matches[1].toLowerCase().split(/_+/).map((s, i) => i > 0 ? s[0].toUpperCase() + s.slice(1) : s).join('');
23
- out[normalizedName] = process.env[name];
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
- if(adapter == 'sqlite'){
40
+ const environment = process.env.NODE_ENV || 'development';
41
+
42
+ if(adapter == 'mysql'){
29
43
  return Object.assign({
30
- filename: `${await this.project.rootPath}/${process.env.NODE_ENV || 'development'}.db`
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
- host: 'localhost',
36
- user: 'root',
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 transport = createTransport(mailConfig);
10
- return (mailOptions = {}) => {
11
- if(process.env.NODE_ENV == 'production') return transport.sendMail(mailOptions);
12
- const { text, html, ...otherMailOptions } = mailOptions;
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
  };
@@ -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.17.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": "ccfaad0d841d6404b4eec608f8c6e04ce83dcec5"
51
+ "gitHead": "bfe451ab9cdec0d5e4da557562c633d86920fb7a"
52
52
  }