-
Notifications
You must be signed in to change notification settings - Fork 13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Model define default value can not work #15
Comments
In the beginning I was using For starters, I'd be a good idea for me to fork For your issue, you could add a Before Create Hook which sets the default value. YourModel.beforeCreate = (instance) => {
if (!instance.createdAt) {
instance.createdAt = Date.now;
}
if (!instance.updatedAt) {
instance.updatedAt= Date.now;
}
}; Of course this is not quite the same as a default value, as it runs only before saving an instance (which should work in your case). Also, in case you want to use an actual neo4j date, you could do the following: import { neo4jDriver } from 'neogma';
type YourProperties = {
createdAt: neo4jDriver.DateTime<any>;
}
// in schema validation:
{
createdAt: {
type: 'any',
required: true,
conform: (v) => neo4jDriver.isDateTime(v),
}
}
// to create the date
const nowDateTime = neo4jDriver.types.DateTime.fromStandardDate(
new Date(),
); I don't know if there's an easier way 😅 Although I could implement an automatic transformation of neo4j <-> js Dates on an Instance level. |
Sorry for late reply 🙈. Use I just want to use the basic timestamp to mark the node. It's too complex when I try to use neo4j Date Object ( have to implement the adapter for two different data structure on application layer ) . If u want to use @Node()
@Relation(() => Order)
export class User {
@Property()
id: number;
@Property()
firstName: string;
@Property()
lastName: string;
@Property()
age: number;
@CreateDateProperty()
createdAt: Date;
} And load the entity file like createConnection({
url: 'bolt://127.0.0.1:7687',
username: 'neo4j',
password: 'neo4j',
entities: [
__dirname + "/entity/*.js"
]
}).then(connection => {
// here you can start to work with your entities
}).catch(error => console.log(error)); |
Thanks for all that info! It'll come in handy when I consider replacing revalidator |
I like the use of decorators as it helps the schema definition clean. The current way is so verbose, but it works. So, I do support decorator, but without removing the current style for backward compatibility. |
neogma/src/ModelOps/ModelOps.ts
Line 487 in c237b20
Here is my model schema:
neogma model use
revalidator
to check the value , butrevalidator
has this issue flatiron/revalidator#85 , thedefault
field does't implement.How about use
class-validator
&class-transformer
to replace the schema implement ?Good
Bad
The text was updated successfully, but these errors were encountered: