mirror of
https://github.com/Sonny93/my-links.git
synced 2025-12-08 14:43:24 +00:00
refactor: use tabs instead of spaces
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
import { Knex } from 'knex';
|
||||
|
||||
export function defaultTableFields(table: Knex.CreateTableBuilder) {
|
||||
table.increments('id').primary().first().unique().notNullable();
|
||||
table.increments('id').primary().first().unique().notNullable();
|
||||
|
||||
table.timestamp('created_at').notNullable();
|
||||
table.timestamp('updated_at').nullable();
|
||||
table.timestamp('created_at').notNullable();
|
||||
table.timestamp('updated_at').nullable();
|
||||
}
|
||||
|
||||
@@ -2,25 +2,25 @@ import { defaultTableFields } from '#database/default_table_fields';
|
||||
import { BaseSchema } from '@adonisjs/lucid/schema';
|
||||
|
||||
export default class CreateUsersTable extends BaseSchema {
|
||||
static tableName = 'users';
|
||||
static tableName = 'users';
|
||||
|
||||
async up() {
|
||||
this.schema.createTableIfNotExists(CreateUsersTable.tableName, (table) => {
|
||||
table.string('email', 254).notNullable().unique();
|
||||
table.string('name', 254).notNullable();
|
||||
table.string('nick_name', 254).nullable();
|
||||
table.text('avatar_url').notNullable();
|
||||
table.boolean('is_admin').defaultTo(0).notNullable();
|
||||
async up() {
|
||||
this.schema.createTableIfNotExists(CreateUsersTable.tableName, (table) => {
|
||||
table.string('email', 254).notNullable().unique();
|
||||
table.string('name', 254).notNullable();
|
||||
table.string('nick_name', 254).nullable();
|
||||
table.text('avatar_url').notNullable();
|
||||
table.boolean('is_admin').defaultTo(0).notNullable();
|
||||
|
||||
table.json('token').nullable();
|
||||
table.string('provider_id').notNullable();
|
||||
table.enum('provider_type', ['google']).notNullable().defaultTo('google');
|
||||
table.json('token').nullable();
|
||||
table.string('provider_id').notNullable();
|
||||
table.enum('provider_type', ['google']).notNullable().defaultTo('google');
|
||||
|
||||
defaultTableFields(table);
|
||||
});
|
||||
}
|
||||
defaultTableFields(table);
|
||||
});
|
||||
}
|
||||
|
||||
async down() {
|
||||
this.schema.dropTable(CreateUsersTable.tableName);
|
||||
}
|
||||
async down() {
|
||||
this.schema.dropTable(CreateUsersTable.tableName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,42 +3,42 @@ import { Visibility } from '#enums/visibility';
|
||||
import { BaseSchema } from '@adonisjs/lucid/schema';
|
||||
|
||||
export default class CreateCollectionTable extends BaseSchema {
|
||||
static tableName = 'collections';
|
||||
private visibilityEnumName = 'collection_visibility';
|
||||
static tableName = 'collections';
|
||||
private visibilityEnumName = 'collection_visibility';
|
||||
|
||||
async up() {
|
||||
this.schema.raw(`DROP TYPE IF EXISTS ${this.visibilityEnumName}`);
|
||||
this.schema.createTableIfNotExists(
|
||||
CreateCollectionTable.tableName,
|
||||
(table) => {
|
||||
table.string('name', 254).notNullable();
|
||||
table.string('description', 254).nullable();
|
||||
table
|
||||
.enum('visibility', Object.values(Visibility), {
|
||||
useNative: true,
|
||||
enumName: this.visibilityEnumName,
|
||||
existingType: false,
|
||||
})
|
||||
.nullable()
|
||||
.defaultTo(Visibility.PRIVATE);
|
||||
table
|
||||
.integer('next_id')
|
||||
.references('id')
|
||||
.inTable('collections')
|
||||
.defaultTo(null);
|
||||
table
|
||||
.integer('author_id')
|
||||
.references('id')
|
||||
.inTable('users')
|
||||
.onDelete('CASCADE');
|
||||
async up() {
|
||||
this.schema.raw(`DROP TYPE IF EXISTS ${this.visibilityEnumName}`);
|
||||
this.schema.createTableIfNotExists(
|
||||
CreateCollectionTable.tableName,
|
||||
(table) => {
|
||||
table.string('name', 254).notNullable();
|
||||
table.string('description', 254).nullable();
|
||||
table
|
||||
.enum('visibility', Object.values(Visibility), {
|
||||
useNative: true,
|
||||
enumName: this.visibilityEnumName,
|
||||
existingType: false,
|
||||
})
|
||||
.nullable()
|
||||
.defaultTo(Visibility.PRIVATE);
|
||||
table
|
||||
.integer('next_id')
|
||||
.references('id')
|
||||
.inTable('collections')
|
||||
.defaultTo(null);
|
||||
table
|
||||
.integer('author_id')
|
||||
.references('id')
|
||||
.inTable('users')
|
||||
.onDelete('CASCADE');
|
||||
|
||||
defaultTableFields(table);
|
||||
}
|
||||
);
|
||||
}
|
||||
defaultTableFields(table);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
async down() {
|
||||
this.schema.raw(`DROP TYPE IF EXISTS ${this.visibilityEnumName}`);
|
||||
this.schema.dropTable(CreateCollectionTable.tableName);
|
||||
}
|
||||
async down() {
|
||||
this.schema.raw(`DROP TYPE IF EXISTS ${this.visibilityEnumName}`);
|
||||
this.schema.dropTable(CreateCollectionTable.tableName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,30 +2,30 @@ import { defaultTableFields } from '#database/default_table_fields';
|
||||
import { BaseSchema } from '@adonisjs/lucid/schema';
|
||||
|
||||
export default class CreateLinksTable extends BaseSchema {
|
||||
static tableName = 'links';
|
||||
static tableName = 'links';
|
||||
|
||||
async up() {
|
||||
this.schema.createTableIfNotExists(CreateLinksTable.tableName, (table) => {
|
||||
table.string('name', 254).notNullable();
|
||||
table.string('description', 254).nullable();
|
||||
table.text('url').notNullable();
|
||||
table.boolean('favorite').notNullable().defaultTo(0);
|
||||
table
|
||||
.integer('collection_id')
|
||||
.references('id')
|
||||
.inTable('collections')
|
||||
.onDelete('CASCADE');
|
||||
table
|
||||
.integer('author_id')
|
||||
.references('id')
|
||||
.inTable('users')
|
||||
.onDelete('CASCADE');
|
||||
async up() {
|
||||
this.schema.createTableIfNotExists(CreateLinksTable.tableName, (table) => {
|
||||
table.string('name', 254).notNullable();
|
||||
table.string('description', 254).nullable();
|
||||
table.text('url').notNullable();
|
||||
table.boolean('favorite').notNullable().defaultTo(0);
|
||||
table
|
||||
.integer('collection_id')
|
||||
.references('id')
|
||||
.inTable('collections')
|
||||
.onDelete('CASCADE');
|
||||
table
|
||||
.integer('author_id')
|
||||
.references('id')
|
||||
.inTable('users')
|
||||
.onDelete('CASCADE');
|
||||
|
||||
defaultTableFields(table);
|
||||
});
|
||||
}
|
||||
defaultTableFields(table);
|
||||
});
|
||||
}
|
||||
|
||||
async down() {
|
||||
this.schema.dropTable(CreateLinksTable.tableName);
|
||||
}
|
||||
async down() {
|
||||
this.schema.dropTable(CreateLinksTable.tableName);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,18 +1,18 @@
|
||||
import { BaseSchema } from '@adonisjs/lucid/schema';
|
||||
|
||||
export default class extends BaseSchema {
|
||||
async up() {
|
||||
this.schema.raw(`
|
||||
async up() {
|
||||
this.schema.raw(`
|
||||
CREATE EXTENSION IF NOT EXISTS unaccent;
|
||||
CREATE EXTENSION IF NOT EXISTS pg_trgm;
|
||||
`);
|
||||
this.schema.raw(`
|
||||
this.schema.raw(`
|
||||
CREATE INDEX ON links USING gin(to_tsvector('english', name));
|
||||
CREATE INDEX ON collections USING gin(to_tsvector('english', name));
|
||||
CREATE INDEX ON links USING gin(to_tsvector('french', name));
|
||||
CREATE INDEX ON collections USING gin(to_tsvector('french', name));
|
||||
`);
|
||||
this.schema.raw(`
|
||||
this.schema.raw(`
|
||||
CREATE OR REPLACE FUNCTION search_text(search_query TEXT, p_author_id INTEGER)
|
||||
RETURNS TABLE (
|
||||
id INTEGER,
|
||||
@@ -45,9 +45,9 @@ export default class extends BaseSchema {
|
||||
$$
|
||||
LANGUAGE plpgsql;
|
||||
`);
|
||||
}
|
||||
}
|
||||
|
||||
async down() {
|
||||
this.schema.raw('DROP FUNCTION IF EXISTS search_text');
|
||||
}
|
||||
async down() {
|
||||
this.schema.raw('DROP FUNCTION IF EXISTS search_text');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,36 +5,36 @@ import { BaseSeeder } from '@adonisjs/lucid/seeders';
|
||||
import { faker } from '@faker-js/faker';
|
||||
|
||||
export default class extends BaseSeeder {
|
||||
static environment = ['development', 'testing'];
|
||||
static environment = ['development', 'testing'];
|
||||
|
||||
async run() {
|
||||
const users = await getUserIds();
|
||||
async run() {
|
||||
const users = await getUserIds();
|
||||
|
||||
const collections = faker.helpers.multiple(
|
||||
() => createRandomCollection(users),
|
||||
{
|
||||
count: 50,
|
||||
}
|
||||
);
|
||||
await Collection.createMany(collections);
|
||||
}
|
||||
const collections = faker.helpers.multiple(
|
||||
() => createRandomCollection(users),
|
||||
{
|
||||
count: 50,
|
||||
}
|
||||
);
|
||||
await Collection.createMany(collections);
|
||||
}
|
||||
}
|
||||
|
||||
export async function getUserIds() {
|
||||
const users = await User.all();
|
||||
return users.map(({ id }) => id);
|
||||
const users = await User.all();
|
||||
return users.map(({ id }) => id);
|
||||
}
|
||||
|
||||
let collectionId = 0;
|
||||
function createRandomCollection(userIds: User['id'][]) {
|
||||
const authorId = faker.helpers.arrayElements(userIds, 1).at(0);
|
||||
collectionId++;
|
||||
return {
|
||||
id: collectionId,
|
||||
name: faker.string.alphanumeric({ length: { min: 5, max: 25 } }),
|
||||
description: faker.string.alphanumeric({ length: { min: 0, max: 254 } }),
|
||||
visibility: Visibility.PRIVATE,
|
||||
nextId: collectionId + 1,
|
||||
authorId,
|
||||
};
|
||||
const authorId = faker.helpers.arrayElements(userIds, 1).at(0);
|
||||
collectionId++;
|
||||
return {
|
||||
id: collectionId,
|
||||
name: faker.string.alphanumeric({ length: { min: 5, max: 25 } }),
|
||||
description: faker.string.alphanumeric({ length: { min: 0, max: 254 } }),
|
||||
visibility: Visibility.PRIVATE,
|
||||
nextId: collectionId + 1,
|
||||
authorId,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -6,41 +6,41 @@ import { BaseSeeder } from '@adonisjs/lucid/seeders';
|
||||
import { faker } from '@faker-js/faker';
|
||||
|
||||
export default class extends BaseSeeder {
|
||||
static environment = ['development', 'testing'];
|
||||
static environment = ['development', 'testing'];
|
||||
|
||||
async run() {
|
||||
const users = await getUserIds();
|
||||
async run() {
|
||||
const users = await getUserIds();
|
||||
|
||||
const links = await Promise.all(
|
||||
faker.helpers.multiple(async () => createRandomLink(users), {
|
||||
count: 500,
|
||||
})
|
||||
);
|
||||
await Link.createMany(links.filter((a) => typeof a !== 'undefined') as any);
|
||||
}
|
||||
const links = await Promise.all(
|
||||
faker.helpers.multiple(async () => createRandomLink(users), {
|
||||
count: 500,
|
||||
})
|
||||
);
|
||||
await Link.createMany(links.filter((a) => typeof a !== 'undefined') as any);
|
||||
}
|
||||
}
|
||||
|
||||
async function getCollectionIds(authorId: User['id']) {
|
||||
const collection = await Collection.findManyBy('author_id', authorId);
|
||||
return collection.map(({ id }) => id);
|
||||
const collection = await Collection.findManyBy('author_id', authorId);
|
||||
return collection.map(({ id }) => id);
|
||||
}
|
||||
|
||||
async function createRandomLink(userIds: User['id'][]) {
|
||||
const authorId = faker.helpers.arrayElements(userIds, 1).at(0)!;
|
||||
const collections = await getCollectionIds(authorId);
|
||||
const authorId = faker.helpers.arrayElements(userIds, 1).at(0)!;
|
||||
const collections = await getCollectionIds(authorId);
|
||||
|
||||
const collectionId = faker.helpers.arrayElements(collections, 1).at(0);
|
||||
if (!collectionId) {
|
||||
return undefined;
|
||||
}
|
||||
const collectionId = faker.helpers.arrayElements(collections, 1).at(0);
|
||||
if (!collectionId) {
|
||||
return undefined;
|
||||
}
|
||||
|
||||
return {
|
||||
id: faker.string.uuid(),
|
||||
name: faker.string.alphanumeric({ length: { min: 5, max: 25 } }),
|
||||
description: faker.string.alphanumeric({ length: { min: 0, max: 254 } }),
|
||||
url: faker.internet.url(),
|
||||
favorite: faker.number.int({ min: 0, max: 1 }),
|
||||
authorId,
|
||||
collectionId,
|
||||
};
|
||||
return {
|
||||
id: faker.string.uuid(),
|
||||
name: faker.string.alphanumeric({ length: { min: 5, max: 25 } }),
|
||||
description: faker.string.alphanumeric({ length: { min: 0, max: 254 } }),
|
||||
url: faker.internet.url(),
|
||||
favorite: faker.number.int({ min: 0, max: 1 }),
|
||||
authorId,
|
||||
collectionId,
|
||||
};
|
||||
}
|
||||
|
||||
@@ -3,31 +3,31 @@ import logger from '@adonisjs/core/services/logger';
|
||||
import { BaseSeeder } from '@adonisjs/lucid/seeders';
|
||||
|
||||
export default class IndexSeeder extends BaseSeeder {
|
||||
private async seed(Seeder: { default: typeof BaseSeeder }) {
|
||||
/**
|
||||
* Do not run when not in a environment specified in Seeder
|
||||
*/
|
||||
if (
|
||||
!Seeder.default.environment ||
|
||||
(!Seeder.default.environment.includes('development') && app.inDev) ||
|
||||
(!Seeder.default.environment.includes('testing') && app.inTest) ||
|
||||
(!Seeder.default.environment.includes('production') && app.inProduction)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
private async seed(Seeder: { default: typeof BaseSeeder }) {
|
||||
/**
|
||||
* Do not run when not in a environment specified in Seeder
|
||||
*/
|
||||
if (
|
||||
!Seeder.default.environment ||
|
||||
(!Seeder.default.environment.includes('development') && app.inDev) ||
|
||||
(!Seeder.default.environment.includes('testing') && app.inTest) ||
|
||||
(!Seeder.default.environment.includes('production') && app.inProduction)
|
||||
) {
|
||||
return;
|
||||
}
|
||||
|
||||
await new Seeder.default(this.client).run();
|
||||
}
|
||||
await new Seeder.default(this.client).run();
|
||||
}
|
||||
|
||||
async run() {
|
||||
logger.info('Start user seed');
|
||||
await this.seed(await import('#database/seeders/user_seeder'));
|
||||
logger.info('User seed done');
|
||||
logger.info('Collection user seed');
|
||||
await this.seed(await import('#database/seeders/collection_seeder'));
|
||||
logger.info('Collection seed done');
|
||||
logger.info('Link user seed');
|
||||
await this.seed(await import('#database/seeders/link_seeder'));
|
||||
logger.info('Link seed done');
|
||||
}
|
||||
async run() {
|
||||
logger.info('Start user seed');
|
||||
await this.seed(await import('#database/seeders/user_seeder'));
|
||||
logger.info('User seed done');
|
||||
logger.info('Collection user seed');
|
||||
await this.seed(await import('#database/seeders/collection_seeder'));
|
||||
logger.info('Collection seed done');
|
||||
logger.info('Link user seed');
|
||||
await this.seed(await import('#database/seeders/link_seeder'));
|
||||
logger.info('Link seed done');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,26 +4,26 @@ import { BaseSeeder } from '@adonisjs/lucid/seeders';
|
||||
import { faker } from '@faker-js/faker';
|
||||
|
||||
export default class extends BaseSeeder {
|
||||
static environment = ['development', 'testing'];
|
||||
static environment = ['development', 'testing'];
|
||||
|
||||
async run() {
|
||||
const users = faker.helpers.multiple(createRandomUser, {
|
||||
count: 25,
|
||||
});
|
||||
await User.createMany(users);
|
||||
}
|
||||
async run() {
|
||||
const users = faker.helpers.multiple(createRandomUser, {
|
||||
count: 25,
|
||||
});
|
||||
await User.createMany(users);
|
||||
}
|
||||
}
|
||||
|
||||
export function createRandomUser() {
|
||||
return {
|
||||
id: faker.number.int(),
|
||||
email: faker.internet.email(),
|
||||
name: faker.internet.userName(),
|
||||
nickName: faker.internet.displayName(),
|
||||
avatarUrl: faker.image.avatar(),
|
||||
isAdmin: false,
|
||||
providerId: faker.number.int(),
|
||||
providerType: 'google' as const,
|
||||
token: {} as GoogleToken,
|
||||
};
|
||||
return {
|
||||
id: faker.number.int(),
|
||||
email: faker.internet.email(),
|
||||
name: faker.internet.userName(),
|
||||
nickName: faker.internet.displayName(),
|
||||
avatarUrl: faker.image.avatar(),
|
||||
isAdmin: false,
|
||||
providerId: faker.number.int(),
|
||||
providerType: 'google' as const,
|
||||
token: {} as GoogleToken,
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user