feat: general自动设置创建时间与更新时间
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
import datetime
|
||||||
import traceback
|
import traceback
|
||||||
|
|
||||||
from app.config.env import env
|
from app.config.env import env
|
||||||
@@ -121,10 +122,19 @@ async def insert(session: AsyncSessionDep, query_config, module_config, debug_da
|
|||||||
|
|
||||||
row_id = get_value(row, 'id', None)
|
row_id = get_value(row, 'id', None)
|
||||||
|
|
||||||
|
# 自动设置row_id
|
||||||
if row_id is None:
|
if row_id is None:
|
||||||
row_id = (await get_id(session, 1))[0]
|
row_id = (await get_id(session, 1))[0]
|
||||||
row['id'] = row_id
|
row['id'] = row_id
|
||||||
|
|
||||||
|
# 自动设置创建时间
|
||||||
|
if get_value(row, 'createdAt', None):
|
||||||
|
row['createdAt'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||||
|
|
||||||
|
# 自动设置更新时间
|
||||||
|
if get_value(row, 'updatedAt', None):
|
||||||
|
row['updatedAt'] = row['createdAt']
|
||||||
|
|
||||||
await invoke_interceptor('before_insert', module_config['base'], row, session)
|
await invoke_interceptor('before_insert', module_config['base'], row, session)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -170,12 +180,22 @@ async def batch_insert(session: AsyncSessionDep, query_config, module_config, de
|
|||||||
}
|
}
|
||||||
create_convertor(module_config)['encode_list'](rows)
|
create_convertor(module_config)['encode_list'](rows)
|
||||||
|
|
||||||
|
# 自动填充row id
|
||||||
rows_without_id = [row for row in rows if get_value(row, 'id', None) is None]
|
rows_without_id = [row for row in rows if get_value(row, 'id', None) is None]
|
||||||
if rows_without_id:
|
if rows_without_id:
|
||||||
new_id_list = await get_id(session, len(rows_without_id))
|
new_id_list = await get_id(session, len(rows_without_id))
|
||||||
for index, row in enumerate(rows_without_id):
|
for index, row in enumerate(rows_without_id):
|
||||||
row['id'] = new_id_list[index]
|
row['id'] = new_id_list[index]
|
||||||
|
|
||||||
|
for row in rows:
|
||||||
|
# 自动设置创建时间
|
||||||
|
if get_value(row, 'createdAt', None):
|
||||||
|
row['createdAt'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||||
|
|
||||||
|
# 自动设置更新时间
|
||||||
|
if get_value(row, 'updatedAt', None):
|
||||||
|
row['updatedAt'] = row['createdAt']
|
||||||
|
|
||||||
await invoke_interceptor('before_batch_insert', module_config['base'], rows, session)
|
await invoke_interceptor('before_batch_insert', module_config['base'], rows, session)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -230,6 +250,8 @@ async def update(session: AsyncSessionDep, query_config, module_config, debug_da
|
|||||||
"error": "row is missing field: id",
|
"error": "row is missing field: id",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
row['updatedAt'] = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||||
|
|
||||||
create_convertor(module_config)['encode_list']([row])
|
create_convertor(module_config)['encode_list']([row])
|
||||||
|
|
||||||
await invoke_interceptor('before_update', module_config['base'], row, session)
|
await invoke_interceptor('before_update', module_config['base'], row, session)
|
||||||
@@ -286,6 +308,11 @@ async def batch_update(session: AsyncSessionDep, query_config, module_config, de
|
|||||||
"rows": rows_without_id,
|
"rows": rows_without_id,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
current_datetime = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
|
||||||
|
|
||||||
|
for row in rows:
|
||||||
|
row['updatedAt'] = current_datetime
|
||||||
|
|
||||||
await invoke_interceptor('before_batch_update', module_config['base'], rows, session)
|
await invoke_interceptor('before_batch_update', module_config['base'], rows, session)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
|||||||
Reference in New Issue
Block a user