fix(database): mock db
This commit is contained in:
		
							parent
							
								
									01e5e1cbac
								
							
						
					
					
						commit
						db3285d452
					
				@ -1,6 +1,6 @@
 | 
				
			|||||||
import { generatePrefixByPath, mockDatabase } from './index';
 | 
					 | 
				
			||||||
import { Collection } from '../collection';
 | 
					import { Collection } from '../collection';
 | 
				
			||||||
import { Database } from '../database';
 | 
					import { Database } from '../database';
 | 
				
			||||||
 | 
					import { generatePrefixByPath, mockDatabase } from './index';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test('collection disable authGenId', async () => {
 | 
					test('collection disable authGenId', async () => {
 | 
				
			||||||
  const db = mockDatabase();
 | 
					  const db = mockDatabase();
 | 
				
			||||||
@ -15,6 +15,7 @@ test('collection disable authGenId', async () => {
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
  await db.sync();
 | 
					  await db.sync();
 | 
				
			||||||
  expect(model.rawAttributes['id']).toBeUndefined();
 | 
					  expect(model.rawAttributes['id']).toBeUndefined();
 | 
				
			||||||
 | 
					  await db.close();
 | 
				
			||||||
});
 | 
					});
 | 
				
			||||||
 | 
					
 | 
				
			||||||
test('new collection', async () => {
 | 
					test('new collection', async () => {
 | 
				
			||||||
 | 
				
			|||||||
@ -1,4 +1,6 @@
 | 
				
			|||||||
 | 
					import { uid } from '@nocobase/utils';
 | 
				
			||||||
import merge from 'deepmerge';
 | 
					import merge from 'deepmerge';
 | 
				
			||||||
 | 
					import { Sequelize } from 'sequelize';
 | 
				
			||||||
import { Database, DatabaseOptions } from '../database';
 | 
					import { Database, DatabaseOptions } from '../database';
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export function generatePrefixByPath() {
 | 
					export function generatePrefixByPath() {
 | 
				
			||||||
@ -12,12 +14,15 @@ export function generatePrefixByPath() {
 | 
				
			|||||||
  return key;
 | 
					  return key;
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export function getConfig(config = {}, options?: any): DatabaseOptions {
 | 
					export function getConfig(config: any = {}, options?: any): DatabaseOptions {
 | 
				
			||||||
  return merge(
 | 
					  if (process.env.DB_DIALECT === 'sqlite') {
 | 
				
			||||||
    {
 | 
					    const defaults = {
 | 
				
			||||||
      dialect: 'sqlite',
 | 
					      dialect: process.env.DB_DIALECT as any,
 | 
				
			||||||
      storage: options?.storage || ':memory:',
 | 
					      storage: ':memory:',
 | 
				
			||||||
      logging: options?.logging || false,
 | 
					      logging: process.env.DB_LOG_SQL === 'on' ? console.log : false,
 | 
				
			||||||
 | 
					      // sync: {
 | 
				
			||||||
 | 
					      //   force: true,
 | 
				
			||||||
 | 
					      // },
 | 
				
			||||||
      hooks: {
 | 
					      hooks: {
 | 
				
			||||||
        beforeDefine(model, options) {
 | 
					        beforeDefine(model, options) {
 | 
				
			||||||
          options.tableName = `${generatePrefixByPath()}_${
 | 
					          options.tableName = `${generatePrefixByPath()}_${
 | 
				
			||||||
@ -25,10 +30,63 @@ export function getConfig(config = {}, options?: any): DatabaseOptions {
 | 
				
			|||||||
          }`;
 | 
					          }`;
 | 
				
			||||||
        },
 | 
					        },
 | 
				
			||||||
      },
 | 
					      },
 | 
				
			||||||
 | 
					    };
 | 
				
			||||||
 | 
					    return merge(defaults, config, options);
 | 
				
			||||||
 | 
					  }
 | 
				
			||||||
 | 
					  const database = `mock_${uid()}`;
 | 
				
			||||||
 | 
					  let dbExists = false;
 | 
				
			||||||
 | 
					  const defaults = {
 | 
				
			||||||
 | 
					    username: process.env.DB_USER,
 | 
				
			||||||
 | 
					    password: process.env.DB_PASSWORD,
 | 
				
			||||||
 | 
					    database: process.env.DB_DATABASE,
 | 
				
			||||||
 | 
					    host: process.env.DB_HOST,
 | 
				
			||||||
 | 
					    port: process.env.DB_PORT as any,
 | 
				
			||||||
 | 
					    dialect: process.env.DB_DIALECT as any,
 | 
				
			||||||
 | 
					    logging: process.env.DB_LOG_SQL === 'on' ? console.log : false,
 | 
				
			||||||
 | 
					    dialectOptions: {
 | 
				
			||||||
 | 
					      charset: 'utf8mb4',
 | 
				
			||||||
 | 
					      collate: 'utf8mb4_unicode_ci',
 | 
				
			||||||
    },
 | 
					    },
 | 
				
			||||||
    config || {},
 | 
					    hooks: {
 | 
				
			||||||
    options,
 | 
					      beforeDefine(model, options) {
 | 
				
			||||||
  ) as any;
 | 
					        options.tableName = `${generatePrefixByPath()}_${
 | 
				
			||||||
 | 
					          options.tableName || options.modelName || options.name.plural
 | 
				
			||||||
 | 
					        }`;
 | 
				
			||||||
 | 
					      },
 | 
				
			||||||
 | 
					      async beforeSync({ sequelize }: any) {
 | 
				
			||||||
 | 
					        if (config.database) {
 | 
				
			||||||
 | 
					          return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        if (dbExists) {
 | 
				
			||||||
 | 
					          return;
 | 
				
			||||||
 | 
					        }
 | 
				
			||||||
 | 
					        return;
 | 
				
			||||||
 | 
					        const db = new Sequelize({
 | 
				
			||||||
 | 
					          username: process.env.DB_USER,
 | 
				
			||||||
 | 
					          password: process.env.DB_PASSWORD,
 | 
				
			||||||
 | 
					          database: process.env.DB_DATABASE,
 | 
				
			||||||
 | 
					          host: process.env.DB_HOST,
 | 
				
			||||||
 | 
					          port: process.env.DB_PORT as any,
 | 
				
			||||||
 | 
					          dialect: process.env.DB_DIALECT as any,
 | 
				
			||||||
 | 
					          logging: process.env.DB_LOG_SQL === 'on' ? console.log : false,
 | 
				
			||||||
 | 
					          dialectOptions: {
 | 
				
			||||||
 | 
					            charset: 'utf8mb4',
 | 
				
			||||||
 | 
					            collate: 'utf8mb4_unicode_ci',
 | 
				
			||||||
 | 
					          },
 | 
				
			||||||
 | 
					        });
 | 
				
			||||||
 | 
					        await db.query(`CREATE DATABASE "${database}";`);
 | 
				
			||||||
 | 
					        await db.close();
 | 
				
			||||||
 | 
					        sequelize.options.database = database;
 | 
				
			||||||
 | 
					        sequelize.config.database = database;
 | 
				
			||||||
 | 
					        const ConnectionManager = sequelize.dialect.connectionManager.constructor;
 | 
				
			||||||
 | 
					        const connectionManager = new ConnectionManager(sequelize.dialect, sequelize);
 | 
				
			||||||
 | 
					        sequelize.dialect.connectionManager = connectionManager;
 | 
				
			||||||
 | 
					        sequelize.connectionManager = connectionManager;
 | 
				
			||||||
 | 
					        dbExists = true;
 | 
				
			||||||
 | 
					      },
 | 
				
			||||||
 | 
					    },
 | 
				
			||||||
 | 
					  };
 | 
				
			||||||
 | 
					  return merge(defaults, config, options);
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export function mockDatabase(options?: DatabaseOptions): Database {
 | 
					export function mockDatabase(options?: DatabaseOptions): Database {
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
		Reference in New Issue
	
	Block a user