pinstripe 0.16.2 → 0.18.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
+ };
@@ -17,7 +17,7 @@ export const Migrator = Class.extend().include({
17
17
  const migrations = Migration.names.map(name => Migration.for(name)).sort((a, b) => a.schemaVersion - b.schemaVersion);
18
18
  for(let i in migrations){
19
19
  const migration = migrations[i];
20
- const isMigrationApplied = this.database.table('pinstripeAppliedMigrations').where({ schemaVersion: migration.schemaVersion }).count() > 0;
20
+ const isMigrationApplied = await this.database.table('pinstripeAppliedMigrations').where({ schemaVersion: migration.schemaVersion }).count() > 0;
21
21
  if(!isMigrationApplied){
22
22
  if(isDevelopmentEnvironment) console.log(`Applying migration: ${migration.name}`);
23
23
  await migration.new(this.database).migrate();
@@ -168,11 +168,12 @@ export const Table = Class.extend().include({
168
168
  },
169
169
 
170
170
  where(...args){
171
- if(typeof args[0] == 'string'){
171
+ if(typeof args[0] == 'string' || Array.isArray(args[0])){
172
172
  this.expressions.push(args);
173
173
  return this;
174
174
  }
175
- const [ scopedBy = {} ] = args;
175
+ const [ scopedBy ] = args;
176
+ if(typeof scopedBy != 'object') throw new Error(`Invalid argument (0) - expected object, array or string.`);
176
177
  const names = Object.keys(scopedBy);
177
178
  while(names.length){
178
179
  const name = names.shift();
@@ -183,6 +184,11 @@ export const Table = Class.extend().include({
183
184
  return this;
184
185
  },
185
186
 
187
+ async tap(fn){
188
+ await fn.call(this);
189
+ return this;
190
+ },
191
+
186
192
  orderBy(...args){
187
193
  this.orderedBy.push(args);
188
194
  return this;
@@ -11,11 +11,9 @@ Database.include({
11
11
  const { tenant } = this.options;
12
12
  if(tenant && out.info.tenants){
13
13
  const { name, host } = tenant;
14
- if(name) {
15
- this.tenant = await out.tenants.where({ name }).first();
16
- } else {
17
- this.tenant = await out.tenants.where({ host }).first();
18
- }
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();
19
17
  }
20
18
  return out;
21
19
  },
@@ -10,7 +10,7 @@ export default {
10
10
  }
11
11
  return Database.new(this.context.root.databaseClient, {
12
12
  tenant: {
13
- name: this.initialParams._headers['x-tenant'],
13
+ name: this.initialParams._headers['x-tenant'] || this.initialParams._url.hostname.replace(/\..*$/, ''),
14
14
  host: this.initialParams._url.hostname
15
15
  }
16
16
  })
@@ -1,33 +1,47 @@
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
 
40
+ const environment = process.env.NODE_ENV || 'development';
41
+
28
42
  if(adapter == 'sqlite'){
29
43
  return Object.assign({
30
- filename: `${await this.project.rootPath}/${process.env.NODE_ENV || 'development'}.db`
44
+ filename: `${await this.project.rootPath}/${environment}.db`
31
45
  }, out);
32
46
  }
33
47
 
@@ -35,6 +49,7 @@ export default {
35
49
  host: 'localhost',
36
50
  user: 'root',
37
51
  password: '',
52
+ database: `${await this.project.name}_${environment}`
38
53
  }, out);
39
54
  }
40
55
  };
@@ -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(){
17
+ return (mailOptions = {}) => {
18
+ const { text, html, ...otherMailOptions } = 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(config){
43
+ const transport = createTransport(config);
44
+ return (mailOptions = {}) => transport.sendMail(mailOptions);
35
45
  }
36
46
  };
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.16.2",
5
+ "version": "0.18.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": "bf3b7e1cb4f5c534a3d8b2136b0123b71c0d0286"
51
+ "gitHead": "b638218b06f4cdea77f3ba0a4f9c082989528ccf"
52
52
  }