Browse Source

Add transaction mechanism

Track changes across devices.
feature/transactions
TheoryOfNekomata 3 years ago
parent
commit
4179f2a94e
21 changed files with 5489 additions and 5703 deletions
  1. +13
    -8
      migrate.ts
  2. +0
    -100
      models/Folder.ts
  3. +0
    -99
      models/Note.ts
  4. +0
    -43
      models/Tag.ts
  5. +1
    -0
      package.json
  6. +7
    -3
      src/models.ts
  7. +40
    -0
      src/models/Folder.ts
  8. +44
    -0
      src/models/Note.ts
  9. +20
    -0
      src/models/Operation.ts
  10. +20
    -0
      src/models/Tag.ts
  11. +32
    -0
      src/models/Transaction.ts
  12. +1
    -1
      src/pages/api/folders.ts
  13. +1
    -1
      src/pages/api/folders/[id].ts
  14. +1
    -1
      src/pages/api/notes.ts
  15. +1
    -1
      src/pages/api/notes/[id].ts
  16. +10
    -0
      src/seeds.ts
  17. +1
    -1
      src/services/Folder.ts
  18. +1
    -1
      src/services/Note.ts
  19. +15
    -0
      src/services/Operation.ts
  20. +1
    -1
      src/services/Storage.ts
  21. +5280
    -5443
      yarn.lock

+ 13
- 8
migrate.ts View File

@@ -1,15 +1,20 @@
import models from './src/models'
import seeds from './src/seeds'

export const up = async queryInterface => {
models.forEach(m => {
queryInterface.createTable(m.tableName, m.rawAttributes)
})
const createTablePromises = models.map(m => queryInterface.createTable(m.tableName, m.rawAttributes))
await Promise.all(createTablePromises)

const seedTablePromise = models
.filter(m => Boolean(seeds[m.modelName]))
.map(m => queryInterface.bulkInsert(m.tableName, seeds[m.modelName]))
return Promise.all(seedTablePromise)
}

export const down = async (queryInterface) => {
models
export const down = async queryInterface => {
const dropTablePromises = models
.reduce((reverse, m) => [m, ...reverse], [])
.forEach(m => {
queryInterface.dropTable(m.tableName)
})
.map(m => queryInterface.dropTable(m.tableName))
return Promise.all(dropTablePromises)
}

+ 0
- 100
models/Folder.ts View File

@@ -1,100 +0,0 @@
import * as DataType from 'sequelize'

export default {
tableName: 'folders',
modelName: 'Folder',
options: {
timestamps: true,
paranoid: true,
createdAt: 'createdAt',
updatedAt: 'updatedAt',
deletedAt: 'deletedAt',
},
rawAttributes: {
id: {
allowNull: true,
primaryKey: true,
type: DataType.UUIDV4,
},
name: {
allowNull: false,
type: DataType.STRING,
},
parentId: {
allowNull: true,
type: DataType.UUIDV4,
},
createdAt: {
allowNull: false,
type: DataType.DATE,
},
updatedAt: {
allowNull: false,
type: DataType.DATE,
},
deletedAt: {
allowNull: true,
type: DataType.DATE,
},
}
}

// import 'reflect-metadata'
// import {
// AllowNull,
// BelongsTo,
// Column,
// CreatedAt,
// DeletedAt,
// ForeignKey,
// HasMany,
// Model,
// PrimaryKey,
// Table,
// UpdatedAt,
// DataType,
// } from 'sequelize-typescript'
// import Model from './Model'
//
// @Table({
// timestamps: true,
// paranoid: true,
// })
//
// export default class Folder extends Model<Folder> {
// @AllowNull
// @PrimaryKey
// @Column(DataType.UUIDV4)
// id?: string
//
// @Column
// name: string
//
// @AllowNull
// @ForeignKey(() => Folder)
// @Column(DataType.UUIDV4)
// parentId?: string
//
// @BelongsTo(() => Folder, 'parentId')
// parent?: Folder
//
// @HasMany(() => Folder, 'parentId')
// children: Folder[]
//
// @Column(DataType.DATE)
// @CreatedAt
// createdAt: Date
//
// @Column(DataType.DATE)
// @UpdatedAt
// updatedAt: Date
//
// @AllowNull
// @Column(DataType.DATE)
// @DeletedAt
// deletedAt?: Date
//
// @HasMany(() => Model, 'folderId')
// notes: Model[]
// }
//

+ 0
- 99
models/Note.ts View File

@@ -1,99 +0,0 @@
import * as DataType from 'sequelize'

export default {
tableName: 'notes',
modelName: 'Note',
options: {
timestamps: true,
paranoid: true,
createdAt: 'createdAt',
updatedAt: 'updatedAt',
deletedAt: 'deletedAt',
},
rawAttributes: {
id: {
allowNull: true,
primaryKey: true,
type: DataType.UUIDV4,
},
title: {
allowNull: false,
type: DataType.STRING,
},
content: {
allowNull: true,
type: DataType.TEXT({ length: 'long', }),
},
folderId: {
allowNull: true,
type: DataType.UUIDV4,
},
createdAt: {
allowNull: false,
type: DataType.DATE,
},
updatedAt: {
allowNull: false,
type: DataType.DATE,
},
deletedAt: {
allowNull: true,
type: DataType.DATE,
},
},
}

// import 'reflect-metadata'
// import {
// AllowNull,
// BelongsTo,
// Column,
// CreatedAt,
// DeletedAt,
// ForeignKey,
// Model,
// PrimaryKey,
// Table,
// UpdatedAt,
// DataType,
// } from 'sequelize-typescript'
// import Folder from './Folder'
//
// @Table({
// timestamps: true,
// paranoid: true,
// })
// export default class Model extends Model<Model> {
// @AllowNull
// @PrimaryKey
// @Column(DataType.UUIDV4)
// id?: string
//
// @Column
// title: string
//
// @AllowNull
// @Column(DataType.TEXT({ length: 'long' }))
// content?: string
//
// @AllowNull
// @ForeignKey(() => Folder)
// @Column(DataType.UUIDV4)
// folderId?: string
//
// @BelongsTo(() => Folder, 'folderId')
// folder?: Folder
//
// @Column(DataType.DATE)
// @CreatedAt
// createdAt: Date
//
// @Column(DataType.DATE)
// @UpdatedAt
// updatedAt: Date
//
// @AllowNull
// @Column(DataType.DATE)
// @DeletedAt
// deletedAt?: Date
// }

+ 0
- 43
models/Tag.ts View File

@@ -1,43 +0,0 @@
import * as DataType from 'sequelize'

export default {
options: {
timestamps: false,
},
modelName: 'Tag',
tableName: 'tags',
rawAttributes: {
id: {
allowNull: true,
primaryKey: true,
type: DataType.UUIDV4,
},
name: {
allowNull: false,
type: DataType.STRING,
},
}
}

// import 'reflect-metadata'
// import {
// AllowNull,
// Column,
// Model,
// PrimaryKey,
// Table,
// DataType,
// } from 'sequelize-typescript'
//
// @Table({
// timestamps: false,
// })
// export default class Tag extends Model<Tag> {
// @AllowNull
// @PrimaryKey
// @Column(DataType.UUIDV4)
// id?: string
//
// @Column
// name: string
// }

+ 1
- 0
package.json View File

@@ -9,6 +9,7 @@
"migrate": "tsc migrate.ts --module commonjs --esModuleInterop --outDir database/migrations && sequelize-cli db:migrate"
},
"dependencies": {
"@fingerprintjs/fingerprintjs": "^3.0.3",
"dotenv": "^8.2.0",
"mobiledoc-kit": "^0.13.1",
"next": "9.5.5",


+ 7
- 3
src/models.ts View File

@@ -1,9 +1,13 @@
import Folder from '../models/Folder'
import Tag from '../models/Tag'
import Note from '../models/Note'
import Folder from './models/Folder'
import Note from './models/Note'
import Tag from './models/Tag'
import Operation from './models/Operation'
import Transaction from './models/Transaction'

export default [
Folder,
Note,
Tag,
Operation,
Transaction,
]

+ 40
- 0
src/models/Folder.ts View File

@@ -0,0 +1,40 @@
import { UUIDV4, STRING, DATE, } from 'sequelize'

export default {
tableName: 'folders',
modelName: 'Folder',
options: {
timestamps: true,
paranoid: true,
createdAt: 'createdAt',
updatedAt: 'updatedAt',
deletedAt: 'deletedAt',
},
rawAttributes: {
id: {
allowNull: true,
primaryKey: true,
type: UUIDV4,
},
name: {
allowNull: false,
type: STRING,
},
parentId: {
allowNull: true,
type: UUIDV4,
},
createdAt: {
allowNull: false,
type: DATE,
},
updatedAt: {
allowNull: false,
type: DATE,
},
deletedAt: {
allowNull: true,
type: DATE,
},
}
}

+ 44
- 0
src/models/Note.ts View File

@@ -0,0 +1,44 @@
import { UUIDV4, STRING, TEXT, DATE, } from 'sequelize'

export default {
tableName: 'notes',
modelName: 'Note',
options: {
timestamps: true,
paranoid: true,
createdAt: 'createdAt',
updatedAt: 'updatedAt',
deletedAt: 'deletedAt',
},
rawAttributes: {
id: {
allowNull: true,
primaryKey: true,
type: UUIDV4,
},
title: {
allowNull: false,
type: STRING,
},
content: {
allowNull: true,
type: TEXT({ length: 'long', }),
},
folderId: {
allowNull: true,
type: UUIDV4,
},
createdAt: {
allowNull: false,
type: DATE,
},
updatedAt: {
allowNull: false,
type: DATE,
},
deletedAt: {
allowNull: true,
type: DATE,
},
},
}

+ 20
- 0
src/models/Operation.ts View File

@@ -0,0 +1,20 @@
import { INTEGER, STRING, } from 'sequelize'

export default {
options: {
timestamps: false,
},
modelName: 'Operation',
tableName: 'operations',
rawAttributes: {
id: {
allowNull: true,
primaryKey: true,
type: INTEGER,
},
name: {
allowNull: false,
type: STRING,
},
}
}

+ 20
- 0
src/models/Tag.ts View File

@@ -0,0 +1,20 @@
import { UUIDV4, STRING, } from 'sequelize'

export default {
options: {
timestamps: false,
},
modelName: 'Tag',
tableName: 'tags',
rawAttributes: {
id: {
allowNull: true,
primaryKey: true,
type: UUIDV4,
},
name: {
allowNull: false,
type: STRING,
},
}
}

+ 32
- 0
src/models/Transaction.ts View File

@@ -0,0 +1,32 @@
import { UUIDV4, STRING, DATE, INTEGER, } from 'sequelize'

export default {
options: {
timestamps: false,
},
modelName: 'Transaction',
tableName: 'transactions',
rawAttributes: {
id: {
allowNull: true,
primaryKey: true,
type: UUIDV4,
},
deviceId: {
allowNull: false,
type: STRING,
},
operation: {
allowNull: false,
type: INTEGER,
},
objectId: {
allowNull: false,
type: STRING,
},
performedAt: {
allowNull: false,
type: DATE,
},
},
}

+ 1
- 1
src/pages/api/folders.ts View File

@@ -1,4 +1,4 @@
import Model from '../../../models/Folder'
import Model from '../../models/Folder'
import * as Service from '../../services/Folder'
import { collection } from '../../services/Controller'



+ 1
- 1
src/pages/api/folders/[id].ts View File

@@ -1,4 +1,4 @@
import Model from '../../../../models/Folder'
import Model from '../../../models/Folder'
import * as Service from '../../../services/Folder'
import { item } from '../../../services/Controller'



+ 1
- 1
src/pages/api/notes.ts View File

@@ -1,4 +1,4 @@
import Model from '../../../models/Note'
import Model from '../../models/Note'
import * as Service from '../../services/Note'
import { collection } from '../../services/Controller'



+ 1
- 1
src/pages/api/notes/[id].ts View File

@@ -1,4 +1,4 @@
import Model from '../../../../models/Note'
import Model from '../../../models/Note'
import * as Service from '../../../services/Note'
import { item } from '../../../services/Controller'



+ 10
- 0
src/seeds.ts View File

@@ -0,0 +1,10 @@
import Operation from './services/Operation'

export default {
'Operation': Object
.entries(Operation)
.map(([name, id]) => ({
id: Number(id),
name,
})),
}

+ 1
- 1
src/services/Folder.ts View File

@@ -1,4 +1,4 @@
import FolderModel from '../../models/Folder'
import FolderModel from '../models/Folder'
import Instance from '../utilities/Instance'
import * as Response from '../utilities/Response'



+ 1
- 1
src/services/Note.ts View File

@@ -1,4 +1,4 @@
import Model from '../../models/Note'
import Model from '../models/Note'
import InferType from '../utilities/Instance'
import * as Response from '../utilities/Response'



+ 15
- 0
src/services/Operation.ts View File

@@ -0,0 +1,15 @@
enum Operation {
'note:read' = 1,
'note:write' = 2,
'note:delete' = 3,
'note:import' = 4,
'note:export' = 5,
'folder:read' = 7,
'folder:write' = 8,
'social:publish' = 9,
'social:unpublish' = 10,
'social:share' = 11,
'userinfo:manage' = 12,
}

export default Operation

+ 1
- 1
src/services/Storage.ts View File

@@ -1,6 +1,6 @@
import { addTime, TimeDivision } from '../utilities/Date'
import * as Serialization from '../utilities/Serialization'
import NoteModel from '../../models/Note'
import NoteModel from '../models/Note'
import InferModel from '../utilities/Instance'
import LocalStorage from './LocalStorage'



+ 5280
- 5443
yarn.lock
File diff suppressed because it is too large
View File


Loading…
Cancel
Save